Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 1.07 KB

File metadata and controls

42 lines (29 loc) · 1.07 KB
— @zhaoworks/fetch

An elegant fetch wrapper inspired by axios and Rust's Result.

Features

  • ✅ Designed to work well with TanStack Query
  • ✅ Response validation with StandardSchema (Zod, Arktype, ...)
  • ✅ Typed HTTP responses & error union (HttpResult<T>)
  • ✅ Built-in timeout and abort controller
  • ✅ Supports FormData, URL query (?query) and parameters (/:id)

Installation

λ bun add @zhaoworks/fetch

Usage

import { HttpClient } from '@zhaoworks/fetch';

export const http = new HttpClient({
  endpoint: 'https://api.example.com',
  headers: () => ({ Authorization: `Bearer ${getToken()}` }),
});

const result = await http.get<{ name: string; }>('/users/:id', {
  params: { id: '123' },
});

if (!result.success) {
  return console.error(result.error.message);
}

console.log(result.data.name);

License

MIT