UNPKG

5.92 kBMarkdownView Raw
1# Common Interfaces
2
3Types in rest.js are based on duck typing; there are no concrete types that need to be constructed. Any JavaScript object matching the general characterisitc for the type can be used. Most of the properties are defined as optional, so even an empty or undefined object may be valid. Clients and interceptors will provided default values as appropriate.
4
5
6<a name="interface-request"></a>
7## Common Request Properties
8
9A request may be represented by either a string or an object. Strings are coerced to an object as soon as they are encountered, where the string's value becomes the value of the path property on the object.
10
11<table>
12<tr>
13 <th>Property</th>
14 <th>Required?</th>
15 <th>Default</th>
16 <th>Description</th>
17</tr>
18<tr>
19 <td>method</td>
20 <td>optional</td>
21 <td>'GET' if no entity, 'POST' with entity</td>
22 <td>HTTP method, commonly GET, POST, PUT, DELETE or HEAD</td>
23</tr>
24<tr>
25 <td>path</td>
26 <td>optional</td>
27 <td><em>empty string</em></td>
28 <td>path template with optional path variables</td>
29</tr>
30<tr>
31 <td>params</td>
32 <td>optional</td>
33 <td><em>none</em></td>
34 <td>name-value parameters for the path template and query string</td>
35</tr>
36<tr>
37 <td>headers</td>
38 <td>optional</td>
39 <td><em>none</em></td>
40 <td>name-value custom HTTP headers to send, in addition to the client provided headers</td>
41</tr>
42<tr>
43 <td>entity</td>
44 <td>optional</td>
45 <td><em>none</em></td>
46 <td>HTTP entity, request/response body</td>
47</tr>
48<tr>
49 <td>canceled</td>
50 <td>provided</td>
51 <td><em>n/a</em></td>
52 <td>indicates if the request was canceled, defined by the root client</td>
53</tr>
54<tr>
55 <td>cancel</td>
56 <td>provided</td>
57 <td><em>n/a</em></td>
58 <td>function that will cancel the request if invoked, defined by the root client</td>
59</tr>
60<tr>
61 <td>originator</td>
62 <td>provided</td>
63 <td><em>n/a</em></td>
64 <td>the first client to handle this request in the interceptor chain, often the outter most wrapped client</td>
65</tr>
66</table>
67
68Interceptors and clients may define additional properties.
69
70
71<a name="interface-response"></a>
72## Common Response Properties
73
74<table>
75<tr>
76 <th>Property</th><th>Description</td>
77</tr>
78<tr>
79 <td>request</td>
80 <td>the request object as received by the root client</td>
81</tr>
82<tr>
83 <td>raw</td>
84 <td>the underlying request object, like XMLHttpRequest in a browser</td>
85</tr>
86<tr>
87 <td>status.code</td>
88 <td>status code of the response (i.e. 200, 404)</td>
89</tr>
90<tr>
91 <td>status.text</td>
92 <td>status phrase of the response (if available)</td>
93</tr>
94<tr>
95 <td>headers</td>
96 <td>response headers hash of normalized name, value pairs</td>
97</tr>
98<tr>
99 <td>entity</td>
100 <td>the response body</td>
101</tr>
102</table>
103
104Interceptors and clients may define additional properties.
105
106
107<a name="interface-client"></a>
108## Client Methods
109
110[Usage](clients.md)
111
112<table>
113<tr>
114 <th>Method</th>
115 <th>Arguments</th>
116 <th>Return</th>
117 <th>Description</th>
118</tr>
119<tr>
120 <td><em>self</em></td>
121 <td>request</td>
122 <td>Promise for Response</td><td>propagates the request retuning a promise for the response</td>
123</tr>
124<tr>
125 <td>skip</td>
126 <td><em>none</em></td>
127 <td>Client</td>
128 <td>returns the parent client. Not available for the root client, a client may also elect to not be skipable.</td>
129</tr>
130<tr>
131 <td>wrap</td>
132 <td>interceptor, config (optional)</td>
133 <td>Client</td>
134 <td>wraps the client with an interceptor returning the resulting client</td>
135</tr>
136</table>
137
138
139<a name="interave-responsepromise"></a>
140## Response Promise Methods
141
142Clients return a promise for a response. In addition to the standard methods for a promise, and the supplimental methods when.js provides, rest.js adds additional methods for easy asynchronous access to parts of the response.
143
144These methods assume a standard response object. If an interceptor or client returns an alternate response object, the behavior of these methods will be unpredictable.
145
146<table>
147<tr>
148 <th>Method</th>
149 <th>Arguments</th>
150 <th>Return</th>
151 <th>Description</th>
152</tr>
153<tr>
154 <td>entity</td>
155 <td><em>none</em></td>
156 <td>Promise&lt;*&gt;</td>
157 <td>Promise for the HTTP response entity</td>
158</tr>
159<tr>
160 <td>status</td>
161 <td><em>none</em></td>
162 <td>Promise&lt;number&gt;</td>
163 <td>Promise for the HTTP response status code</td>
164</tr>
165<tr>
166 <td>headers</td>
167 <td><em>none</em></td>
168 <td>Promise&lt;Headers&gt;</td>
169 <td>Promise for the HTTP response headers map</td>
170</tr>
171<tr>
172 <td>header</td>
173 <td>headerName</td>
174 <td>Promise&lt;Header&gt;</td>
175 <td>Promise for a specific HTTP response headers. A Header may be a string or an array of strings depending on the number of the header name occurances in the response</td>
176</tr>
177</table>
178
179
180<a name="interface-interceptor"></a>
181## Interceptor Methods
182
183[Usage](interceptors.md)
184
185<table>
186<tr>
187 <th>Method</th>
188 <th>Arguments</th>
189 <th>Return</th>
190 <th>Description</th>
191</tr>
192<tr>
193 <td><em>self</em></td>
194 <td>parent Client (optional), config (optional)</td>
195 <td>Client</td>
196 <td>creates a new client wrapping the parent client with the interceptor and provided configuration.</td>
197</tr>
198</table>
199
200Both the parent and config arguments are typically optional. The default client is commonly used if a parent client is not specified. An interceptor may require certain config properties, in which case the config object is no longer optional.
201
202
203<a name="interface-converter"></a>
204## MIME Converter
205
206[Usage](mime.md#mime-converters-custom)
207
208<table>
209<tr>
210 <th>Method</th>
211 <th>Arguments</th>
212 <th>Return</th>
213 <th>Description</th>
214</tr>
215<tr>
216 <td>read</td>
217 <td>string</td>
218 <td><em>any</em> | Promise&lt;*&gt;</td>
219 <td>reads a response entity as a string converting it into any other type</td>
220</tr>
221<tr>
222 <td>write</td>
223 <td><em>any</em></td>
224 <td>string | Promise&lt;string&gt;</td>
225 <td>writes a request entity as a string converting it from any other type</td>
226</tr>
227</table>