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
protectedreadonlyresource:T
Defined in: http/Resource.ts:46
Methods
toArray()
abstracttoArray():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()
staticcollection<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()
staticwithoutWrapping():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()
staticwithWrapping():void
Defined in: http/Resource.ts:100
Re-enable the { data: ... } wrapper (the default).
Returns
void
wrapping()
staticwrapping():boolean
Defined in: http/Resource.ts:105
Returns whether wrapping is currently enabled.
Returns
boolean