From 2a0424d55c910def3c2950965765d528b819d829 Mon Sep 17 00:00:00 2001 From: Mathieu Gamache Date: Thu, 13 Jun 2024 16:08:31 -0400 Subject: [PATCH 1/2] Fix relative path + try improve debug --- .vscode/launch.json | 23 +++++++++++++++++++ v1.yaml => debug/v1.yaml | 0 .../src/openapiTypescriptHelper.ts | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json rename v1.yaml => debug/v1.yaml (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a54fa12 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,23 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/packages/create-schemas/src/bin.ts", + "args": [ + "${workspaceFolder}\\debug\\v1.yaml", + "-o ./schema.ts" + ], + "stopOnEntry": false, + "outputCapture": "std", + } + ] +} diff --git a/v1.yaml b/debug/v1.yaml similarity index 100% rename from v1.yaml rename to debug/v1.yaml diff --git a/packages/create-schemas/src/openapiTypescriptHelper.ts b/packages/create-schemas/src/openapiTypescriptHelper.ts index aa03165..93e9fe3 100644 --- a/packages/create-schemas/src/openapiTypescriptHelper.ts +++ b/packages/create-schemas/src/openapiTypescriptHelper.ts @@ -2,8 +2,10 @@ import openapiTS, { astToString, type OpenAPITSOptions } from "openapi-typescrip import { generateExportEndpointsTypeDeclaration, generateExportSchemaTypeDeclaration, getSchemaNames } from "./astHelper.ts"; export async function generateSchemas(openApiPath: string, outputPath: string, openApiTsOptions: OpenAPITSOptions): Promise { + const CWD = new URL(`file://${process.cwd()}/`); + // Create a TypeScript AST from the OpenAPI schema - const ast = await openapiTS(new URL(openApiPath), openApiTsOptions); + const ast = await openapiTS(new URL(openApiPath, CWD), openApiTsOptions); // Find the node where all the DTOs are defined, and extract their names const schemaNames = getSchemaNames(ast); From e926f8d404f6b7dc59b721c670ce8229449c754d Mon Sep 17 00:00:00 2001 From: Mathieu Gamache Date: Thu, 13 Jun 2024 16:33:49 -0400 Subject: [PATCH 2/2] Cleanup --- .vscode/launch.json | 4 +- CONTRIBUTING.md | 5 +- debug/schema.ts | 80 +++++++++++++++++++++++ packages/create-schemas/src/argsHelper.ts | 2 +- 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 debug/schema.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index a54fa12..dc740a3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,14 +7,14 @@ { "type": "node", "request": "launch", - "name": "Launch Program", + "name": "Debug Create-Schemas", "skipFiles": [ "/**" ], "program": "${workspaceFolder}/packages/create-schemas/src/bin.ts", "args": [ "${workspaceFolder}\\debug\\v1.yaml", - "-o ./schema.ts" + "-o ${workspaceFolder}\\debug\\schema.ts" ], "stopOnEntry": false, "outputCapture": "std", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 60eeea2..3e63b8b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -32,7 +32,10 @@ create-schemas [path_to_openapi_documents] -o [output-path] ### Debug -In VSCode, after building the package you can debug it by using the command `debug` inside `packages/create-schemas/package.json`. +1. Execute the command `pnpm dev` to have hot-reload of changes +2. Add breakpoints +3. In VSCode use the launch settings `Debug Create-Schemas` + ## How to release packages diff --git a/debug/schema.ts b/debug/schema.ts new file mode 100644 index 0000000..8037e3a --- /dev/null +++ b/debug/schema.ts @@ -0,0 +1,80 @@ +export interface paths { + "/good-vibes-points/{userId}": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** Get the current number of good vibe for a user */ + get: operations["GetGoodVibesPoint"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; +} +export type webhooks = Record; +export interface components { + schemas: { + GetGoodVibePointsResult: { + /** Format: int32 */ + point: number; + }; + ProblemDetails: { + type?: string | null; + title?: string | null; + /** Format: int32 */ + status?: number | null; + detail?: string | null; + instance?: string | null; + [key: string]: unknown; + }; + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} +export type $defs = Record; +export interface operations { + GetGoodVibesPoint: { + parameters: { + query?: never; + header?: never; + path: { + userId: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Success */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["GetGoodVibePointsResult"]; + }; + }; + /** @description Bad Request */ + 400: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ProblemDetails"]; + }; + }; + }; + }; +} +export type GetGoodVibePointsResult = components["schemas"]["GetGoodVibePointsResult"]; +export type ProblemDetails = components["schemas"]["ProblemDetails"]; + +export type Endpoints = keyof paths; diff --git a/packages/create-schemas/src/argsHelper.ts b/packages/create-schemas/src/argsHelper.ts index 7fbdd07..6212769 100644 --- a/packages/create-schemas/src/argsHelper.ts +++ b/packages/create-schemas/src/argsHelper.ts @@ -9,7 +9,7 @@ export function getOutputPath(args: string[]): string { } }); - return flags.output || "openapi-types.ts"; + return (flags.output || "openapi-types.ts").trim(); } export function getOpenApiTsOptionForArgs(args: string[]): OpenAPITSOptions {