TypeScript-first HTTP and WebSocket toolkit
Docs • Examples • Issues • Roadmap
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.
npm install @zimic/http @zimic/fetchimport { 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.
| 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 |
-
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.
Describe your HTTP API as a TypeScript schema, with typed headers, search params, and form data.
-
HTTP schemas
Write the paths, methods, parameters, bodies, and responses by status code of an API by hand, in one TypeScript type. Your code,
@zimic/fetch, and@zimic/interceptoruse it to type requests and responses. -
Generated from OpenAPI
Already have an OpenAPI document? Generate the schema from a local file or a URL with
zimic-http typegen. -
Typed headers, search params, and form data
HttpHeaders,HttpSearchParams, andHttpFormDatatype their entries with the schema and work wherever the native classes do.
Learn more:
@zimic/http- Introduction@zimic/http- Getting started@zimic/http- Guides@zimic/http- API@zimic/http- CLI
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/httpschema. Each response is typed by its status code, so checkingresponse.statusnarrows the body. -
Defaults, listeners, and errors easy to log
Set a base URL and headers once, and refresh tokens or retry requests in
fetch.onRequestandfetch.onResponse. When a request fails, throwresponse.error. It carries the request and the response, andtoObject()gives your logs the URL, method, status, headers, and bodies. -
The fetch you know
createFetchreturns afetchyou 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:
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.
WebSocketClientandWebSocketServer, which attaches to a Node.js HTTP or HTTPS server, share it so both sides agree on every message. -
A client you already know
WebSocketClientfollows the nativeWebSocketAPI, in browsers and Node.js. -
Awaitable connections
No more wrapping
openandcloseevents in promises. Awaitclient.open()andclient.close(), with optional timeouts, and reconnect the same client with its listeners.
Learn more:
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/httpand@zimic/wsschemas. 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 withhandler.times(), and letinterceptor.checkTimes()fail the test when your application sent something else. See the guides for HTTP and WebSocket.
Learn more:
@zimic/interceptor- Introduction@zimic/interceptor- Getting started@zimic/interceptor- Guides@zimic/interceptor- API@zimic/interceptor- CLI
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.
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.
If you like Zimic and want to support the project, consider becoming a sponsor!
The changelog is available on our GitHub Releases page.
Interested in contributing to Zimic? Check out our contributing guide to get started!
