@maciekdev/fetcher
v1.14.1
Published
HTTP client based on fetch API
Downloads
20
Maintainers
Readme
Fetcher
A lightweight and simple fetch wrapper for Node.js and potentially the browser.
Fetcher provides a minimal and straightforward way to make HTTP requests using the familiar fetch API. It aims to simplify common use cases without adding unnecessary complexity.
Features
- Lightweight: Minimal dependencies and a small footprint.
- Simple API: Mirrors the standard
fetchAPI for ease of use. - TypeScript Support: Written in TypeScript with type definitions included.
Installation
pnpm add @maciekdev/fetcher
# or
npm install @maciekdev/fetcher
# or
yarn add @maciekdev/fetcher
# or
bun add @maciekdev/fetcherExample usage
// In this example, the `zod` library is used for schema validation.
// Fetcher is compatible with any standard schema validation library.
import { z } from "zod/v4";
import { createFetcherInstance } from "@maciekdev/fetcher";
export const fetcher = createFetcherInstance({
baseURL: "https://yourApiPath.com",
});
const zodSchema = z.object({
username: z.string(),
});
const myData = await fetcher({
method: "GET",
url: "/test-endpoint",
schema: zodSchema,
});
console.log(myData?.data.username);
/*
username: string | undefined
Typesafe!
*/API reference
createFetcherInstance(options | undefined)
Parameters:
options?: (FetcherInstanceOptions) Configuration object for fetcher instance.
fetcher(options)
Parameters:
options?: (FetcherOptions) Configuration object for fetcher instance.
Options config:
- `responseType`?: "json" | "text" | "arrayBuffer";
- `method`: "GET" | "POST" | "DELETE" | "PUT" | "PATCH";
- `url`: string;
- `body`?: FormData | Record<string, unknown>;
- `schema`?: `zodSchema`;
- `throwOnError`?: boolean;
- `signal`?: AbortSignal;
- `headers`?: Record<string, string>;
- `headersStrategy`?: "merge" | "overwrite" | "omit-global";
- `cache`?: "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached";
- `credentials`?: "include" | "omit" | "same-origin";
- `keepalive`?: boolean;
- `mode`?: "cors" | "no-cors" | "same-origin";
- `priority`?: "auto" | "high" | "low";
- `redirect`?: "follow" | "error" | "manual";
- `referrer`?: "about:client" | "client" | "no-referrer";
- `referrerPolicy`?: "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url";
- `integrity`?: string;
- `onErrorThrown`?: (error: Error) => void;License
MIT
