UNPKG

6.17 kBMarkdownView Raw
1# Clients
2
3- Provided Clients
4 - [Default Client](#module-rest)
5 - [XMLHttpReqest Client](#module-rest/client/xhr)
6 - [Node Client](#module-rest/client/node)
7 - [JSONP Client](#module-rest/client/jsonp)
8 - [IE XDomainRequest Client](#module-rest/client/xdr)
9
10
11## Overview
12
13A rest.js [client](interfaces.md#interface-client) is simply a function that accepts an argument as the [request](interfaces.md#interface-request) and returns a promise for the [response](interfaces.md#interface-response).
14
15Clients are typically extended by wrapping interceptors that wrap the client core behavior providing additional functionality and returning an enriched client.
16
17```javascript
18client = rest.wrap(interceptor);
19assert.same(rest, client.skip());
20```
21
22See the [interceptor docs](interceptors.md) for more information on interceptors and wrapping.
23
24
25## Provided Clients
26
27The provided clients are the root of the interceptor chain. They are responsible for the lowest level mechanics of making requests and handling responses. In most cases, the developer doesn't need to be concerned with the particulars of the client, as the best client for the available environment will be chosen automatically.
28
29
30<a name="module-rest"></a>
31### Default Client
32
33`rest` ([src](../rest.js))
34
35The default client is also the main module for the rest.js package. It's not a client implementation, but an alias to the best client for a platform. When running within a browser, the XHR client is used; when running within Node.js, the Node client is used. As other JavaScript environments are supported, the default client will continue to map directly to the most appropriate client implementation.
36
37The default client is used internally when no custom client is configured. There are times, when it's useful to change the default client; such as when the automatic environment sniffing is wrong, or you want to add support for a new environment that rest.js doesn't yet understand. In these cases, you can set, get and reset the default client using the `rest.setDefaultClient(client)`, `rest.getDefaultClient()` and `rest.resetDefaultClient()` methods respectively.
38
39While it may be tempting to change the default client for application level concerns, changing the default will impact all consumers. In just about every case, using an [Interceptor](./interceptors.md) is preferred.
40
41
42<a name="module-rest/client/xhr"></a>
43### XMLHttpReqest Client
44
45`rest/client/xhr` ([src](../client/xhr.js))
46
47The default client for browsers. The XHR client utilizes the XMLHttpRequest object provided by many browsers. Most every browser has direct support for XHR today. The `rest/interceptor/ie/xhr` interceptor can provided fall back support for older IE without native XHR.
48
49**Special Properties**
50
51<table>
52<tr>
53 <th>Property</th>
54 <th>Required?</th>
55 <th>Default</th>
56 <th>Description</th>
57</tr>
58<tr>
59 <td>request.engine</td>
60 <td>optional</td>
61 <td>window.XMLHttpRequest</td>
62 <td>The XMLHttpRequest instance to use</td>
63</tr>
64<tr>
65 <td>request.mixin</td>
66 <td>optional</td>
67 <td><em>none</em></td>
68 <td>Additional properties to mix into the XHR object</td>
69</tr>
70</table>
71
72**Know limitations**
73
74The XHR client has the same security restrictions as the traditional XMLHttpRequest object. For browsers that support XHR v1, that means that requests may only be made to the same origin as the web page. The origin being defined by the scheme, host and port. XHR v2 clients have support for Cross-origin Resource Sharing (CORS). CORS enabled clients have the ability to make requests to any HTTP based service assuming the server is willing to participate in the [CORS dance](http://www.html5rocks.com/en/tutorials/cors/).
75
76
77<a name="module-rest/client/node"></a>
78### Node Client
79
80`rest/client/node` ([src](../client/node.js))
81
82The default client for Node.js. The Node client uses the 'http' and 'https' modules.
83
84Node specific settings may be modified via the `mixin` request property. Adding new certificate authorities to trust or changing the agent pool are rare, but sometimes necessary. See the [Node docs](http://nodejs.org/api/https.html#https_https_request_options_callback) for details about supported properties.
85
86**Special Properties**
87
88<table>
89<tr>
90 <th>Property</th>
91 <th>Required?</th>
92 <th>Default</th>
93 <th>Description</th>
94</tr>
95<tr>
96 <td>request.mixin</td>
97 <td>optional</td>
98 <td><em>empty</em></td>
99 <td>Additional Node.js only parameters</td>
100</tr>
101</table>
102
103
104<a name="module-rest/client/jsonp"></a>
105### JSONP Client
106
107`rest/client/jsonp` ([src](../client/jsonp.js))
108
109JSONP client for browsers. Allows basic cross-origin GETs via script tags. This client is typically employed via the `rest/interceptor/jsonp` interceptor. Never used as the default client.
110
111**Special Properties**
112
113<table>
114<tr>
115 <th>Property</th>
116 <th>Required?</th>
117 <th>Default</th>
118 <th>Description</th>
119</tr>
120<tr>
121 <td>request.callback.param</td>
122 <td>optional</td>
123 <td>'callback'</td>
124 <td>URL parameter that contains the JSONP callback function's name</td>
125</tr>
126<tr>
127 <td>request.callback.prefix</td>
128 <td>optional</td>
129 <td>'jsonp'</td>
130 <td>common prefix for callback function names as they are placed on the window object</td>
131</tr>
132<tr>
133 <td>request.callback.name</td>
134 <td>optional</td>
135 <td><em>generated</em></td>
136 <td>pins the name of the callback function, useful for cases where the server doesn't allow custom callback names. Generally not recommended.</td>
137</tr>
138</table>
139
140
141<a name="module-rest/client/xdr"></a>
142### IE XDomainRequest Client
143
144`rest/client/xdr` ([src](../client/xdr.js))
145
146Cross-origin support available within IE, in particular IE 8 and 9. This client is typically employed via the `rest/interceptor/ie/xdomain` interceptor. Never used as the default client.
147
148**Know limitations**
149
150- only GET and POST methods are available
151- must use same scheme as origin http-to-http, https-to-https
152- no headers, request or response (the response Content-Type is available)
153- no response status code
154
155[Limitation details](http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx)