2.1.5 • Published 9 years ago

resti v2.1.5

Weekly downloads
41
License
ISC
Repository
github
Last release
9 years ago

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

2.1.5

9 years ago

2.1.4

9 years ago

2.1.3

9 years ago

2.1.2

9 years ago

2.1.0

9 years ago

2.0.9

9 years ago

2.0.8

9 years ago

2.0.7

9 years ago

2.0.6

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.1.6

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

0.0.1

10 years ago

1.0.1

10 years ago

1.0.31

10 years ago

1.0.30

10 years ago

1.0.29

10 years ago

1.0.28

10 years ago

1.0.27

10 years ago

1.0.26

10 years ago

1.0.25

10 years ago

1.0.23

10 years ago

1.0.22

10 years ago

1.0.21

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.0

10 years ago