import type { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema'; import type { OpenAPIObject as OpenAPIObject_v3_0, ParameterObject as ParameterObject_v3_0, PathItemObject as PathItemObject_v3_0, ReferenceObject as ReferenceObject_v3_0, SchemaObject as SchemaObject_v3_0 } from 'openapi3-ts/oas30'; import type { OpenAPIObject as OpenAPIObject_v3_1, ParameterObject as ParameterObject_v3_1, PathItemObject as PathItemObject_v3_1, ReferenceObject as ReferenceObject_v3_1, SchemaObject as SchemaObject_v3_1 } from 'openapi3-ts/oas31'; import type { Merge, OmitIndexSignature, SetRequired } from 'type-fest'; import type { formatTypeScript, makeRelativeImportPath, saveFile } from './utils/index.js'; export type OpenApiDocument = (OpenAPIObject_v3_0 | OpenAPIObject_v3_1) & { definitions?: Record>; }; type OpenApiObject_v3_0 = PathItemObject_v3_0 | SchemaObject_v3_0 | ParameterObject_v3_0 | ReferenceObject_v3_0; type OpenApiObject_v3_1 = PathItemObject_v3_1 | SchemaObject_v3_1 | ParameterObject_v3_1 | ReferenceObject_v3_1; export type OpenApiObject = OpenApiObject_v3_0 | OpenApiObject_v3_1; type OpenApiParameter_v3_0 = ParameterObject_v3_0 | ReferenceObject_v3_0; type OpenApiParameter_v3_1 = ParameterObject_v3_1 | ReferenceObject_v3_1; export type OpenApiParameter = OpenApiParameter_v3_0 | OpenApiParameter_v3_1; export type JSONSchema = OmitIndexSignature; export type SchemaPatcher = (params: { schema: JSONSchema; }) => void; export type RefHandling = 'import' | 'inline' | 'keep'; export type ImportExtension = 'js' | 'ts' | 'none'; export type IdMapper = (input: { id: string; }) => string; export type Options = { /** * Path to an OpenAPI document (JSON or YAML). */ openApiDocument: string; /** * OpenAPI definition paths to generate JSON Schemas from. */ targets: { /** * Paths pointing to objects/records of definitions; each entry will be generated. * * @example ["components.schemas"] */ collections?: string[]; /** * Paths pointing to individual definitions to generate. * * @example ["paths./users/{id}"] */ single?: string[]; }; /** * File extension appended to relative import specifiers in generated artifacts. * Match this to your project's TypeScript `moduleResolution` setting. * * @default "js" */ importExtension?: ImportExtension; /** * Hook called for every generated schema node, allowing programmatic mutation before output. */ schemaPatcher?: SchemaPatcher; /** * Directory where generated schemas will be written. * * @default "/schemas-autogenerated" */ outputPath?: string; /** * List of plugins for custom generation behavior (e.g. Fastify integration). */ plugins?: ReturnType[]; /** * If `true`, suppress logging output. * * @default false */ silent?: boolean; /** * Strategy for `$ref` processing. * * - `"import"` — generate cross-file imports for referenced schemas * - `"inline"` — inline referenced schemas at the usage site * - `"keep"` — preserve `$ref` strings as-is * * @default "import" */ refHandling?: RefHandling; /** * Function mapping internal schema id strings to custom `$id` or import names. * * @default ({ id }) => id */ idMapper?: IdMapper; }; export type OptionsWithDefaults = Merge, { targets: Required; }>; /** * Meta data for representing a specific openApi definition. * * @property `id` - Internal canonical identifier; used as the `SchemaMetaDataMap` key and to derive file paths. Eg `"/components/schemas/MySchema"` * @property `$id` - Value emitted as the schema's JSON Schema `$id`. Computed as `idMapper({ id })`; equals `id` unless a custom `idMapper` is provided. * @property `isRef` - True if schemas is used as `$ref` * @property `shouldBeGenerated` - True is the schema has to be generated * @property `uniqueName` - JavaScript-safe identifier derived from `id`, used as the import/export name in generated TypeScript. Eg: `"componentsSchemasMySchema"` * @property `openApiDefinition` - Original dereferenced openAPI definition * @property `originalSchema` - Original dereferenced JSON schema * @property `fileContent` - Text content of schema file * * @property `absoluteDirName` - Absolute path pointing to schema folder (posix or win32). Eg: `"Users/username/output/path/components/schemas"` * @property `absolutePath` - Absolute path pointing to schema file (posix or win32). Eg: `"Users/username/output/path/components/schemas/MySchema.ts"` * @property `absoluteImportPath` - Absolute import path (posix or win32, without extension). Eg: `"Users/username/output/path/components/schemas/MySchema"` */ export type SchemaMetaData = { id: string; $id: string; isRef: boolean; shouldBeGenerated: boolean; uniqueName: string; openApiDefinition?: OpenApiObject; originalSchema: JSONSchema; fileContent?: string; absoluteDirName: string; absolutePath: string; absoluteImportPath: string; }; export type SchemaMetaDataMap = Map; export type ReturnPayload = { outputPath: string; metaData: { schemas: SchemaMetaDataMap; }; }; type OnInitInput = { options: OptionsWithDefaults; }; type OnBeforeGenerationInput = ReturnPayload & { options: OptionsWithDefaults; utils: { makeRelativeImportPath: typeof makeRelativeImportPath; formatTypeScript: typeof formatTypeScript; saveFile: typeof saveFile; }; }; type OnBeforeFileSave = ReturnPayload & { options: OptionsWithDefaults; utils: { makeRelativeImportPath: typeof makeRelativeImportPath; formatTypeScript: typeof formatTypeScript; saveFile: typeof saveFile; }; }; export type Plugin = (options: PluginOptions) => { name?: string; onInit?: (input: OnInitInput) => Promise; onBeforeGeneration?: (input: OnBeforeGenerationInput) => Promise; onBeforeSaveFile?: (input: OnBeforeFileSave) => Promise; }; export {};