From 7b5f96592a10db211a960e62e4cec1b3cc6ad1a5 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 6 Oct 2024 23:25:52 +0200 Subject: [PATCH] changed getResponsetype with better formatting and more use of guard clauses and null checks --- .npmignore | 3 ++- package-lock.json | 30 ++++++++++++++------- package.json | 2 +- src/generateEndpoints.ts | 56 +++++++++++++++++++++------------------- 4 files changed, 53 insertions(+), 38 deletions(-) diff --git a/.npmignore b/.npmignore index 4343832..b029f54 100644 --- a/.npmignore +++ b/.npmignore @@ -2,4 +2,5 @@ tests/ docs/ node_modules/ src/ -dist/*.map \ No newline at end of file +dist/*.map +package-lock.json \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c58cdd9..80752e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,7 @@ "license": "ISC", "dependencies": { "node-fetch": "^3.3.2", - "yargs": "^17.7.2", - "zod": "^3.23.8" + "yargs": "^17.7.2" }, "bin": { "generate-api": "dist/main.js" @@ -84,6 +83,17 @@ "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", "dev": true }, + "node_modules/@types/node": { + "version": "22.7.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.4.tgz", + "integrity": "sha512-y+NPi1rFzDs1NdQHHToqeiX2TIS79SWEAw9GYhkkx8bD0ChpfqC+n2j5OXOCpzfojBEBt6DnEnnG9MY0zk1XLg==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "undici-types": "~6.19.2" + } + }, "node_modules/@types/yargs": { "version": "17.0.33", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", @@ -400,6 +410,14 @@ "node": ">=14.17" } }, + "node_modules/undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", + "dev": true, + "license": "MIT", + "peer": true + }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", @@ -471,14 +489,6 @@ "engines": { "node": ">=6" } - }, - "node_modules/zod": { - "version": "3.23.8", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", - "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } } } } diff --git a/package.json b/package.json index c73ce70..9f79efa 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "dependencies": { "node-fetch": "^3.3.2", - "yargs": "^17.7.2", + "yargs": "^17.7.2" }, "devDependencies": { "@types/yargs": "^17.0.33", diff --git a/src/generateEndpoints.ts b/src/generateEndpoints.ts index 656cd8b..381dcd9 100755 --- a/src/generateEndpoints.ts +++ b/src/generateEndpoints.ts @@ -56,36 +56,40 @@ function getSchemaType( } function getResponseType(responses: OpenAPIV3.ResponsesObject): string { - const successResponses = ["200", "201", "204"]; - for (const status of successResponses) { - const response = responses[status]; - if (response && "content" in response) { - const content = response.content; - if (content) { - if ( - "application/pdf" in content || - "application/octet-stream" in content - ) { - const schema = - content["application/pdf"]?.schema || - content["application/octet-stream"]?.schema; - if (schema) { - return getSchemaType(schema, "application/pdf"); - } - } - if ("application/json" in content) { - const schema = content["application/json"].schema; - if (schema) { - return getSchemaType(schema, "application/json"); - } + const successResponses = new Set(["200", "201", "204"]); + + for (const [status, response] of Object.entries(responses)) { + if (!successResponses.has(status) || !response){ + continue; + } + + if (typeof response !== 'object' || response === null) { + continue; + } + + if (!('content' in response) || !response.content) { + return 'void'; + } + + const content = response.content; + const supportedTypes = [ + 'application/pdf', + 'application/octet-stream', + 'application/json' + ] as const; + + for (const type of supportedTypes) { + const schemaContent = content[type]; + if (schemaContent && 'schema' in schemaContent) { + const schema = schemaContent.schema; + if (schema) { + return getSchemaType(schema, type); } } - } else if (response) { - // For responses without content (e.g., 204 No Content) - return "void"; } } - return "any"; + + return 'any'; } function getRequestBodyType(