Skip to main content
Deno 2 is finally here 🎉️
Learn more

deno module deno doc CI codecov vr scripts license

Fetcher

Decorators that makes fetch friendly.

Getting started

Use a fetch-like function with extra features, thanks to provided decorators.

import { useAll } 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 = useAll<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

useAll() returns a fetcher object 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 {
      useBaseURL,
      useJsonResponse,
    } from 'https://deno.land/x/fetcher/mod.ts'
    
    const baseURL = 'https://dummyjson.com'
    const username = 'kminchelle'
    const password = '0lelplR'
    
    const fetcher = useJsonResponse(useBaseURL())
    
    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