Skip to main content
zerotal

Documentation


Documentation / @zerotal/core / http / Resource

Abstract Class: Resource<T>

Defined in: http/Resource.ts:45

Type Parameters

T

T

Constructors

Constructor

new Resource<T>(resource): Resource<T>

Defined in: http/Resource.ts:46

Parameters

resource

T

Returns

Resource<T>

Properties

resource

protected readonly resource: T

Defined in: http/Resource.ts:46

Methods

toArray()

abstract toArray(): Record<string, unknown>

Defined in: http/Resource.ts:49

Override this to define the serialized representation.

Returns

Record<string, unknown>


toJson()

toJson(): Record<string, unknown>

Defined in: http/Resource.ts:55

Return the resource as a plain object. Includes the { data: ... } wrapper unless withoutWrapping() was called.

Returns

Record<string, unknown>


toResponse()

toResponse(status?): Response

Defined in: http/Resource.ts:66

Return a Response with the JSON-serialized resource and the given status.

Parameters

status?

number = 200

Returns

Response

Example

ctx.response = new UserResource(user).toResponse(201);

collection()

static collection<T, R>(ResourceClass, items, meta?): Record<string, unknown> | Record<string, unknown>[]

Defined in: http/Resource.ts:78

Serialize a collection of models, wrapping the array in { data: [...] }.

Type Parameters

T

T

R

R extends Resource<T>

Parameters

ResourceClass

(item) => R

items

T[]

meta?

Record<string, unknown>

Returns

Record<string, unknown> | Record<string, unknown>[]

Example

ctx.json(UserResource.collection(UserResource, users));

withoutWrapping()

static withoutWrapping(): void

Defined in: http/Resource.ts:95

Disable the { data: ... } wrapping for all resources in this request. Call once at the application level if you prefer flat responses.

Returns

void


withWrapping()

static withWrapping(): void

Defined in: http/Resource.ts:100

Re-enable the { data: ... } wrapper (the default).

Returns

void


wrapping()

static wrapping(): boolean

Defined in: http/Resource.ts:105

Returns whether wrapping is currently enabled.

Returns

boolean