@haverstack/axios-fetch-adapter

中文文档
0.12.0发布于 2023/12/24MITTypeScriptESM + CJS零依赖近 30 天下载 3.5万

An Axios adapter that uses native fetch or custom fetch functions. Useful for Cloudflare Workers and ServiceWorker environments.

原始使用文档
文档目录7 节
  1. Install
  2. Use
  3. Using with custom fetch functions
  4. Using with the Square Node.js SDK
  5. Development
  6. Acknowledgements
  7. License

axios-fetch-adapter

npm badge checks badge codecov badge license badge

An Axios adapter that uses native fetch or a custom fetch function. Useful for Cloudflare Workers and ServiceWorker environments.

Note: This adapter was designed for version 0.21.1 of Axios, which is still used in prominent e-commerce SDKs.

Install

npm install @haverstack/axios-fetch-adapter

Use

The default use-case for this library is:

import axios from "axios";
import fetchAdapter from "@haverstack/axios-fetch-adapter";

const client = axios.create({
  adapter: fetchAdapter
});

Using with custom fetch functions

If your application does not use a globally-available fetch, you can specify your own custom fetch function instead:

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
});

If your application allows for using non-fully-qualified URLs, e.g. /foo, use the disableRequest option to pass URLs directly to your custom fetch function without creating a Request object:

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
});

Note: A side effect of the disableRequest option is that the AxiosResponse object will only have the request URL in its request property instead of a Request object. This means that accessing, for example, response.request.url will throw an error.

Using with the Square Node.js SDK

To use this library with the square package to manage your Square resources:

import { Client, Environment } from "square";
import fetchAdapter from "@haverstack/axios-fetch-adapter";

const client = new Client({
  accessToken,
  environment,
  unstable_httpClientOptions: { adapter: fetchAdapter }
});

Development

# Run tests
npm run test

# Check tests, linting, and formatting
npm run check

# Fix linting and formatting
npm run fix

A Miniflare testing environment is used in order to simulate a Cloudflare Worker or a ServiceWorker. This testing environment is also useful because Node does not have a native implementation of fetch.

Acknowledgements

The code in this repo draws heavily from the following projects:

  • vespaiach/axios-fetch-adapter: Most of the initial code in this repo was copied from here. Licensed MIT.
  • axios/axios: The buildFullPath function from axios has been copied here and modified to be more flexible. Licensed MIT.

License

MIT

版本与发布记录

@haverstack/axios-fetch-adapter 共发布 14 个版本,首次发布于 2022/11/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.12.0latest2023/12/242 年前
0.11.02023/12/232 年前
0.10.02023/12/232 年前
0.9.02023/12/232 年前
0.8.02023/08/143 年前
0.7.02023/04/033 年前

关于 @haverstack/axios-fetch-adapter 的常见问题

@haverstack/axios-fetch-adapter 是什么?

An Axios adapter that uses native fetch or custom fetch functions. Useful for Cloudflare Workers and ServiceWorker environments.

如何安装 @haverstack/axios-fetch-adapter?

在项目目录运行 npm install @haverstack/axios-fetch-adapter,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:axios(^0.21.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@haverstack/axios-fetch-adapter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@haverstack/axios-fetch-adapter 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@haverstack/axios-fetch-adapter 可以商用吗?

@haverstack/axios-fetch-adapter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@haverstack/axios-fetch-adapter 的下载量有多少?

根据 npm 官方下载统计,@haverstack/axios-fetch-adapter 在 2026/10/18 – 2026/11/16 这 30 天内被下载 35,435 次,日均约 1,181 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@haverstack/axios-fetch-adapter 还在维护吗?

最新版本 0.12.0 发布于 2023/12/24(2 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。