Skip to content

gbyesiltas/axios-fetch-adapter

 
 

Repository files navigation

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

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

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

To use with a custom fetch function:

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

To use with the Square API:

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

About

An Axios adapter that uses native `fetch`. Useful for Cloudflare Workers and ServiceWorker environments.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.4%
  • JavaScript 1.6%