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:
stringThe path to a local OpenAPI schema file or an URL to fetch it. Version 3 is supported as YAML or JSON.
-
output:
string | undefinedThe path to write the generated types to. If not provided, the types will be written to stdout.
-
serviceName:
stringThe 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[] | undefinedOne 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 /usersmatches a single method and path, while* /usersmatches any method to the/userspath;GET /users*matches anyGETrequest whose path starts with/users, andGET /users/**/*matches anyGETrequest to any sub-path of/users.
Negative filters can be created by prefixing the expression with
!. For example,!GET /userswill exclude paths matchingGET /users. -
filterFile:
string | undefinedA path to a file containing filter expressions. One expression is expected per line and the format is the same as used in a
--filteroption. Comments are prefixed with#. A filter file can be used alongside additional--filterexpressions.
-
import { generateTypesFromOpenAPI } from '@zimic/http/typegen';
await generateTypesFromOpenAPI({
input: './schema.yaml',
output: './schema.ts',
serviceName: 'MyService',
filters: ['* /users**'],
includeComments: true,
prune: true,
});