From 168ab002a27c5bc14884eeec58b88f55a51f6dad Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 15:53:12 +0200 Subject: [PATCH 01/23] wip on a new transformer --- .../src/transform/components-types.ts | 36 +++++++++++++++++++ .../openapi-typescript/src/transform/index.ts | 7 ++++ 2 files changed, 43 insertions(+) create mode 100644 packages/openapi-typescript/src/transform/components-types.ts diff --git a/packages/openapi-typescript/src/transform/components-types.ts b/packages/openapi-typescript/src/transform/components-types.ts new file mode 100644 index 000000000..8e263fb6b --- /dev/null +++ b/packages/openapi-typescript/src/transform/components-types.ts @@ -0,0 +1,36 @@ +import type { ComponentsObject, GlobalContext } from "../types.js"; +import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js"; +import transformResponseObject from "./response-object.js"; +import transformSchemaObject from "./schema-object.js"; + +export default function transformComponentsObjectToTypes(components: ComponentsObject, ctx: GlobalContext): string { + let { indentLv } = ctx; + const output: string[] = []; + indentLv++; + + // schemas + if (components.schemas) { + output.push(indent("schemas: {", indentLv)); + indentLv++; + for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) { + const c = getSchemaObjectComment(schemaObject, indentLv); + if (c) output.push(indent(c, indentLv)); + let key = escObjKey(name); + if (ctx.immutableTypes || schemaObject.readOnly) key = tsReadonly(key); + const schemaType = transformSchemaObject(schemaObject, { + path: `#/components/schemas/${name}`, + ctx: { ...ctx, indentLv: indentLv }, + }); + + output.push(indent(`${key}: ${schemaType};`, indentLv)); + } + indentLv--; + output.push(indent("};", indentLv)); + } else { + output.push(indent("schemas: never;", indentLv)); + } + + indentLv--; + output.push(indent("}", indentLv)); + return output.join("\n"); +} diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index c42079f24..2b542e6d0 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -1,5 +1,6 @@ import type { GlobalContext, OpenAPI3 } from "../types.js"; import transformComponentsObject from "./components-object.js"; +import transformComponentsObjectToTypes from "./components-types.js"; import transformPathsObject from "./paths-object.js"; import transformSchemaObjectMap from "./schema-object-map.js"; import transformWebhooksObject from "./webhooks-object.js"; @@ -26,5 +27,11 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record Date: Fri, 28 Jul 2023 16:03:49 +0200 Subject: [PATCH 02/23] it's working whoooo --- packages/openapi-typescript/src/index.ts | 8 +++++--- .../src/transform/components-types.ts | 12 ++---------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index 4ce63225f..a42998119 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -12,7 +12,7 @@ import transformResponseObject from "./transform/response-object.js"; import transformSchemaObject from "./transform/schema-object.js"; import transformSchemaObjectMap from "./transform/schema-object-map.js"; import { error, escObjKey, getDefaultFetch, getEntries, getSchemaObjectComment, indent } from "./utils.js"; - +import transformComponentsObjectToTypes from "./transform/components-types.js"; export * from "./types.js"; // expose all types to consumers const EMPTY_OBJECT_RE = /^\s*\{?\s*\}?\s*$/; @@ -118,6 +118,8 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.inject) output.push(options.inject); // 2c. root schema + const types = transformComponentsObjectToTypes((allSchemas["."].schema as OpenAPI3).components!, ctx); + const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); for (const k of Object.keys(rootTypes)) { if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { @@ -238,7 +240,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op "type Without = { [P in Exclude]?: never };", "type XOR = (T | U) extends object ? (Without & U) | (Without & T) : T | U;", "type OneOf = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR, ...Rest]> : never;", - "", + "" ); } @@ -247,7 +249,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op output.splice(1, 0, "/** WithRequired type helpers */", "type WithRequired = T & { [P in K]-?: T[P] };", ""); } - return output.join("\n"); + return output.join("\n").concat(types); } export default openapiTS; diff --git a/packages/openapi-typescript/src/transform/components-types.ts b/packages/openapi-typescript/src/transform/components-types.ts index 8e263fb6b..839373d81 100644 --- a/packages/openapi-typescript/src/transform/components-types.ts +++ b/packages/openapi-typescript/src/transform/components-types.ts @@ -1,17 +1,13 @@ import type { ComponentsObject, GlobalContext } from "../types.js"; import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js"; -import transformResponseObject from "./response-object.js"; import transformSchemaObject from "./schema-object.js"; export default function transformComponentsObjectToTypes(components: ComponentsObject, ctx: GlobalContext): string { - let { indentLv } = ctx; + const { indentLv } = ctx; const output: string[] = []; - indentLv++; // schemas if (components.schemas) { - output.push(indent("schemas: {", indentLv)); - indentLv++; for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) { const c = getSchemaObjectComment(schemaObject, indentLv); if (c) output.push(indent(c, indentLv)); @@ -22,15 +18,11 @@ export default function transformComponentsObjectToTypes(components: ComponentsO ctx: { ...ctx, indentLv: indentLv }, }); - output.push(indent(`${key}: ${schemaType};`, indentLv)); + output.push(indent(`export type ${key} = ${schemaType};\n`, indentLv)); } - indentLv--; - output.push(indent("};", indentLv)); } else { output.push(indent("schemas: never;", indentLv)); } - indentLv--; - output.push(indent("}", indentLv)); return output.join("\n"); } From 1a361b1f75ed3b429990c982388627574362ffa8 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 16:04:49 +0200 Subject: [PATCH 03/23] rm useless import --- packages/openapi-typescript/src/transform/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 2b542e6d0..29888b48a 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -1,6 +1,5 @@ import type { GlobalContext, OpenAPI3 } from "../types.js"; import transformComponentsObject from "./components-object.js"; -import transformComponentsObjectToTypes from "./components-types.js"; import transformPathsObject from "./paths-object.js"; import transformSchemaObjectMap from "./schema-object-map.js"; import transformWebhooksObject from "./webhooks-object.js"; From bc38ba8d30a7e9e85904adf8add14bf3ce0d0faa Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 16:12:11 +0200 Subject: [PATCH 04/23] with flags --- packages/openapi-typescript/bin/cli.js | 16 +++------------- packages/openapi-typescript/src/index.ts | 4 ++-- packages/openapi-typescript/src/types.ts | 2 ++ 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/openapi-typescript/bin/cli.js b/packages/openapi-typescript/bin/cli.js index 0e568e46e..96f67f574 100755 --- a/packages/openapi-typescript/bin/cli.js +++ b/packages/openapi-typescript/bin/cli.js @@ -28,6 +28,7 @@ Options --path-params-as-types (optional) Substitute path parameter names with their respective types --alphabetize (optional) Sort types alphabetically --exclude-deprecated (optional) Exclude deprecated fields from types + --root-types (optional) Export schemas types at root level `; const OUTPUT_FILE = "FILE"; @@ -44,19 +45,7 @@ if (args.includes("-it")) errorAndExit(`The -it alias has been deprecated. Use " const flags = parser(args, { array: ["header"], - boolean: [ - "help", - "version", - "defaultNonNullable", - "emptyObjectsUnknown", - "immutableTypes", - "contentNever", - "exportType", - "supportArrayLength", - "pathParamsAsTypes", - "alphabetize", - "excludeDeprecated", - ], + boolean: ["help", "version", "defaultNonNullable", "emptyObjectsUnknown", "immutableTypes", "contentNever", "exportType", "supportArrayLength", "pathParamsAsTypes", "alphabetize", "excludeDeprecated", "rootTypes"], string: ["auth", "header", "headersObject", "httpMethod"], alias: { header: ["x"], @@ -107,6 +96,7 @@ async function generateSchema(pathToSpec) { pathParamsAsTypes: flags.pathParamsAsTypes, alphabetize: flags.alphabetize, excludeDeprecated: flags.excludeDeprecated, + rootTypes: flags.rootTypes, }); // output diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index a42998119..a42eb34d3 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -118,7 +118,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.inject) output.push(options.inject); // 2c. root schema - const types = transformComponentsObjectToTypes((allSchemas["."].schema as OpenAPI3).components!, ctx); + const schemasExportedTypes = options.rootTypes ? transformComponentsObjectToTypes((allSchemas["."].schema as OpenAPI3).components!, ctx) : ""; const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); for (const k of Object.keys(rootTypes)) { @@ -249,7 +249,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op output.splice(1, 0, "/** WithRequired type helpers */", "type WithRequired = T & { [P in K]-?: T[P] };", ""); } - return output.join("\n").concat(types); + return output.join("\n").concat(schemasExportedTypes); } export default openapiTS; diff --git a/packages/openapi-typescript/src/types.ts b/packages/openapi-typescript/src/types.ts index 87ce34f6c..ef24852f7 100644 --- a/packages/openapi-typescript/src/types.ts +++ b/packages/openapi-typescript/src/types.ts @@ -648,6 +648,8 @@ export interface OpenAPITSOptions { httpMethod?: string; /** (optional) Export type instead of interface */ exportType?: boolean; + /** (optional) Generate schemas types at root level */ + rootTypes?: boolean; /** (optional) Generate tuples using array minItems / maxItems */ supportArrayLength?: boolean; /** (optional) Substitute path parameter names with their respective types */ From 0047e8e71492c9d60af0d462c1273ada91a4f4dc Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 16:33:36 +0200 Subject: [PATCH 05/23] update docs --- docs/src/content/docs/cli.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/content/docs/cli.md b/docs/src/content/docs/cli.md index 37448ff07..7ddca1a92 100644 --- a/docs/src/content/docs/cli.md +++ b/docs/src/content/docs/cli.md @@ -55,6 +55,7 @@ _Thanks, [@psmyrdek](https://github.com/psmyrdek)!_ | `--support-array-length` | | `false` | Generate tuples using array `minItems` / `maxItems` | | `--alphabetize` | | `false` | Sort types alphabetically | | `--exclude-deprecated` | | `false` | Exclude deprecated fields from types | +| `--root-types` | | `false` | Export schemas types at root level | ### `--path-params-as-types` From b97eedf45fc768080f9463e2177732d4023b4ff6 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 22:40:58 +0200 Subject: [PATCH 06/23] wip --- packages/openapi-typescript/package.json | 1 + packages/openapi-typescript/src/transform/components-types.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/openapi-typescript/package.json b/packages/openapi-typescript/package.json index ef2263e9c..cd2850c3c 100644 --- a/packages/openapi-typescript/package.json +++ b/packages/openapi-typescript/package.json @@ -60,6 +60,7 @@ }, "dependencies": { "ansi-colors": "^4.1.3", + "camelcase": "^7.0.1", "fast-glob": "^3.3.1", "js-yaml": "^4.1.0", "supports-color": "^9.4.0", diff --git a/packages/openapi-typescript/src/transform/components-types.ts b/packages/openapi-typescript/src/transform/components-types.ts index 839373d81..49aa6a455 100644 --- a/packages/openapi-typescript/src/transform/components-types.ts +++ b/packages/openapi-typescript/src/transform/components-types.ts @@ -1,3 +1,4 @@ +import camelCase from "camelcase"; import type { ComponentsObject, GlobalContext } from "../types.js"; import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js"; import transformSchemaObject from "./schema-object.js"; @@ -18,7 +19,7 @@ export default function transformComponentsObjectToTypes(components: ComponentsO ctx: { ...ctx, indentLv: indentLv }, }); - output.push(indent(`export type ${key} = ${schemaType};\n`, indentLv)); + output.push(indent(`export type ${camelCase(key, { pascalCase: true })} = ${schemaType};\n`, indentLv)); } } else { output.push(indent("schemas: never;", indentLv)); From 0fb7444f565db78bb78231af07eb948e0ffb2d97 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 22:43:35 +0200 Subject: [PATCH 07/23] final version --- packages/openapi-typescript/src/transform/components-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-typescript/src/transform/components-types.ts b/packages/openapi-typescript/src/transform/components-types.ts index 49aa6a455..682d9a575 100644 --- a/packages/openapi-typescript/src/transform/components-types.ts +++ b/packages/openapi-typescript/src/transform/components-types.ts @@ -19,7 +19,7 @@ export default function transformComponentsObjectToTypes(components: ComponentsO ctx: { ...ctx, indentLv: indentLv }, }); - output.push(indent(`export type ${camelCase(key, { pascalCase: true })} = ${schemaType};\n`, indentLv)); + output.push(indent(`export type ${camelCase(name, { pascalCase: true })} = ${schemaType};\n`, indentLv)); } } else { output.push(indent("schemas: never;", indentLv)); From 6f9d7b978b7147941e1e2194f9245c49c0e377af Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 28 Jul 2023 23:19:21 +0200 Subject: [PATCH 08/23] update lockfile --- .../examples/nextjs/lib/api/v1.d.ts | 68 ++++++++ .../examples/react-query/src/lib/api/v1.d.ts | 68 ++++++++ .../examples/sveltekit/src/lib/api/v1.d.ts | 68 ++++++++ packages/openapi-fetch/test/v1.d.ts | 149 ++++++++++++++++++ .../openapi-typescript/src/transform/index.ts | 2 +- pnpm-lock.yaml | 8 + 6 files changed, 362 insertions(+), 1 deletion(-) diff --git a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts index 55ffdce79..12f10750a 100644 --- a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts @@ -100,6 +100,74 @@ export interface components { export type $defs = Record; +export interface types { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts index 55ffdce79..12f10750a 100644 --- a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts @@ -100,6 +100,74 @@ export interface components { export type $defs = Record; +export interface types { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts index 55ffdce79..12f10750a 100644 --- a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts @@ -100,6 +100,74 @@ export interface components { export type $defs = Record; +export interface types { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/test/v1.d.ts b/packages/openapi-fetch/test/v1.d.ts index 0f71500b6..442c36c39 100644 --- a/packages/openapi-fetch/test/v1.d.ts +++ b/packages/openapi-fetch/test/v1.d.ts @@ -424,6 +424,155 @@ export interface components { export type $defs = Record; +export interface types { + schemas: { + Post: { + title: string; + body: string; + publish_date?: number; + }; + StringArray: string[]; + User: { + email: string; + age?: number; + avatar?: string; + }; + }; + responses: { + AllPostsGet: { + content: { + "application/json": components["schemas"]["Post"][]; + }; + }; + CreatePost: { + content: { + "application/json": { + status: string; + }; + }; + }; + CreateTag: { + content: { + "application/json": { + status: string; + }; + }; + }; + CreateReply: { + content: { + "application/json;charset=utf-8": { + message: string; + }; + }; + }; + Contact: { + content: { + "text/html": string; + }; + }; + Error: { + content: { + "application/json": { + code: number; + message: string; + }; + }; + }; + PatchPost: { + content: { + "application/json": { + status: string; + }; + }; + }; + PostDelete: { + content: { + "application/json": { + status: string; + }; + }; + }; + PostGet: { + content: { + "application/json": components["schemas"]["Post"]; + }; + }; + StringArray: { + content: { + "application/json": components["schemas"]["StringArray"]; + }; + }; + Tag: { + content: { + "application/json": string; + }; + }; + User: { + content: { + "application/json": components["schemas"]["User"]; + }; + }; + }; + parameters: never; + requestBodies: { + CreatePost: { + content: { + "application/json": { + title: string; + body: string; + publish_date: number; + }; + }; + }; + CreatePostOptional?: { + content: { + "application/json": { + title: string; + body: string; + publish_date: number; + }; + }; + }; + CreateTag: { + content: { + "application/json": { + description?: string; + }; + }; + }; + CreateReply: { + content: { + "application/json;charset=utf-8": { + message: string; + replied_at: number; + }; + }; + }; + Contact: { + content: { + "multipart/form-data": { + name: string; + email: string; + subject: string; + message: string; + }; + }; + }; + PatchPost: { + content: { + "application/json": { + properties?: null; + title?: string; + body?: string; + publish_date?: number; + }; + }; + }; + }; + headers: never; + pathItems: never; +} + export type external = Record; export interface operations { diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 29888b48a..18dccd262 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -27,7 +27,7 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record=10'} + /camelcase@7.0.1: + resolution: {integrity: sha512-xlx1yCK2Oc1APsPXDL2LdlNP6+uu8OCDdhOBSVT279M/S+y75O30C2VuD8T2ogdePBBl7PfPF4504tnLgX3zfw==} + engines: {node: '>=14.16'} + dev: false + /caniuse-lite@1.0.30001528: resolution: {integrity: sha512-0Db4yyjR9QMNlsxh+kKWzQtkyflkG/snYheSzkjmvdEtEXB1+jt7A2HmSEiO6XIJPIbo92lHNGNySvE5pZcs5Q==} dev: false From 2bd776021e7c0fd03edcccafa5bb9adc463b426c Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Mon, 31 Jul 2023 10:38:40 +0200 Subject: [PATCH 09/23] add jean smaug as contributor --- packages/openapi-typescript/package.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/openapi-typescript/package.json b/packages/openapi-typescript/package.json index cd2850c3c..556b753fb 100644 --- a/packages/openapi-typescript/package.json +++ b/packages/openapi-typescript/package.json @@ -6,6 +6,12 @@ "name": "Drew Powers", "email": "drew@pow.rs" }, + "contributors": [ + { + "name": "Jean Smaug", + "url": "https://maximeblanc.fr" + } + ], "license": "MIT", "bin": { "openapi-typescript": "bin/cli.js" From 315313902c99862eeb6a45269c4d62b9dc9eae33 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 25 Aug 2023 14:44:25 +0200 Subject: [PATCH 10/23] wip --- packages/openapi-typescript/src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index a42eb34d3..9546f91db 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -122,11 +122,22 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); for (const k of Object.keys(rootTypes)) { + console.log("🚀 ~ file: index.ts:109 ~ k:", k); if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); } else { output.push(`export type ${k} = Record;`, ""); } + + if (options.rootTypes) { + if (k === "components") { + console.log(typeof rootTypes[k]); + // for (const element of rootTypes[k]) { + // console.log("🚀 ~ file: index.ts:119 ~ element:", element); + // } + } + } + delete rootTypes[k]; delete allSchemas["."]; // garbage collect, but also remove from next step (external) } From f06f25a75bf528aad82bca392da41eb0c5082654 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Fri, 25 Aug 2023 17:35:42 +0200 Subject: [PATCH 11/23] en vrai why not --- packages/openapi-typescript/src/index.ts | 46 +++++++++++-------- .../openapi-typescript/src/transform/index.ts | 2 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index 9546f91db..f123e783b 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -118,30 +118,39 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.inject) output.push(options.inject); // 2c. root schema - const schemasExportedTypes = options.rootTypes ? transformComponentsObjectToTypes((allSchemas["."].schema as OpenAPI3).components!, ctx) : ""; + const typedComponents = (allSchemas["."].schema as OpenAPI3).components!; + + const schemasExportedTypes = options.rootTypes ? transformComponentsObjectToTypes(typedComponents, ctx) : ""; const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); - for (const k of Object.keys(rootTypes)) { - console.log("🚀 ~ file: index.ts:109 ~ k:", k); - if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { - output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); - } else { - output.push(`export type ${k} = Record;`, ""); - } - if (options.rootTypes) { - if (k === "components") { - console.log(typeof rootTypes[k]); - // for (const element of rootTypes[k]) { - // console.log("🚀 ~ file: index.ts:119 ~ element:", element); - // } - } + if (options.rootTypes) { + for (const schema of Object.keys(typedComponents.schemas as object)) { + output.push(`export type ${schema} = components["schema"]["${schema}"];`); } - - delete rootTypes[k]; - delete allSchemas["."]; // garbage collect, but also remove from next step (external) } + // for (const k of Object.keys(rootTypes)) { + + // if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { + // output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); + // } else { + // output.push(`export type ${k} = Record;`, ""); + // } + + // if (options.rootTypes) { + // if (k === "components") { + // console.log(typeof rootTypes[k]); + // // for (const element of rootTypes[k]) { + // // console.log("🚀 ~ file: index.ts:119 ~ element:", element); + // // } + // } + // } + + // delete rootTypes[k]; + // delete allSchemas["."]; // garbage collect, but also remove from next step (external) + // } + // 2d. external schemas (subschemas) const externalKeys = Object.keys(allSchemas); // root schema (".") should already be removed if (externalKeys.length) { @@ -261,6 +270,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op } return output.join("\n").concat(schemasExportedTypes); + // return output.join("\n"); } export default openapiTS; diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 18dccd262..2e96f3b1f 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -30,7 +30,7 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record Date: Fri, 25 Aug 2023 17:38:22 +0200 Subject: [PATCH 12/23] add line break --- packages/openapi-typescript/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index f123e783b..543cd4f95 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -126,7 +126,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.rootTypes) { for (const schema of Object.keys(typedComponents.schemas as object)) { - output.push(`export type ${schema} = components["schema"]["${schema}"];`); + output.push(`export type ${schema} = components["schema"]["${schema}"];\n`); } } From ce87a0ae1ba8d3fd2ccb328cf91979bc3bb2be66 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 16:40:14 +0200 Subject: [PATCH 13/23] remove useless stuff --- packages/openapi-typescript/src/index.ts | 10 ++----- .../src/transform/components-types.ts | 29 ------------------- 2 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 packages/openapi-typescript/src/transform/components-types.ts diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index 543cd4f95..c3eb84365 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -12,7 +12,6 @@ import transformResponseObject from "./transform/response-object.js"; import transformSchemaObject from "./transform/schema-object.js"; import transformSchemaObjectMap from "./transform/schema-object-map.js"; import { error, escObjKey, getDefaultFetch, getEntries, getSchemaObjectComment, indent } from "./utils.js"; -import transformComponentsObjectToTypes from "./transform/components-types.js"; export * from "./types.js"; // expose all types to consumers const EMPTY_OBJECT_RE = /^\s*\{?\s*\}?\s*$/; @@ -120,10 +119,6 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op // 2c. root schema const typedComponents = (allSchemas["."].schema as OpenAPI3).components!; - const schemasExportedTypes = options.rootTypes ? transformComponentsObjectToTypes(typedComponents, ctx) : ""; - - const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); - if (options.rootTypes) { for (const schema of Object.keys(typedComponents.schemas as object)) { output.push(`export type ${schema} = components["schema"]["${schema}"];\n`); @@ -260,7 +255,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op "type Without = { [P in Exclude]?: never };", "type XOR = (T | U) extends object ? (Without & U) | (Without & T) : T | U;", "type OneOf = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR, ...Rest]> : never;", - "" + "", ); } @@ -269,8 +264,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op output.splice(1, 0, "/** WithRequired type helpers */", "type WithRequired = T & { [P in K]-?: T[P] };", ""); } - return output.join("\n").concat(schemasExportedTypes); - // return output.join("\n"); + return output.join("\n"); } export default openapiTS; diff --git a/packages/openapi-typescript/src/transform/components-types.ts b/packages/openapi-typescript/src/transform/components-types.ts deleted file mode 100644 index 682d9a575..000000000 --- a/packages/openapi-typescript/src/transform/components-types.ts +++ /dev/null @@ -1,29 +0,0 @@ -import camelCase from "camelcase"; -import type { ComponentsObject, GlobalContext } from "../types.js"; -import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js"; -import transformSchemaObject from "./schema-object.js"; - -export default function transformComponentsObjectToTypes(components: ComponentsObject, ctx: GlobalContext): string { - const { indentLv } = ctx; - const output: string[] = []; - - // schemas - if (components.schemas) { - for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) { - const c = getSchemaObjectComment(schemaObject, indentLv); - if (c) output.push(indent(c, indentLv)); - let key = escObjKey(name); - if (ctx.immutableTypes || schemaObject.readOnly) key = tsReadonly(key); - const schemaType = transformSchemaObject(schemaObject, { - path: `#/components/schemas/${name}`, - ctx: { ...ctx, indentLv: indentLv }, - }); - - output.push(indent(`export type ${camelCase(name, { pascalCase: true })} = ${schemaType};\n`, indentLv)); - } - } else { - output.push(indent("schemas: never;", indentLv)); - } - - return output.join("\n"); -} From b093e13cffaf01528560f843e58c55e9d69fa7f0 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 16:47:26 +0200 Subject: [PATCH 14/23] wip --- packages/openapi-typescript/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index c3eb84365..824567775 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -121,7 +121,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.rootTypes) { for (const schema of Object.keys(typedComponents.schemas as object)) { - output.push(`export type ${schema} = components["schema"]["${schema}"];\n`); + output.push(`export type ${schema} = external["."]["components"]["schemas"]["${schema}"];\n`); } } From e418b780b20303341803e5125acda195cdad4adc Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 16:51:24 +0200 Subject: [PATCH 15/23] clean --- packages/openapi-typescript/src/index.ts | 21 ------------------- .../openapi-typescript/src/transform/index.ts | 11 ---------- 2 files changed, 32 deletions(-) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index 824567775..29352ab1e 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -125,27 +125,6 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op } } - // for (const k of Object.keys(rootTypes)) { - - // if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { - // output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); - // } else { - // output.push(`export type ${k} = Record;`, ""); - // } - - // if (options.rootTypes) { - // if (k === "components") { - // console.log(typeof rootTypes[k]); - // // for (const element of rootTypes[k]) { - // // console.log("🚀 ~ file: index.ts:119 ~ element:", element); - // // } - // } - // } - - // delete rootTypes[k]; - // delete allSchemas["."]; // garbage collect, but also remove from next step (external) - // } - // 2d. external schemas (subschemas) const externalKeys = Object.keys(allSchemas); // root schema (".") should already be removed if (externalKeys.length) { diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 2e96f3b1f..656fe0fb5 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -1,7 +1,6 @@ import type { GlobalContext, OpenAPI3 } from "../types.js"; import transformComponentsObject from "./components-object.js"; import transformPathsObject from "./paths-object.js"; -import transformSchemaObjectMap from "./schema-object-map.js"; import transformWebhooksObject from "./webhooks-object.js"; /** transform top-level schema */ @@ -22,15 +21,5 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record Date: Tue, 29 Aug 2023 16:53:09 +0200 Subject: [PATCH 16/23] restore --- packages/openapi-typescript/src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index 29352ab1e..f92c346ff 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -117,6 +117,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op if (options.inject) output.push(options.inject); // 2c. root schema + const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx); const typedComponents = (allSchemas["."].schema as OpenAPI3).components!; if (options.rootTypes) { @@ -125,6 +126,16 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op } } + for (const k of Object.keys(rootTypes)) { + if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { + output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); + } else { + output.push(`export type ${k} = Record;`, ""); + } + delete rootTypes[k]; + delete allSchemas["."]; // garbage collect, but also remove from next step (external) + } + // 2d. external schemas (subschemas) const externalKeys = Object.keys(allSchemas); // root schema (".") should already be removed if (externalKeys.length) { From 9cefed8cad39b3b0c2854dd2b43357da25a4f49e Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 16:54:51 +0200 Subject: [PATCH 17/23] add to context --- packages/openapi-typescript/src/index.ts | 1 + packages/openapi-typescript/src/types.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/openapi-typescript/src/index.ts b/packages/openapi-typescript/src/index.ts index f92c346ff..86af778d0 100644 --- a/packages/openapi-typescript/src/index.ts +++ b/packages/openapi-typescript/src/index.ts @@ -54,6 +54,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op silent: options.silent ?? false, supportArrayLength: options.supportArrayLength ?? false, excludeDeprecated: options.excludeDeprecated ?? false, + rootTypes: options.rootTypes ?? false, }; // 1. load schema (and subschemas) diff --git a/packages/openapi-typescript/src/types.ts b/packages/openapi-typescript/src/types.ts index ef24852f7..0d5718faa 100644 --- a/packages/openapi-typescript/src/types.ts +++ b/packages/openapi-typescript/src/types.ts @@ -711,6 +711,7 @@ export interface GlobalContext { silent: boolean; supportArrayLength: boolean; excludeDeprecated: boolean; + rootTypes: boolean; } export type $defs = Record; From f669d4f2b0872312a92bab3ced4626c3a5eaf19f Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 16:56:53 +0200 Subject: [PATCH 18/23] rm useless dep --- .../examples/nextjs/lib/api/v1.d.ts | 70 -------- .../examples/react-query/src/lib/api/v1.d.ts | 70 -------- .../examples/sveltekit/src/lib/api/v1.d.ts | 70 -------- packages/openapi-fetch/test/v1.d.ts | 151 ------------------ packages/openapi-typescript/package.json | 1 - pnpm-lock.yaml | 8 - 6 files changed, 370 deletions(-) diff --git a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts index 12f10750a..fad1c0cc0 100644 --- a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts @@ -98,76 +98,6 @@ export interface components { pathItems: never; } -export type $defs = Record; - -export interface types { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts index 12f10750a..fad1c0cc0 100644 --- a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts @@ -98,76 +98,6 @@ export interface components { pathItems: never; } -export type $defs = Record; - -export interface types { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts index 12f10750a..fad1c0cc0 100644 --- a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts @@ -98,76 +98,6 @@ export interface components { pathItems: never; } -export type $defs = Record; - -export interface types { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/test/v1.d.ts b/packages/openapi-fetch/test/v1.d.ts index 442c36c39..f3c182d18 100644 --- a/packages/openapi-fetch/test/v1.d.ts +++ b/packages/openapi-fetch/test/v1.d.ts @@ -422,157 +422,6 @@ export interface components { pathItems: never; } -export type $defs = Record; - -export interface types { - schemas: { - Post: { - title: string; - body: string; - publish_date?: number; - }; - StringArray: string[]; - User: { - email: string; - age?: number; - avatar?: string; - }; - }; - responses: { - AllPostsGet: { - content: { - "application/json": components["schemas"]["Post"][]; - }; - }; - CreatePost: { - content: { - "application/json": { - status: string; - }; - }; - }; - CreateTag: { - content: { - "application/json": { - status: string; - }; - }; - }; - CreateReply: { - content: { - "application/json;charset=utf-8": { - message: string; - }; - }; - }; - Contact: { - content: { - "text/html": string; - }; - }; - Error: { - content: { - "application/json": { - code: number; - message: string; - }; - }; - }; - PatchPost: { - content: { - "application/json": { - status: string; - }; - }; - }; - PostDelete: { - content: { - "application/json": { - status: string; - }; - }; - }; - PostGet: { - content: { - "application/json": components["schemas"]["Post"]; - }; - }; - StringArray: { - content: { - "application/json": components["schemas"]["StringArray"]; - }; - }; - Tag: { - content: { - "application/json": string; - }; - }; - User: { - content: { - "application/json": components["schemas"]["User"]; - }; - }; - }; - parameters: never; - requestBodies: { - CreatePost: { - content: { - "application/json": { - title: string; - body: string; - publish_date: number; - }; - }; - }; - CreatePostOptional?: { - content: { - "application/json": { - title: string; - body: string; - publish_date: number; - }; - }; - }; - CreateTag: { - content: { - "application/json": { - description?: string; - }; - }; - }; - CreateReply: { - content: { - "application/json;charset=utf-8": { - message: string; - replied_at: number; - }; - }; - }; - Contact: { - content: { - "multipart/form-data": { - name: string; - email: string; - subject: string; - message: string; - }; - }; - }; - PatchPost: { - content: { - "application/json": { - properties?: null; - title?: string; - body?: string; - publish_date?: number; - }; - }; - }; - }; - headers: never; - pathItems: never; -} - export type external = Record; export interface operations { diff --git a/packages/openapi-typescript/package.json b/packages/openapi-typescript/package.json index 556b753fb..224e75892 100644 --- a/packages/openapi-typescript/package.json +++ b/packages/openapi-typescript/package.json @@ -66,7 +66,6 @@ }, "dependencies": { "ansi-colors": "^4.1.3", - "camelcase": "^7.0.1", "fast-glob": "^3.3.1", "js-yaml": "^4.1.0", "supports-color": "^9.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62550461e..1628ce5f0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -244,9 +244,6 @@ importers: ansi-colors: specifier: ^4.1.3 version: 4.1.3 - camelcase: - specifier: ^7.0.1 - version: 7.0.1 fast-glob: specifier: ^3.3.1 version: 3.3.1 @@ -3007,11 +3004,6 @@ packages: resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} engines: {node: '>=10'} - /camelcase@7.0.1: - resolution: {integrity: sha512-xlx1yCK2Oc1APsPXDL2LdlNP6+uu8OCDdhOBSVT279M/S+y75O30C2VuD8T2ogdePBBl7PfPF4504tnLgX3zfw==} - engines: {node: '>=14.16'} - dev: false - /caniuse-lite@1.0.30001528: resolution: {integrity: sha512-0Db4yyjR9QMNlsxh+kKWzQtkyflkG/snYheSzkjmvdEtEXB1+jt7A2HmSEiO6XIJPIbo92lHNGNySvE5pZcs5Q==} dev: false From 0a6de93475fb147af8205ced1848a7ee10b0ff7d Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Tue, 29 Aug 2023 17:00:32 +0200 Subject: [PATCH 19/23] restore --- packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts | 2 ++ .../openapi-fetch/examples/react-query/src/lib/api/v1.d.ts | 2 ++ .../openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts | 2 ++ packages/openapi-fetch/test/v1.d.ts | 2 ++ packages/openapi-typescript/src/transform/index.ts | 5 +++++ 5 files changed, 13 insertions(+) diff --git a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts index fad1c0cc0..55ffdce79 100644 --- a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts @@ -98,6 +98,8 @@ export interface components { pathItems: never; } +export type $defs = Record; + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts index fad1c0cc0..55ffdce79 100644 --- a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts @@ -98,6 +98,8 @@ export interface components { pathItems: never; } +export type $defs = Record; + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts index fad1c0cc0..55ffdce79 100644 --- a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts +++ b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts @@ -98,6 +98,8 @@ export interface components { pathItems: never; } +export type $defs = Record; + export type external = Record; export interface operations { diff --git a/packages/openapi-fetch/test/v1.d.ts b/packages/openapi-fetch/test/v1.d.ts index f3c182d18..0f71500b6 100644 --- a/packages/openapi-fetch/test/v1.d.ts +++ b/packages/openapi-fetch/test/v1.d.ts @@ -422,6 +422,8 @@ export interface components { pathItems: never; } +export type $defs = Record; + export type external = Record; export interface operations { diff --git a/packages/openapi-typescript/src/transform/index.ts b/packages/openapi-typescript/src/transform/index.ts index 656fe0fb5..c42079f24 100644 --- a/packages/openapi-typescript/src/transform/index.ts +++ b/packages/openapi-typescript/src/transform/index.ts @@ -1,6 +1,7 @@ import type { GlobalContext, OpenAPI3 } from "../types.js"; import transformComponentsObject from "./components-object.js"; import transformPathsObject from "./paths-object.js"; +import transformSchemaObjectMap from "./schema-object-map.js"; import transformWebhooksObject from "./webhooks-object.js"; /** transform top-level schema */ @@ -21,5 +22,9 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record Date: Mon, 2 Oct 2023 16:59:59 +0200 Subject: [PATCH 20/23] rm definition types --- .../examples/nextjs/lib/api/v1.d.ts | 193 ------------------ .../examples/react-query/src/lib/api/v1.d.ts | 193 ------------------ .../examples/sveltekit/src/app.d.ts | 12 -- .../examples/sveltekit/src/lib/api/v1.d.ts | 193 ------------------ 4 files changed, 591 deletions(-) delete mode 100644 packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts delete mode 100644 packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts delete mode 100644 packages/openapi-fetch/examples/sveltekit/src/app.d.ts delete mode 100644 packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts diff --git a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts deleted file mode 100644 index 55ffdce79..000000000 --- a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts +++ /dev/null @@ -1,193 +0,0 @@ -/** - * This file was auto-generated by openapi-typescript. - * Do not make direct changes to the file. - */ - - -export interface paths { - "/breeds": { - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - get: operations["getBreeds"]; - }; - "/fact": { - /** - * Get Random Fact - * @description Returns a random fact - */ - get: operations["getRandomFact"]; - }; - "/facts": { - /** - * Get a list of facts - * @description Returns a a list of facts - */ - get: operations["getFacts"]; - }; -} - -export type webhooks = Record; - -export interface components { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - -export type $defs = Record; - -export type external = Record; - -export interface operations { - - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - getBreeds: { - parameters: { - query?: { - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["Breed"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get Random Fact - * @description Returns a random fact - */ - getRandomFact: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"]; - }; - }; - /** @description not found */ - 404: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get a list of facts - * @description Returns a a list of facts - */ - getFacts: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; -} diff --git a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts deleted file mode 100644 index 55ffdce79..000000000 --- a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts +++ /dev/null @@ -1,193 +0,0 @@ -/** - * This file was auto-generated by openapi-typescript. - * Do not make direct changes to the file. - */ - - -export interface paths { - "/breeds": { - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - get: operations["getBreeds"]; - }; - "/fact": { - /** - * Get Random Fact - * @description Returns a random fact - */ - get: operations["getRandomFact"]; - }; - "/facts": { - /** - * Get a list of facts - * @description Returns a a list of facts - */ - get: operations["getFacts"]; - }; -} - -export type webhooks = Record; - -export interface components { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - -export type $defs = Record; - -export type external = Record; - -export interface operations { - - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - getBreeds: { - parameters: { - query?: { - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["Breed"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get Random Fact - * @description Returns a random fact - */ - getRandomFact: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"]; - }; - }; - /** @description not found */ - 404: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get a list of facts - * @description Returns a a list of facts - */ - getFacts: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; -} diff --git a/packages/openapi-fetch/examples/sveltekit/src/app.d.ts b/packages/openapi-fetch/examples/sveltekit/src/app.d.ts deleted file mode 100644 index f59b884c5..000000000 --- a/packages/openapi-fetch/examples/sveltekit/src/app.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -// See https://kit.svelte.dev/docs/types#app -// for information about these interfaces -declare global { - namespace App { - // interface Error {} - // interface Locals {} - // interface PageData {} - // interface Platform {} - } -} - -export {}; diff --git a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts deleted file mode 100644 index 55ffdce79..000000000 --- a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts +++ /dev/null @@ -1,193 +0,0 @@ -/** - * This file was auto-generated by openapi-typescript. - * Do not make direct changes to the file. - */ - - -export interface paths { - "/breeds": { - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - get: operations["getBreeds"]; - }; - "/fact": { - /** - * Get Random Fact - * @description Returns a random fact - */ - get: operations["getRandomFact"]; - }; - "/facts": { - /** - * Get a list of facts - * @description Returns a a list of facts - */ - get: operations["getFacts"]; - }; -} - -export type webhooks = Record; - -export interface components { - schemas: { - /** - * Breed model - * @description Breed - */ - Breed: { - /** - * Breed - * Format: string - * @description Breed - */ - breed?: string; - /** - * Country - * Format: string - * @description Country - */ - country?: string; - /** - * Origin - * Format: string - * @description Origin - */ - origin?: string; - /** - * Coat - * Format: string - * @description Coat - */ - coat?: string; - /** - * Pattern - * Format: string - * @description Pattern - */ - pattern?: string; - }; - /** - * CatFact model - * @description CatFact - */ - CatFact: { - /** - * Fact - * Format: string - * @description Fact - */ - fact?: string; - /** - * Length - * Format: int32 - * @description Length - */ - length?: number; - }; - Error: { - code: number; - message: string; - }; - }; - responses: never; - parameters: never; - requestBodies: never; - headers: never; - pathItems: never; -} - -export type $defs = Record; - -export type external = Record; - -export interface operations { - - /** - * Get a list of breeds - * @description Returns a a list of breeds - */ - getBreeds: { - parameters: { - query?: { - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["Breed"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get Random Fact - * @description Returns a random fact - */ - getRandomFact: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"]; - }; - }; - /** @description not found */ - 404: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; - /** - * Get a list of facts - * @description Returns a a list of facts - */ - getFacts: { - parameters: { - query?: { - /** @description maximum length of returned fact */ - max_length?: number; - /** @description limit the amount of results returned */ - limit?: number; - }; - }; - responses: { - /** @description successful operation */ - 200: { - content: { - "application/json": components["schemas"]["CatFact"][]; - }; - }; - /** @description error */ - default: { - content: { - "application/json": components["schemas"]["Error"]; - }; - }; - }; - }; -} From 2d37958c52bba88d31caa9b674ed68e8e3776729 Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Mon, 2 Oct 2023 17:03:33 +0200 Subject: [PATCH 21/23] restore types --- .../examples/nextjs/lib/api/v1.d.ts | 193 ++++++++++++++++++ .../examples/react-query/src/lib/api/v1.d.ts | 193 ++++++++++++++++++ .../examples/sveltekit/src/lib/api/v1.d.ts | 193 ++++++++++++++++++ 3 files changed, 579 insertions(+) create mode 100644 packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts create mode 100644 packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts create mode 100644 packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts diff --git a/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts new file mode 100644 index 000000000..55ffdce79 --- /dev/null +++ b/packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts @@ -0,0 +1,193 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + + +export interface paths { + "/breeds": { + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + get: operations["getBreeds"]; + }; + "/fact": { + /** + * Get Random Fact + * @description Returns a random fact + */ + get: operations["getRandomFact"]; + }; + "/facts": { + /** + * Get a list of facts + * @description Returns a a list of facts + */ + get: operations["getFacts"]; + }; +} + +export type webhooks = Record; + +export interface components { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + +export type $defs = Record; + +export type external = Record; + +export interface operations { + + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + getBreeds: { + parameters: { + query?: { + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["Breed"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get Random Fact + * @description Returns a random fact + */ + getRandomFact: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"]; + }; + }; + /** @description not found */ + 404: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get a list of facts + * @description Returns a a list of facts + */ + getFacts: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; +} diff --git a/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts new file mode 100644 index 000000000..55ffdce79 --- /dev/null +++ b/packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts @@ -0,0 +1,193 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + + +export interface paths { + "/breeds": { + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + get: operations["getBreeds"]; + }; + "/fact": { + /** + * Get Random Fact + * @description Returns a random fact + */ + get: operations["getRandomFact"]; + }; + "/facts": { + /** + * Get a list of facts + * @description Returns a a list of facts + */ + get: operations["getFacts"]; + }; +} + +export type webhooks = Record; + +export interface components { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + +export type $defs = Record; + +export type external = Record; + +export interface operations { + + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + getBreeds: { + parameters: { + query?: { + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["Breed"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get Random Fact + * @description Returns a random fact + */ + getRandomFact: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"]; + }; + }; + /** @description not found */ + 404: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get a list of facts + * @description Returns a a list of facts + */ + getFacts: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; +} diff --git a/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts new file mode 100644 index 000000000..55ffdce79 --- /dev/null +++ b/packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts @@ -0,0 +1,193 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + + +export interface paths { + "/breeds": { + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + get: operations["getBreeds"]; + }; + "/fact": { + /** + * Get Random Fact + * @description Returns a random fact + */ + get: operations["getRandomFact"]; + }; + "/facts": { + /** + * Get a list of facts + * @description Returns a a list of facts + */ + get: operations["getFacts"]; + }; +} + +export type webhooks = Record; + +export interface components { + schemas: { + /** + * Breed model + * @description Breed + */ + Breed: { + /** + * Breed + * Format: string + * @description Breed + */ + breed?: string; + /** + * Country + * Format: string + * @description Country + */ + country?: string; + /** + * Origin + * Format: string + * @description Origin + */ + origin?: string; + /** + * Coat + * Format: string + * @description Coat + */ + coat?: string; + /** + * Pattern + * Format: string + * @description Pattern + */ + pattern?: string; + }; + /** + * CatFact model + * @description CatFact + */ + CatFact: { + /** + * Fact + * Format: string + * @description Fact + */ + fact?: string; + /** + * Length + * Format: int32 + * @description Length + */ + length?: number; + }; + Error: { + code: number; + message: string; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + +export type $defs = Record; + +export type external = Record; + +export interface operations { + + /** + * Get a list of breeds + * @description Returns a a list of breeds + */ + getBreeds: { + parameters: { + query?: { + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["Breed"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get Random Fact + * @description Returns a random fact + */ + getRandomFact: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"]; + }; + }; + /** @description not found */ + 404: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; + /** + * Get a list of facts + * @description Returns a a list of facts + */ + getFacts: { + parameters: { + query?: { + /** @description maximum length of returned fact */ + max_length?: number; + /** @description limit the amount of results returned */ + limit?: number; + }; + }; + responses: { + /** @description successful operation */ + 200: { + content: { + "application/json": components["schemas"]["CatFact"][]; + }; + }; + /** @description error */ + default: { + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; +} From 94c9b8fa14760b67aaae9b20b10dd2ddbd05c90b Mon Sep 17 00:00:00 2001 From: Maxime BLANC Date: Mon, 2 Oct 2023 18:02:43 +0200 Subject: [PATCH 22/23] udpate tests --- .../openapi-typescript/test/index.test.ts | 58 +++++++++++++++++++ packages/openapi-typescript/test/load.test.ts | 1 + .../test/response-object.test.ts | 1 + 3 files changed, 60 insertions(+) diff --git a/packages/openapi-typescript/test/index.test.ts b/packages/openapi-typescript/test/index.test.ts index dc1254a14..c390c7e2a 100644 --- a/packages/openapi-typescript/test/index.test.ts +++ b/packages/openapi-typescript/test/index.test.ts @@ -1283,6 +1283,64 @@ export type $defs = Record; export type external = Record; +export type operations = Record; +`); + }); + }); + + describe("rootTypes helpers", () => { + test("should be added only when used", async () => { + const generated = await openapiTS( + { + openapi: "3.1", + info: { title: "Test", version: "1.0" }, + components: { + schemas: { + User: { + allOf: [ + { + type: "object", + properties: { firstName: { type: "string" }, lastName: { type: "string" } }, + }, + { + type: "object", + properties: { middleName: { type: "string" } }, + }, + ], + required: ["firstName", "lastName"], + }, + }, + }, + }, + { rootTypes: true }, + ); + expect(generated).toBe(`${BOILERPLATE}${WITH_REQUIRED_TYPE_HELPERS} +export type User = external["."]["components"]["schemas"]["User"]; + +export type paths = Record; + +export type webhooks = Record; + +export interface components { + schemas: { + User: WithRequired<{ + firstName?: string; + lastName?: string; + } & { + middleName?: string; + }, "firstName" | "lastName">; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + +export type $defs = Record; + +export type external = Record; + export type operations = Record; `); }); diff --git a/packages/openapi-typescript/test/load.test.ts b/packages/openapi-typescript/test/load.test.ts index d0c0251ee..0ba4f4efd 100644 --- a/packages/openapi-typescript/test/load.test.ts +++ b/packages/openapi-typescript/test/load.test.ts @@ -117,6 +117,7 @@ async function load(schema: URL | Subschema | Readable, options?: Partial Date: Mon, 2 Oct 2023 18:07:29 +0200 Subject: [PATCH 23/23] add missing rootTypes in tests --- packages/openapi-typescript/test/components-object.test.ts | 1 + packages/openapi-typescript/test/header-object.test.ts | 1 + packages/openapi-typescript/test/operation-object.test.ts | 1 + packages/openapi-typescript/test/path-item-object.test.ts | 1 + packages/openapi-typescript/test/paths-object.test.ts | 1 + packages/openapi-typescript/test/request-body-object.test.ts | 1 + packages/openapi-typescript/test/schema-object.test.ts | 1 + packages/openapi-typescript/test/webhooks-object.test.ts | 1 + 8 files changed, 8 insertions(+) diff --git a/packages/openapi-typescript/test/components-object.test.ts b/packages/openapi-typescript/test/components-object.test.ts index 1ab7bd925..553cf3ace 100644 --- a/packages/openapi-typescript/test/components-object.test.ts +++ b/packages/openapi-typescript/test/components-object.test.ts @@ -17,6 +17,7 @@ const options: GlobalContext = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }; const basicSchema: ComponentsObject = { diff --git a/packages/openapi-typescript/test/header-object.test.ts b/packages/openapi-typescript/test/header-object.test.ts index d3bba46a8..d2a00217e 100644 --- a/packages/openapi-typescript/test/header-object.test.ts +++ b/packages/openapi-typescript/test/header-object.test.ts @@ -19,6 +19,7 @@ const options: TransformHeaderObjectOptions = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }, }; diff --git a/packages/openapi-typescript/test/operation-object.test.ts b/packages/openapi-typescript/test/operation-object.test.ts index 905779d7c..9187af818 100644 --- a/packages/openapi-typescript/test/operation-object.test.ts +++ b/packages/openapi-typescript/test/operation-object.test.ts @@ -17,6 +17,7 @@ const ctx: GlobalContext = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }; const options = { ctx, path: "#/paths/~get-item" }; diff --git a/packages/openapi-typescript/test/path-item-object.test.ts b/packages/openapi-typescript/test/path-item-object.test.ts index 5d60330e7..f76a51c10 100644 --- a/packages/openapi-typescript/test/path-item-object.test.ts +++ b/packages/openapi-typescript/test/path-item-object.test.ts @@ -19,6 +19,7 @@ const options: TransformPathItemObjectOptions = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }, }; diff --git a/packages/openapi-typescript/test/paths-object.test.ts b/packages/openapi-typescript/test/paths-object.test.ts index 8a79598b9..10eb54ce9 100644 --- a/packages/openapi-typescript/test/paths-object.test.ts +++ b/packages/openapi-typescript/test/paths-object.test.ts @@ -17,6 +17,7 @@ const options: GlobalContext = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }; describe("Paths Object", () => { diff --git a/packages/openapi-typescript/test/request-body-object.test.ts b/packages/openapi-typescript/test/request-body-object.test.ts index ea313534e..28e354928 100644 --- a/packages/openapi-typescript/test/request-body-object.test.ts +++ b/packages/openapi-typescript/test/request-body-object.test.ts @@ -19,6 +19,7 @@ const options: TransformRequestBodyObjectOptions = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }, }; diff --git a/packages/openapi-typescript/test/schema-object.test.ts b/packages/openapi-typescript/test/schema-object.test.ts index 0d60e5014..e45987976 100644 --- a/packages/openapi-typescript/test/schema-object.test.ts +++ b/packages/openapi-typescript/test/schema-object.test.ts @@ -19,6 +19,7 @@ const options: TransformSchemaObjectOptions = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }, }; diff --git a/packages/openapi-typescript/test/webhooks-object.test.ts b/packages/openapi-typescript/test/webhooks-object.test.ts index 6414e4339..57cfdd2ef 100644 --- a/packages/openapi-typescript/test/webhooks-object.test.ts +++ b/packages/openapi-typescript/test/webhooks-object.test.ts @@ -17,6 +17,7 @@ const options: GlobalContext = { supportArrayLength: false, transform: undefined, excludeDeprecated: false, + rootTypes: false, }; describe("Webhooks Object", () => {