Skip to content

Commit ef0c5b5

Browse files
committed
refactor: improve operation ID handling and URL formatting in generators
This commit includes: - Updated operation ID generation to remove unnecessary PascalCase transformations. - Enhanced URL formatting in Axios method calls for better readability. - Streamlined the React Query generator to align with the updated operation ID handling.
1 parent a378030 commit ef0c5b5

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/generator/clientGenerator.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
5151
const responseObj = response as OpenAPIV3.ResponseObject;
5252
const desc = "description" in responseObj ? responseObj.description : "";
5353
const contentType = responseObj.content?.["application/json"]?.schema;
54-
const typeName = `${operationId}Response${code}`;
54+
const typeName = pascalCase(`${operationId}Response${code}`);
5555

5656
if (contentType) {
5757
if (desc) {
@@ -95,7 +95,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
9595
const hasData = (parameters && parameters.length > 0) || operation.requestBody;
9696

9797
let dataType = "undefined";
98-
const namedType = operationId;
98+
const namedType = pascalCase(operationId);
9999
if (hasData) {
100100
if (requestBody && dataProps.length > 0) {
101101
dataType = `T.${namedType}Request & { ${dataProps.join("; ")} }`;
@@ -112,11 +112,11 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
112112
const successResponse = Object.entries(responses).find(([code]) => code.startsWith("2"));
113113
const responseType = successResponse ? `T.${`${namedType}Response${successResponse[0]}`}` : "any";
114114

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

117117
const methodBody = [
118118
"const apiClient = getApiClient();",
119-
`const url = \`${urlWithParams}\`;`,
119+
`const url = ${urlWithParams};`,
120120
queryParams.length > 0
121121
? `const queryData = {
122122
${queryParams.map((p) => `["${p.name}"]: data["${p.name}"]`).join(",\n ")}
@@ -214,9 +214,7 @@ export function generateApiClient(spec: OpenAPIV3.Document, config: OpenAPIConfi
214214
operations.push({
215215
method: method,
216216
path,
217-
operationId: pascalCase(
218-
`${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
219-
),
217+
operationId: `${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
220218
summary: operation.summary,
221219
description: operation.description,
222220
parameters: resolveParameters([...(pathItem.parameters || []), ...(operation.parameters || [])]),

src/generator/reactQueryGenerator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ export function generateReactQuery(spec: OpenAPIV3.Document): string {
6565
operations.push({
6666
method: method.toUpperCase(),
6767
path,
68-
operationId: pascalCase(
69-
`${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
70-
),
68+
operationId: `${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
7169
summary: operation.summary,
7270
description: operation.description,
7371
parameters: [

0 commit comments

Comments
 (0)