Skip to content

Commit 19f9f36

Browse files
docs(readme): add custom fetch section; (#7024)
1 parent 3cac78c commit 19f9f36

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
- [🆕 Rate limiting](#-progress-capturing)
8484
- [🆕 AxiosHeaders](#-axiosheaders)
8585
- [🔥 Fetch adapter](#-fetch-adapter)
86+
- [🔥 Custom fetch](#-custom-fetch)
87+
- [🔥 Using with Tauri](#-using-with-tauri)
88+
- [🔥 Using with SvelteKit](#-using-with-sveltekit-)
8689
- [Semver](#semver)
8790
- [Promises](#promises)
8891
- [TypeScript](#typescript)
@@ -1626,6 +1629,77 @@ const {data} = fetchAxios.get(url);
16261629
The adapter supports the same functionality as `xhr` adapter, **including upload and download progress capturing**.
16271630
Also, it supports additional response types such as `stream` and `formdata` (if supported by the environment).
16281631
1632+
### 🔥 Custom fetch
1633+
1634+
Starting from `v1.12.0`, you can customize the fetch adapter to use a custom fetch API instead of environment globals.
1635+
You can pass a custom `fetch` function, `Request`, and `Response` constructors via env config.
1636+
This can be helpful in case of custom environments & app frameworks.
1637+
1638+
Also, when using a custom fetch, you may need to set custom Request and Response too. If you don't set them, global objects will be used.
1639+
If your custom fetch api does not have these objects, and the globals are incompatible with a custom fetch,
1640+
you must disable their use inside the fetch adapter by passing null.
1641+
1642+
> Note: Setting `Request` & `Response` to `null` will make it impossible for the fetch adapter to capture the upload & download progress.
1643+
1644+
Basic example:
1645+
1646+
```js
1647+
import customFetchFunction from 'customFetchModule';
1648+
1649+
const instance = axios.create({
1650+
adapter: 'fetch',
1651+
onDownloadProgress(e) {
1652+
console.log('downloadProgress', e);
1653+
},
1654+
env: {
1655+
fetch: customFetchFunction,
1656+
Request: null, // undefined -> use the global constructor
1657+
Response: null
1658+
}
1659+
});
1660+
```
1661+
1662+
#### 🔥 Using with Tauri
1663+
1664+
A minimal example of setting up Axios for use in a [Tauri](https://tauri.app/plugin/http-client/) app with a platform fetch function that ignores CORS policy for requests.
1665+
1666+
```js
1667+
import { fetch } from "@tauri-apps/plugin-http";
1668+
import axios from "axios";
1669+
1670+
const instance = axios.create({
1671+
adapter: 'fetch',
1672+
onDownloadProgress(e) {
1673+
console.log('downloadProgress', e);
1674+
},
1675+
env: {
1676+
fetch
1677+
}
1678+
});
1679+
1680+
const {data} = await instance.get("https://google.com");
1681+
```
1682+
1683+
#### 🔥 Using with SvelteKit
1684+
1685+
[SvelteKit](https://svelte.dev/docs/kit/web-standards#Fetch-APIs) framework has a custom implementation of the fetch function for server rendering (so called `load` functions), and also uses relative paths,
1686+
which makes it incompatible with the standard URL API. So, Axios must be configured to use the custom fetch API:
1687+
1688+
```js
1689+
export async function load({ fetch }) {
1690+
const {data: post} = await axios.get('https://jsonplaceholder.typicode.com/posts/1', {
1691+
adapter: 'fetch',
1692+
env: {
1693+
fetch,
1694+
Request: null,
1695+
Response: null
1696+
}
1697+
});
1698+
1699+
return { post };
1700+
}
1701+
```
1702+
16291703
## Semver
16301704
16311705
Since Axios has reached a `v.1.0.0` we will fully embrace semver as per the spec [here](https://semver.org/)

0 commit comments

Comments
 (0)