0.1.0 • Published 2 years ago
@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.
Features
- GET Requests: Fetch data from a URL.
- POST Requests: Send text, JSON, Blob, FormData, and ArrayBuffer data to a URL.
- Error Handling: Throws
HTTPErrorfor HTTP errors andZodErrorfor 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.
Installation
npm install @shrimpcoder/ts-fetcherIntegration with Zod
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 asunknown.
npm install zodimport { Fetcher } from 'ts-fetcher';
import { URLBuilder } from '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); // unknownUsage
For detailed usage, please refer to here.
License
This project is licensed under the MIT License.
0.1.0
2 years ago