resti

```javascript npm install --save resti ```

Usage no npm install needed!

<script type="module">
  import resti from 'https://cdn.skypack.dev/resti';
</script>

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