Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/default-request-params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"swagger-typescript-api": patch
---

Add possibility to change default request params

Useful when overriding the http client, and you want to make sure the request params are set.

Can be set to `""` in order to not make it optional.
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ const generateCommand = defineCommand({
description: "unwrap the data item from the response",
default: codeGenBaseConfig.unwrapResponseData,
},
"default-request-params": {
type: "string",
description: "request parameters for each API request",
default: codeGenBaseConfig.defaultRequestParams,
},
},
run: async ({ args }) => {
const customConfig = await loadConfig<GenerateApiParams>({
Expand All @@ -298,6 +303,7 @@ const generateCommand = defineCommand({
debug: args.debug,
defaultResponseAsSuccess: args["default-as-success"],
defaultResponseType: args["default-response"],
defaultRequestParams: args["default-request-params"],
disableThrowOnError: args["disable-throw-on-error"],
enumNamesAsValues: args["enum-names-as-values"],
extractEnums: args["extract-enums"],
Expand Down
1 change: 1 addition & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class CodeGenConfig {
onFormatRouteName: (_routeInfo: unknown, _templateRouteName: unknown) => {},
};
defaultResponseType;
defaultRequestParams = "{}";
singleHttpClient = false;
httpClientType = CONSTANTS.HTTP_CLIENT.FETCH;
unwrapResponseData = false;
Expand Down
2 changes: 1 addition & 1 deletion templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const requestConfigParam = {
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
optional: true,
type: "RequestParams",
defaultValue: "{}",
defaultValue: config.defaultRequestParams ?? "{}",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Empty String Falsely Treated as Optional Parameter

When config.defaultRequestParams is an empty string "", the argToTmpl function's !defaultValue check treats it as falsy. This incorrectly makes the parameter optional, contradicting the intent that an empty string should result in a required parameter without a default value.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor Author

@k1rd3rf k1rd3rf Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an actual bug. Create a fix in e1c2ff4

Comment on lines 16 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid spreading undefined params when no default provided

When defaultRequestParams is configured as an empty string, this template still marks the request params argument as optional but without a default. The generated methods then spread ...params in the request object, so calling an API method without passing params throws TypeError: Cannot convert undefined or null to object instead of making the argument required (as the configuration description implies). Set optional based on whether a default value exists so the parameter becomes mandatory when no default is provided, and apply the same change in the modular template.

Useful? React with 👍 / 👎.

}

const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
Expand Down
2 changes: 1 addition & 1 deletion templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const requestConfigParam = {
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
optional: true,
type: "RequestParams",
defaultValue: "{}",
defaultValue: config.defaultRequestParams ?? "{}",
}

const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
Expand Down
Loading