@ exodus/ fetch- factory (英语)
基于主机域管理和设置全局请求头的功能 .
此模块允许您配置特定主机域和全球的默认信头 。 它提供了自定义的获取函数,在每个请求中自动包含这些信头.
特征
- 设置适用于所有请求的全局信头 .
- 配置特定主机域的标题 .
- 将每个请求的自定义标题与已有标题合并.
- 支持为expo.io和expo-int.com设置默认标识符头.
安装
使用 npm 安装软件包 :
npm install @exodus/fetch-factory
使用量
导入和初始化
const { FetchFactory } = require('@exodus/fetch-factory')
const nodeFetch = require("node-fetch")
const headerSetter = new FetchFactory(nodeFetch)
const exchangeFetcher = headerSetter.setRequestHeaders({
"authentication": "<my-key">
}, ["exchange.io"]).create()
exchangeFetcher('https://exchange.io/api/data', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}).then((response) => {
// handle response
})
方法
setHeaders(headers, allowedHostDomains = ['*']): FetchFactory
为指定的主机域设置自定义信头 .
- 信头 (对象): 要设置的信头 .
- 允许HostDomains (Array):这些标题应该应用的域或子域列表. 为一个域所设置的标题也会适用于其子域.
setDefaultExodusIdentifierHeaders({ appId, appVersion, appBuild }): FetchFactory
设置默认标识符头( S)x-exodus-app-id, (中文(简体) ), x-exodus-version, (中文(简体) ), x-requested-with),用于expo.io和expo-int.com域及其子域. 导出为 hosts.EXODUS_HOST 财务报告和审定财务报表 hosts.EXODUS_INT_HOST。 。 。 .
- appid (String):应用程序ID.
- appVersion (String):应用程序版本.
- appBuild (String):应用程序构建标识符.
create()
返回自定义的抓取函数,该函数包括已配置的标题.
fetchFn (Function): 原始获取函数.