Skip to content

Commit

Permalink
refactor(typegen): removed optional when required path params are used
Browse files Browse the repository at this point in the history
  • Loading branch information
pas-mike committed Dec 18, 2023
1 parent 343c555 commit 2c7a37b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
28 changes: 0 additions & 28 deletions packages/typegen/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions packages/typegen/src/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` }),
Expand All @@ -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` });
Expand Down

0 comments on commit 2c7a37b

Please sign in to comment.