Skip to content

hastom/fetch

Repository files navigation

A little sugar to Fetch API

Axios-like request methods:

import { fetcher } from '@hastom/fetch'

await fetcher.get('https://google.com')

Auto-stringify get-params:

import { fetcher } from '@hastom/fetch'

await fetcher('https://google.com', {
  params: {
    query: 'string',
    and: ['also', 'arrays']
  }
})

Auto-strigify body to json and set content-type to application/json:

import { fetcher } from '@hastom/fetch'

await fetcher.post('https://google.com', {
  body: {
    query: 'string',
    and: ['also', 'arrays']
  }
})

Create instances with default params:

import { createFetchInstance } from '@hastom/fetch'

const authorizedFetch = createFetchInstance({
  headers: {
    Authorization: 'Bearer ...',
  },
})

await authorizedFetch.get('https://my-api.com/private-data')

and set base url:

import { createFetchInstance } from '@hastom/fetch'

const myApi = createFetchInstance({
  baseURL: 'https://my-api.com',
})

await myApi.get('/important-data')

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published