Skip to content

Commit 0ce35d7

Browse files
authored
feat: Add the inject option (#1766)
* feat: Add the inject option * Create little-pets-perform.md
1 parent 990a16f commit 0ce35d7

File tree

7 files changed

+44
-1
lines changed

7 files changed

+44
-1
lines changed

.changeset/little-pets-perform.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"openapi-typescript-docs": minor
3+
"openapi-typescript": minor
4+
---
5+
6+
feat: Add the inject option

docs/node.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The Node API supports all the [CLI flags](/cli#options) in `camelCase` format, p
8989
| `postTransform` | `Function` | | Same as `transform` but runs _after_ the TypeScript transformation |
9090
| `silent` | `boolean` | `false` | Silence warning messages (fatal errors will still show) |
9191
| `cwd` | `string \| URL` | `process.cwd()` | (optional) Provide the current working directory to help resolve remote `$ref`s (if needed). |
92+
| `inject` | `string` | | Inject arbitrary TypeScript types into the start of the file |
9293

9394
### transform / postTransform
9495

docs/zh/node.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Node API 支持所有 [CLI 参数](/zh/cli#命令行参数)(采用 `camelCase`
8989
| `postTransform` | `Function` | |`transform` 相同,但在 TypeScript 转换之后运行 |
9090
| `silent` | `boolean` | `false` | 静默警告消息(致命错误仍将显示) |
9191
| `cwd` | `string \| URL` | `process.cwd()` | (可选)提供当前工作目录以帮助解析远程 `$ref`(如果需要的话)。 |
92+
| `inject` | `string` | | 在文件开头注入任意的 TypeScript 类型 |
9293

9394
### transform / postTransform
9495

packages/openapi-typescript/src/index.ts

+1
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 });

packages/openapi-typescript/src/transform/index.ts

+6-1
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 }),

packages/openapi-typescript/src/types.ts

+3
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>;

packages/openapi-typescript/test/index.test.ts

+26
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)