2.1.5 • Published 8 years ago

resti v2.1.5

Weekly downloads
41
License
ISC
Repository
github
Last release
8 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

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.0

8 years ago

2.0.9

8 years ago

2.0.8

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

0.0.1

9 years ago

1.0.1

9 years ago

1.0.31

9 years ago

1.0.30

9 years ago

1.0.29

9 years ago

1.0.28

9 years ago

1.0.27

9 years ago

1.0.26

9 years ago

1.0.25

9 years ago

1.0.23

9 years ago

1.0.22

9 years ago

1.0.21

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.0

9 years ago