Class: Resource
Abstract class representing a RESTful resource. As the Resource
class inherits from Controllable
, its derived classes can be exposed by a Synapse API. As it inherits from State
, its instances also represent valid request responses.
Hierarchy
↳ Resource
Index
Constructors
Metadata Properties
Other Properties
Factory Methods
- ACCEPTED
- BAD_REQUEST
- CONFLICT
- CREATED
- FORBIDDEN
- INTERNAL_SERVER_ERROR
- NOT_FOUND
- NO_CONTENT
- OK
- UNAUTHORIZED
Other Methods
Constructors
constructor
+ new Resource(status
: number, message
: string): Resource
Inherited from State.constructor
Defined in lib/State.ts:33
Parameters:
Name | Type | Default | Description |
---|---|---|---|
status |
number | - | The HTTP status code to be assigned to the instance's metadata. |
message |
string | "" | A string message to be assigned to the instance's metadata. |
Returns: Resource
Metadata Properties
$dependencies
• $dependencies: Array‹string› = []
Inherited from State.$dependencies
Defined in lib/State.ts:33
An array of paths upon which the response data depends.
$message
• $message: string = ""
Defined in lib/State.ts:23
A string describing the response.
$query
• $query: string = null
Defined in lib/State.ts:28
An HTTP query string representing the requested path and validated arguments.
$status
• $status: number
Defined in lib/State.ts:18
An HTTP status code describing the response.
$type
• $type: string = null
Defined in lib/State.ts:13
The derived class name of the instance.
Other Properties
Static
router
▪ router: Router
Inherited from Controllable.router
Defined in lib/abstract/Controllable.ts:91
Static
schema
▪ schema: Schema
Inherited from Validatable.schema
Defined in lib/abstract/Validatable.ts:24
An instance of Schema
defining the properties necessary to construct an instance of the derived class.
Factory Methods
Static
ACCEPTED
▸ ACCEPTED(message
: any): State‹›
Defined in lib/State.ts:96
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
BAD_REQUEST
▸ BAD_REQUEST(message
: any): State‹›
Inherited from State.BAD_REQUEST
Defined in lib/State.ts:110
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
CONFLICT
▸ CONFLICT(message
: any): State‹›
Defined in lib/State.ts:138
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
CREATED
▸ CREATED(message
: any): State‹›
Defined in lib/State.ts:89
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
FORBIDDEN
▸ FORBIDDEN(message
: any): State‹›
Inherited from State.FORBIDDEN
Defined in lib/State.ts:124
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
INTERNAL_SERVER_ERROR
▸ INTERNAL_SERVER_ERROR(message
: any): State‹›
Inherited from State.INTERNAL_SERVER_ERROR
Defined in lib/State.ts:145
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
NOT_FOUND
▸ NOT_FOUND(message
: any): State‹›
Inherited from State.NOT_FOUND
Defined in lib/State.ts:131
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
NO_CONTENT
▸ NO_CONTENT(): State‹›
Inherited from State.NO_CONTENT
Defined in lib/State.ts:103
Creates a standard HTTP response.
Returns: State‹›
Static
OK
▸ OK(message
: any): State‹›
Defined in lib/State.ts:82
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Static
UNAUTHORIZED
▸ UNAUTHORIZED(message
: any): State‹›
Inherited from State.UNAUTHORIZED
Defined in lib/State.ts:117
Creates a standard HTTP response.
Parameters:
Name | Type | Default |
---|---|---|
message |
any | null |
Returns: State‹›
Other Methods
export
▸ export(): object
Defined in lib/Resource.ts:49
Returns an object containing all properties of the instance, excluding State
metadata.
Returns: object
isError
▸ isError(): boolean
Defined in lib/State.ts:46
Checks if the instance represents an error.
Returns: boolean
path
▸ path(): string
Defined in lib/Resource.ts:19
Returns the path that uniquely locates an instance (i.e. the path to which a GET
request would return the instance). By default, this is the root
path followed by the value on the instance corresponding to the first field on the derived class's schema that extends type Id
(e.g. '/user/123'); however, derived classes may override this behavior.
Returns: string
render
▸ render(): object
Defined in lib/Resource.ts:34
Returns: object
serialize
▸ serialize(): string
Inherited from State.serialize
Defined in lib/State.ts:57
Returns a serialized version of the public representation of the instance for network transport.
Returns: string
toJSON
▸ toJSON(): object
Defined in lib/State.ts:68
Returns a public representation of the instance metadata, with the instance's rendered
payload assigned to the property payload
on the resulting object. Called when an the instance is converted to JSON via JSON.stringify
.
Returns: object
-
message: string = this.$message
-
payload: any = this.render()
-
query: string = this.$query
-
status: number = this.$status
-
type: string = this.$type
uses
▸ uses(...states
: Array‹State›): this
Defined in lib/State.ts:62
Adds the given states to the instance's dependencies
, such that when those states are invalidated, so will be the instance.
Parameters:
Name | Type |
---|---|
...states |
Array‹State› |
Returns: this
Static
collection
▸ collection‹T›(this
: T, data
: Array‹object›): Promise‹Collection›
Defined in lib/Resource.ts:111
(async) Given an array of objects data
, attempts to restore
each object and convert the resulting Resource
instances to a Collection
.
Type parameters:
▪ T: typeof Resource
Parameters:
Name | Type | Description |
---|---|---|
this |
T | - |
data |
Array‹object› | An array of objects representing resource states. |
Returns: Promise‹Collection›
A promise resolving to a collection of resources.
Static
Protected
controller
▸ controller(options
: ControllerOptions, method
: any): Controller
Inherited from Controllable.controller
Defined in lib/abstract/Controllable.ts:102
Creates an instance of Controller
intended to be attached to a derived class as a static property.
Parameters:
Name | Type | Description |
---|---|---|
options |
ControllerOptions | An object defining the endpoint method and pattern, authorizers, schema, and dependencies. |
method |
any | A function defining endpoint business logic. |
Returns: Controller
Static
create
▸ create‹T›(this
: T, data
: object): Promise‹InstanceType‹T››
Defined in lib/Resource.ts:121
(async) Like Resource.restore
, attempts to create a new instance of the derived class from the plain object data
. Throws an Error
if data
cannot be validated using the derived class's schema
. The resulting State
will have the HTTP status CREATED
.
Type parameters:
▪ T: typeof Resource
Parameters:
Name | Type | Description |
---|---|---|
this |
T | - |
data |
object | The key-value pairs from which to construct the Resource instance. |
Returns: Promise‹InstanceType‹T››
Static
restore
▸ restore‹T›(this
: T, data
: object): Promise‹InstanceType‹T››
Defined in lib/Resource.ts:88
(async) Attempts to create a new instance of the derived class from the plain object data
. Throws an Error
if data
cannot be validated using the derived class's schema
. The resulting State
will have the HTTP status OK
.
Type parameters:
▪ T: typeof Resource
Parameters:
Name | Type | Description |
---|---|---|
this |
T | - |
data |
object | The key-value pairs from which to construct the Resource instance. |
Returns: Promise‹InstanceType‹T››
Static
root
▸ root(): string
Overrides Controllable.root
Defined in lib/Resource.ts:60
Returns the path from which all endpoints on the derived class originate.
Returns: string
Static
union
▸ union(...Classes
: Array‹typeof Resource›): Schema
Defined in lib/Resource.ts:73
Returns a new Schema
containing all fields of the derived class's schema plus all fields defined on the schemas of each Resource
type in Classes
. In case of a collision between field names, precedence will be given to former Resources
in Classes
, with highest precedence given to the derived class on which the method was called.
Parameters:
Name | Type | Description |
---|---|---|
...Classes |
Array‹typeof Resource› | The Resource |
Returns: Schema