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

4.9.3 • Public • Published

codecov npm

Inspired by contactlab/appy.

pipe(
  mkRequest(() => 'Network error'),
  withBaseURL('https://example.com', () => 'Invalid URL'),
  withURLSearchParams({
    foo: 'bar',
  }),
  withTimeout(5_000, () => 'Out of time'),
  withJSON({
    foo: 'bar',
  }),
  ensureStatus(
    n => n === 200,
    () => 'Unexpected status code',
  ),
  asJSON(() => 'Invalid response body'),
  runFetchMPT('/path'),
)

instead of

const controller = new AbortController()

const id = setTimeout(() => controller.abort(), 5_000)

try {
  const resp = await fetch('https://example.com/path?foo=bar', {
    body: JSON.stringify({ foo: 'bar' }),
    headers: {
      'Content-Type': 'application/json',
    },
    signal: controller.signal,
  })

  clearTimeout(id)

  if (resp.status !== 200) {
    throw new Error('Request failed')
  }

  const json = await resp.json()
} catch (e) {
  // Handle error
}

Readme

Keywords

Package Sidebar

Install

npm i @equt/fetch

Weekly Downloads

1

Version

4.9.3

License

MIT

Unpacked Size

146 kB

Total Files

30

Last publish

Collaborators

  • equt