superagent中文文档|superagent js中文教程|解析

npm npmdoc 3年前 (2021-10-18) 1325次浏览

superagent中文文档|superagent js中文教程|解析

安装命令:npm i superagent

superagent

构建状态
代码覆盖率
代码风格
风格更漂亮
用姑娘做的
执照

小型渐进式客户端 HTTP 请求库,与 Node.js 模块具有相同的 API,支持许多高级 HTTP 客户端功能

目录

安装

纳米

npm install superagent

纱线

yarn add superagent

用法

节点

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);
  }
})();

浏览器

浏览器就绪的缩小版superagent只有 6 KB(缩小和 gzipped)!

该模块的浏览器就绪版本可通过jsdelivrunpkg 获得,也可在node_modules/superagent/dist下载文件夹中获得superagent

请注意,我们还提供带有.js而不是.min.js文件扩展名的未缩小版本

香草JS

如果您只是<script>到处使用标签,这就是您的解决方案

<script src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise,Symbol,Object.setPrototypeOf,Object.getOwnPropertySymbols,Set"></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> 

捆绑器

如果您正在使用browserify的WebPack汇总,或其他打捆,然后就可以按照相同的用法的节点之上。

支持的平台

  • 节点:v6.x+

  • 浏览器(参见.browserslistrc):

    npx browserslist
    and_chr 71
    and_ff 64
    and_qq 1.2
    and_uc 11.8
    android 67
    android 4.4.3-4.4.4
    baidu 7.12
    bb 10
    bb 7
    chrome 73
    chrome 72
    chrome 71
    edge 18
    edge 17
    firefox 66
    firefox 65
    ie 11
    ie 10
    ie 9
    ie_mob 11
    ie_mob 10
    ios_saf 12.0-12.1
    ios_saf 11.3-11.4
    op_mini all
    op_mob 46
    op_mob 12.1
    opera 58
    opera 57
    safari 12
    safari 11.1
    samsung 8.2
    samsung 7.2-7.4

所需的浏览器功能

我们建议使用https://polyfill.io(特别是上面VanillaJS 中提到的包):

<script src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise,Symbol,Object.setPrototypeOf,Object.getOwnPropertySymbols,Set"></script>
  • IE 9-10需要一个填充工具为PromiseArray.fromSymbolObject.getOwnPropertySymbols,和Object.setPrototypeOf
  • IE 9 需要一个polyfillwindow.FormData(我们推荐formdata-polyfill)和Set

插件

SuperAgent 可以通过插件轻松扩展。

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
  });

现有插件:

请为您的插件添加前缀,superagent-*以便其他人可以轻松找到它。

对于诸如 couchdb 和 oauth 之类的 SuperAgent 扩展,请访问wiki

从以前的版本升级

我们的重大更改主要来自很少使用的功能和更严格的错误处理。

  • 5.x 到 6.x

    • 重试行为仍然是可选的,但是我们现在有一个更细粒度的状态代码和错误代码列表,我们可以重试(请参阅更新的文档)
    • 内容类型匹配不区分大小写的特定问题已修复
    • 现在 IE 9 需要设置,请参阅所需的浏览器功能以获取更多信息
  • 4.x 到 5.x

    • 我们已经实现了Lass的构建设置来简化我们的堆栈和 linting
    • 未缩小的浏览器构建大小已从 48KB 减少到 20KB(通过tinyify和最新版本的 Babel 使用@babel/preset-env.browserslistrc
    • 使用caniuse-lite添加了 Linting 支持eslint-plugin-compat
    • 我们现在可以更轻松地使用我们希望支持的 Node 版本 .babelrc
  • 3.x 到 4.x

    • 确保您运行的是 Node 6 或更高版本。我们已经放弃了对 Node 4 的支持。
    • 我们已经开始使用 ES6,为了与 Internet Explorer 兼容,您可能需要使用 Babel。
    • 我们建议从.end()回调迁移.then()or await
  • 2.x 到 3.x

    • 确保您运行的是 Node 4 或更高版本。我们已经放弃了对 Node 0.x 的支持。
    • 测试.send()多次调用的代码无效的调用.send()现在将抛出而不是发送垃圾。
  • 1.x 到 2.x

    • 如果.parse()浏览器版本中使用,请将其重命名为.serialize().
    • 如果您依赖undefined查询字符串值作为文本“未定义”按字面发送,请改为检查缺失值。?key=undefined现在?key(没有值)。
    • 如果您.then()在 Internet Explorer 中使用,请确保您有一个添加全局Promise对象的 polyfill
  • 0.x 到 1.x:

    • 而不是 1 参数回调.end(function(res){})使用.then(res => {}).

贡献者

姓名
科内尔·莱辛斯基
彼得·莱昂斯
亨特·洛夫蒂斯
尼克·鲍

执照

麻省理工学院© TJ Holowaychuk


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:superagent中文文档|superagent js中文教程|解析
喜欢 (0)
.excerpt .focus {display:none}