Axios Adapter
Hyper Fetch's axios adapter is a simple integration that allows you to use the popular axios HTTP client for making
requests, while still benefiting from all of Hyper Fetch's features like caching, persistence, and request management.
Using the axios adapter gives you the best of both worlds.
- Leverage
axios: Use familiaraxiosoptions, headers, and error handling. - Seamless Integration: Combines
axioswith Hyper Fetch's caching, queuing, and persistence. - Track Progress: Get detailed upload and download progress events.
- Full Control: Access
axiosresponse details and error objects when needed.
Getting Started
To use the axios adapter:
- Install the package:
- npm
- yarn
- pnpm
npm install @hyper-fetch/axios
yarn add @hyper-fetch/axios
pnpm add @hyper-fetch/axios
- Import it and set it on your
Clientinstance:
import { AxiosAdapter } from "@hyper-fetch/axios"
import { Client } from "@hyper-fetch/core";
import { axiosAdapter } from "@hyper-fetch/axios";
const client = new Client({ url: "base-url" }).setAdapter(axiosAdapter);
- ...and voila! It's done. Now you can set
axiosoptions on your requests.
Usage
You can pass any valid axios request configuration options to your Hyper Fetch requests. These options can be set when
you create a request or when you send it.
Here's an example of how to use some common axios options:
import { client } from "./client";
// Create a request with axios-specific options
const getUsers = client.createRequest()({
endpoint: "/users",
options: {
// Set a 5-second timeout for the request
timeout: 5000,
// Add authentication credentials
auth: {
username: "your-username",
password: "your-password",
},
},
});
Or use the setOptions method:
import { client } from "./client";
const getUsers = client
.createRequest()({
endpoint: "/users",
})
.setOptions({
// Set a 5-second timeout for the request
timeout: 5000,
// Add authentication credentials
auth: {
username: "your-username",
password: "your-password",
},
});
See More
For a full list of available axios request configuration options, please refer to the
official axios documentation.
While you can use most axios options, some are managed by Hyper Fetch to ensure proper integration. You should not
pass the following options, as they will be overwritten by the adapter:
urlbaseURLmethoddata(usepayloadinsendmethod instead)onUploadProgress(useonUploadProgressinsendmethod instead)onDownloadProgress(useonDownloadProgressinsendmethod instead)signal(managed by Hyper Fetch for cancellation)