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

4.10.0 • 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
}

Dependencies (0)

    Dev Dependencies (16)

    Package Sidebar

    Install

    npm i @equt/fetch

    Weekly Downloads

    59

    Version

    4.10.0

    License

    MIT

    Unpacked Size

    146 kB

    Total Files

    30

    Last publish

    Collaborators

    • equt