Skip to content

Commit 62e227b

Browse files
committed
fix: update Axios method generation for improved data handling
This commit modifies the Axios method generation to ensure that the axiosConfig object is initialized correctly, and changes the variable name from formData to bodyData for clarity. It also updates the request handling to correctly pass bodyData in the API client call, enhancing the overall data management in the generated methods.
1 parent 9794274 commit 62e227b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/generator/clientGenerator.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
101101
const urlWithParams = urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "${data.$1}")}\`` : `"${path}"`;
102102

103103
const methodBody = [
104-
`${hasData ? "const { axiosConfig, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
104+
`${hasData ? "const { axiosConfig = {}, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
105105
"const apiClient = getApiClient();",
106106
`const url = ${urlWithParams};`,
107107
queryParams.length > 0
@@ -117,29 +117,35 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
117117
};`
118118
: "",
119119
formDataSchema?.properties
120-
? `const formData = new FormData();
120+
? `const bodyData = new FormData();
121121
${Object.entries(formDataSchema.properties)
122122
.map(([key, prop]) => {
123123
const schemaProperty = prop as OpenAPIV3.SchemaObject;
124124
const isBinary = schemaProperty.format === "binary";
125125
return formDataSchema?.required?.includes(key)
126-
? `formData.append("${key}", ${isBinary ? "" : "String("}${queryParams.length > 0 ? "bodyData" : "data"}.${key}${isBinary ? "" : ")"});`
126+
? `bodyData.append("${key}", ${isBinary ? "" : "String("}${queryParams.length > 0 ? "bodyData" : "data"}.${key}${isBinary ? "" : ")"});`
127127
: `if (${queryParams.length > 0 ? "bodyData" : "data"}.${key} != null) {
128-
formData.append("${key}", ${isBinary ? "" : "String("}${queryParams.length > 0 ? "bodyData" : "data"}.${key}${isBinary ? "" : ")"});
128+
bodyData.append("${key}", ${isBinary ? "" : "String("}${queryParams.length > 0 ? "bodyData" : "data"}.${key}${isBinary ? "" : ")"});
129129
}`;
130130
})
131131
.join("\n ")}`
132132
: "",
133-
`const res = await apiClient.${method}<${responseType}>(url, {
134-
${queryParams.length > 0 ? "params: queryData," : ""}
135-
${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
136-
${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...axiosConfig?.headers }, ...axiosConfig },` : "...axiosConfig"}
137-
});
138-
return res.data;`,
133+
queryParams.length > 0 ? "axiosConfig.params = queryData;" : "",
134+
isFormData
135+
? "axiosConfig.headers = { ...axiosConfig.headers, 'Content-Type': 'multipart/form-data' };"
136+
: "",
137+
requestBody
138+
? `const res = await apiClient.${method}<${responseType}>(url, bodyData, axiosConfig);`
139+
: `const res = await apiClient.${method}<${responseType}>(url, axiosConfig);`,
140+
"return res.data;",
139141
]
140142
.filter(Boolean)
141143
.join("\n ");
142144

145+
// ${queryParams.length > 0 ? "params: queryData," : ""}
146+
// ${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
147+
// ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...axiosConfig?.headers }, ...axiosConfig },` : "...axiosConfig"}
148+
143149
const requestParms = hasData
144150
? `props: T.${pascalCase(operationId)}Params & { axiosConfig?: AxiosRequestConfig; }`
145151
: "props?: { axiosConfig?: AxiosRequestConfig }";

0 commit comments

Comments
 (0)