A library for working with REST resources.
var resourcery = require('resourcery');
var resource = resourcery.resource('/foo/{bar}{?baz}')
.actions({ get: { method: 'GET', params: { bar: 'qux' } } })
.build();
resource.get({ baz: 'foobar' })
.then(function (barResource) {
// ...
})
.catch(function (error) {
// ...
});
Options can be a string containing a URI template or an object with the following properties:
Required. A URI template string, as defined in RFC6570.
Optional. The transport object that is responsible for making HTTP requests. It can be replaced with a mock transport for end-to-end and unit testing.
This method must be called at the end of the configuration chain. Returns an object that contains the resource actions. The action methods can be invoked with the following arguments:
- GET actions:
Resource.action([parameters], [options], [callback])
- non-GET actions:
Resource.action([parameters], data, [options], [callback])
The options
argument is an object containing setting overrides for the underlying transport. When overriding transport options, you must pass null
to the preceding arguments. For example:
resource.get(null, { headers: { 'X-Foo': 'foo' } });
A mock transport is provided for faking requests during development and for end-to-end or unit testing.
Creates a new request handler with the following methods:
Delays the response by the specified interval, in milliseconds.
Returns a response with the provided data when a matching request is found. The data
and headers
arguments accept callback functions that when called return the respective values.
Returns a wrapper around the default API. The provided options are applied to all resources created by the wrapped factory function:
var fakeTransport = { request: function () { console.log('fake'); } };
var r = resourcery.defaults({ transport: fakeTransport });
var resource = r.resource('https://example.org/{userId}')
.actions({ get: { method: 'GET' });
In the above example, all resources created from the wrapped object will use the fake transport.
Portions of this project were inspired by concepts from AngularJS.
The MIT License Copyright (c) 2010-2015 Google, Inc. https://angularjs.org
Refer to angular/angular.js for the full copyright notice.