Quick Start
Quick Start
This guide will get you up and running with @shkumbinhsn/fetcher in just a few minutes.
Basic Usage
The simplest way to use the library is as a drop-in replacement for fetch:
import { fetcher } from '@shkumbinhsn/fetcher';
// Works exactly like fetchconst data = await fetcher('/api/users');console.log(data);With Schema Validation
For type-safe responses, add a schema:
import { fetcher } from '@shkumbinhsn/fetcher';import { z } from 'zod';
// Define your data structureconst UserSchema = z.object({ id: z.string(), name: z.string(), email: z.string().email()});
// Get fully typed responseconst user = await fetcher('/api/users/123', { schema: UserSchema});
// TypeScript knows the exact shape of userconsole.log(user.name); // ✓ Type-safeconsole.log(user.age); // ✗ TypeScript errorHTTP Methods
Use any HTTP method just like with fetch:
// GET (default)const user = await fetcher('/api/users/123', { schema: UserSchema});
// POSTconst newUser = await fetcher('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' }), schema: UserSchema});
// PUTconst updatedUser = await fetcher('/api/users/123', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Jane Doe' }), schema: UserSchema});
// DELETEawait fetcher('/api/users/123', { method: 'DELETE'});Error Handling
Handle errors with custom error classes:
import { fetcher, defineError } from '@shkumbinhsn/fetcher';import { z } from 'zod';
// Define error schemasconst NotFoundError = defineError( 404, z.object({ message: z.string(), resource: z.string() }), 'NotFoundError');
const ValidationError = defineError( 400, z.object({ errors: z.array(z.object({ field: z.string(), message: z.string() })) }), 'ValidationError');
// Use in requeststry { const user = await fetcher('/api/users/999', { schema: UserSchema, errors: [NotFoundError, ValidationError] });} catch (error) { if (error instanceof NotFoundError) { console.log(`${error.data.resource} not found`); } else if (error instanceof ValidationError) { error.data.errors.forEach(err => console.log(`${err.field}: ${err.message}`) ); }}Authentication
Add authentication headers like any fetch request:
const user = await fetcher('/api/users/me', { headers: { 'Authorization': 'Bearer your-jwt-token' }, schema: UserSchema});What’s Next?
Now that you have the basics down, explore more advanced features: