@shrimpcoder/ts-fetcher - v0.1.0

Fetcher

Fetcher is a utility class for making HTTP requests with various data formats (text, JSON, Blob, FormData, ArrayBuffer). It provides a unified interface for handling requests and responses, ensuring consistency and reusability across your codebase.

  • GET Requests: Fetch data from a URL.
  • POST Requests: Send text, JSON, Blob, FormData, and ArrayBuffer data to a URL.
  • Error Handling: Throws HTTPError for HTTP errors and ZodError for schema validation errors.
  • Response Parsing: Automatically parses responses based on content type.
  • URL Builder: Provides an object-oriented interface for building URLs.
  • Query Builder: Provides an object-oriented interface for building query parameters.
npm install @shrimpcoder/ts-fetcher

Fetcher supports integration with Zod. You can use Zod schemas to validate responses.

[!Note] You are not required to use Zod schemas.
If no Zod schema is specified, the response will be returned as unknown.

npm install zod
import { Fetcher } from '@shrimpcoder/ts-fetcher';
import { URLBuilder } from '@shrimpcoder/ts-fetcher';
import { z } from 'zod';

const urlBuilder = new URLBuilder({ baseUrl: 'https://api.example.com' });
const schema = z.object({ name: z.string() });

await Fetcher.get(urlBuilder, schema); // {name: string}
await Fetcher.get(urlBuilder); // unknown

For detailed usage, please refer to here.

This project is licensed under the MIT License.