navios
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

Navios

Drop-in minimalistic axios replacement based on a native fetch.

Why?

axios is a great library, but it has some issues:

  • It's quite big
  • It's using old XMLHttpRequest API on browser and http 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

Installation

npm install --save navios

or

yarn add navios

Basic Usage

import navios from 'navios'

navios.get('https://example.com').then((response) => {
  console.log(response.data)
})

Usage with interceptors

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

API

create(config)

Creates a new instance of navios with a custom configuration.

config

  • 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, like fetch from undici
  • FormData - Custom FormData implementation
  • URLSearchParams - Custom URLSearchParams implementation

client.request(requestConfig)

Make a request with custom configuration.

requestConfig

  • url - URL for the request (will be combined with baseURL)
  • method - HTTP method
  • headers - Custom headers
  • params - URL search parameters (will be appended to the URL), can be an object or URLSearchParams
  • data - Request body, can be an object or FormData
  • responseType - Response type: json, text, blob, arrayBuffer, formData, stream
  • validateStatus - Custom function to validate status code
  • baseURL - Base URL for the request, overrides the instance baseURL
  • credentials - Request credentials: omit, same-origin, include
  • cancelToken - AbortSignal to cancel the request

Plus other fetch RequestInit options.

client.get(url[, config])

Make a GET request.

client.delete(url[, config])

Make a DELETE request.

client.head(url[, config])

Make a HEAD request.

client.options(url[, config])

Make a OPTIONS request.

client.post(url[, data[, config]])

Make a POST request.

client.put(url[, data[, config]])

Make a PUT request.

client.patch(url[, data[, config]])

Make a PATCH request.

client.defaults

Default configuration for the client. Can be modified to affect all requests, like change default headers.

client.interceptors

Interceptors for requests and responses.

client.interceptors.request.use(onFulfilled[, onRejected])

Add a request interceptor.

client.interceptors.response.use(onFulfilled[, onRejected])

Add a response interceptor.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.1.10alpha
0.4.080latest

Version History

VersionDownloads (Last 7 Days)Published
0.4.080
0.4.0-alpha.50
0.4.0-alpha.40
0.4.0-alpha.30
0.4.0-alpha.20
0.4.0-alpha.10
0.4.0-alpha.00
0.3.00
0.3.0-alpha.584
0.3.0-alpha.40
0.3.0-alpha.30
0.3.0-alpha.20
0.3.0-alpha.10
0.3.0-alpha.01
0.2.31
0.2.20
0.2.10
0.2.00
0.1.30
0.1.20
0.1.10

Package Sidebar

Install

npm i navios

Weekly Downloads

166

Version

0.4.0

License

MIT

Unpacked Size

114 kB

Total Files

29

Last publish

Collaborators

  • arilas