Typegen
@zimic/http/typegen
exports resources to generate types programmatically. We recommend using the
zimic-http typegen
CLI, but this is still a valid alternative for more advanced
use cases.
Related:
generateTypesFromOpenAPI()
Generate types from an OpenAPI schema.
await generateTypesFromOpenAPI(options);
Arguments:
- options:
OpenAPITypegenOptions
-
input:
string
The path to a local OpenAPI schema file or an URL to fetch it. Version 3 is supported as YAML or JSON.
-
output:
string | undefined
The path to write the generated types to. If not provided, the types will be written to stdout.
-
serviceName:
string
The name of the service to use in the generated types.
-
includeComments:
boolean | undefined
(default:true
)Whether to include comments in the generated types.
-
prune:
boolean | undefined
(default:true
)Whether to remove unused operations and components from the generated types. This is useful for reducing the size of the output file.
-
filters:
string[] | undefined
One or more expressions filtering which endpoints to include. Filters must follow the format
<method> <path>
, where:<method>
: one HTTP method, a list of HTTP methods separated by commas, or*
to match any HTTP method;<path>
: a literal path or a glob.*
matches zero or more characters in a segment (except/
), while**
matches zero or more characters across segments (may include/
). For example,GET /users
matches a single method and path, while* /users
matches any method to the/users
path;GET /users*
matches anyGET
request whose path starts with/users
, andGET /users/**/*
matches anyGET
request to any sub-path of/users
.
Negative filters can be created by prefixing the expression with
!
. For example,!GET /users
will exclude paths matchingGET /users
. -
filterFile:
string | undefined
A path to a file containing filter expressions. One expression is expected per line and the format is the same as used in a
--filter
option. Comments are prefixed with#
. A filter file can be used alongside additional--filter
expressions.
-
import { generateTypesFromOpenAPI } from '@zimic/http/typegen';
await generateTypesFromOpenAPI({
input: './schema.yaml',
output: './schema.ts',
serviceName: 'MyService',
filters: ['* /users**'],
includeComments: true,
prune: true,
});