1 | ## API Resource
|
2 |
|
3 | > Simple wrapper for http resources for client-side JavaScript or Node.js
|
4 |
|
5 | ## Setup
|
6 |
|
7 | `npm install api-resource`
|
8 |
|
9 | ## Simple usage
|
10 |
|
11 | ```javascript
|
12 | var Resource = require('api-resource');
|
13 |
|
14 | var listUsers = new Resource({
|
15 | method: 'get',
|
16 | route: 'http://localhost/users/:group',
|
17 | }),
|
18 | createUser: new Resource({
|
19 | method: 'post',
|
20 | route: 'http://localhost/users',
|
21 | });
|
22 |
|
23 | // Fires GET request to http://localhost/users/followers?q=foo
|
24 | listUsers.query({ // Request payload + route variables
|
25 | group: 'followers'
|
26 | }, { // Query params
|
27 | q: 'foo'
|
28 | })
|
29 | .then(function (res) {
|
30 | // Response code was successful (<= 200)
|
31 | var results = res.body;
|
32 | }, function (res) {
|
33 | // Response code was not successful (> 200)
|
34 | throw new Error('Users list responded ' + res.status + ', ' + res.body);
|
35 | })
|
36 |
|
37 | // Fires GET request to http://localhost/users/followers?q=foo
|
38 | createUser.query({ // Request payload + route variables
|
39 | username: 'johndoe'
|
40 | })
|
41 | .then(function (res) {
|
42 | var response = res;
|
43 | });
|
44 | ```
|
45 |
|
46 | ## Middleware example
|
47 |
|
48 | ```javascript
|
49 | var Resource = require('api-resource'),
|
50 | token = 'arbitrary-auth-token',
|
51 | session;
|
52 |
|
53 | var sessionEndpoint = new Resource({
|
54 | method: 'get',
|
55 | route: 'http://localhost/session'
|
56 | });
|
57 |
|
58 | sessionEndpoint.use(function (req, next) {
|
59 | // Always add authorisation header to this resource
|
60 | req.setRequestHeader('Authorization', token);
|
61 |
|
62 | // Bind a callback to an event on this resource
|
63 | this.once('success', function (res) {
|
64 | console.log('Session received correctly');
|
65 | });
|
66 | });
|
67 |
|
68 | sessionEndpoint.query()
|
69 | .then(function (res) {
|
70 | session = res.body;
|
71 | });
|
72 | ```
|
73 |
|
74 | ## Resource
|
75 |
|
76 | Resource instances are also event-emitters.
|
77 | After querying them you can listen you can handle responses by using the promised form or by binding callbacks to their events.
|
78 |
|
79 | #### Options
|
80 |
|
81 | * `method` (*String*) Case insensitive HTTP method (E.g. `'post'`)
|
82 | * `route` (*String*) Dynamic resource route (E.g. `'http://www.foo.bar/users/:username'`)
|
83 |
|
84 | #### Events
|
85 |
|
86 | * `response` Fired when a response is received, with a response object passed as argument
|
87 | * `success` Fired when a successful response (Status <= 200) is received, with a response object passed as argument
|
88 | * `failure` Fired when a unsuccessful response (Status > 200) is received, with a response object passed as argument
|
89 | * `error` Fired when an error occurs sending the XMLHttpRequest, with the error passed as argument
|
90 |
|
91 | #### Methods
|
92 |
|
93 | `.query([ data ], [ query_params ])` Query endpoint. The values in the `data` object will also be used to build the URL if the resource route is dynamic. Query params will be added to the final URL as a query string.
|
94 | `.use(middleware)` Add a middleware function to the resource. The function will be called on the `Resource` instance and receive as arguments the `XMLHttpRequest` request instance and the `next` function to proceed.
|
95 |
|
96 | ## Test
|
97 |
|
98 | Tests are currently written on server-side only. Run `npm install` and `npm test` to test.
|
99 |
|
100 | ## Contribution
|
101 |
|
102 | Contributions are welcome as long as documented and tested.
|
103 |
|
104 | ## License
|
105 |
|
106 | Copyright (c) 2014 Kano Computing Ltd. - Released under the [MIT license](https://github.com/KanoComputing/js-api-resource/blob/master/LICENSE) |
\ | No newline at end of file |