UNPKG

10.1 kBMarkdownView Raw
1# superagent
2
3[![build status](https://img.shields.io/travis/visionmedia/superagent.svg)](https://travis-ci.org/visionmedia/superagent)
4[![code coverage](https://img.shields.io/codecov/c/github/visionmedia/superagent.svg)](https://codecov.io/gh/visionmedia/superagent)
5[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
6[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
7[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
8[![license](https://img.shields.io/github/license/visionmedia/superagent.svg)](LICENSE)
9
10> Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features
11
12
13## Table of Contents
14
15* [Install](#install)
16* [Usage](#usage)
17 * [Node](#node)
18 * [Browser](#browser)
19* [Supported Platforms](#supported-platforms)
20 * [Required Browser Features](#required-browser-features)
21* [Plugins](#plugins)
22* [Upgrading from previous versions](#upgrading-from-previous-versions)
23* [Contributors](#contributors)
24* [License](#license)
25
26
27## Install
28
29[npm][]:
30
31```sh
32npm install superagent
33```
34
35[yarn][]:
36
37```sh
38yarn add superagent
39```
40
41
42## Usage
43
44### Node
45
46```js
47const superagent = require('superagent');
48
49// callback
50superagent
51 .post('/api/pet')
52 .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
53 .set('X-API-Key', 'foobar')
54 .set('accept', 'json')
55 .end((err, res) => {
56 // Calling the end function will send the request
57 });
58
59// promise with then/catch
60superagent.post('/api/pet').then(console.log).catch(console.error);
61
62// promise with async/await
63(async () => {
64 try {
65 const res = await superagent.post('/api/pet');
66 console.log(res);
67 } catch (err) {
68 console.error(err);
69 }
70})();
71```
72
73### Browser
74
75**The browser-ready, minified version of `superagent` is only 50 KB (minified and gzipped).**
76
77Browser-ready versions of this module are available via [jsdelivr][], [unpkg][], and also in the `node_modules/superagent/dist` folder in downloads of the `superagent` package.
78
79> Note that we also provide unminified versions with `.js` instead of `.min.js` file extensions.
80
81#### VanillaJS
82
83This is the solution for you if you're just using `<script>` tags everywhere!
84
85```html
86<script src="https://polyfill.io/v3/polyfill.min.js?features=WeakRef,BigInt"></script>
87<script src="https://cdn.jsdelivr.net/npm/superagent"></script>
88<!-- if you wish to use unpkg.com instead: -->
89<!-- <script src="https://unpkg.com/superagent"></script> -->
90<script type="text/javascript">
91 (function() {
92 // superagent is exposed as `window.superagent`
93 // if you wish to use "request" instead please
94 // uncomment the following line of code:
95 // `window.request = superagent;`
96 superagent
97 .post('/api/pet')
98 .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
99 .set('X-API-Key', 'foobar')
100 .set('accept', 'json')
101 .end(function (err, res) {
102 // Calling the end function will send the request
103 });
104 })();
105</script>
106```
107
108#### Bundler
109
110If you are using [browserify][], [webpack][], [rollup][], or another bundler, then you can follow the same usage as [Node](#node) above.
111
112
113## Supported Platforms
114
115* Node: v6.x+
116* Browsers (see [.browserslistrc](.browserslistrc)):
117
118 ```sh
119 npx browserslist
120 ```
121
122 ```sh
123 and_chr 102
124 and_ff 101
125 and_qq 10.4
126 and_uc 12.12
127 android 101
128 chrome 103
129 chrome 102
130 chrome 101
131 chrome 100
132 edge 103
133 edge 102
134 edge 101
135 firefox 101
136 firefox 100
137 firefox 91
138 ios_saf 15.5
139 ios_saf 15.4
140 ios_saf 15.2-15.3
141 ios_saf 15.0-15.1
142 ios_saf 14.5-14.8
143 ios_saf 14.0-14.4
144 ios_saf 12.2-12.5
145 kaios 2.5
146 op_mini all
147 op_mob 64
148 opera 86
149 opera 85
150 safari 15.5
151 safari 15.4
152 samsung 17.0
153 samsung 16.0
154 ```
155
156### Required Browser Features
157
158We recommend using <https://polyfill.io> (specifically with the bundle mentioned in [VanillaJS](#vanillajs) above):
159
160```html
161<script src="https://polyfill.io/v3/polyfill.min.js?features=WeakRef,BigInt"></script>
162```
163
164* WeakRef is not supported in Opera 85, iOS Safari 12.2-12.5
165* BigInt is not supported in iOS Safari 12.2-12.5
166
167
168## Plugins
169
170SuperAgent is easily extended via plugins.
171
172```js
173const nocache = require('superagent-no-cache');
174const superagent = require('superagent');
175const prefix = require('superagent-prefix')('/static');
176
177superagent
178 .get('/some-url')
179 .query({ action: 'edit', city: 'London' }) // query string
180 .use(prefix) // Prefixes *only* this request
181 .use(nocache) // Prevents caching of *only* this request
182 .end((err, res) => {
183 // Do something
184 });
185```
186
187Existing plugins:
188
189* [superagent-no-cache](https://github.com/johntron/superagent-no-cache) - prevents caching by including Cache-Control header
190* [superagent-prefix](https://github.com/johntron/superagent-prefix) - prefixes absolute URLs (useful in test environment)
191* [superagent-suffix](https://github.com/timneutkens1/superagent-suffix) - suffix URLs with a given path
192* [superagent-mock](https://github.com/M6Web/superagent-mock) - simulate HTTP calls by returning data fixtures based on the requested URL
193* [superagent-mocker](https://github.com/shuvalov-anton/superagent-mocker) — simulate REST API
194* [superagent-cache](https://github.com/jpodwys/superagent-cache) - A global SuperAgent patch with built-in, flexible caching
195* [superagent-cache-plugin](https://github.com/jpodwys/superagent-cache-plugin) - A SuperAgent plugin with built-in, flexible caching
196* [superagent-jsonapify](https://github.com/alex94puchades/superagent-jsonapify) - A lightweight [json-api](http://jsonapi.org/format/) client addon for superagent
197* [superagent-serializer](https://github.com/zzarcon/superagent-serializer) - Converts server payload into different cases
198* [superagent-httpbackend](https://www.npmjs.com/package/superagent-httpbackend) - stub out requests using AngularJS' $httpBackend syntax
199* [superagent-throttle](https://github.com/leviwheatcroft/superagent-throttle) - queues and intelligently throttles requests
200* [superagent-charset](https://github.com/magicdawn/superagent-charset) - add charset support for node's SuperAgent
201* [superagent-verbose-errors](https://github.com/jcoreio/superagent-verbose-errors) - include response body in error messages for failed requests
202* [superagent-declare](https://github.com/damoclark/superagent-declare) - A simple [declarative](https://en.wikipedia.org/wiki/Declarative_programming) API for SuperAgent
203* [superagent-node-http-timings](https://github.com/webuniverseio/superagent-node-http-timings) - measure http timings in node.js
204
205Please prefix your plugin with `superagent-*` so that it can easily be found by others.
206
207For SuperAgent extensions such as couchdb and oauth visit the [wiki](https://github.com/visionmedia/superagent/wiki).
208
209
210## Upgrading from previous versions
211
212Please see [GitHub releases page](https://github.com/visionmedia/superagent/releases) for the current changelog.
213
214Our breaking changes are mostly in rarely used functionality and from stricter error handling.
215
216* [6.0 to 6.1](https://github.com/visionmedia/superagent/releases/tag/v6.1.0)
217 * Browser behaviour changed to match Node when serializing `application/x-www-form-urlencoded`, using `arrayFormat: 'indices'` semantics of `qs` library. (See: <https://www.npmjs.com/package/qs#stringifying>)
218* [5.x to 6.x](https://github.com/visionmedia/superagent/releases/tag/v6.0.0):
219 * Retry behavior is still opt-in, however we now have a more fine-grained list of status codes and error codes that we retry against (see updated docs)
220 * A specific issue with Content-Type matching not being case-insensitive is fixed
221 * Set is now required for IE 9, see [Required Browser Features](#required-browser-features) for more insight
222* [4.x to 5.x](https://github.com/visionmedia/superagent/releases/tag/v5.0.0):
223 * We've implemented the build setup of [Lass](https://lass.js.org) to simplify our stack and linting
224 * Unminified browserified build size has been reduced from 48KB to 20KB (via `tinyify` and the latest version of Babel using `@babel/preset-env` and `.browserslistrc`)
225 * Linting support has been added using `caniuse-lite` and `eslint-plugin-compat`
226 * We can now target what versions of Node we wish to support more easily using `.babelrc`
227* [3.x to 4.x](https://github.com/visionmedia/superagent/releases/tag/v4.0.0-alpha.1):
228 * Ensure you're running Node 6 or later. We've dropped support for Node 4.
229 * We've started using ES6 and for compatibility with Internet Explorer you may need to use Babel.
230 * We suggest migrating from `.end()` callbacks to `.then()` or `await`.
231* [2.x to 3.x](https://github.com/visionmedia/superagent/releases/tag/v3.0.0):
232 * Ensure you're running Node 4 or later. We've dropped support for Node 0.x.
233 * Test code that calls `.send()` multiple times. Invalid calls to `.send()` will now throw instead of sending garbage.
234* [1.x to 2.x](https://github.com/visionmedia/superagent/releases/tag/v2.0.0):
235 * If you use `.parse()` in the *browser* version, rename it to `.serialize()`.
236 * If you rely on `undefined` in query-string values being sent literally as the text "undefined", switch to checking for missing value instead. `?key=undefined` is now `?key` (without a value).
237 * If you use `.then()` in Internet Explorer, ensure that you have a polyfill that adds a global `Promise` object.
238* 0.x to 1.x:
239 * Instead of 1-argument callback `.end(function(res){})` use `.then(res => {})`.
240
241
242## Contributors
243
244| Name |
245| ------------------- |
246| **Kornel Lesiński** |
247| **Peter Lyons** |
248| **Hunter Loftis** |
249| **Nick Baugh** |
250
251
252## License
253
254[MIT](LICENSE) © TJ Holowaychuk
255
256
257##
258
259[npm]: https://www.npmjs.com/
260
261[yarn]: https://yarnpkg.com/
262
263[jsdelivr]: https://www.jsdelivr.com/
264
265[unpkg]: https://unpkg.com/
266
267[browserify]: https://github.com/browserify/browserify
268
269[webpack]: https://github.com/webpack/webpack
270
271[rollup]: https://github.com/rollup/rollup