@faasjs/request

License: MITopen in new windowNPM Stable Versionopen in new windowNPM Beta Versionopen in new window

FaasJS's request module.

Install

npm install @faasjs/request

Modules

Type aliases

Functions

Type aliases

Request

Ƭ Request: Object

Type declaration

NameType
body?{ [key: string]: any; }
headers?http.OutgoingHttpHeaders
host?string
method?string
path?string
query?http.OutgoingHttpHeaders

RequestOptions

Ƭ RequestOptions: Object

Type declaration

NameTypeDescription
agent?boolean-
auth?stringHTTP 认证头,格式为 user:password
body?{ [key: string]: any; } | string请求体
downloadStream?NodeJS.WritableStream下载流,用于直接将响应内容保存到本地文件,通过 fs.createWriteStream 创建
file?string上传文件的完整路径
headers?http.OutgoingHttpHeaders请求头
method?string请求方法,默认为 GET
passphrase?string-
pfx?Buffer-
query?{ [key: string]: any; }请求参数,放置于 path 后,若需放置在 body 中,请使用 body 参数
timeout?number最长耗时,单位为毫秒
parse?(body: string) => anybody 解析器,默认为 JSON.parse

Response

Ƭ Response<T>: Object

Type parameters

NameType
Tany

Type declaration

NameType
bodyT
headershttp.OutgoingHttpHeaders
request?Request
statusCode?number
statusMessage?string

Functions

querystringify

querystringify(obj): string

Parameters

NameType
objany

Returns

string


request

request<T>(url, [options={}]?): Promise<Response<T>>

发起网络请求

url https://faasjs.com/doc/request.html

Type parameters

NameType
Tany

Parameters

NameTypeDescription
urlstring请求路径或完整网址
[options={}]RequestOptions参数和配置

Returns

Promise<Response<T>>


setMock

setMock(handler): void

设置模拟请求

example setMock(async (url, options) => Promise.resolve({ headers: {}, statusCode: 200, body: { data: 'ok' } }))

Parameters

NameTypeDescription
handlerMock模拟函数,若设置为 null 则表示清除模拟函数

Returns

void