Skip to content

un-ts/x-fetch

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8d9a862 · Jan 29, 2024

History

16 Commits
Jan 29, 2024
Dec 14, 2023
Dec 14, 2023
Nov 7, 2023
Jan 18, 2024
Jan 29, 2024
Jan 29, 2024
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Dec 14, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Nov 7, 2023
Dec 14, 2023
Nov 7, 2023
Jan 29, 2024
Nov 7, 2023
Dec 14, 2023
Jan 29, 2024
Dec 14, 2023
Nov 9, 2023
Dec 14, 2023
Jan 18, 2024

Repository files navigation

x-fetch

GitHub Actions Codecov type-coverage npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier changesets

A simple but elegant fetch API wrapper, use fetch like a charm

TOC

Usage

Install

# pnpm
pnpm add x-fetch

# yarn
yarn add x-fetch

# npm
npm i x-fetch

API

import { ApiMethod, createFetchApi, fetchApi, interceptors } from 'x-fetch'

// plain url, GET method
await fetchApi('url')

// with options, `method`, `body`, `query`, etc.
await fetchApi('url', {
  method: ApiMethod.POST, // or 'POST'
  // plain object or array, or BodyInit
  body: {},
  // URLSearchParametersOptions
  query: {
    key: 'value',
  },
  // json: boolean, // whether auto stringify body to json, default true for plain object or array, otherwise false
  // type: 'arrayBuffer' | 'blob' | 'json' | 'text' | null, `null` means plain `Response`
})

const interceptor: ApiInterceptor = (req, next) => {
  // do something with req
  const res = await next(req)
  // do something with res
  return res
}

// add interceptor
interceptors.use(interceptor)

// remove interceptor
interceptors.eject(interceptor)

// create a new isolated `fetchApi` with its own `interceptors`
const { fetchApi, interceptors } = createFetchApi()

Sponsors

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Backers

Backers

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

MIT © JounQin@1stG.me