Attributes
Includes Deno configuration
Repository
Current version released
2 years ago
Fetcher
Friendly fetch with full capabilities.
Getting started
Use a fetch-like function with extra features, thanks to provided decorators.
import { Fetcher } 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 = Fetcher({ 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
Fetcher() 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 with defaultOptions and config arguments.
Examples :
POST a JSON payload with a base URL
import type { BaseURLOptions, JsonFetcher, JsonOptions, } from 'https://deno.land/x/fetcher/mod.ts' import { Fetcher } from 'https://deno.land/x/fetcher/mod.ts' const baseURL = 'https://dummyjson.com' const username = 'kminchelle' const password = '0lelplR' const fetcher = Fetcher< JsonFetcher<BaseURLOptions & JsonOptions & RequestInit> >( { baseURL }, { useBaseURL: true, useDefaults: true, useJson: true, }, ) 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', { method: 'POST', json: { username, password }, }) console.log('data:', data)
License
The MIT License