Skip to content
zimicjsPublic

About

TypeScript-first HTTP and WebSocket toolkit

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

23 stars

Watchers

1 watching

Forks

Latest commit

 

History

2,703 Commits

Folders and files

Repository files navigation

Zimic

TypeScript-first HTTP and WebSocket toolkit

Docs   •   Examples   •   Issues   •   Roadmap

CI  Coverage  License  Stars

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


Zimic lets you declare an HTTP or WebSocket API once, as a TypeScript schema, and use it to type your requests, messages, and the interceptors in your tests. TypeScript flags code that does not match the schema. Pick the packages your project needs.

Quick start

npm install @zimic/http @zimic/fetch
import { HttpSchema } from '@zimic/http';
import { createFetch } from '@zimic/fetch';

type Schema = HttpSchema<{
  '/users': {
    GET: {
      response: {
        200: { body: { username: string }[] };
      };
    };
  };
}>;

const fetch = createFetch<Schema>({ baseURL: 'http://localhost:3000' });

const response = await fetch('/users', { method: 'GET' });
const users = await response.json(); // { username: string }[]

Intercept the same endpoint in tests with @zimic/interceptor, or type WebSocket clients and servers with @zimic/ws.

Which package do I need?

You want to Install
Type an HTTP API, by hand or from OpenAPI @zimic/http
Make typed HTTP requests @zimic/http and @zimic/fetch
Intercept HTTP requests in tests or development @zimic/http and @zimic/interceptor
Type WebSocket clients and servers (experimental) @zimic/ws
Intercept WebSocket messages (experimental) @zimic/ws and @zimic/interceptor

Highlights

  • Type-safe by design

    Types come from your schema, not from casts or guesses. Mistakes show up in your editor, and autocomplete fills in the rest.

  • HTTP and WebSocket

    Requests and messages follow the same model. Declare a schema, then type clients, WebSocket servers, and interceptors with it.

  • Native APIs

    Where a web API exists, Zimic follows it, so what you know carries over.

  • Always in sync

    Your schema is the single source of truth. Change it once, and TypeScript shows every place to update.

Ecosystem

@zimic/http

Describe your HTTP API as a TypeScript schema, with typed headers, search params, and form data.

Learn more:

@zimic/fetch

A type-safe API client built on the native fetch.

  • Typed requests and responses

    Paths autocomplete, and path params, search params, headers, and bodies are checked against your @zimic/http schema. Each response is typed by its status code, so checking response.status narrows the body.

  • Defaults, listeners, and errors easy to log

    Set a base URL and headers once, and refresh tokens or retry requests in fetch.onRequest and fetch.onResponse. When a request fails, throw response.error. It carries the request and the response, and toObject() gives your logs the URL, method, status, headers, and bodies.

  • The fetch you know

    createFetch returns a fetch you call like the native one. Its requests and responses are native too, so they work with any code that expects the Fetch API.

Learn more:

@zimic/ws

A WebSocket client and server with messages typed by one schema.

  • Typed messages and connections

    A WebSocket schema types every message sent and received, and the paths, search params, and subprotocols of each connection. WebSocketClient and WebSocketServer, which attaches to a Node.js HTTP or HTTPS server, share it so both sides agree on every message.

  • A client you already know

    WebSocketClient follows the native WebSocket API, in browsers and Node.js.

  • Awaitable connections

    No more wrapping open and close events in promises. Await client.open() and client.close(), with optional timeouts, and reconnect the same client with its listeners.

Learn more:

@zimic/interceptor

Intercept the HTTP requests and WebSocket messages of your application and answer them with typed mocks, in tests and development.

  • Typed mocks

    Declare HTTP and WebSocket interceptors with your @zimic/http and @zimic/ws schemas. TypeScript flags a mock with the wrong path, parameters, body, or message. WebSocket support is experimental.

  • Network-level interception

    Interceptors handle the real requests and messages your application sends, so its code stays unchanged. Local interceptors run in the same process as your application, and remote interceptors use an interceptor server to serve other processes, such as in end-to-end tests.

  • Declarative assertions

    Restrict what each handler matches with handler.with(), set how many times with handler.times(), and let interceptor.checkTimes() fail the test when your application sent something else. See the guides for HTTP and WebSocket.

Learn more:

Tip

The packages share schemas, so a @zimic/fetch client and an HTTP interceptor use the types of the same @zimic/http schema, and a @zimic/ws client and a WebSocket interceptor use the same @zimic/ws schema. @zimic/interceptor intercepts at the network level, so it intercepts requests from any HTTP client and, with remote interceptors, any WebSocket client. @zimic/fetch and @zimic/ws work with other interception libraries too. See our @zimic/fetch testing guide and WebSocket interceptor testing guide for more information.

Examples

Runnable projects show Zimic with Next.js (App and Pages Router), Vitest (Node.js, browser mode, JSDOM), Jest (Node.js, JSDOM), Playwright, and OpenAPI typegen. See the examples.

Sponsors

If you like Zimic and want to support the project, consider becoming a sponsor!

Zimic Sponsors

Changelog

The changelog is available on our GitHub Releases page.

Contributing

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

About

TypeScript-first HTTP and WebSocket toolkit

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

23 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages