Skip to main content

Typegen

@zimic/interceptor uses @zimic/http, which can infer types from OpenAPI documentation and generate ready-to-use schemas to type your @zimic/interceptor interceptors.

OpenAPI

To generate a schema from a local OpenAPI document, use the @zimic/http CLI:

zimic-http typegen openapi ./schema.json \
--output ./schema.ts \
--service-name MyService

You can also fetch a remote OpenAPI documentation using a URL:

zimic-http typegen openapi https://example.com/api/openapi.json \
--output ./schema.ts \
--service-name MyService

Using a generated schema

The typegen output file will contain your HTTP schema, including the generated types for each endpoint. You can then use this schema in your @zimic/interceptor interceptor.

import { createHttpInterceptor } from '@zimic/interceptor/http';

import { MyServiceSchema } from './schema';

const interceptor = createHttpInterceptor<MyServiceSchema>({
baseURL: 'http://localhost:3000',
});

And that's it! You now have a typed interceptor to mock responses in development and testing. Whenever the OpenAPI documentation changes, you can regenerate the schema to update your types and keep your interceptor up to date.

Our OpenAPI typegen example demonstrates how to use the type generation in practice.