Skip to content

Commit be7bcb9

Browse files
committed
refactor: enhance query option generation in React Query generator
This commit includes: - Added the HTTP method to the operation object for improved query option generation. - Updated the naming convention for query options to reflect the method used. - Streamlined the operation ID handling to maintain consistency across the generator.
1 parent 48f8675 commit be7bcb9

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
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": "[email protected]"

src/generator/reactQueryGenerator.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { camelCase, pascalCase, sanitizeTypeName, specTitle } from "../utils";
33
import type { OperationInfo } from "./clientGenerator";
44

55
function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document): string {
6-
const { operationId, parameters, requestBody } = operation;
6+
const { operationId, parameters, requestBody, method } = operation;
77

88
const hasData = (parameters && parameters.length > 0) || operation.requestBody;
99

@@ -35,10 +35,11 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
3535
: []),
3636
];
3737

38-
const namedQuery = camelCase(operationId);
38+
const namedQueryOptions = camelCase(`get_${operationId}_Query_Options`);
39+
const namedQuery = camelCase(`${method}_${operationId}`);
3940

4041
return `
41-
export const ${namedQuery}QueryOptions = (
42+
export const ${namedQueryOptions} = (
4243
${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
4344
) => {
4445
const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
@@ -63,9 +64,9 @@ export function generateReactQuery(spec: OpenAPIV3.Document): string {
6364
const operation = pathItem[method as keyof OpenAPIV3.PathItemObject] as OpenAPIV3.OperationObject;
6465
if (!operation) return;
6566
operations.push({
66-
method: method.toUpperCase(),
67+
method: method,
6768
path,
68-
operationId: `${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
69+
operationId: `${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
6970
summary: operation.summary,
7071
description: operation.description,
7172
parameters: [

0 commit comments

Comments
 (0)