Skip to content

Commit 85e58f7

Browse files
committed
refactor: enhance Axios and React Query generators with props handling
This commit includes: - Updated Axios method to destructure props for axiosConfig, improving flexibility. - Adjusted React Query generator to handle props similarly, ensuring consistent parameter management. - Maintained consistent formatting in package.json for better readability.
1 parent 3811b6d commit 85e58f7

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"license": "MIT",
55
"description": "Generate Axios API clients and React Query options from OpenAPI specifications",
66
"exports": "./dist/index.js",
7-
"files": [
8-
"src",
9-
"dist"
10-
],
7+
"files": ["src", "dist"],
118
"author": {
129
"name": "Oliver Winter",
1310
"email": "owinter86@gmail.com"

src/generator/clientGenerator.ts

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

117117
const methodBody = [
118+
`${hasData ? "const { axiosConfig, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
118119
"const apiClient = getApiClient();",
119120
`const url = ${urlWithParams};`,
120121
queryParams.length > 0
@@ -146,7 +147,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
146147
`const res = await apiClient.${method}<${responseType}>(url, {
147148
${queryParams.length > 0 ? "params: queryData," : ""}
148149
${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
149-
${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config?.headers }, ...config },` : "...config"}
150+
${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...axiosConfig?.headers }, ...axiosConfig },` : "...axiosConfig"}
150151
});
151152
return res.data;`,
152153
]
@@ -155,7 +156,9 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
155156

156157
return `
157158
${jsDocLines.join("\n ")}
158-
export async function ${camelCase(operationId)}(data${hasData ? `: ${dataType}` : "?: undefined"}, config?: AxiosRequestConfig): Promise<${responseType}> {
159+
export async function ${camelCase(operationId)}(props: ${
160+
hasData ? `${dataType} & { axiosConfig?: AxiosRequestConfig; }` : "{ axiosConfig?: AxiosRequestConfig }"
161+
} ): Promise<${responseType}> {
159162
${methodBody}
160163
}`;
161164
}

src/generator/reactQueryGenerator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
3939
const namedQuery = camelCase(`${operationId}`);
4040

4141
return `
42-
export const ${namedQueryOptions} = (
43-
${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_?: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
42+
export const ${namedQueryOptions} = (
43+
${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
4444
) => {
45+
${hasData ? "const { axiosConfig, ...params } = props || {};" : "const { axiosConfig } = props || {};"}
4546
const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
4647
return queryOptions({
47-
queryKey: ['${camelCase(operationId)}', ${hasData ? "params" : "undefined"}],
48-
queryFn: enabled ? () => apiClient.${namedQuery}(${hasData ? "params" : "undefined"}, config) : skipToken,
48+
queryKey: ['${camelCase(operationId)}', ${hasData ? "params" : ""}],
49+
queryFn: enabled ? () => apiClient.${namedQuery}(${hasData ? "{...params, axiosConfig}" : "{axiosConfig}"}) : skipToken,
4950
});
5051
};`;
5152
}

0 commit comments

Comments
 (0)