bfx-facs-http (英语)
简化了http对承诺和召回方法的要求.
配置
Fac 不支持配置文件,而是支持下面定义的选项:
baseUrl<String?>- 可选基础urltimeout<Number?>- 可选请求超时, 默认为节点默认超时( 0)abortTimeout<Number?>- 在 ms 中可选中止超时, 自动创建一个中止控制器, 在指定的持续时间后中止请求 。 默认为 0( 损坏) 。 按请求abortTimeout覆盖此值。 当呼叫者自己提供时被忽略signaldebug<(Boolean|Number)?>- 可选调试旗, 默认为假qs<(String|Array<String>|Object)?>- 可选、默认的查询字符串参数dnsCaching<Boolean?>- 默认禁用选项 dns 缓存
API 密码
请求
执行通用 HTTP 请求
参数 :
path<String>- 完全请求url或路径opts<Object?>- 可选请求选项body<(String|Object)?>- 可选请求体,如果Json编码,也可以是对象headers<Object<String, String>?>可选请求头method<String?>- 目前支持的可选http方法get, (中文(简体) ),head, (中文(简体) ),post, (中文(简体) ),put, (中文(简体) ),patch, (中文(简体) ),delete, (中文(简体) ),options默认值为getredirect<Boolean?>- 可选,跟踪方向或视其为错误,默认则视其为错误auth<Object?>- HTTP基本授权证书。 覆盖信头authorization。请注意 只有 HTTP Basic auth 通过此参数可配置 .username<String?>password<String?>
agent<http.Agent?>- 可选用户代理compress<Boolean?>- 可选,支持gzip/deflate内容编码timeout<Number?>- 可选请求超时, 默认在 fac 设置( 3s) 中提供encoding<(String|Object)?>- 可选请求和答复编码, 如果字符串同时适用于请求和答复. 默认编码是请求和响应的文本req<String?>- 可选请求体编码,支持:json财务报告和审定财务报表text。 。 。 . 如果没有提供值,则将被视为文本res<String?>- 可选反应机体编码,支持:json, (中文(简体) ),text财务报告和审定财务报表raw。 。 。 . 如果没有提供价值,将被视为文字, 如果提供不支持的值,则返回缓冲. 如果值为raw然后返回正文流,用于文件下载
qs<(String|Array<String>|Object)?>- 可选的查询字符串参数signal<AbortSignal?>- 可选的中止控制器信号abortTimeout<Number?>- 可选择的中止超时,以毫秒计,用于此请求,覆盖设施级别abortTimeout。当signal提供dnsCaching<Boolean?>- 可选,允许 dns 缓存
cb<Function?>- 可选回调功能,如果不提供回调,将被视为承诺
回应:
Promise<{ body: any, headers: object }>|void- 服务器响应、承诺或回调结果
实例:
// GET
const { body, headers } = await fac.request('https://example.com')
// POST
const { body, headers } = await fac.request('https://example.com/submit', { method: 'post', body: { foo: 'bar' }, encoding: 'json' })
// OPTIONS
const { headers } = await fac.request('https://api-pub.bitfinex.com/v2/conf/pub:list:currency', { method: 'options' })
// Callback
fac.request(
'/data/store',
{ method: 'patch', body: 'test=33&foo=bar', headers: { 'content-type': 'application/x-www-form-urlencoded' } },
(err, resp) => {
...
})
// Request without options
fac.request('/data', (err, resp) => {
...
})
// error handling
fac.request('/data/store', (err) => {
console.log(err.message) // ERR_HTTP: 500 - Internal Server Error
console.log(err.status) // 500
console.log(err.statusText) // Internal Server Error
console.log(err.response) // { auth: 'failed' }
})
// file download
const stream = require('stream')
const { body: resp } = await fac.request('/file', { encoding: { res: 'raw' } }) // raw means return stream
await new Promise((resolve, reject) => {
stream.pipeline(resp, fs.createWriteStream(writefile), (err) => err ? reject(err) : resolve())
})
// abort signal
const abortController = new AbortController()
setTimeout(() => {
abortController.abort()
}, 1000)
const { body: resp } = await fac.request('/data', { signal: abortController.signal })
// abort timeout (auto-creates AbortController)
const { body: resp } = await fac.request('/data', { abortTimeout: 5000 })
fac.get (英语)
执行 HTTP 获取请求
参数 :
- 与请求方法相同
method段落
回应:
- 与请求方法相同
示例
await fac.get('https://api-pub.bitfinex.com/v2/conf/pub:list:currency')
图片
执行 HTTP POST 请求
参数 :
- 与请求方法相同
method段落
回应:
- 与请求方法相同
示例
const FormData = require('form-data')
const form = new FormData();
form.append('a', 1);
const reqOpts = {
body: form,
headers: form.getHeaders()
}
await fac.post('/submit', reqOpts)
直径.put
执行 HTTP PUT 请求
参数 :
- 与请求方法相同
method段落
回应:
- 与请求方法相同
示例
const reqOpts = {
body: JSON.stringify({ a: 1 }),
headers: {
'Content-Type': 'application/json'
}
}
await fac.put('/data/store', reqOpts)
传真:fac.patch
执行 HTTP PATCH 请求
参数 :
- 与请求方法相同
method段落
回应:
- 与请求方法相同
示例
const reqOpts = {
body: { a: 1 },
encoding: 'json'
}
await fac.patch('/data/store', reqOpts)
传真
执行 HTTP DELETE 请求
参数 :
- 与请求方法相同
method段落
回应:
- 与请求方法相同
示例
const reqOpts = {
body: JSON.stringify({ id: 31 }),
headers: {
'Content-Type': 'application/json'
}
}
await fac.delete('/books', reqOpts)
头部
执行 HTTP 头部请求
参数 :
- 与请求方法相同
method段落
回应:
- HTTP 响应头
示例
const { headers } = await fac.head('https://api-pub.bitfinex.com/v2/conf/pub:list:currency')
console.log(headers['content-type']) // 'application/json; charset=utf-8'
fac. 选项
执行 HTTP 选项请求
参数 :
- 与请求方法相同
method段落
回应:
- HTTP 响应头
示例
const { headers } = await fac.head('https://api-pub.bitfinex.com/v2/conf/pub:list:currency')
console.log(headers['allow']) // 'GET,PUT,POST'