react-query-lightbase-codegen 3.1.0 → 3.1.2

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.
@@ -98,7 +98,7 @@ function generateAxiosMethod(operation, spec) {
98
98
  ${queryParams.map((p) => `["${p.name}"]: data["${p.name}"]`).join(",\n ")}
99
99
  };`
100
100
  : "",
101
- requestBodySchema?.properties
101
+ requestBodySchema?.properties && !formDataSchema?.properties
102
102
  ? `const bodyData = {
103
103
  ${Object.entries(requestBodySchema.properties)
104
104
  .map(([key]) => `["${key}"]: data["${key}"]`)
@@ -22,18 +22,19 @@ function generateQueryOptions(operation, spec) {
22
22
  !requestBodySchema.type?.includes("object") &&
23
23
  !requestBodySchema.type?.includes("array");
24
24
  // Get required parameter names from both parameters and request body
25
+ const isFormData = requestBody && "content" in requestBody && requestBody.content?.["multipart/form-data"];
25
26
  const requiredParams = [
26
27
  ...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
27
- ...(content
28
- ? getRequiredFields(content, {
29
- schemas: spec.components?.schemas || {},
30
- })
31
- : []),
32
- ...(requestBody && "content" in requestBody && requestBody.content?.["multipart/form-data"]?.schema
28
+ // Use formData schema for required fields when multipart/form-data, otherwise use content schema
29
+ ...(isFormData && requestBody.content["multipart/form-data"].schema
33
30
  ? getRequiredFields(requestBody.content["multipart/form-data"].schema, {
34
31
  schemas: spec.components?.schemas || {},
35
32
  })
36
- : []),
33
+ : content
34
+ ? getRequiredFields(content, {
35
+ schemas: spec.components?.schemas || {},
36
+ })
37
+ : []),
37
38
  ];
38
39
  const namedQueryOptions = (0, utils_1.camelCase)(`get${operationId}QueryOptions`);
39
40
  const namedQuery = (0, utils_1.camelCase)(`${operationId}`);
package/dist/utils.js CHANGED
@@ -200,12 +200,21 @@ function getTypeFromSchema(schema) {
200
200
  }
201
201
  // Handle enums as union types
202
202
  if ("enum" in schema && schema.enum) {
203
+ // Filter out null/undefined enum values (some specs include null for nullable enums)
204
+ const hasNull = schema.enum.some((e) => e === null);
205
+ const nullSuffix = hasNull ? " | null" : "";
203
206
  if (Object.values(schema.enum)?.length > 0) {
204
- return (Object.values(schema.enum)
205
- .map((e) => (typeof e === "string" ? `'${e}'` : e))
206
- .join(" | ") + nullable);
207
+ const values = Object.values(schema.enum)
208
+ .filter((e) => e != null)
209
+ .map((e) => (typeof e === "string" ? `'${e}'` : e));
210
+ if (values.length === 0)
211
+ return `unknown${nullable}`;
212
+ return values.join(" | ") + nullSuffix + nullable;
207
213
  }
208
- return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
214
+ const values = schema.enum.filter((e) => e != null).map((e) => (typeof e === "string" ? `'${e}'` : e));
215
+ if (values.length === 0)
216
+ return `unknown${nullable}`;
217
+ return values.join(" | ") + nullSuffix + nullable;
209
218
  }
210
219
  // Handle types based on the "type" property
211
220
  if ("type" in schema) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-query-lightbase-codegen",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "license": "MIT",
5
5
  "description": "Generate Axios API clients and React Query options from OpenAPI specifications",
6
6
  "exports": "./dist/index.js",
@@ -123,7 +123,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
123
123
  };`
124
124
  : "",
125
125
 
126
- requestBodySchema?.properties
126
+ requestBodySchema?.properties && !formDataSchema?.properties
127
127
  ? `const bodyData = {
128
128
  ${Object.entries(requestBodySchema.properties)
129
129
  .map(([key]) => `["${key}"]: data["${key}"]`)
@@ -37,18 +37,19 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
37
37
  !requestBodySchema.type?.includes("array");
38
38
 
39
39
  // Get required parameter names from both parameters and request body
40
+ const isFormData = requestBody && "content" in requestBody && requestBody.content?.["multipart/form-data"];
40
41
  const requiredParams = [
41
42
  ...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
42
- ...(content
43
- ? getRequiredFields(content, {
44
- schemas: (spec.components?.schemas as { [key: string]: OpenAPIV3.SchemaObject }) || {},
45
- })
46
- : []),
47
- ...(requestBody && "content" in requestBody && requestBody.content?.["multipart/form-data"]?.schema
43
+ // Use formData schema for required fields when multipart/form-data, otherwise use content schema
44
+ ...(isFormData && requestBody.content["multipart/form-data"].schema
48
45
  ? getRequiredFields(requestBody.content["multipart/form-data"].schema, {
49
46
  schemas: (spec.components?.schemas as { [key: string]: OpenAPIV3.SchemaObject }) || {},
50
47
  })
51
- : []),
48
+ : content
49
+ ? getRequiredFields(content, {
50
+ schemas: (spec.components?.schemas as { [key: string]: OpenAPIV3.SchemaObject }) || {},
51
+ })
52
+ : []),
52
53
  ];
53
54
 
54
55
  const namedQueryOptions = camelCase(`get${operationId}QueryOptions`);
package/src/utils.ts CHANGED
@@ -228,14 +228,19 @@ export function getTypeFromSchema(
228
228
 
229
229
  // Handle enums as union types
230
230
  if ("enum" in schema && schema.enum) {
231
+ // Filter out null/undefined enum values (some specs include null for nullable enums)
232
+ const hasNull = schema.enum.some((e) => e === null);
233
+ const nullSuffix = hasNull ? " | null" : "";
231
234
  if (Object.values(schema.enum)?.length > 0) {
232
- return (
233
- Object.values(schema.enum)
234
- .map((e) => (typeof e === "string" ? `'${e}'` : e))
235
- .join(" | ") + nullable
236
- );
235
+ const values = Object.values(schema.enum)
236
+ .filter((e) => e != null)
237
+ .map((e) => (typeof e === "string" ? `'${e}'` : e));
238
+ if (values.length === 0) return `unknown${nullable}`;
239
+ return values.join(" | ") + nullSuffix + nullable;
237
240
  }
238
- return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
241
+ const values = schema.enum.filter((e) => e != null).map((e) => (typeof e === "string" ? `'${e}'` : e));
242
+ if (values.length === 0) return `unknown${nullable}`;
243
+ return values.join(" | ") + nullSuffix + nullable;
239
244
  }
240
245
 
241
246
  // Handle types based on the "type" property