You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # superagent
  2. [![build status](https://img.shields.io/travis/visionmedia/superagent.svg)](https://travis-ci.org/visionmedia/superagent)
  3. [![code coverage](https://img.shields.io/codecov/c/github/visionmedia/superagent.svg)](https://codecov.io/gh/visionmedia/superagent)
  4. [![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
  5. [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  6. [![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
  7. [![license](https://img.shields.io/github/license/visionmedia/superagent.svg)](LICENSE)
  8. > Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features
  9. ## Table of Contents
  10. * [Install](#install)
  11. * [Usage](#usage)
  12. * [Node](#node)
  13. * [Browser](#browser)
  14. * [Supported Platforms](#supported-platforms)
  15. * [Required Browser Features](#required-browser-features)
  16. * [Plugins](#plugins)
  17. * [Upgrading from previous versions](#upgrading-from-previous-versions)
  18. * [Contributors](#contributors)
  19. * [License](#license)
  20. ## Install
  21. [npm][]:
  22. ```sh
  23. npm install superagent
  24. ```
  25. [yarn][]:
  26. ```sh
  27. yarn add superagent
  28. ```
  29. ## Usage
  30. ### Node
  31. ```js
  32. const superagent = require('superagent');
  33. // callback
  34. superagent
  35. .post('/api/pet')
  36. .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
  37. .set('X-API-Key', 'foobar')
  38. .set('accept', 'json')
  39. .end((err, res) => {
  40. // Calling the end function will send the request
  41. });
  42. // promise with then/catch
  43. superagent.post('/api/pet').then(console.log).catch(console.error);
  44. // promise with async/await
  45. (async () => {
  46. try {
  47. const res = await superagent.post('/api/pet');
  48. console.log(res);
  49. } catch (err) {
  50. console.error(err);
  51. }
  52. })();
  53. ```
  54. ### Browser
  55. **The browser-ready, minified version of `superagent` is only 6 KB (minified and gzipped)!**
  56. Browser-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.
  57. > Note that we also provide unminified versions with `.js` instead of `.min.js` file extensions.
  58. #### VanillaJS
  59. This is the solution for you if you're just using `<script>` tags everywhere!
  60. ```html
  61. <script src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise,Symbol,Object.setPrototypeOf,Object.getOwnPropertySymbols"></script>
  62. <script src="https://cdn.jsdelivr.net/npm/superagent"></script>
  63. <!-- if you wish to use unpkg.com instead: -->
  64. <!-- <script src="https://unpkg.com/superagent"></script> -->
  65. <script type="text/javascript">
  66. (function() {
  67. // superagent is exposed as `window.superagent`
  68. // if you wish to use "request" instead please
  69. // uncomment the following line of code:
  70. // `window.request = superagent;`
  71. superagent
  72. .post('/api/pet')
  73. .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
  74. .set('X-API-Key', 'foobar')
  75. .set('accept', 'json')
  76. .end(function (err, res) {
  77. // Calling the end function will send the request
  78. });
  79. })();
  80. </script>
  81. ```
  82. #### Bundler
  83. If you are using [browserify][], [webpack][], [rollup][], or another bundler, then you can follow the same usage as [Node](#node) above.
  84. ## Supported Platforms
  85. * Node: v6.x+
  86. * Browsers (see [.browserslistrc](.browserslistrc)):
  87. ```sh
  88. npx browserslist
  89. ```
  90. ```sh
  91. and_chr 71
  92. and_ff 64
  93. and_qq 1.2
  94. and_uc 11.8
  95. android 67
  96. android 4.4.3-4.4.4
  97. baidu 7.12
  98. bb 10
  99. bb 7
  100. chrome 73
  101. chrome 72
  102. chrome 71
  103. edge 18
  104. edge 17
  105. firefox 66
  106. firefox 65
  107. ie 11
  108. ie 10
  109. ie 9
  110. ie_mob 11
  111. ie_mob 10
  112. ios_saf 12.0-12.1
  113. ios_saf 11.3-11.4
  114. op_mini all
  115. op_mob 46
  116. op_mob 12.1
  117. opera 58
  118. opera 57
  119. safari 12
  120. safari 11.1
  121. samsung 8.2
  122. samsung 7.2-7.4
  123. ```
  124. ### Required Browser Features
  125. We recommend using <https://polyfill.io> (specifically with the bundle mentioned in [VanillaJS](#vanillajs) above):
  126. ```html
  127. <script src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise,Symbol,Object.setPrototypeOf,Object.getOwnPropertySymbols"></script>
  128. ```
  129. * IE 9-10 requires a polyfill for `Promise`, `Array.from`, `Symbol`, `Object.getOwnPropertySymbols`, and `Object.setPrototypeOf`
  130. * IE 9 requires a polyfill for `window.FormData` (we recommend [formdata-polyfill][])
  131. ## Plugins
  132. SuperAgent is easily extended via plugins.
  133. ```js
  134. const nocache = require('superagent-no-cache');
  135. const superagent = require('superagent');
  136. const prefix = require('superagent-prefix')('/static');
  137. superagent
  138. .get('/some-url')
  139. .query({ action: 'edit', city: 'London' }) // query string
  140. .use(prefix) // Prefixes *only* this request
  141. .use(nocache) // Prevents caching of *only* this request
  142. .end((err, res) => {
  143. // Do something
  144. });
  145. ```
  146. Existing plugins:
  147. * [superagent-no-cache](https://github.com/johntron/superagent-no-cache) - prevents caching by including Cache-Control header
  148. * [superagent-prefix](https://github.com/johntron/superagent-prefix) - prefixes absolute URLs (useful in test environment)
  149. * [superagent-suffix](https://github.com/timneutkens1/superagent-suffix) - suffix URLs with a given path
  150. * [superagent-mock](https://github.com/M6Web/superagent-mock) - simulate HTTP calls by returning data fixtures based on the requested URL
  151. * [superagent-mocker](https://github.com/shuvalov-anton/superagent-mocker) — simulate REST API
  152. * [superagent-cache](https://github.com/jpodwys/superagent-cache) - A global SuperAgent patch with built-in, flexible caching
  153. * [superagent-cache-plugin](https://github.com/jpodwys/superagent-cache-plugin) - A SuperAgent plugin with built-in, flexible caching
  154. * [superagent-jsonapify](https://github.com/alex94puchades/superagent-jsonapify) - A lightweight [json-api](http://jsonapi.org/format/) client addon for superagent
  155. * [superagent-serializer](https://github.com/zzarcon/superagent-serializer) - Converts server payload into different cases
  156. * [superagent-httpbackend](https://www.npmjs.com/package/superagent-httpbackend) - stub out requests using AngularJS' $httpBackend syntax
  157. * [superagent-throttle](https://github.com/leviwheatcroft/superagent-throttle) - queues and intelligently throttles requests
  158. * [superagent-charset](https://github.com/magicdawn/superagent-charset) - add charset support for node's SuperAgent
  159. * [superagent-verbose-errors](https://github.com/jcoreio/superagent-verbose-errors) - include response body in error messages for failed requests
  160. * [superagent-declare](https://github.com/damoclark/superagent-declare) - A simple [declarative](https://en.wikipedia.org/wiki/Declarative_programming) API for SuperAgent
  161. * [superagent-node-http-timings](https://github.com/webuniverseio/superagent-node-http-timings) - measure http timings in node.js
  162. Please prefix your plugin with `superagent-*` so that it can easily be found by others.
  163. For SuperAgent extensions such as couchdb and oauth visit the [wiki](https://github.com/visionmedia/superagent/wiki).
  164. ## Upgrading from previous versions
  165. Our breaking changes are mostly in rarely used functionality and from stricter error handling.
  166. * [4.x to 5.x](https://github.com/visionmedia/superagent/releases/tag/v5.0.0):
  167. * We've implemented the build setup of [Lass](https://lass.js.org) to simplify our stack and linting
  168. * 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`)
  169. * Linting support has been added using `caniuse-lite` and `eslint-plugin-compat`
  170. * We can now target what versions of Node we wish to support more easily using `.babelrc`
  171. * [3.x to 4.x](https://github.com/visionmedia/superagent/releases/tag/v4.0.0-alpha.1):
  172. * Ensure you're running Node 6 or later. We've dropped support for Node 4.
  173. * We've started using ES6 and for compatibility with Internet Explorer you may need to use Babel.
  174. * We suggest migrating from `.end()` callbacks to `.then()` or `await`.
  175. * [2.x to 3.x](https://github.com/visionmedia/superagent/releases/tag/v3.0.0):
  176. * Ensure you're running Node 4 or later. We've dropped support for Node 0.x.
  177. * Test code that calls `.send()` multiple times. Invalid calls to `.send()` will now throw instead of sending garbage.
  178. * [1.x to 2.x](https://github.com/visionmedia/superagent/releases/tag/v2.0.0):
  179. * If you use `.parse()` in the _browser_ version, rename it to `.serialize()`.
  180. * 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).
  181. * If you use `.then()` in Internet Explorer, ensure that you have a polyfill that adds a global `Promise` object.
  182. * 0.x to 1.x:
  183. * Instead of 1-argument callback `.end(function(res){})` use `.then(res => {})`.
  184. ## Contributors
  185. | Name |
  186. | ------------------- |
  187. | **Kornel Lesiński** |
  188. | **Peter Lyons** |
  189. | **Hunter Loftis** |
  190. | **Nick Baugh** |
  191. ## License
  192. [MIT](LICENSE) © TJ Holowaychuk
  193. ##
  194. [npm]: https://www.npmjs.com/
  195. [yarn]: https://yarnpkg.com/
  196. [formdata-polyfill]: https://www.npmjs.com/package/formdata-polyfill
  197. [jsdelivr]: https://www.jsdelivr.com/
  198. [unpkg]: https://unpkg.com/
  199. [browserify]: https://github.com/browserify/browserify
  200. [webpack]: https://github.com/webpack/webpack
  201. [rollup]: https://github.com/rollup/rollup