# resti

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

Or grab the script from the [build folder](https://github.com/ergusto/resti/tree/master/build). Documentation can be found [here](docs/index.md).

## Overview


Create resources to interact with REST APIs.

```javascript
// 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:

```javascript
// 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.

```javascript
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';
    }
});
```

```javascript
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