diff --git a/src/index.ts b/src/index.ts index fdaaee575..5d6714274 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,17 +10,17 @@ import { transformAll } from "./transform/index.js"; import { makeApiPathsEnum } from "./transform/paths.js"; export * from "./types.js"; // expose all types to consumers -export const WARNING_MESSAGE = `/** -* This file was auto-generated by openapi-typescript. -* Do not make direct changes to the file. -*/ +export const COMMENT_HEADER = `/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ `; /** * This function is the entry to the program and allows the user to pass in a remote schema and/or local schema. - * The URL or schema and headers can be passed in either programtically and/or via the CLI. + * The URL or schema and headers can be passed in either programmatically and/or via the CLI. * Remote schemas are fetched from a server that supplies JSON or YAML format via an HTTP GET request. File based schemas * are loaded in via file path, most commonly prefixed with the file:// format. Alternatively, the user can pass in * OpenAPI2 or OpenAPI3 schema objects that can be parsed directly by the function without reading the file system. @@ -38,14 +38,15 @@ async function openapiTS( const ctx: GlobalContext = { additionalProperties: options.additionalProperties || false, auth: options.auth, + commentHeader: typeof options.commentHeader === "string" ? options.commentHeader : COMMENT_HEADER, defaultNonNullable: options.defaultNonNullable || false, formatter: options && typeof options.formatter === "function" ? options.formatter : undefined, immutableTypes: options.immutableTypes || false, - rawSchema: options.rawSchema || false, makePathsEnum: options.makePathsEnum || false, - version: options.version || 3, - supportArrayLength: options.supportArrayLength, pathParamsAsTypes: options.pathParamsAsTypes, + rawSchema: options.rawSchema || false, + supportArrayLength: options.supportArrayLength, + version: options.version || 3, }; // note: we may be loading many large schemas into memory at once; take care to reuse references without cloning @@ -76,7 +77,7 @@ async function openapiTS( } // 2. generate raw output - let output = WARNING_MESSAGE; + let output = ctx.commentHeader; // 2a. root schema if (!options?.version && !ctx.rawSchema) ctx.version = swaggerVersion(rootSchema as any); // note: root version cascades down to all subschemas diff --git a/src/types.ts b/src/types.ts index 52ad60efc..c26912f68 100644 --- a/src/types.ts +++ b/src/types.ts @@ -165,21 +165,26 @@ export interface SwaggerToTSOptions { * (optional) Substitute path parameter names with their respective types. */ pathParamsAsTypes?: boolean; + /** + * (optional) Provide your own comment header that prefixes the generated file. + * Note this isn’t validated, so any string entered will be accepted as-is. + */ + commentHeader?: string; } /** Context passed to all submodules */ export interface GlobalContext { additionalProperties: boolean; auth?: string; + commentHeader: string; + defaultNonNullable: boolean; formatter?: SchemaFormatter; immutableTypes: boolean; - defaultNonNullable: boolean; - /** (optional) Should logging be suppressed? (necessary for STDOUT) */ - silent?: boolean; + makePathsEnum: boolean; namespace?: string; + pathParamsAsTypes?: boolean; rawSchema: boolean; - makePathsEnum: boolean; - version: number; + silent?: boolean; supportArrayLength?: boolean; - pathParamsAsTypes?: boolean; + version: number; }