axios-fetch-适应器
Axios 适应器,使用本地 fetch 或习惯 fetch 函数。 对云浮工人和服务工人环境有用.
说明: 这个适配器是针对版本设计的
0.21.1axios,它仍然用于突出的电子商务 SDKs.
安装
npm install @haverstack/axios-fetch-adapter
使用
此库的默认使用大小写是 :
import axios from "axios";
import fetchAdapter from "@haverstack/axios-fetch-adapter";
const client = axios.create({
adapter: fetchAdapter
});
使用自定义 fetch 函数
如果您的应用程序没有使用全球通用的 fetch,您可以指定自己的习惯 fetch 函数代替:
import axios from "axios";
import { createFetchAdapter } from "@haverstack/axios-fetch-adapter";
import myCustomFetch from "my-custom-fetch";
const myCustomFetchAdapter = createFetchAdapter({ fetch: myCustomFetch });
const client = axios.create({
adapter: myCustomFetchAdapter
});
如果您的应用程序允许使用不完全合格的URL,例如. /foo,使用 disableRequest 选项将 URL 直接传递到您的自定义 fetch 函数不创建 Request 对象 :
import axios from "axios";
import { createFetchAdapter } from "@haverstack/axios-fetch-adapter";
import myCustomFetch from "my-custom-fetch";
const customAdapter = createFetchAdapter({
fetch: myCustomFetch,
disableRequest: true
});
const client = axios.create({
adapter: myCustomFetchAdapter
});
说明: 其副作用为 disableRequest 选项是: AxiosResponse 对象只会在其 request 属性而不是 Request 对象。 这意味着,例如,获取, response.request.url 将投出一个错误.
与 Square 节点使用 SDK
import { Client, Environment } from "square";
import fetchAdapter from "@haverstack/axios-fetch-adapter";
const client = new Client({
accessToken,
environment,
unstable_httpClientOptions: { adapter: fetchAdapter }
});
发展
# Run tests
npm run test
# Check tests, linting, and formatting
npm run check
# Fix linting and formatting
npm run fix
A 类 迷你灯 测试环境用于模拟Cloudflare工人或服务工人。 这种测试环境也是有用的,因为节点没有本地执行 fetch。 。 。 .
鸣谢
此 repo 中的代码大量取自以下项目:
- vespaiach/xios-fetch-适应器: 此repo中的大部分初始代码是从这里复制的. 授权麻省理工学院.
- 轴/轴编号:
buildFullPath函数从axios已被复制并修改为更灵活。 授权麻省理工学院.