Skip to content

Commit 398b9c2

Browse files
committed
fix: streamline naming conventions in client and query generators
This commit includes: - Simplified the generation of operation IDs by removing unnecessary transformations. - Updated the handling of Axios method calls to use the correct method name directly. - Enhanced type definitions in the schema generator to utilize consistent naming practices. - Improved the React Query generator to handle parameters more effectively. - Cleaned up imports in the utils module by removing unused functions.
1 parent 14cad16 commit 398b9c2

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = pascalCase(operationId);
98+
const namedType = operationId;
9999
if (hasData) {
100100
if (requestBody && dataProps.length > 0) {
101101
dataType = `T.${namedType}Request & { ${dataProps.join("; ")} }`;
@@ -143,10 +143,10 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
143143
})
144144
.join("\n ")}`
145145
: "",
146-
`return apiClient.${method.toLowerCase()}<${responseType}>(url, {
146+
`return apiClient.${method}<${responseType}>(url, {
147147
${queryParams.length > 0 ? "params: queryData," : ""}
148148
${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
149-
${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config.headers }, ...config },` : "...config"}
149+
${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config?.headers }, ...config },` : "...config"}
150150
});`,
151151
]
152152
.filter(Boolean)
@@ -212,9 +212,11 @@ export function generateApiClient(spec: OpenAPIV3.Document, config: OpenAPIConfi
212212
const operation = pathItem[method as keyof OpenAPIV3.PathItemObject] as OpenAPIV3.OperationObject;
213213
if (!operation) return;
214214
operations.push({
215-
method: method.toUpperCase(),
215+
method: method,
216216
path,
217-
operationId: `${method}${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
217+
operationId: pascalCase(
218+
`${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
219+
),
218220
summary: operation.summary,
219221
description: operation.description,
220222
parameters: resolveParameters([...(pathItem.parameters || []), ...(operation.parameters || [])]),

src/generator/reactQueryGenerator.ts

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

55
function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document): string {
@@ -39,7 +39,7 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
3939

4040
return `
4141
export const ${namedQuery}QueryOptions = (
42-
${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : ""}
42+
${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
4343
) => {
4444
const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
4545
return queryOptions({
@@ -65,7 +65,9 @@ export function generateReactQuery(spec: OpenAPIV3.Document): string {
6565
operations.push({
6666
method: method.toUpperCase(),
6767
path,
68-
operationId: `${method}${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
68+
operationId: pascalCase(
69+
`${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
70+
),
6971
summary: operation.summary,
7072
description: operation.description,
7173
parameters: [

src/generator/schemaGenerator.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { OpenAPIV3 } from "openapi-types";
2-
import { camelCase, pascalCase, sanitizePropertyName, sanitizeTypeName } from "../utils";
2+
import { pascalCase, sanitizePropertyName, sanitizeTypeName } from "../utils";
33

44
interface SchemaContext {
55
schemas: { [key: string]: OpenAPIV3.SchemaObject };
@@ -63,23 +63,20 @@ function getTypeFromSchema(
6363
}
6464

6565
function generateTypeDefinition(
66-
badName: string,
66+
name: string,
6767
schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
6868
context: SchemaContext
6969
): string {
7070
const description = !("$ref" in schema) && schema.description ? `/**\n * ${schema.description}\n */\n` : "";
71-
72-
const name = sanitizeTypeName(badName);
73-
7471
const typeValue = getTypeFromSchema(schema, context);
7572

7673
// Use 'type' for primitives, unions, and simple types
7774
// Use 'interface' only for complex objects with properties
7875
const isInterface = !("$ref" in schema) && schema.type === "object" && schema.properties;
79-
const namedInterface = pascalCase(name);
76+
8077
return isInterface
8178
? `${description}export interface ${name} ${typeValue}\n\n`
82-
: `${description}export type ${namedInterface} = ${typeValue}\n\n`;
79+
: `${description}export type ${name} = ${typeValue}\n\n`;
8380
}
8481

8582
/**
@@ -108,13 +105,16 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
108105

109106
const operationObject = operation as OpenAPIV3.OperationObject;
110107
if (!operationObject) continue;
108+
const operationId = pascalCase(
109+
`${method}_${sanitizeTypeName(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`
110+
);
111111

112112
// Generate request body type
113113
if (operationObject.requestBody) {
114114
const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
115115
const jsonContent = content["application/json"] || content["multipart/form-data"];
116116
if (jsonContent?.schema) {
117-
const typeName = sanitizeTypeName(`${operationObject.operationId}Request`);
117+
const typeName = sanitizeTypeName(`${operationId}Request`);
118118
output += generateTypeDefinition(typeName, jsonContent.schema as OpenAPIV3.SchemaObject, context);
119119
}
120120
}
@@ -125,8 +125,7 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
125125
const responseObj = response as OpenAPIV3.ResponseObject;
126126
const content = responseObj.content?.["application/json"];
127127
if (content?.schema) {
128-
const opName = `${method}${sanitizeTypeName(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`;
129-
const typeName = sanitizeTypeName(`${opName}_Response${code}`);
128+
const typeName = sanitizeTypeName(`${operationId}Response${code}`);
130129
output += generateTypeDefinition(typeName, content.schema as OpenAPIV3.SchemaObject, context);
131130
}
132131
}

src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export function camelCase(str: string): string {
99

1010
export function pascalCase(str: string): string {
1111
return str
12-
.toLowerCase()
1312
.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase())
1413
.replace(/^[a-z]/, (c) => c.toUpperCase());
1514
}

0 commit comments

Comments
 (0)