0.1.2 • Published 4 months ago

@zimic/fetch v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

CI  Coverage  License  Stars

NPM Downloads - @zimic/fetch  Bundle size - @zimic/fetch



@zimic/fetch is a minimal (1 kB minified and gzipped), zero-dependency, and type-safe fetch-like API client.

!WARNING

:construction: This library is experimental.

Features

  • :sparkles: Type-safe fetch: Create a type-safe fetch-like API client. Use your @zimic/http schema and have your requests and responses fully typed by default.
  • :muscle: Developer experience: @zimic/fetch seeks to be as compatible with the native Fetch API as possible, while providing an ergonomic interface to improve type safety. Define default options to apply to your requests, such as a base URL, headers, search parameters, and more. Inspect and modify requests and responses using onRequest and onResponse listeners.

Getting started

Check our getting started guide.

Installation

ManagerCommand
npmnpm install @zimic/http @zimic/fetch --save
yarnyarn add @zimic/http @zimic/fetch
pnpmpnpm add @zimic/http @zimic/fetch

Basic usage

  1. Declare your HTTP schema using @zimic/http:

    import { type HttpSchema } from '@zimic/http';
    
    interface User {
      username: string;
    }
    
    interface RequestError {
      code: string;
      message: string;
    }
    
    type Schema = HttpSchema<{
      '/users': {
        POST: {
          request: { body: User };
          response: {
            201: { body: User };
            400: { body: RequestError };
            409: { body: RequestError };
            500: { body: RequestError };
          };
        };
    
        GET: {
          request: {
            searchParams: {
              query?: string;
              limit?: `${number}`;
            };
          };
          response: {
            200: { body: User[] };
            404: { body: RequestError };
            500: { body: RequestError };
          };
        };
      };
    
      '/users/:userId': {
        PATCH: {
          request: {
            headers: { authorization: string };
            body: Partial<User>;
          };
          response: {
            204: {};
            400: { body: RequestError };
            500: { body: RequestError };
          };
        };
      };
    }>;
  2. Create your fetch client:

    import { createFetch } from '@zimic/fetch';
    
    const fetch = createFetch<Schema>({
      baseURL: 'http://localhost:3000',
    });
  3. Enjoy requests and responses typed by default!

    const response = await fetch('/users', {
      method: 'GET',
      searchParams: { query: 'u', limit: '10' },
    });
    
    if (response.status === 404) {
      return null; // Not found
    }
    
    if (!response.ok) {
      throw response.error;
    }
    
    const users = await response.json();
    return users; // User[]

Documentation

Examples

Visit our examples to see how to use Zimic with popular frameworks, libraries, and use cases.

Changelog

The changelog is available on our GitHub Releases page.

Contributing

Interested in contributing to Zimic? Check out our contributing guide to get started!

0.1.2-canary.0

4 months ago

0.1.2

4 months ago

0.1.0-canary.20

5 months ago

0.1.0-canary.21

5 months ago

0.1.1-canary.1

5 months ago

0.1.0-canary.19

5 months ago

0.1.0

5 months ago

0.1.1

5 months ago

0.1.0-canary.10

5 months ago

0.1.0-canary.11

5 months ago

0.1.0-canary.12

5 months ago

0.1.0-canary.17

5 months ago

0.1.0-canary.18

5 months ago

0.1.0-canary.13

5 months ago

0.1.0-canary.9

5 months ago

0.1.0-canary.14

5 months ago

0.1.0-canary.15

5 months ago

0.1.0-canary.16

5 months ago

0.1.0-canary.7

5 months ago

0.1.0-canary.6

5 months ago

0.1.0-canary.5

5 months ago

0.1.0-canary.4

5 months ago

0.1.0-canary.3

5 months ago

0.1.0-canary.2

5 months ago

0.1.0-canary.1

5 months ago

0.1.0-canary.0

5 months ago