superagent README

The project's README from github.com/forwardemail/superagent. As one markdown file: README.md.

superagent

build status code coverage code style styled with prettier made with lass license

Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features. Maintained for Forward Email and Lad.

Table of Contents

Install

npm:

npm install superagent

yarn:

yarn add superagent

Usage

Node

const superagent = require('superagent');

// callback
superagent
  .post('/api/pet')
  .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
  .set('X-API-Key', 'foobar')
  .set('accept', 'json')
  .end((err, res) => {
    // Calling the end function will send the request
  });

// promise with then/catch
superagent.post('/api/pet').then(console.log).catch(console.error);

// promise with async/await
(async () => {
  try {
    const res = await superagent.post('/api/pet');
    console.log(res);
  } catch (err) {
    console.error(err);
  }
})();

Browser

The browser-ready, minified version of superagent is only 50 KB (minified and gzipped).

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.

Note that we also provide unminified versions with .js instead of .min.js file extensions.

VanillaJS

This is the solution for you if you're just using <script> tags everywhere!

<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=WeakRef,BigInt"></script>
<script src="https://cdn.jsdelivr.net/npm/superagent"></script>
<!-- if you wish to use unpkg.com instead: -->
<!-- <script src="https://unpkg.com/superagent"></script> -->
<script type="text/javascript">
  (function() {
    // superagent is exposed as `window.superagent`
    // if you wish to use "request" instead please
    // uncomment the following line of code:
    // `window.request = superagent;`
    superagent
      .post('/api/pet')
      .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
      .set('X-API-Key', 'foobar')
      .set('accept', 'json')
      .end(function (err, res) {
        // Calling the end function will send the request
      });
  })();
</script>

Bundler

If you are using browserify, webpack, rollup, or another bundler, then you can follow the same usage as Node above.

Supported Platforms

Required Browser Features

We recommend using https://cdnjs.cloudflare.com/polyfill/ (specifically with the bundle mentioned in VanillaJS above):

<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=WeakRef,BigInt"></script>

Plugins

SuperAgent is easily extended via plugins.

const nocache = require('superagent-no-cache');
const superagent = require('superagent');
const prefix = require('superagent-prefix')('/static');

superagent
  .get('/some-url')
  .query({ action: 'edit', city: 'London' }) // query string
  .use(prefix) // Prefixes *only* this request
  .use(nocache) // Prevents caching of *only* this request
  .end((err, res) => {
    // Do something
  });

Existing plugins:

Please prefix your plugin with superagent-* so that it can easily be found by others.

For SuperAgent extensions such as couchdb and oauth visit the wiki.

Upgrading from previous versions

Please see GitHub releases page for the current changelog.

Our breaking changes are mostly in rarely used functionality and from stricter error handling.

Contributors

Name
Kornel Lesiński
Peter Lyons
Hunter Loftis
Nick Baugh

License

MIT © TJ Holowaychuk

superagent (forwardemail/superagent)

Searching Database…

这是什么?Superagent 是一个小巧、渐进式的 HTTP 请求库,在浏览器和 Node.js 里用同一套 API,支持很多高级的 HTTP 客户端功能。
它是给浏览器用的,还是给 Node.js 用的?两者都行——它是同构的。一套 API 同时覆盖 Node 和浏览器,在浏览器端有现成的构建文件,压缩并 gzip 后只有 50 KB,可以通过 jsdelivr、unpkg 或包里的 dist 文件夹获取。
怎么把它加到我的项目里?用 npm 或 yarn 安装即可(npm install superagent)。在浏览器里可以省去打包:直接从 CDN 或包里的 dist 文件夹加载预构建的压缩文件。
对 Node 版本有要求吗?按其 engines 字段的声明,需要 Node.js 14.18.0 或更高版本。浏览器端则只需要一个 script 标签或通过打包工具导入,没有其他要求。
从旧版本升级时有什么要注意的?主要是不兼容变更:破坏性改动集中在很少用到的功能和更严格的错误处理上,所以升级前最好查看 GitHub releases 上的版本变更日志。
在手机浏览器上也能用吗,还是只支持电脑?都能用。支持的浏览器范围包括较新版本的 Chrome、Firefox、Edge、Opera 和 Safari,以及 iOS Safari、Android Chrome、三星浏览器、QQ 浏览器、UC 浏览器手机版和 KaiOS 2.5。
怎么发起一个带查询参数的请求?链式调用即可:superagent.get('/some-url').query({ action: 'edit', city: 'London' }).end((err, res) => { ... })——同样的链式写法还可以通过 .use() 挂载插件。
它有插件系统吗?有——插件通过 .use() 按请求挂载。生态里有缓存、限流、模拟(mock)、URL 前缀、字符集处理、cheerio 解析(res.$)以及 AWS 请求签名等插件;插件名称应以 superagent- 为前缀。
superagent 现在还在积极维护吗?是的。它由 Forward Email 和 Lad 团队——也就是 @forwardemail 和 @ladjs,以及 @spamscanner、@breejs、@cabinjs 和 @lassjs——负责维护,从 2011 年一直用到现在,变更日志发布在 GitHub releases 上。
Size
7 MB
Stars
16.6K
Age
15y
Commits · 90 Days