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
- Base URL :
- add the ability to set a base URL and then use a simple relative value in
url, usefull when fetch is used multiple times to request the same base URL (DRY). - add
BaseURLOptionssupport to fetch options.
- add the ability to set a base URL and then use a simple relative value in
- Basic auth :
- add authentication ability based on Basic Auth credentials
- add
BasicAuthOptionssupport to fetch options.
- Bearer :
- add authentification ability based on a token in request header
- add
BearerOptionssupport to fetch options.
- Defaults :
- add ability to set default options at Fetcher creation.
- Error handling
:
- add error throwing abilitty when a response send an HTTP error
- add
ErrorHandlingOptionssupport to fetch options.
- JSON response & payload
:
- add support for JSON response and JSON payload
- return the response body object (JSON data) instead of a
Response - add
JsonOptionssupport to fetch options.
- Query params
:
- add support for request query parameters as a plain object
- add
QueryParamsOptionssupport to fetch options.
- Timeout :
- add a timeout feature, aborting the request whenever the server did not respond until the specified duration
- add
TimeoutOptionssupport to fetch options.
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 }, { withBaseURL: true, withDefaults: true, withJson: 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