@hastom/fetch
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i @hastom/fetch

Weekly Downloads

3

Version

1.0.3

License

MIT

Unpacked Size

13.2 kB

Total Files

5

Last publish

Collaborators

  • hastom