From b2151ef75b08afa577d7cf2537a7e125b04bec52 Mon Sep 17 00:00:00 2001 From: phk422 Date: Wed, 17 Jul 2024 09:43:20 +0800 Subject: [PATCH 1/2] feat: Add the inject option --- docs/node.md | 1 + docs/zh/node.md | 1 + packages/openapi-typescript/src/index.ts | 1 + .../openapi-typescript/src/transform/index.ts | 7 ++++- packages/openapi-typescript/src/types.ts | 3 +++ .../openapi-typescript/test/index.test.ts | 26 +++++++++++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/node.md b/docs/node.md index e3f00b52c..d99ebbd67 100644 --- a/docs/node.md +++ b/docs/node.md @@ -89,6 +89,7 @@ The Node API supports all the [CLI flags](/cli#options) in `camelCase` format, p | `postTransform` | `Function` | | Same as `transform` but runs _after_ the TypeScript transformation | | `silent` | `boolean` | `false` | Silence warning messages (fatal errors will still show) | | `cwd` | `string \| URL` | `process.cwd()` | (optional) Provide the current working directory to help resolve remote `$ref`s (if needed). | +| `inject` | `string` | | Inject arbitrary TypeScript types into the start of the file | ### transform / postTransform diff --git a/docs/zh/node.md b/docs/zh/node.md index b90b50968..f5c6da583 100644 --- a/docs/zh/node.md +++ b/docs/zh/node.md @@ -89,6 +89,7 @@ Node API 支持所有 [CLI 参数](/zh/cli#命令行参数)(采用 `camelCase` | `postTransform` | `Function` | | 与 `transform` 相同,但在 TypeScript 转换之后运行 | | `silent` | `boolean` | `false` | 静默警告消息(致命错误仍将显示) | | `cwd` | `string \| URL` | `process.cwd()` | (可选)提供当前工作目录以帮助解析远程 `$ref`(如果需要的话)。 | +| `inject` | `string` | | 在文件开头注入任意的 TypeScript 类型 | ### transform / postTransform diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index c69cb228c..89c4e6276 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -83,6 +83,7 @@ export default async function openapiTS( propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false, redoc, silent: options.silent ?? false, + inject: options.inject ?? undefined, transform: typeof options.transform === "function" ? options.transform : undefined, resolve($ref) { return resolveRef(schema, $ref, { silent: options.silent ?? false }); diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 89ba043d4..b7b1ee891 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -1,5 +1,5 @@ import ts, { type InterfaceDeclaration, type TypeLiteralNode } from "typescript"; -import { NEVER, STRING, tsModifiers, tsRecord } from "../lib/ts.js"; +import { NEVER, STRING, stringToAST, tsModifiers, tsRecord } from "../lib/ts.js"; import { createRef, debug } from "../lib/utils.js"; import type { GlobalContext, OpenAPI3 } from "../types.js"; import transformComponentsObject from "./components-object.js"; @@ -19,6 +19,11 @@ const transformers: Record($ref: string): T | undefined; + inject?: string; } export type $defs = Record; diff --git a/packages/openapi-typescript/test/index.test.ts b/packages/openapi-typescript/test/index.test.ts index 1de917433..45877cace 100644 --- a/packages/openapi-typescript/test/index.test.ts +++ b/packages/openapi-typescript/test/index.test.ts @@ -668,6 +668,32 @@ export type operations = Record;`, // options: DEFAULT_OPTIONS, }, ], + [ + "inject option", + { + given: { + openapi: "3.1", + info: { title: "Test", version: "1.0" }, + }, + want: `type Foo = string; +type Bar = number; +export type paths = Record; +export type webhooks = Record; +export interface components { + schemas: never; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} +export type $defs = Record; +export type operations = Record;`, + options: { + inject: "type Foo = string;\ntype Bar = number;", + }, + }, + ], ]; for (const [testName, { given, want, options, ci }] of tests) { From 4ec9175319b9fbc4db2f87ca63271ef763d8d6b8 Mon Sep 17 00:00:00 2001 From: Hongkun Peng Date: Wed, 17 Jul 2024 09:45:11 +0800 Subject: [PATCH 2/2] Create little-pets-perform.md --- .changeset/little-pets-perform.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/little-pets-perform.md diff --git a/.changeset/little-pets-perform.md b/.changeset/little-pets-perform.md new file mode 100644 index 000000000..332bfe71b --- /dev/null +++ b/.changeset/little-pets-perform.md @@ -0,0 +1,6 @@ +--- +"openapi-typescript-docs": minor +"openapi-typescript": minor +--- + +feat: Add the inject option