JSPM

  • Created
  • Published
  • Downloads 424
  • Score
    100M100P100Q97978F
  • License MIT

An even simpler wrapper around native Fetch to strip boilerplate from your fetching code.

Package Exports

  • itty-fetcher

Readme

itty-fetcher

npm package Build Status Open Issues

Super lightweight (~500 bytes) wrapper to simplify native fetch calls using common HTTP methods.

Features

  • Fully typed/TypeScript support
  • Automatically parses responses (optional)
  • Automatically serializes object payloads
  • Accepts any HTTP method (including user-defined)
  • 404, 400, 500, errors actually throw to allow easier catching
  • still allows any native fetch options (including headers, etc) to be sent

Simple Usage

import { fetcher } from 'itty-fetcher'

// create a basic fetcher with default options
const basics = fetcher()

// skips the body parsing for normal GET requests
await basics.get('https://api.kittens.com/v1/names/?max=2') // ['Fluffy', 'Mittens']

// set a base for simplifying repeated calls
const api = fetcher({ base: 'https://api.kittens.com/v1/' })

// then use it... base will be prepended to urls
await api.get('names/?max=2') // ['Fluffy', 'Mittens']

// automatic handle sending payloads (no need to stringify and set headers)
await api.post('create-a-cat', { name: 'Halsey', age: 3 }) // { id: 'Q4AW', name: 'Halsey', age: 3 }

// use any conceivable HTTP method
api.put('kitten/13', { name: 'Different Cat' }) // sends using PUT method
api.foo('kitten/13', { name: 'Different Cat' }) // sends using FOO method

// ERROR HANDLING: 400, 404, 500, etc will actually throw, allowing an easy catch
api
  .get('not-a-valid-path')
  .catch(({ status, message }) => {
    console.log('received a status', status, 'error with message:', message)
  })

ADVANCED USAGE

// skipping autoParse returns full Response control
const unparsed = fetcher({ autoParse: false })

unparsed
  .get('https://api.kittens.com/v1/names/?max=2')
  .then(response => {
    if (response.ok) return response.json()
  })

// can send all native fetch options through in 3rd param
fetcher()
  .post('https://api.kittens.com/v1/names/?max=2',
        { payload: 'is second param' },
        {
          credentials: 'same-origin',
          cache: 'no-cache',
          headers: {
            ['my-fancy-header']: 'will be sent'
          }
        }
  )

Installation

npm install itty-fetcher

API

fetcher(options?: FetcherOptions): FetcherType

Factory function to create the IttyDurable class (with options) for your Durable Object to extend.

Option Type(s) Default Description
autoParse boolean true By default, all responses are parsed to JSON/text/etc. To access the Response directly, set this to false.
base string '' Use this to prefix all future fetch calls, for example { base: "https://api.foo.bar/v1" }, allows future calls such as fetcher.get('kittens/14') to work by automatically prepending the base URL.

Special Thanks

I have to thank my friends and colleagues that helped me through the idea itself, implementation details, and importantly... made it possible for me to muck through making this a TS-first library. Huge thanks for that!!