Types in rest.js are based on duck typing; there are no concrete types that need to be constructed. Any JavaScript object matching the general characterisitc for the type can be used. Most of the properties are defined as optional, so even an empty or undefined object may be valid. Clients and interceptors will provided default values as appropriate.
A request may be represented by either a string or an object. Strings are coerced to an object as soon as they are encountered, where the string's value becomes the value of the path property on the object.
Property | Required? | Default | Description |
---|---|---|---|
method | optional | 'GET' if no entity, 'POST' with entity | HTTP method, commonly GET, POST, PUT, DELETE or HEAD |
path | optional | empty string | path template with optional path variables |
params | optional | none | name-value parameters for the path template and query string |
headers | optional | none | name-value custom HTTP headers to send, in addition to the client provided headers |
entity | optional | none | HTTP entity, request/response body |
canceled | provided | n/a | indicates if the request was canceled, defined by the root client |
cancel | provided | n/a | function that will cancel the request if invoked, defined by the root client |
originator | provided | n/a | the first client to handle this request in the interceptor chain, often the outter most wrapped client |
Interceptors and clients may define additional properties.
Property | Description |
---|---|
request | the request object as received by the root client |
raw | the underlying request object, like XMLHttpRequest in a browser |
url | the actual URL requested. In some cases this value may be misleading, such as after a redirect in a browser. The value reported is URL for the request before the redirect. |
status.code | status code of the response (i.e. 200, 404) |
status.text | status phrase of the response (if available) |
headers | response headers hash of normalized name, value pairs |
entity | the response body |
Interceptors and clients may define additional properties.
Method | Arguments | Return | Description |
---|---|---|---|
self | request | Promise for Response | propagates the request retuning a promise for the response |
skip | none | Client | returns the parent client. Not available for the root client, a client may also elect to not be skipable. |
wrap | interceptor, config (optional) | Client | wraps the client with an interceptor returning the resulting client |
Clients return a promise for a response. In addition to the standard methods for a promise, and the supplimental methods when.js provides, rest.js adds additional methods for easy asynchronous access to parts of the response.
These methods assume a standard response object. If an interceptor or client returns an alternate response object, the behavior of these methods will be unpredictable.
Method | Arguments | Return | Description |
---|---|---|---|
entity | none | Promise<*> | Promise for the HTTP response entity |
status | none | Promise<number> | Promise for the HTTP response status code |
headers | none | Promise<Headers> | Promise for the HTTP response headers map |
header | headerName | Promise<Header> | Promise for a specific HTTP response headers. A Header may be a string or an array of strings depending on the number of the header name occurrences in the response |
follow | relationship | ResponsePromise<Response> | Traverse to the resource identified by the relationship. An array of relationships will traverse deep into the API following each relationship in turn. Params for templated URIs can be express in the form `{ rel: relationship, params: { ... } }` |
Method | Arguments | Return | Description |
---|---|---|---|
self | parent Client (optional), config (optional) | Client | creates a new client wrapping the parent client with the interceptor and provided configuration. |
Both the parent and config arguments are typically optional. The default client is commonly used if a parent client is not specified. An interceptor may require certain config properties, in which case the config object is no longer optional.
Method | Arguments | Return | Description |
---|---|---|---|
read | string | any | Promise<*> | reads a response entity as a string converting it into any other type |
write | any | string | Promise<string> | writes a request entity as a string converting it from any other type |