请求人
一个发送 HTTP 请求的简单库 .
使用量
要发送 HTTP 请求 .
import { createRequestSender } from '@bigcommerce/request-sender';
const requestSender = createRequestSender();
// GET request
requestSender.get('/foobars')
.then(({ body }) => console.log(body));
// POST request
requestSender.post('/foobars', { body: { name: 'Foobar' } })
.then(({ body }) => console.log(body));
取消待决请求
import { createRequestSender, createTimeout } from '@bigcommerce/request-sender';
const timeout = createTimeout(100);
const requestSender = createRequestSender();
requestSender.get('/foobars', { timeout })
.catch(({ status }) => console.log(status));
timeout.cancel();
API 密码
创建请求森德 ()
创建一个新的实例: RequestSender。 。 。 .
创建 Timeout( 延迟: 号码 )
创建一个新的实例: Timeout如果 delay 定义,实例将在指定期间后自动超时。 否则,它将一直呆到 complete() 名为.
请求人
发送请求(url:字符串,选项:请求选项):承诺
获取( URL: 字符串, 选项: 请求选项): 保证
post(url:字符串,选项:请求选项):承诺
put(url:字符串,选项:请求选项):承诺
补丁( URL: 字符串, 选项: 请求选项): 保证
删除( url: 字符串, 选项: 请求选项): 承诺
使用 GET, (中文(简体) ), POST, (中文(简体) ), PUT, (中文(简体) ), PATCH 或者说 DELETE 方法。 或者,你可以打电话 sendRequest 并指定请求方法作为参数.
超时
完整( ): 空;
手动完成超时 .
请求选项
有吗?
请求有效载荷.
默认 : null
编码Params:布尔?
URL编码参数.
默认 : true
标题:对象?
请求信头.
默认 : { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json', }
参数:对象?
URL参数. 它们被串成一个查询字符串.
默认 : null
方法:字符串?
请求方法。 如果调用其中一种便利方法(get, (中文(简体) ), post 页:1.
默认 : GET
证书:布尔?
与 XMLHttpRequest.withCredentials。 。 。 .
默认 : true
超时:超时?
定义是否想要暂停请求 .
默认 : null
回应
机构:任何
反应体.
默认 : null
页眉: 对象
答头.
默认 : {}
状态: 号码?
反应状态代码。 返回时 0 如果请求被取消.
默认 : undefined
状态图文集: 字符串?
反应状态文本.
默认 : undefined
发展
一些有用的命令
# To test
npm test
# To lint
npm run lint
# To release
npm run release
更多命令请见 package.json
许可证
麻省理工学院