Typegen
@zimic/fetch
uses @zimic/http
, which can infer types from
OpenAPI documentation and generate ready-to-use
schemas to type your @zimic/fetch
clients.
OpenAPI​
To generate a schema from a local OpenAPI document, use the @zimic/http
CLI:
- JSON
- YAML
zimic-http typegen openapi ./schema.json \
--output ./schema.ts \
--service-name MyService
zimic-http typegen openapi ./schema.yaml \
--output ./schema.ts \
--service-name MyService
You can also fetch a remote OpenAPI documentation using a URL:
- JSON
- YAML
zimic-http typegen openapi https://example.com/api/openapi.json \
--output ./schema.ts \
--service-name MyService
zimic-http typegen openapi https://example.com/api/openapi.yaml \
--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/fetch
client.
import { createFetch } from '@zimic/fetch';
import { MyServiceSchema } from './schema';
const fetch = createFetch<MyServiceSchema>({
baseURL: 'http://localhost:3000',
});
And that's it! You now have a typed fetch client to make requests to the API. Whenever the OpenAPI documentation changes, you can regenerate the schema to update your types and keep your client up to date.
Our OpenAPI typegen example demonstrates how to use the type generation in practice.