UNPKG

6.84 kBMarkdownView Raw
1Rest Template
2=============
3
4Just enough client, as you need it. Make HTTP requests from a browser or Node.js applying the only the client features you need. Configure a client once, and share it safely throughout your application. Easily extend with interceptors that wrap the request and/or response, or MIME type converters for rich data formats.
5
6
7Build Status
8------------
9
10<table>
11 <tr><td>Master</td><td><a href="http://travis-ci.org/scothis/rest" target="_blank"><img src="https://secure.travis-ci.org/scothis/rest.png?branch=master" /></a></tr>
12 <tr><td>Development</td><td><a href="http://travis-ci.org/scothis/rest" target="_blank"><img src="https://secure.travis-ci.org/scothis/rest.png?branch=dev" /></a></tr>
13</table>
14
15
16Getting Started
17---------------
18
19Rest can be installed via NPM, or from source.
20
21To install without source:
22
23 $ npm install rest-template
24
25From source:
26
27 $ npm install
28
29Rest is designed to run in a browser environment, utilizing [AMD modules](https://github.com/amdjs/amdjs-api/wiki/AMD), or within [Node.js](http://nodejs.org/). [curl](https://github.com/cujojs/curl) is highly recommended as an AMD loader, although any loader should work.
30
31An ECMAScript 5 compatible environment is assumed. Older browsers, ::cough:: IE, that do not support ES5 natively can be shimmed. Any shim should work, although we've tested against cujo's [poly](https://github.com/cujojs/poly)
32
33
34Usage
35-----
36
37Using Rest is easy. The core clients provide limited functionality around the request and response lifecycle. The input and response objects are normalized to support portability between browser and server environments.
38
39The response from a client is a promise that is resolved when the remote request finishes.
40
41The core client behavior can be augmented with interceptors. An interceptor wraps the client and transforms the request and response. For example: an interceptor may authenticate a request, or reject the promise if an error is encountered. Interceptors may be combined to create a client with the desired behavior. A configured interceptor acts just like a client.
42
43
44### Making a basic request: ###
45
46```javascript
47define(['rest'], function(client) {
48 client({ path: '/' }).then(function(response) {
49 console.log('response: ', response);
50 });
51});
52```
53
54In this example, you can see that the request object is very simple, it just includes the path. All of the attributes of a request are optional.
55
56The response should look familiar as well, it contains all the fields you would expect, including the response headers (many clients ignore the headers).
57
58
59### Working with JSON: ###
60
61If you're paying attention, you may have noticed that the response.entity in the previous example is a String. Often we need to work with more complex data types. For this, Rest supports a rich set of MIME type conversions with the `mime` interceptor. The correct converter will automatically be chosen based on the Content-Type response header. Custom converts can be registered for a MIME type, more on that later...
62
63```javascript
64define(['rest/interceptor/mime'], function(mime) {
65 var client = mime();
66 client({ path: '/data.json' }).then(function(response) {
67 console.log('response: ', response);
68 });
69});
70```
71
72Before an interceptor can be used, it needs to be configured. In this case, we will accept the default configuration, and obtain a client. Now when we see the response, the entity will be a JS object instead of a String.
73
74
75### Composing Interceptors: ###
76
77```javascript
78define(['rest/interceptor/mime', 'rest/interceptor/errorCode'], function(mime, errorCode) {
79 var client = mime();
80 client = errorCode(client, { code: 500 });
81 client({ path: '/data.json' }).then(
82 function(response) {
83 console.log('response: ', response);
84 },
85 function(response) {
86 console.error('response error: ', response);
87 }
88 );
89});
90```
91
92In this example, we take the client create by the `mime` interceptor, and wrap it in the `errorCode` interceptor. The errorCode interceptor can accept a configuration object that indicates what status codes should be considered an error. In this case we override the default value of <=400, to only reject with 500 or greater status code.
93
94Since the errorCode interceptor can reject the response promise, we also add a second handler function to receive the response for requests in error.
95
96Clients can continue to be composed with interceptors as needed. At any point the client as configured can be shared. It is safe to share clients and allow other parts of your application to continue to compose other clients around the shared core. Your client is protected from additional interceptors that other parts of the application may add.
97
98
99### Custom MIME Converters: ###
100
101```javascript
102define(['rest/mime/registry'], function(registry) {
103 registry.register('application/vnd.com.example', {
104 read: function(str) {
105 var obj;
106 // do string to object conversions
107 return obj;
108 },
109 write: function(obj) {
110 var str;
111 // do object to string conversions
112 return str;
113 }
114 });
115});
116```
117
118Registering a custom converter is a simple as calling the register function on the mime registry with the type and converter. A converter has just two methods: `read` and `write`. Read converts a String to a more complex Object. Write converts an Object back into a String to be sent to the server. HTTP is fundamentally a text based protocol after all.
119
120Built in converters are available under `rest/mime/type/{type}`, as an example, JSON support is located at `rest/mime/type/application/json`. You never need to know this as a consumer, but it's a good place to find examples.
121
122
123Reporting Issues
124----------------
125
126Please report issues on [GitHub](https://github.com/scothis/rest/issues). Include a brief description of the error, information about the runtime (including shims) and any error messages.
127
128Feature requests are also welcome.
129
130
131Running the Tests
132-----------------
133
134The test suite can be run in two different modes: in node, or in a browser. We use [Buster.JS](http://busterjs.org/) as the test driver, buster is installed automatically with other dependencies.
135
136Before running the test suite for the first time:
137
138 $ npm install
139
140To run the suite in node:
141
142 $ npm test
143
144To run the suite in a browser:
145
146 $ npm start
147 browse to http://localhost:8282/ in the browser(s) you wish to test. It can take a few seconds to start.
148
149
150Thanks
151------
152
153* Arjen Poutsma - Creator of Spring's RestTemplate
154* Brian Cavalier - cujo.js lead
155* John Hann - cujo.js lead
156* VMware - for allowing this project to be open sourced
157
158
159Change Log
160----------
161
1620.7.5
163- Initial release, everything is new
164
\No newline at end of file