From 2c7a37bbd8037dd64a5f7ec3b1218ab242f6cb8d Mon Sep 17 00:00:00 2001 From: pas-mike Date: Mon, 18 Dec 2023 15:13:31 +0000 Subject: [PATCH] refactor(typegen): removed optional when required path params are used --- packages/typegen/package-lock.json | 28 ---------------------------- packages/typegen/src/typegen.ts | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/packages/typegen/package-lock.json b/packages/typegen/package-lock.json index 150edda..0dd3260 100644 --- a/packages/typegen/package-lock.json +++ b/packages/typegen/package-lock.json @@ -14,7 +14,6 @@ "axios": ">=0.25.0", "indent-string": "^4.0.0", "lodash": "^4.17.21", - "openapi-client-axios": "^7.4.0", "openapi-types": "^12.1.0", "yargs": "^17.3.0" }, @@ -1524,11 +1523,6 @@ "version": "1.0.2", "license": "MIT" }, - "node_modules/bath-es5": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/bath-es5/-/bath-es5-3.0.3.tgz", - "integrity": "sha512-PdCioDToH3t84lP40kUFCKWCOCH389Dl1kbC8FGoqOwamxsmqxxnJSXdkTOsPoNHXjem4+sJ+bbNoQm5zeCqxg==" - }, "node_modules/brace-expansion": { "version": "1.1.11", "dev": true, @@ -1793,11 +1787,6 @@ "node": ">=0.4.0" } }, - "node_modules/dereference-json-schema": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/dereference-json-schema/-/dereference-json-schema-0.2.1.tgz", - "integrity": "sha512-uzJsrg225owJyRQ8FNTPHIuBOdSzIZlHhss9u6W8mp7jJldHqGuLv9cULagP/E26QVJDnjtG8U7Dw139mM1ydA==" - }, "node_modules/detect-newline": { "version": "3.1.0", "dev": true, @@ -3485,23 +3474,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/openapi-client-axios": { - "version": "7.4.0", - "resolved": "https://registry.npmjs.org/openapi-client-axios/-/openapi-client-axios-7.4.0.tgz", - "integrity": "sha512-qD7LjzKWfs/Rp+9NfFrtzpxVzlJHqau3O0W/wQ8oEzyvhokdJKkq08rzINcElBVuz+KZO+jUOC0zJz8r+IRPyQ==", - "dependencies": { - "bath-es5": "^3.0.3", - "dereference-json-schema": "^0.2.1", - "openapi-types": "^12.1.3" - }, - "funding": { - "url": "https://github.com/sponsors/anttiviljami" - }, - "peerDependencies": { - "axios": ">=0.25.0", - "js-yaml": "^4.1.0" - } - }, "node_modules/openapi-types": { "version": "12.1.3", "resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-12.1.3.tgz", diff --git a/packages/typegen/src/typegen.ts b/packages/typegen/src/typegen.ts index 7fba921..f4ba8c6 100644 --- a/packages/typegen/src/typegen.ts +++ b/packages/typegen/src/typegen.ts @@ -105,9 +105,16 @@ function generateMethodForOperation(methodName: string, operation: Operation, ex // parameters arg const normalizedOperationId = convertKeyToTypeName(operationId); const normalizedPath = convertKeyToTypeName(operation.path); - const parameterTypePaths = _.chain([ + + const pathParameterTypePaths = _.chain([ _.find(exportTypes, { schemaRef: `#/paths/${normalizedOperationId}/pathParameters` }), _.find(exportTypes, { schemaRef: `#/paths/${normalizedPath}/pathParameters` }), + ]) + .filter() + .map('path') + .value(); + + const parameterTypePaths = _.chain([ _.find(exportTypes, { schemaRef: `#/paths/${normalizedOperationId}/queryParameters` }), _.find(exportTypes, { schemaRef: `#/paths/${normalizedPath}/queryParameters` }), _.find(exportTypes, { schemaRef: `#/paths/${normalizedOperationId}/headerParameters` }), @@ -117,10 +124,16 @@ function generateMethodForOperation(methodName: string, operation: Operation, ex ]) .filter() .map('path') - .value(); + .value() + .concat(pathParameterTypePaths); const parametersType = !_.isEmpty(parameterTypePaths) ? parameterTypePaths.join(' & ') : 'UnknownParamsObject'; - const parametersArg = `parameters?: Parameters<${parametersType}> | null`; + let parametersArg = `parameters?: Parameters<${parametersType}> | null`; + + // All path parameters are required + if (_.isEmpty(pathParameterTypePaths)) { + parametersArg = `parameters: Parameters<${parametersType}>`; + } // payload arg const requestBodyType = _.find(exportTypes, { schemaRef: `#/paths/${normalizedOperationId}/requestBody` });