resti
v2.1.5
Published
```javascript npm install --save resti ```
Downloads
19
Readme
resti
npm install --save resti
Or grab the script from the build folder. Documentation can be found here.
Overview
Create resources to interact with REST APIs.
// create base resource at URI http://swapi.co/api
var api = new resti('http://swapi.co/api');
Build up a resource by using the 'one' and 'many methods', then create and send a request:
// resource for multiple starships
api.starships = api.many('starships');
// resource for single starship
var milleniumFalcon = api.starships.one(10);
// sends GET request to http://swapi.co/api/starships/10
milleniumFalcon.get(function(res) {
if (res.ok)
doSomethingWithResponse(res.body);
});
Middleware
Use connect/express-like middleware to intercept and respond to requests.
api.before(function attachToken(req, next) {
const token = cookie.load('token');
// only send request if we have a token
if (token) {
req.header('Authorization', token);
next();
} else {
// user's token has expired and must sign in again
window.location.href = '/login';
}
});
api.use(function(res, next) {
// Flag for 401 HTTP status code.
if (res.unauthorized) {
return res.redirect('/unauthorized');
}
next();
});
Endpoints inherit the middleware of their parents as long as it is defined on the parent before cloning.
License
MIT