react-query-lightbase-codegen 2.0.0 → 2.0.1

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.
@@ -69,7 +69,7 @@ function generateAxiosMethod(operation, spec) {
69
69
  // Add request body type if it exists
70
70
  const hasData = (parameters && parameters.length > 0) || operation.requestBody;
71
71
  let dataType = "undefined";
72
- const namedType = (0, utils_1.pascalCase)(operationId);
72
+ const namedType = operationId;
73
73
  if (hasData) {
74
74
  if (requestBody && dataProps.length > 0) {
75
75
  dataType = `T.${namedType}Request & { ${dataProps.join("; ")} }`;
@@ -117,10 +117,10 @@ function generateAxiosMethod(operation, spec) {
117
117
  })
118
118
  .join("\n ")}`
119
119
  : "",
120
- `return apiClient.${method.toLowerCase()}<${responseType}>(url, {
120
+ `return apiClient.${method}<${responseType}>(url, {
121
121
  ${queryParams.length > 0 ? "params: queryData," : ""}
122
122
  ${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
123
- ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config.headers }, ...config },` : "...config"}
123
+ ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config?.headers }, ...config },` : "...config"}
124
124
  });`,
125
125
  ]
126
126
  .filter(Boolean)
@@ -179,9 +179,9 @@ function generateApiClient(spec, config) {
179
179
  if (!operation)
180
180
  return;
181
181
  operations.push({
182
- method: method.toUpperCase(),
182
+ method: method,
183
183
  path,
184
- operationId: `${method}${(0, utils_1.sanitizeTypeName)(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
184
+ operationId: (0, utils_1.pascalCase)(`${method}_${(0, utils_1.sanitizeTypeName)(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`),
185
185
  summary: operation.summary,
186
186
  description: operation.description,
187
187
  parameters: resolveParameters([...(pathItem.parameters || []), ...(operation.parameters || [])]),
@@ -31,7 +31,7 @@ function generateQueryOptions(operation, spec) {
31
31
  const namedQuery = (0, utils_1.camelCase)(operationId);
32
32
  return `
33
33
  export const ${namedQuery}QueryOptions = (
34
- ${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : ""}
34
+ ${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
35
35
  ) => {
36
36
  const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
37
37
  return queryOptions({
@@ -56,7 +56,7 @@ function generateReactQuery(spec) {
56
56
  operations.push({
57
57
  method: method.toUpperCase(),
58
58
  path,
59
- operationId: `${method}${(0, utils_1.sanitizeTypeName)(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
59
+ operationId: (0, utils_1.pascalCase)(`${method}_${(0, utils_1.sanitizeTypeName)(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`),
60
60
  summary: operation.summary,
61
61
  description: operation.description,
62
62
  parameters: [
@@ -51,17 +51,15 @@ function getTypeFromSchema(schema, context) {
51
51
  return "any";
52
52
  }
53
53
  }
54
- function generateTypeDefinition(badName, schema, context) {
54
+ function generateTypeDefinition(name, schema, context) {
55
55
  const description = !("$ref" in schema) && schema.description ? `/**\n * ${schema.description}\n */\n` : "";
56
- const name = (0, utils_1.sanitizeTypeName)(badName);
57
56
  const typeValue = getTypeFromSchema(schema, context);
58
57
  // Use 'type' for primitives, unions, and simple types
59
58
  // Use 'interface' only for complex objects with properties
60
59
  const isInterface = !("$ref" in schema) && schema.type === "object" && schema.properties;
61
- const namedInterface = (0, utils_1.pascalCase)(name);
62
60
  return isInterface
63
61
  ? `${description}export interface ${name} ${typeValue}\n\n`
64
- : `${description}export type ${namedInterface} = ${typeValue}\n\n`;
62
+ : `${description}export type ${name} = ${typeValue}\n\n`;
65
63
  }
66
64
  /**
67
65
  * Generates TypeScript interface definitions from OpenAPI schemas
@@ -88,12 +86,13 @@ function generateTypeDefinitions(spec) {
88
86
  const operationObject = operation;
89
87
  if (!operationObject)
90
88
  continue;
89
+ const operationId = (0, utils_1.pascalCase)(`${method}_${(0, utils_1.sanitizeTypeName)(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`);
91
90
  // Generate request body type
92
91
  if (operationObject.requestBody) {
93
92
  const content = operationObject.requestBody.content;
94
93
  const jsonContent = content["application/json"] || content["multipart/form-data"];
95
94
  if (jsonContent?.schema) {
96
- const typeName = (0, utils_1.sanitizeTypeName)(`${operationObject.operationId}Request`);
95
+ const typeName = (0, utils_1.sanitizeTypeName)(`${operationId}Request`);
97
96
  output += generateTypeDefinition(typeName, jsonContent.schema, context);
98
97
  }
99
98
  }
@@ -103,8 +102,7 @@ function generateTypeDefinitions(spec) {
103
102
  const responseObj = response;
104
103
  const content = responseObj.content?.["application/json"];
105
104
  if (content?.schema) {
106
- const opName = `${method}${(0, utils_1.sanitizeTypeName)(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`;
107
- const typeName = (0, utils_1.sanitizeTypeName)(`${opName}_Response${code}`);
105
+ const typeName = (0, utils_1.sanitizeTypeName)(`${operationId}Response${code}`);
108
106
  output += generateTypeDefinition(typeName, content.schema, context);
109
107
  }
110
108
  }
package/dist/utils.js CHANGED
@@ -13,7 +13,6 @@ function camelCase(str) {
13
13
  }
14
14
  function pascalCase(str) {
15
15
  return str
16
- .toLowerCase()
17
16
  .replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase())
18
17
  .replace(/^[a-z]/, (c) => c.toUpperCase());
19
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-query-lightbase-codegen",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "description": "Generate Axios API clients and React Query options from OpenAPI specifications",
6
6
  "exports": "./dist/index.js",
@@ -95,7 +95,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
95
95
  const hasData = (parameters && parameters.length > 0) || operation.requestBody;
96
96
 
97
97
  let dataType = "undefined";
98
- const namedType = pascalCase(operationId);
98
+ const namedType = operationId;
99
99
  if (hasData) {
100
100
  if (requestBody && dataProps.length > 0) {
101
101
  dataType = `T.${namedType}Request & { ${dataProps.join("; ")} }`;
@@ -143,10 +143,10 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
143
143
  })
144
144
  .join("\n ")}`
145
145
  : "",
146
- `return apiClient.${method.toLowerCase()}<${responseType}>(url, {
146
+ `return apiClient.${method}<${responseType}>(url, {
147
147
  ${queryParams.length > 0 ? "params: queryData," : ""}
148
148
  ${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"}
150
150
  });`,
151
151
  ]
152
152
  .filter(Boolean)
@@ -212,9 +212,11 @@ export function generateApiClient(spec: OpenAPIV3.Document, config: OpenAPIConfi
212
212
  const operation = pathItem[method as keyof OpenAPIV3.PathItemObject] as OpenAPIV3.OperationObject;
213
213
  if (!operation) return;
214
214
  operations.push({
215
- method: method.toUpperCase(),
215
+ method: method,
216
216
  path,
217
- operationId: `${method}${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
217
+ operationId: pascalCase(
218
+ `${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
219
+ ),
218
220
  summary: operation.summary,
219
221
  description: operation.description,
220
222
  parameters: resolveParameters([...(pathItem.parameters || []), ...(operation.parameters || [])]),
@@ -1,5 +1,5 @@
1
1
  import type { OpenAPIV3 } from "openapi-types";
2
- import { camelCase, sanitizeTypeName, specTitle } from "../utils";
2
+ import { camelCase, pascalCase, sanitizeTypeName, specTitle } from "../utils";
3
3
  import type { OperationInfo } from "./clientGenerator";
4
4
 
5
5
  function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document): string {
@@ -39,7 +39,7 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
39
39
 
40
40
  return `
41
41
  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]>`}
43
43
  ) => {
44
44
  const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
45
45
  return queryOptions({
@@ -65,7 +65,9 @@ export function generateReactQuery(spec: OpenAPIV3.Document): string {
65
65
  operations.push({
66
66
  method: method.toUpperCase(),
67
67
  path,
68
- operationId: `${method}${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`,
68
+ operationId: pascalCase(
69
+ `${method}_${sanitizeTypeName(operation.operationId || `${path.replace(/\W+/g, "_")}`)}`
70
+ ),
69
71
  summary: operation.summary,
70
72
  description: operation.description,
71
73
  parameters: [
@@ -1,5 +1,5 @@
1
1
  import type { OpenAPIV3 } from "openapi-types";
2
- import { camelCase, pascalCase, sanitizePropertyName, sanitizeTypeName } from "../utils";
2
+ import { pascalCase, sanitizePropertyName, sanitizeTypeName } from "../utils";
3
3
 
4
4
  interface SchemaContext {
5
5
  schemas: { [key: string]: OpenAPIV3.SchemaObject };
@@ -63,23 +63,20 @@ function getTypeFromSchema(
63
63
  }
64
64
 
65
65
  function generateTypeDefinition(
66
- badName: string,
66
+ name: string,
67
67
  schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
68
68
  context: SchemaContext
69
69
  ): string {
70
70
  const description = !("$ref" in schema) && schema.description ? `/**\n * ${schema.description}\n */\n` : "";
71
-
72
- const name = sanitizeTypeName(badName);
73
-
74
71
  const typeValue = getTypeFromSchema(schema, context);
75
72
 
76
73
  // Use 'type' for primitives, unions, and simple types
77
74
  // Use 'interface' only for complex objects with properties
78
75
  const isInterface = !("$ref" in schema) && schema.type === "object" && schema.properties;
79
- const namedInterface = pascalCase(name);
76
+
80
77
  return isInterface
81
78
  ? `${description}export interface ${name} ${typeValue}\n\n`
82
- : `${description}export type ${namedInterface} = ${typeValue}\n\n`;
79
+ : `${description}export type ${name} = ${typeValue}\n\n`;
83
80
  }
84
81
 
85
82
  /**
@@ -108,13 +105,16 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
108
105
 
109
106
  const operationObject = operation as OpenAPIV3.OperationObject;
110
107
  if (!operationObject) continue;
108
+ const operationId = pascalCase(
109
+ `${method}_${sanitizeTypeName(operationObject.operationId || `${path.replace(/\W+/g, "_")}`)}`
110
+ );
111
111
 
112
112
  // Generate request body type
113
113
  if (operationObject.requestBody) {
114
114
  const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
115
115
  const jsonContent = content["application/json"] || content["multipart/form-data"];
116
116
  if (jsonContent?.schema) {
117
- const typeName = sanitizeTypeName(`${operationObject.operationId}Request`);
117
+ const typeName = sanitizeTypeName(`${operationId}Request`);
118
118
  output += generateTypeDefinition(typeName, jsonContent.schema as OpenAPIV3.SchemaObject, context);
119
119
  }
120
120
  }
@@ -125,8 +125,7 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
125
125
  const responseObj = response as OpenAPIV3.ResponseObject;
126
126
  const content = responseObj.content?.["application/json"];
127
127
  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}`);
130
129
  output += generateTypeDefinition(typeName, content.schema as OpenAPIV3.SchemaObject, context);
131
130
  }
132
131
  }
package/src/utils.ts CHANGED
@@ -9,7 +9,6 @@ export function camelCase(str: string): string {
9
9
 
10
10
  export function pascalCase(str: string): string {
11
11
  return str
12
- .toLowerCase()
13
12
  .replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase())
14
13
  .replace(/^[a-z]/, (c) => c.toUpperCase());
15
14
  }