superagent 3.5.1 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/History.md +8 -0
- package/Makefile +2 -2
- package/Readme.md +3 -3
- package/changelog.sh +7 -0
- package/docs/head.html +1 -0
- package/docs/index.md +65 -69
- package/docs/style.css +5 -0
- package/docs/tail.html +14 -7
- package/lib/client.js +6 -33
- package/lib/node/index.js +41 -50
- package/lib/request-base.js +30 -1
- package/package.json +16 -16
- package/superagent.js +43 -58
- package/yarn.lock +3889 -0
- package/lib/is-function.js +0 -15
package/History.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
# 3.6.0 (2017-08-20)
|
|
2
|
+
|
|
3
|
+
* Support disabling TCP_NODELAY option (#1240) (xiamengyu)
|
|
4
|
+
* Send payload in query string for GET and HEAD shorthand API (Peter Lyons)
|
|
5
|
+
* Support passphrase with pfx certificate (Paul Westerdale (ABRS Limited))
|
|
6
|
+
* Documentation improvements (Peter Lyons)
|
|
7
|
+
* Fixed duplicated query string params (#1200) (Kornel)
|
|
8
|
+
|
|
1
9
|
# 3.5.1 (2017-03-18)
|
|
2
10
|
|
|
3
11
|
* Allow crossDomain errors to be retried (#1194) (Michael Olson)
|
package/Makefile
CHANGED
|
@@ -44,12 +44,12 @@ index.html: docs/index.md docs/head.html docs/tail.html
|
|
|
44
44
|
> $@
|
|
45
45
|
|
|
46
46
|
docclean:
|
|
47
|
-
rm -f index.html test.html
|
|
47
|
+
rm -f index.html docs/test.html
|
|
48
48
|
|
|
49
49
|
test-docs: docs/head.html docs/tail.html
|
|
50
50
|
make test REPORTER=doc \
|
|
51
51
|
| cat docs/head.html - docs/tail.html \
|
|
52
|
-
> test.html
|
|
52
|
+
> docs/test.html
|
|
53
53
|
|
|
54
54
|
clean:
|
|
55
55
|
rm -fr superagent.js components
|
package/Readme.md
CHANGED
|
@@ -19,7 +19,7 @@ Works with [browserify](https://github.com/substack/node-browserify) and should
|
|
|
19
19
|
```js
|
|
20
20
|
request
|
|
21
21
|
.post('/api/pet')
|
|
22
|
-
.send({ name: 'Manny', species: 'cat' })
|
|
22
|
+
.send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
|
|
23
23
|
.set('X-API-Key', 'foobar')
|
|
24
24
|
.set('Accept', 'application/json')
|
|
25
25
|
.end(function(err, res){
|
|
@@ -64,8 +64,8 @@ Existing plugins:
|
|
|
64
64
|
* [superagent-suffix](https://github.com/timneutkens1/superagent-suffix) - suffix URLs with a given path
|
|
65
65
|
* [superagent-mock](https://github.com/M6Web/superagent-mock) - simulate HTTP calls by returning data fixtures based on the requested URL
|
|
66
66
|
* [superagent-mocker](https://github.com/shuvalov-anton/superagent-mocker) — simulate REST API
|
|
67
|
-
* [superagent-cache](https://github.com/jpodwys/superagent-cache) - A global SuperAgent patch with built-in, flexible caching
|
|
68
|
-
* [superagent-cache-plugin](https://github.com/jpodwys/superagent-cache-plugin) - A SuperAgent plugin with built-in, flexible caching
|
|
67
|
+
* [superagent-cache](https://github.com/jpodwys/superagent-cache) - A global SuperAgent patch with built-in, flexible caching
|
|
68
|
+
* [superagent-cache-plugin](https://github.com/jpodwys/superagent-cache-plugin) - A SuperAgent plugin with built-in, flexible caching
|
|
69
69
|
* [superagent-jsonapify](https://github.com/alex94puchades/superagent-jsonapify) - A lightweight [json-api](http://jsonapi.org/format/) client addon for superagent
|
|
70
70
|
* [superagent-serializer](https://github.com/zzarcon/superagent-serializer) - Converts server payload into different cases
|
|
71
71
|
* [superagent-use](https://github.com/koenpunt/superagent-use) - A client addon to apply plugins to all requests.
|
package/changelog.sh
ADDED
package/docs/head.html
CHANGED
package/docs/index.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
# SuperAgent
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.js!
|
|
5
5
|
|
|
6
6
|
request
|
|
7
7
|
.post('/api/pet')
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
|
|
19
19
|
## Test documentation
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
The following [test documentation](docs/test.html) was generated with [Mocha's](http://mochajs.org/) "doc" reporter, and directly reflects the test suite. This provides an additional source of documentation.
|
|
22
22
|
|
|
23
23
|
## Request basics
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
A request can be initiated by invoking the appropriate method on the `request` object, then calling `.end()` to send the request. For example a simple __GET__ request:
|
|
26
26
|
|
|
27
27
|
request
|
|
28
28
|
.get('/search')
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
A method string may also be passed:
|
|
34
34
|
|
|
35
35
|
request('GET', '/search').end(callback);
|
|
36
36
|
|
|
@@ -38,7 +38,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
38
38
|
|
|
39
39
|
request('GET', '/search').then(success, failure);
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
The __Node__ client may also provide absolute URLs. In browsers absolute URLs won't work unless the server implements [CORS](#cors).
|
|
42
42
|
|
|
43
43
|
request
|
|
44
44
|
.get('http://example.com/search')
|
|
@@ -46,7 +46,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
46
46
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
The __Node__ client supports making requests to [Unix Domain Sockets](http://en.wikipedia.org/wiki/Unix_domain_socket):
|
|
50
50
|
|
|
51
51
|
// pattern: https?+unix://SOCKET_PATH/REQUEST_PATH
|
|
52
52
|
// Use `%2F` as `/` in SOCKET_PATH
|
|
@@ -56,7 +56,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
56
56
|
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
__DELETE__, __HEAD__, __PATCH__, __POST__, and __PUT__ requests can also be used, simply change the method name:
|
|
60
60
|
|
|
61
61
|
request
|
|
62
62
|
.head('/favicon.ico')
|
|
@@ -64,7 +64,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
64
64
|
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
__DELETE__ can be also called as `.del()` for compatibility with old IE where `delete` is a reserved word.
|
|
68
68
|
|
|
69
69
|
The HTTP method defaults to __GET__, so if you wish, the following is valid:
|
|
70
70
|
|
|
@@ -74,7 +74,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
74
74
|
|
|
75
75
|
## Setting header fields
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
Setting header fields is simple, invoke `.set()` with a field name and value:
|
|
78
78
|
|
|
79
79
|
request
|
|
80
80
|
.get('/search')
|
|
@@ -82,7 +82,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
82
82
|
.set('Accept', 'application/json')
|
|
83
83
|
.end(callback);
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
You may also pass an object to set several fields in a single call:
|
|
86
86
|
|
|
87
87
|
request
|
|
88
88
|
.get('/search')
|
|
@@ -91,7 +91,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
91
91
|
|
|
92
92
|
## `GET` requests
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
The `.query()` method accepts objects, which when used with the __GET__ method will form a query-string. The following will produce the path `/search?query=Manny&range=1..5&order=desc`.
|
|
95
95
|
|
|
96
96
|
request
|
|
97
97
|
.get('/search')
|
|
@@ -102,7 +102,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
102
102
|
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
Or as a single object:
|
|
106
106
|
|
|
107
107
|
request
|
|
108
108
|
.get('/search')
|
|
@@ -111,7 +111,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
111
111
|
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
The `.query()` method accepts strings as well:
|
|
115
115
|
|
|
116
116
|
request
|
|
117
117
|
.get('/querystring')
|
|
@@ -120,7 +120,7 @@ ES6 promises are supported. *Instead* of `.end()` you can call `.then()`:
|
|
|
120
120
|
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
Or joined:
|
|
124
124
|
|
|
125
125
|
request
|
|
126
126
|
.get('/querystring')
|
|
@@ -143,27 +143,27 @@ You can also use the `.query()` method for HEAD requests. The following will pro
|
|
|
143
143
|
|
|
144
144
|
## `POST` / `PUT` requests
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
A typical JSON __POST__ request might look a little like the following, where we set the Content-Type header field appropriately, and "write" some data, in this case just a JSON string.
|
|
147
147
|
|
|
148
148
|
request.post('/user')
|
|
149
149
|
.set('Content-Type', 'application/json')
|
|
150
150
|
.send('{"name":"tj","pet":"tobi"}')
|
|
151
151
|
.end(callback)
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
Since JSON is undoubtedly the most common, it's the _default_! The following example is equivalent to the previous.
|
|
154
154
|
|
|
155
155
|
request.post('/user')
|
|
156
156
|
.send({ name: 'tj', pet: 'tobi' })
|
|
157
157
|
.end(callback)
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
Or using multiple `.send()` calls:
|
|
160
160
|
|
|
161
161
|
request.post('/user')
|
|
162
162
|
.send({ name: 'tj' })
|
|
163
163
|
.send({ pet: 'tobi' })
|
|
164
164
|
.end(callback)
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
By default sending strings will set the `Content-Type` to `application/x-www-form-urlencoded`,
|
|
167
167
|
multiple calls will be concatenated with `&`, here resulting in `name=tj&pet=tobi`:
|
|
168
168
|
|
|
169
169
|
request.post('/user')
|
|
@@ -171,24 +171,30 @@ You can also use the `.query()` method for HEAD requests. The following will pro
|
|
|
171
171
|
.send('pet=tobi')
|
|
172
172
|
.end(callback);
|
|
173
173
|
|
|
174
|
-
|
|
174
|
+
SuperAgent formats are extensible, however by default "json" and "form" are supported. To send the data as `application/x-www-form-urlencoded` simply invoke `.type()` with "form", where the default is "json". This request will __POST__ the body "name=tj&pet=tobi".
|
|
175
175
|
|
|
176
176
|
request.post('/user')
|
|
177
177
|
.type('form')
|
|
178
178
|
.send({ name: 'tj' })
|
|
179
179
|
.send({ pet: 'tobi' })
|
|
180
180
|
.end(callback)
|
|
181
|
+
|
|
182
|
+
Sending a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData) object is also supported. The following example will __POST__ the content of the HTML form identified by id="myForm":
|
|
183
|
+
|
|
184
|
+
request.post('/user')
|
|
185
|
+
.send(new FormData(document.getElementById('myForm')))
|
|
186
|
+
.end(callback)
|
|
181
187
|
|
|
182
188
|
## Setting the `Content-Type`
|
|
183
189
|
|
|
184
|
-
|
|
190
|
+
The obvious solution is to use the `.set()` method:
|
|
185
191
|
|
|
186
192
|
request.post('/user')
|
|
187
193
|
.set('Content-Type', 'application/json')
|
|
188
194
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
195
|
+
As a short-hand the `.type()` method is also available, accepting
|
|
196
|
+
the canonicalized MIME type name complete with type/subtype, or
|
|
197
|
+
simply the extension name such as "xml", "json", "png", etc:
|
|
192
198
|
|
|
193
199
|
request.post('/user')
|
|
194
200
|
.type('application/json')
|
|
@@ -231,7 +237,7 @@ If you are calling Facebook's API, be sure to send an `Accept: application/json`
|
|
|
231
237
|
|
|
232
238
|
## Query strings
|
|
233
239
|
|
|
234
|
-
`
|
|
240
|
+
`req.query(obj)` is a method which may be used to build up a query-string. For example populating `?format=json&dest=/login` on a __POST__:
|
|
235
241
|
|
|
236
242
|
request
|
|
237
243
|
.post('/')
|
|
@@ -240,7 +246,7 @@ If you are calling Facebook's API, be sure to send an `Accept: application/json`
|
|
|
240
246
|
.send({ post: 'data', here: 'wahoo' })
|
|
241
247
|
.end(callback);
|
|
242
248
|
|
|
243
|
-
|
|
249
|
+
By default the query string is not assembled in any particular order. An asciibetically-sorted query string can be enabled with `req.sortQuery()`. You may also provide a custom sorting comparison function with `req.sortQuery(myComparisonFn)`. The comparison function should take 2 arguments and return a negative/zero/positive integer.
|
|
244
250
|
|
|
245
251
|
```js
|
|
246
252
|
// default order
|
|
@@ -293,19 +299,19 @@ request
|
|
|
293
299
|
|
|
294
300
|
## Parsing response bodies
|
|
295
301
|
|
|
296
|
-
|
|
302
|
+
SuperAgent will parse known response-body data for you, currently supporting `application/x-www-form-urlencoded`, `application/json`, and `multipart/form-data`.
|
|
297
303
|
|
|
298
|
-
|
|
304
|
+
You can set a custom parser (that takes precedence over built-in parsers) with the `.buffer(true).parse(fn)` method. If response buffering is not enabled (`.buffer(false)`) then the `response` event will be emitted without waiting for the body parser to finish, so `response.body` won't be available.
|
|
299
305
|
|
|
300
306
|
### JSON / Urlencoded
|
|
301
307
|
|
|
302
|
-
|
|
308
|
+
The property `res.body` is the parsed object, for example if a request responded with the JSON string '{"user":{"name":"tobi"}}', `res.body.user.name` would be "tobi". Likewise the x-www-form-urlencoded value of "user[name]=tobi" would yield the same result. Only one level of nesting is supported. If you need more complex data, send JSON instead.
|
|
303
309
|
|
|
304
|
-
|
|
310
|
+
Arrays are sent by repeating the key. `.send({color: ['red','blue']})` sends `color=red&color=blue`. If you want the array keys to contain `[]` in their name, you must add it yourself, as SuperAgent doesn't add it automatically.
|
|
305
311
|
|
|
306
312
|
### Multipart
|
|
307
313
|
|
|
308
|
-
|
|
314
|
+
The Node client supports _multipart/form-data_ via the [Formidable](https://github.com/felixge/node-formidable) module. When parsing multipart responses, the object `res.files` is also available to you. Suppose for example a request responds with the following multipart body:
|
|
309
315
|
|
|
310
316
|
--whoop
|
|
311
317
|
Content-Disposition: attachment; name="image"; filename="tobi.png"
|
|
@@ -319,7 +325,7 @@ request
|
|
|
319
325
|
Tobi
|
|
320
326
|
--whoop--
|
|
321
327
|
|
|
322
|
-
|
|
328
|
+
You would have the values `res.body.name` provided as "Tobi", and `res.files.image` as a `File` object containing the path on disk, filename, and other properties.
|
|
323
329
|
|
|
324
330
|
### Binary
|
|
325
331
|
|
|
@@ -340,32 +346,27 @@ For more information, see the Mozilla Developer Network [xhr.responseType docs](
|
|
|
340
346
|
|
|
341
347
|
## Response properties
|
|
342
348
|
|
|
343
|
-
|
|
349
|
+
Many helpful flags and properties are set on the `Response` object, ranging from the response text, parsed response body, header fields, status flags and more.
|
|
344
350
|
|
|
345
351
|
### Response text
|
|
346
352
|
|
|
347
|
-
|
|
348
|
-
property is always present for the client API, and only when the mime type
|
|
349
|
-
matches "text/*", "*/json", or "x-www-form-urlencoded" by default for node. The
|
|
350
|
-
reasoning is to conserve memory, as buffering text of large bodies such as multipart files or images is extremely inefficient.
|
|
351
|
-
|
|
352
|
-
To force buffering see the "Buffering responses" section.
|
|
353
|
+
The `res.text` property contains the unparsed response body string. This property is always present for the client API, and only when the mime type matches "text/*", "*/json", or "x-www-form-urlencoded" by default for node. The reasoning is to conserve memory, as buffering text of large bodies such as multipart files or images is extremely inefficient. To force buffering see the "Buffering responses" section.
|
|
353
354
|
|
|
354
355
|
### Response body
|
|
355
356
|
|
|
356
|
-
|
|
357
|
+
Much like SuperAgent can auto-serialize request data, it can also automatically parse it. When a parser is defined for the Content-Type, it is parsed, which by default includes "application/json" and "application/x-www-form-urlencoded". The parsed object is then available via `res.body`.
|
|
357
358
|
|
|
358
359
|
### Response header fields
|
|
359
360
|
|
|
360
|
-
|
|
361
|
+
The `res.header` contains an object of parsed header fields, lowercasing field names much like node does. For example `res.header['content-length']`.
|
|
361
362
|
|
|
362
363
|
### Response Content-Type
|
|
363
364
|
|
|
364
|
-
|
|
365
|
+
The Content-Type response header is special-cased, providing `res.type`, which is void of the charset (if any). For example the Content-Type of "text/html; charset=utf8" will provide "text/html" as `res.type`, and the `res.charset` property would then contain "utf8".
|
|
365
366
|
|
|
366
367
|
### Response status
|
|
367
368
|
|
|
368
|
-
|
|
369
|
+
The response status flags help determine if the request was a success, among other useful information, making SuperAgent ideal for interacting with RESTful web services. These flags are currently defined as:
|
|
369
370
|
|
|
370
371
|
var type = status / 100 | 0;
|
|
371
372
|
|
|
@@ -391,7 +392,7 @@ For more information, see the Mozilla Developer Network [xhr.responseType docs](
|
|
|
391
392
|
|
|
392
393
|
## Aborting requests
|
|
393
394
|
|
|
394
|
-
|
|
395
|
+
To abort requests simply invoke the `req.abort()` method.
|
|
395
396
|
|
|
396
397
|
## Timeouts
|
|
397
398
|
|
|
@@ -417,7 +418,7 @@ Timeout errors have a `.timeout` property.
|
|
|
417
418
|
|
|
418
419
|
## Authentication
|
|
419
420
|
|
|
420
|
-
|
|
421
|
+
In both Node and browsers auth available via the `.auth()` method:
|
|
421
422
|
|
|
422
423
|
request
|
|
423
424
|
.get('http://local')
|
|
@@ -425,17 +426,17 @@ Timeout errors have a `.timeout` property.
|
|
|
425
426
|
.end(callback);
|
|
426
427
|
|
|
427
428
|
|
|
428
|
-
|
|
429
|
+
In the _Node_ client Basic auth can be in the URL as "user:pass":
|
|
429
430
|
|
|
430
431
|
request.get('http://tobi:learnboost@local').end(callback);
|
|
431
432
|
|
|
432
|
-
|
|
433
|
+
By default only `Basic` auth is used. In browser you can add `{type:'auto'}` to enable all methods built-in in the browser (Digest, NTLM, etc.):
|
|
433
434
|
|
|
434
435
|
request.auth('digest', 'secret', {type:'auto'})
|
|
435
436
|
|
|
436
437
|
## Following redirects
|
|
437
438
|
|
|
438
|
-
|
|
439
|
+
By default up to 5 redirects will be followed, however you may specify this with the `res.redirects(n)` method:
|
|
439
440
|
|
|
440
441
|
request
|
|
441
442
|
.get('/some.png')
|
|
@@ -444,7 +445,7 @@ Timeout errors have a `.timeout` property.
|
|
|
444
445
|
|
|
445
446
|
## Preserving cookies
|
|
446
447
|
|
|
447
|
-
|
|
448
|
+
In Node SuperAgent does not save cookies by default, but you can use the `.agent()` method to create a copy of SuperAgent that saves cookies. Each copy has a separate cookie jar.
|
|
448
449
|
|
|
449
450
|
const agent = request.agent();
|
|
450
451
|
agent
|
|
@@ -453,11 +454,11 @@ Timeout errors have a `.timeout` property.
|
|
|
453
454
|
return agent.get('/cookied-page');
|
|
454
455
|
});
|
|
455
456
|
|
|
456
|
-
|
|
457
|
+
In browsers cookies are managed automatically by the browser, and there is no `.agent()` method.
|
|
457
458
|
|
|
458
459
|
## Piping data
|
|
459
460
|
|
|
460
|
-
|
|
461
|
+
The Node client allows you to pipe data to and from the request. For example piping a file's contents as the request:
|
|
461
462
|
|
|
462
463
|
const request = require('superagent');
|
|
463
464
|
const fs = require('fs');
|
|
@@ -467,7 +468,7 @@ Timeout errors have a `.timeout` property.
|
|
|
467
468
|
req.type('json');
|
|
468
469
|
stream.pipe(req);
|
|
469
470
|
|
|
470
|
-
|
|
471
|
+
Or piping the response to a file:
|
|
471
472
|
|
|
472
473
|
const stream = fs.createWriteStream('path/to/my.json');
|
|
473
474
|
const req = request.get('/some.json');
|
|
@@ -475,11 +476,11 @@ Timeout errors have a `.timeout` property.
|
|
|
475
476
|
|
|
476
477
|
## Multipart requests
|
|
477
478
|
|
|
478
|
-
|
|
479
|
+
SuperAgent is also great for _building_ multipart requests for which it provides methods `.attach()` and `.field()`.
|
|
479
480
|
|
|
480
481
|
### Attaching files
|
|
481
482
|
|
|
482
|
-
|
|
483
|
+
As mentioned a higher-level API is also provided, in the form of `.attach(name, [path], [filename])` and `.field(name, value)`/`.field(object)`. Attaching several files is simple, you can also provide a custom filename for the attachment, otherwise the basename of the attached file is used.
|
|
483
484
|
|
|
484
485
|
request
|
|
485
486
|
.post('/upload')
|
|
@@ -490,7 +491,7 @@ Timeout errors have a `.timeout` property.
|
|
|
490
491
|
|
|
491
492
|
### Field values
|
|
492
493
|
|
|
493
|
-
|
|
494
|
+
Much like form fields in HTML, you can set field values with the `.field(name, value)` method. Suppose you want to upload a few images with your name and email, your request might look something like this:
|
|
494
495
|
|
|
495
496
|
request
|
|
496
497
|
.post('/upload')
|
|
@@ -502,22 +503,19 @@ Timeout errors have a `.timeout` property.
|
|
|
502
503
|
|
|
503
504
|
## Compression
|
|
504
505
|
|
|
505
|
-
|
|
506
|
+
The node client supports compressed responses, best of all, you don't have to do anything! It just works.
|
|
506
507
|
|
|
507
508
|
## Buffering responses
|
|
508
509
|
|
|
509
|
-
|
|
510
|
-
as "text/plain", "text/html" etc you may invoke `req.buffer(false)`.
|
|
510
|
+
To force buffering of response bodies as `res.text` you may invoke `req.buffer()`. To undo the default of buffering for text responses such as "text/plain", "text/html" etc you may invoke `req.buffer(false)`.
|
|
511
511
|
|
|
512
|
-
|
|
513
|
-
handle both buffered and unbuffered responses in the same callback.
|
|
512
|
+
When buffered the `res.buffered` flag is provided, you may use this to handle both buffered and unbuffered responses in the same callback.
|
|
514
513
|
|
|
515
514
|
## CORS
|
|
516
515
|
|
|
517
|
-
|
|
516
|
+
For security reasons, browsers will block cross-origin requests unless the server opts-in using CORS headers. Browsers will also make extra __OPTIONS__ requests to check what HTTP headers and methods are allowed by the server. [Read more about CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS).
|
|
518
517
|
|
|
519
|
-
|
|
520
|
-
from the origin, however only when `Access-Control-Allow-Origin` is _not_ a wildcard ("*"), and `Access-Control-Allow-Credentials` is "true".
|
|
518
|
+
The `.withCredentials()` method enables the ability to send cookies from the origin, however only when `Access-Control-Allow-Origin` is _not_ a wildcard ("*"), and `Access-Control-Allow-Credentials` is "true".
|
|
521
519
|
|
|
522
520
|
request
|
|
523
521
|
.get('http://api.example.com:4001/')
|
|
@@ -548,13 +546,11 @@ An "error" event is also emitted, with you can listen for:
|
|
|
548
546
|
|
|
549
547
|
});
|
|
550
548
|
|
|
551
|
-
|
|
549
|
+
Note that a 4xx or 5xx response with super agent **are** considered an error by default. For example if you get a 500 or 403 response, this status information will be available via `err.status`. Errors from such responses also contain an `err.response` field with all of the properties mentioned in "[Response properties](#response-properties)". The library behaves in this way to handle the common case of wanting success responses and treating HTTP error status codes as errors while still allowing for custom logic around specific error conditions.
|
|
552
550
|
|
|
553
|
-
|
|
551
|
+
Network failures, timeouts, and other errors that produce no response will contain no `err.status` or `err.response` fields.
|
|
554
552
|
|
|
555
|
-
|
|
556
|
-
When an HTTP error occurs (4xx or 5xx response) the `res.error` property is an `Error` object,
|
|
557
|
-
this allows you to perform checks such as:
|
|
553
|
+
If you wish to handle 404 or other HTTP error responses, you can query the `err.status` property. When an HTTP error occurs (4xx or 5xx response) the `res.error` property is an `Error` object, this allows you to perform checks such as:
|
|
558
554
|
|
|
559
555
|
if (err && err.status === 404) {
|
|
560
556
|
alert('oh no ' + res.body.message);
|
|
@@ -576,7 +572,7 @@ Alternatively, you can use the `.ok(callback)` method to decide whether a respon
|
|
|
576
572
|
SuperAgent fires `progress` events on upload and download of large files.
|
|
577
573
|
|
|
578
574
|
request.post(url)
|
|
579
|
-
.attach(file)
|
|
575
|
+
.attach('field_name', file)
|
|
580
576
|
.on('progress', event => {
|
|
581
577
|
/* the event is:
|
|
582
578
|
{
|
|
@@ -605,7 +601,7 @@ Note that SuperAgent expects the global `Promise` object to be present. You'll n
|
|
|
605
601
|
|
|
606
602
|
SuperAgent has two implementations: one for web browsers (using XHR) and one for Node.JS (using core http module). By default Browserify and WebPack will pick the browser version.
|
|
607
603
|
|
|
608
|
-
If want to use WebPack to compile code for Node.JS, you *must* specify [node target](webpack.github.io/docs/configuration.html#target) in its configuration.
|
|
604
|
+
If want to use WebPack to compile code for Node.JS, you *must* specify [node target](https://webpack.github.io/docs/configuration.html#target) in its configuration.
|
|
609
605
|
|
|
610
606
|
### Using browser version in electron
|
|
611
607
|
|
package/docs/style.css
CHANGED
|
@@ -80,3 +80,8 @@ code .init { color: #2F6FAD }
|
|
|
80
80
|
code .string { color: #5890AD }
|
|
81
81
|
code .keyword { color: #8A6343 }
|
|
82
82
|
code .number { color: #2F6FAD }
|
|
83
|
+
|
|
84
|
+
/* override tocbot style to avoid vertical white line in table of content */
|
|
85
|
+
.toc-link::before {
|
|
86
|
+
content: initial;
|
|
87
|
+
}
|
package/docs/tail.html
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
</div>
|
|
2
2
|
<a href="http://github.com/visionmedia/superagent"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
|
|
3
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1
|
|
3
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
|
4
4
|
<script>
|
|
5
5
|
$('code').each(function(){
|
|
6
6
|
$(this).html(highlight($(this).text()));
|
|
@@ -17,13 +17,20 @@
|
|
|
17
17
|
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
|
|
18
18
|
}
|
|
19
19
|
</script>
|
|
20
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/
|
|
21
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>
|
|
20
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.js"></script>
|
|
22
21
|
<script>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
// Only use tocbot for main docs, not test docs
|
|
23
|
+
if (document.querySelector('#superagent')) {
|
|
24
|
+
tocbot.init({
|
|
25
|
+
// Where to render the table of contents.
|
|
26
|
+
tocSelector: '#menu',
|
|
27
|
+
// Where to grab the headings to build the table of contents.
|
|
28
|
+
contentSelector: '#content',
|
|
29
|
+
// Which headings to grab inside of the contentSelector element.
|
|
30
|
+
headingSelector: 'h2',
|
|
31
|
+
smoothScroll: false
|
|
32
|
+
});
|
|
33
|
+
}
|
|
27
34
|
</script>
|
|
28
35
|
</body>
|
|
29
36
|
</html>
|
package/lib/client.js
CHANGED
|
@@ -15,7 +15,6 @@ if (typeof window !== 'undefined') { // Browser window
|
|
|
15
15
|
var Emitter = require('component-emitter');
|
|
16
16
|
var RequestBase = require('./request-base');
|
|
17
17
|
var isObject = require('./is-object');
|
|
18
|
-
var isFunction = require('./is-function');
|
|
19
18
|
var ResponseBase = require('./response-base');
|
|
20
19
|
var shouldRetry = require('./should-retry');
|
|
21
20
|
|
|
@@ -60,7 +59,7 @@ request.getXHR = function () {
|
|
|
60
59
|
try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
|
|
61
60
|
try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
|
|
62
61
|
}
|
|
63
|
-
throw Error("Browser-only
|
|
62
|
+
throw Error("Browser-only version of superagent could not find XHR");
|
|
64
63
|
};
|
|
65
64
|
|
|
66
65
|
/**
|
|
@@ -170,7 +169,7 @@ request.parseString = parseString;
|
|
|
170
169
|
request.types = {
|
|
171
170
|
html: 'text/html',
|
|
172
171
|
json: 'application/json',
|
|
173
|
-
xml: '
|
|
172
|
+
xml: 'text/xml',
|
|
174
173
|
urlencoded: 'application/x-www-form-urlencoded',
|
|
175
174
|
'form': 'application/x-www-form-urlencoded',
|
|
176
175
|
'form-data': 'application/x-www-form-urlencoded'
|
|
@@ -526,10 +525,10 @@ Request.prototype.auth = function(user, pass, options){
|
|
|
526
525
|
this.username = user;
|
|
527
526
|
this.password = pass;
|
|
528
527
|
break;
|
|
529
|
-
|
|
528
|
+
|
|
530
529
|
case 'bearer': // usage would be .auth(accessToken, { type: 'bearer' })
|
|
531
530
|
this.set('Authorization', 'Bearer ' + user);
|
|
532
|
-
break;
|
|
531
|
+
break;
|
|
533
532
|
}
|
|
534
533
|
return this;
|
|
535
534
|
};
|
|
@@ -643,32 +642,6 @@ Request.prototype.pipe = Request.prototype.write = function(){
|
|
|
643
642
|
throw Error("Streaming is not supported in browser version of superagent");
|
|
644
643
|
};
|
|
645
644
|
|
|
646
|
-
/**
|
|
647
|
-
* Compose querystring to append to req.url
|
|
648
|
-
*
|
|
649
|
-
* @api private
|
|
650
|
-
*/
|
|
651
|
-
|
|
652
|
-
Request.prototype._appendQueryString = function(){
|
|
653
|
-
var query = this._query.join('&');
|
|
654
|
-
if (query) {
|
|
655
|
-
this.url += (this.url.indexOf('?') >= 0 ? '&' : '?') + query;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
if (this._sort) {
|
|
659
|
-
var index = this.url.indexOf('?');
|
|
660
|
-
if (index >= 0) {
|
|
661
|
-
var queryArr = this.url.substring(index + 1).split('&');
|
|
662
|
-
if (isFunction(this._sort)) {
|
|
663
|
-
queryArr.sort(this._sort);
|
|
664
|
-
} else {
|
|
665
|
-
queryArr.sort();
|
|
666
|
-
}
|
|
667
|
-
this.url = this.url.substring(0, index) + '?' + queryArr.join('&');
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
};
|
|
671
|
-
|
|
672
645
|
/**
|
|
673
646
|
* Check if `obj` is a host object,
|
|
674
647
|
* we don't want to serialize these :)
|
|
@@ -701,7 +674,7 @@ Request.prototype.end = function(fn){
|
|
|
701
674
|
this._callback = fn || noop;
|
|
702
675
|
|
|
703
676
|
// querystring
|
|
704
|
-
this.
|
|
677
|
+
this._finalizeQueryString();
|
|
705
678
|
|
|
706
679
|
return this._end();
|
|
707
680
|
};
|
|
@@ -834,7 +807,7 @@ request.get = function(url, data, fn){
|
|
|
834
807
|
request.head = function(url, data, fn){
|
|
835
808
|
var req = request('HEAD', url);
|
|
836
809
|
if ('function' == typeof data) fn = data, data = null;
|
|
837
|
-
if (data) req.
|
|
810
|
+
if (data) req.query(data);
|
|
838
811
|
if (fn) req.end(fn);
|
|
839
812
|
return req;
|
|
840
813
|
};
|