Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions templates/default/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;

const requestConfigParam = {
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
optional: true,
optional: !!config.defaultRequestParams,
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

}

const argToTmpl = ({ name, optional, type, defaultValue }) => `${name}${!defaultValue && optional ? '?' : ''}: ${type}${defaultValue ? ` = ${defaultValue}` : ''}`;
Expand Down
4 changes: 2 additions & 2 deletions templates/modular/procedure-call.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const isFetchTemplate = config.httpClientType === HTTP_CLIENT.FETCH;

const requestConfigParam = {
name: specificArgNameResolver.resolve(RESERVED_REQ_PARAMS_ARG_NAMES),
optional: true,
optional: !!config.defaultRequestParams,
type: "RequestParams",
defaultValue: "{}",
defaultValue: config.defaultRequestParams ?? "{}",
}

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