react-query-lightbase-codegen 2.1.2 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,8 +85,9 @@ function generateAxiosMethod(operation, spec) {
85
85
  }
86
86
  }
87
87
  // Get response type from 2xx response
88
- const successResponse = Object.entries(responses).find(([code]) => code.startsWith("2"));
89
- const responseType = successResponse ? `T.${`${namedType}Response${successResponse[0]}`}` : "any";
88
+ const responseType = responseDetails?.[0] && "content" in responseDetails[1]
89
+ ? `T.${`${namedType}Response${responseDetails[0]}`}`
90
+ : "unknown";
90
91
  const urlWithParams = urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "${data.$1}")}\`` : `"${path}"`;
91
92
  const methodBody = [
92
93
  `${hasData ? "const { axiosConfig, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
@@ -28,7 +28,7 @@ function generateQueryOptions(operation, spec) {
28
28
  })
29
29
  : []),
30
30
  ];
31
- const namedQueryOptions = `get${operationId}QueryOptions`;
31
+ const namedQueryOptions = (0, utils_1.camelCase)(`get${operationId}QueryOptions`);
32
32
  const namedQuery = (0, utils_1.camelCase)(`${operationId}`);
33
33
  return `
34
34
  export const ${namedQueryOptions} = (
@@ -55,7 +55,7 @@ function generateReactQuery(spec) {
55
55
  operations.push({
56
56
  method: method,
57
57
  path,
58
- operationId: `${operation.operationId || `${path.replace(/\W+/g, "_")}`}`,
58
+ operationId: (0, utils_1.sanitizeTypeName)(`${operation.operationId || `${path.replace(/\W+/g, "_")}`}`),
59
59
  summary: operation.summary,
60
60
  description: operation.description,
61
61
  parameters: [
@@ -61,8 +61,8 @@ function generateTypeDefinition(name, schema, context) {
61
61
  // Use 'interface' only for complex objects with properties
62
62
  const isInterface = !("$ref" in schema) && schema.type === "object" && schema.properties;
63
63
  return isInterface
64
- ? `${description}export interface ${name} ${typeValue}\n\n`
65
- : `${description}export type ${name} = ${typeValue}\n\n`;
64
+ ? `${description}export interface ${(0, utils_1.sanitizeTypeName)(name)} ${typeValue}\n\n`
65
+ : `${description}export type ${(0, utils_1.sanitizeTypeName)(name)} = ${typeValue}\n\n`;
66
66
  }
67
67
  /**
68
68
  * Generates TypeScript interface definitions from OpenAPI schemas
@@ -89,13 +89,13 @@ function generateTypeDefinitions(spec) {
89
89
  const operationObject = operation;
90
90
  if (!operationObject)
91
91
  continue;
92
- const operationId = (0, utils_1.pascalCase)(`${(0, utils_1.sanitizeTypeName)(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`);
92
+ const operationId = `${(0, utils_1.sanitizeTypeName)(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`;
93
93
  // Generate request body type
94
94
  if (operationObject.requestBody) {
95
95
  const content = operationObject.requestBody.content;
96
96
  const jsonContent = content["application/json"] || content["multipart/form-data"];
97
97
  if (jsonContent?.schema) {
98
- const typeName = (0, utils_1.sanitizeTypeName)(`${operationId}Request`);
98
+ const typeName = `${operationId}Request`;
99
99
  output += generateTypeDefinition(typeName, jsonContent.schema, context);
100
100
  }
101
101
  }
@@ -105,7 +105,7 @@ function generateTypeDefinitions(spec) {
105
105
  const responseObj = response;
106
106
  const content = responseObj.content?.["application/json"];
107
107
  if (content?.schema) {
108
- const typeName = (0, utils_1.sanitizeTypeName)(`${operationId}Response${code}`);
108
+ const typeName = `${operationId}Response${code}`;
109
109
  output += generateTypeDefinition(typeName, content.schema, context);
110
110
  }
111
111
  }
package/dist/utils.js CHANGED
@@ -48,7 +48,7 @@ function sanitizePropertyName(name) {
48
48
  * @returns The sanitized type name with invalid characters replaced by underscores
49
49
  */
50
50
  function sanitizeTypeName(name) {
51
- return name.replace(/[^a-zA-Z0-9_]/g, "_").replace(/_+$/, "");
51
+ return pascalCase(name.replace(/[^a-zA-Z0-9_]/g, "_").replace(/_+$/, ""));
52
52
  }
53
53
  function specTitle(spec) {
54
54
  return camelCase(spec.info.title.toLowerCase().replace(/\s+/g, "-"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-query-lightbase-codegen",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "license": "MIT",
5
5
  "description": "Generate Axios API clients and React Query options from OpenAPI specifications",
6
6
  "exports": "./dist/index.js",
@@ -109,8 +109,11 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
109
109
  }
110
110
 
111
111
  // Get response type from 2xx response
112
- const successResponse = Object.entries(responses).find(([code]) => code.startsWith("2"));
113
- const responseType = successResponse ? `T.${`${namedType}Response${successResponse[0]}`}` : "any";
112
+
113
+ const responseType =
114
+ responseDetails?.[0] && "content" in responseDetails[1]
115
+ ? `T.${`${namedType}Response${responseDetails[0]}`}`
116
+ : "unknown";
114
117
 
115
118
  const urlWithParams = urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "${data.$1}")}\`` : `"${path}"`;
116
119
 
@@ -1,5 +1,5 @@
1
1
  import type { OpenAPIV3 } from "openapi-types";
2
- import { camelCase, specTitle } from "../utils";
2
+ import { camelCase, sanitizeTypeName, specTitle } from "../utils";
3
3
  import type { OperationInfo } from "./clientGenerator";
4
4
 
5
5
  function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document): string {
@@ -35,7 +35,7 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
35
35
  : []),
36
36
  ];
37
37
 
38
- const namedQueryOptions = `get${operationId}QueryOptions`;
38
+ const namedQueryOptions = camelCase(`get${operationId}QueryOptions`);
39
39
  const namedQuery = camelCase(`${operationId}`);
40
40
 
41
41
  return `
@@ -64,7 +64,7 @@ export function generateReactQuery(spec: OpenAPIV3.Document): string {
64
64
  operations.push({
65
65
  method: method,
66
66
  path,
67
- operationId: `${operation.operationId || `${path.replace(/\W+/g, "_")}`}`,
67
+ operationId: sanitizeTypeName(`${operation.operationId || `${path.replace(/\W+/g, "_")}`}`),
68
68
  summary: operation.summary,
69
69
  description: operation.description,
70
70
  parameters: [
@@ -78,8 +78,8 @@ function generateTypeDefinition(
78
78
  const isInterface = !("$ref" in schema) && schema.type === "object" && schema.properties;
79
79
 
80
80
  return isInterface
81
- ? `${description}export interface ${name} ${typeValue}\n\n`
82
- : `${description}export type ${name} = ${typeValue}\n\n`;
81
+ ? `${description}export interface ${sanitizeTypeName(name)} ${typeValue}\n\n`
82
+ : `${description}export type ${sanitizeTypeName(name)} = ${typeValue}\n\n`;
83
83
  }
84
84
 
85
85
  /**
@@ -108,16 +108,14 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
108
108
 
109
109
  const operationObject = operation as OpenAPIV3.OperationObject;
110
110
  if (!operationObject) continue;
111
- const operationId = pascalCase(
112
- `${sanitizeTypeName(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`
113
- );
111
+ const operationId = `${sanitizeTypeName(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`;
114
112
 
115
113
  // Generate request body type
116
114
  if (operationObject.requestBody) {
117
115
  const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
118
116
  const jsonContent = content["application/json"] || content["multipart/form-data"];
119
117
  if (jsonContent?.schema) {
120
- const typeName = sanitizeTypeName(`${operationId}Request`);
118
+ const typeName = `${operationId}Request`;
121
119
  output += generateTypeDefinition(typeName, jsonContent.schema as OpenAPIV3.SchemaObject, context);
122
120
  }
123
121
  }
@@ -128,7 +126,7 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
128
126
  const responseObj = response as OpenAPIV3.ResponseObject;
129
127
  const content = responseObj.content?.["application/json"];
130
128
  if (content?.schema) {
131
- const typeName = sanitizeTypeName(`${operationId}Response${code}`);
129
+ const typeName = `${operationId}Response${code}`;
132
130
  output += generateTypeDefinition(typeName, content.schema as OpenAPIV3.SchemaObject, context);
133
131
  }
134
132
  }
package/src/utils.ts CHANGED
@@ -46,7 +46,7 @@ export function sanitizePropertyName(name: string): string {
46
46
  * @returns The sanitized type name with invalid characters replaced by underscores
47
47
  */
48
48
  export function sanitizeTypeName(name: string): string {
49
- return name.replace(/[^a-zA-Z0-9_]/g, "_").replace(/_+$/, "");
49
+ return pascalCase(name.replace(/[^a-zA-Z0-9_]/g, "_").replace(/_+$/, ""));
50
50
  }
51
51
 
52
52
  export function specTitle(spec: OpenAPIV3.Document): string {