Skip to content

Commit 817ebeb

Browse files
committed
fix: correct encoding of URL parameters in Axios method generation
This commit updates the Axios method generation to ensure that URL parameters are correctly encoded using the appropriate syntax. The change improves the handling of dynamic data in URL paths, enhancing the reliability of the generated API client methods.
1 parent 638644f commit 817ebeb

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/generator/clientGenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
101101
: "unknown";
102102

103103
const urlWithParams =
104-
urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "encodeURIComponent(data.$1)")}\`` : `"${path}"`;
104+
urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "encodeURIComponent(${data.$1})")}\`` : `"${path}"`;
105105

106106
const methodBody = [
107107
`${hasData ? "const { axiosConfig = {}, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
108108
"const apiClient = getApiClient();",
109109
`const url = ${urlWithParams};`,
110110
queryParams.length > 0
111111
? `const queryData = new URLSearchParams();
112-
${queryParams.map((p) => `queryData.append("${p.name}", encodeURIComponent(data["${p.name}"]));`).join("\n ")}`
112+
${queryParams.map((p) => `if (p.name) { queryData.append("${p.name}", encodeURIComponent(data["${p.name}"]));}`)}`
113113
: "",
114114
requestBodySchema?.properties
115115
? `const bodyData = {

0 commit comments

Comments
 (0)