Skip to main content
Deno 2 is finally here šŸŽ‰ļø
Learn more

deno module deno doc CI codecov license

Fetcher

Decorators for a more friendly fetch.

Getting started

Thanks to provided decorators, use a fetch-like with extra features: baseURL, query params, timeout, json response…

import { decoratedFetcher } from 'https://deno.land/x/fetcher/mod.ts'

type Data = { name: string; username: string }

const email = 'Lucio_Hettinger@annie.ca'

const baseURL = 'https://jsonplaceholder.typicode.com'
const fetcher = decoratedFetcher<Data[]>({ baseURL })
try {
  console.log('Fetching /users from jsonplaceholder…')
  const data: Data[] = await fetcher.fetch(
    '/users',
    { query: { email }, timeout: 5000 },
  )
  const { name, username } = data[0]
  console.log('Success!')
  console.log('Result:', { name, username })
} catch (err) {
  console.error(err)
}

Features

Usecases

The decoratedFetcher object is decorated with all the decorators, enabling all the features in one.

If you don’t need all the features, feel free to customize your fetcher instance with the decorators you want.

Examples :

  • POST a JSON payload with a base URL

    import {
      withBaseURL,
      withJsonResponse,
    } from 'https://deno.land/x/fetcher/mod.ts'
    
    const baseURL = 'https://dummyjson.com'
    const username = 'kminchelle'
    const password = '0lelplR'
    
    const fetcher = withJsonResponse(withBaseURL())
    
    type Data = {
      id: number
      username: string
      email: string
      firstName: string
      lastName: string
      gender: string
      image: string
      token: string
    }
    
    const data = await fetcher.fetch<Data>('/auth/login', {
      baseURL,
      method: 'POST',
      json: { username, password },
    })
    
    console.log('data:', data)

License

The MIT License