Drop-in minimalistic axios
replacement based on a native fetch
.
axios
is a great library, but it has some issues:
- It's quite big
- It's using old
XMLHttpRequest
API on browser andhttp
on Node.js which brings some inconsistencies - It's slow on Node.js
- It's not supporting Next.JS caching mechanism, because it's not using
fetch
npm install --save navios
or
yarn add navios
import navios from 'navios'
navios.get('https://example.com').then((response) => {
console.log(response.data)
})
import { create } from 'navios'
const client = create({
baseURL: 'https://example.com/api/',
headers: {
'X-Custom-Header': 'foobar',
},
})
client.interceptors.request.use((config) => {
console.log('Request to', config.url)
return config
})
client.interceptors.response.use(
(response) => {
console.log('Response from', response.config.url)
return response
},
(error) => {
console.error('Error from', error.config.url)
if (error.response.status === 401) {
console.error('Unauthorized')
}
return Promise.reject(error)
},
)
client.get('users').then((response) => {
console.log(response.data)
})
Creates a new instance of navios
with a custom configuration.
-
baseURL
- Base URL for requests -
headers
- Default headers -
responseType
- Default response type:json
,text
,blob
,arrayBuffer
,formData
,stream
-
validateStatus
- Custom function to validate status code -
adapter
- Custom adapter function, likefetch
fromundici
-
FormData
- CustomFormData
implementation -
URLSearchParams
- CustomURLSearchParams
implementation
Make a request with custom configuration.
-
url
- URL for the request (will be combined withbaseURL
) -
method
- HTTP method -
headers
- Custom headers -
params
- URL search parameters (will be appended to the URL), can be an object orURLSearchParams
-
data
- Request body, can be an object orFormData
-
responseType
- Response type:json
,text
,blob
,arrayBuffer
,formData
,stream
-
validateStatus
- Custom function to validate status code -
baseURL
- Base URL for the request, overrides the instancebaseURL
-
credentials
- Request credentials:omit
,same-origin
,include
-
cancelToken
- AbortSignal to cancel the request
Plus other fetch
RequestInit options.
Make a GET
request.
Make a DELETE
request.
Make a HEAD
request.
Make a OPTIONS
request.
Make a POST
request.
Make a PUT
request.
Make a PATCH
request.
Default configuration for the client. Can be modified to affect all requests, like change default headers.
Interceptors for requests and responses.
Add a request interceptor.
Add a response interceptor.