Skip to content

Commit b78600d

Browse files
authored
feat: Add the inject option (openapi-ts#1766)
* feat: Add the inject option * Create little-pets-perform.md
1 parent e69c873 commit b78600d

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default async function openapiTS(
8383
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,
8484
redoc,
8585
silent: options.silent ?? false,
86+
inject: options.inject ?? undefined,
8687
transform: typeof options.transform === "function" ? options.transform : undefined,
8788
resolve($ref) {
8889
return resolveRef(schema, $ref, { silent: options.silent ?? false });

src/transform/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ts, { type InterfaceDeclaration, type TypeLiteralNode } from "typescript";
2-
import { NEVER, STRING, tsModifiers, tsRecord } from "../lib/ts.js";
2+
import { NEVER, STRING, stringToAST, tsModifiers, tsRecord } from "../lib/ts.js";
33
import { createRef, debug } from "../lib/utils.js";
44
import type { GlobalContext, OpenAPI3 } from "../types.js";
55
import transformComponentsObject from "./components-object.js";
@@ -19,6 +19,11 @@ const transformers: Record<SchemaTransforms, (node: any, options: GlobalContext)
1919
export default function transformSchema(schema: OpenAPI3, ctx: GlobalContext) {
2020
const type: ts.Node[] = [];
2121

22+
if (ctx.inject) {
23+
const injectNodes = stringToAST(ctx.inject) as ts.Node[];
24+
type.push(...injectNodes);
25+
}
26+
2227
for (const root of Object.keys(transformers) as SchemaTransforms[]) {
2328
const emptyObj = ts.factory.createTypeAliasDeclaration(
2429
/* modifiers */ tsModifiers({ export: true }),

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ export interface OpenAPITSOptions {
660660
* @see https://redocly.com/docs/cli/configuration/
661661
*/
662662
redocly?: RedoclyConfig;
663+
/** Inject arbitrary TypeScript types into the start of the file */
664+
inject?: string;
663665
}
664666

665667
/** Context passed to all submodules */
@@ -688,6 +690,7 @@ export interface GlobalContext {
688690
transform: OpenAPITSOptions["transform"];
689691
/** retrieve a node by $ref */
690692
resolve<T>($ref: string): T | undefined;
693+
inject?: string;
691694
}
692695

693696
export type $defs = Record<string, SchemaObject>;

test/index.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,32 @@ export type operations = Record<string, never>;`,
668668
// options: DEFAULT_OPTIONS,
669669
},
670670
],
671+
[
672+
"inject option",
673+
{
674+
given: {
675+
openapi: "3.1",
676+
info: { title: "Test", version: "1.0" },
677+
},
678+
want: `type Foo = string;
679+
type Bar = number;
680+
export type paths = Record<string, never>;
681+
export type webhooks = Record<string, never>;
682+
export interface components {
683+
schemas: never;
684+
responses: never;
685+
parameters: never;
686+
requestBodies: never;
687+
headers: never;
688+
pathItems: never;
689+
}
690+
export type $defs = Record<string, never>;
691+
export type operations = Record<string, never>;`,
692+
options: {
693+
inject: "type Foo = string;\ntype Bar = number;",
694+
},
695+
},
696+
],
671697
];
672698

673699
for (const [testName, { given, want, options, ci }] of tests) {

0 commit comments

Comments
 (0)