rest-facade
Node.js module that abstracts the process of consuming a REST endpoint.
Installation
npm install rest-facade
Usage
Create a new endpoint client
When creating a new client, a URL must be given as first arguments. If the URL have dynamic params, those variable params must be marked with the colon notation, as shown below.
var rest = require('rest-facade');
var options = {
headers: {
Authorization: 'Bearer token'
},
errorFormatter: {
name: 'error.title',
message: 'error.text',
}
};
var Users = new rest.Client('http://domain.com/users/:id', options);
var UserVideos = new rest.Client('http://domain.com/users/:userId/videos/:slug');
Get all instances from the API
The getAll()
method can take an optional object as first parameters specifying the URL params. Considering the UserVideos model from last example:
UserVideos
.getAll({ userId: 4 })
.then(function (videos) {
console.log(videos.length, 'videos retrieved');
});
Get one instance from the API.
Users
.get({ id: 4 })
.then(function (user) {
console.log(user);
});
Create a new instance and save it to the API
The create method can be called using several signatures.
create(data)
returns a Promise.create(urlParams, data)
returns a Promise.create(data, callback)
doesn't return a promise.create(urlParams, data, callback)
doesn't return a promise.
Users
.create({ firstName: 'John', lastName: 'Doe' });
.then(function (user) {
console.log('User created');
});
UserVideos
.create({ userId: 4 }, { title: 'Learning Javascript', slug: 'learn-javascript' })
.then(function (video) {
console.log('User video created');
}):
Delete an instance from the API by ID
As it was the case with the create()
method, delete()
can also be called with different signatures.
delete(urlParams)
returns a Promise.delete(callback)
returns a Promise.delete(urlParams, callback)
doesn't return a Promise.
Users
.delete({ id: userId })
.then(function () {
console.log('User deleted');
});
UserVideos
.delete({ slug: 'learn-javascript' })
.then(function () {
});
Update an instance.
There are 2 ways to update data, if you are using correctly the HTTP methods, 1 is PUT and the other one is PATCH, rest-facade supports both of them:Client.update
and Client.patch
.
As with the previous methods, an object with the URL parameters must be provided as first argument. The second argument must be an object with the new data.
PUT request
Users
.update({ id: userId }, data)
.then(function () {
console.log('User updated');
});
PATCH request
Users
.patch({ id: userId }, data)
.then(function () {
console.log('User updated');
});
Both functions work exactly the same, the only differenct is the method used to perform the request.
Callbacks
All methods support callbacks. However, if a callback function is given no promise will be returned. Callbacks must always be provided after all other function arguments. E.g.:
Users.getAll(function (err, users) {
console.log(users.length, 'users found');
});
Query String
All methods accept an object with URL params as first argument. The properties in this object will be used to format the URL as shown above. However, the properties defined in this object, but not in the endpoint URL, will be added as query string params.
N.B. any properties in a given options
object whose values are Functions will be ignored with regard to generating the query string.
var Users = new rest.Client('http://domain.com/users/:id');
Users.get({ id: 1 });
Users.getAll({ page: 1, pageSize: 10 });
There may be some cases when you are working with an API that follows a different naming convention, and it is not really clean to have mixed naming conventions in our code.
Users.getAll({ page: 1, 'page_size': 10 });
You can solve this problem by specifing a naming convention when creating the Rest Client. The naming convention can be any of snakeCase
, camelCase
, pascalCase
, paramCase
, or any other implemented by the change-case library.
var Users = rest.Client('http://domain.com/users/:id', { query: { convertCase: 'snakeCase' }});
Users.getAll({ page: 1, pageSize: 10 });
Arrays
By default, arrays in the querystring will be formmated this way: ?a=1&a=2&a=2
. However, you can change it to comma separated values ?a=1,2,3
by setting the query.repeatParams
option to false
.
var client = new rest.Client(url, { query: { repeatParams: false }});
Body Case Conversion
Rest-facade provides options for converting the body of the request/response. So, let's say you are consuming an API implemented in Python (using snake_case) and you want it converted to camelCase. You would specify the following options:
var client = new rest.Client(url, {
request: {
body: {
convertCase: 'snakeCase'
}
},
response: {
body: {
convertCase: 'camelCase'
}
}
});
Once that's done, you can send any request and the body of it will be converted to snake_case. E.g.
client.create({ firstName: 'John', lastName: 'Doe' });
The same way, all the responses from the server will converted to the specified case (camelCase in this example).
Per-Request Customization
Sometimes you need to do some customization to each individual request that is sent to the consumed API,
a likely candidate is for adding request-specific headers.
This can be done in two ways:
- defining a function in global options under
options.request.customizer
- passing in a options object to a method call that contains a "special"
_requestCustomizer
property (which should be a function as well!)
You can define both, in which case both will be applied (in the order listed above).
In each case the function is passed the req
and params
representing the API call in question.