react-query-lightbase-codegen 2.5.11 → 3.1.0
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.
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +180 -0
- package/dist/generateHooks.js +14 -57
- package/dist/generateHooks.js.map +1 -1
- package/dist/generator/clientGenerator.d.ts +0 -10
- package/dist/generator/clientGenerator.js +50 -82
- package/dist/generator/reactQueryGenerator.js +7 -44
- package/dist/generator/schemaGenerator.js +40 -51
- package/dist/utils.d.ts +35 -1
- package/dist/utils.js +118 -14
- package/dist/utils.js.map +1 -1
- package/package.json +4 -1
- package/src/cli.ts +212 -0
- package/src/generator/clientGenerator.ts +60 -110
- package/src/generator/reactQueryGenerator.ts +15 -50
- package/src/generator/schemaGenerator.ts +51 -62
- package/src/utils.ts +144 -15
|
@@ -2,17 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateReactQuery = generateReactQuery;
|
|
4
4
|
const utils_1 = require("../utils");
|
|
5
|
-
function resolveSchema(schema, spec) {
|
|
6
|
-
if (!schema)
|
|
7
|
-
return undefined;
|
|
8
|
-
if ("$ref" in schema) {
|
|
9
|
-
const index = schema.$ref.split("/").pop();
|
|
10
|
-
return spec.components?.schemas?.[index];
|
|
11
|
-
}
|
|
12
|
-
return schema;
|
|
13
|
-
}
|
|
14
5
|
function generateQueryOptions(operation, spec) {
|
|
15
|
-
const { operationId, parameters, requestBody,
|
|
6
|
+
const { operationId, parameters, requestBody, deprecated } = operation;
|
|
16
7
|
const hasData = (parameters && parameters.length > 0) || operation.requestBody;
|
|
17
8
|
// Helper to get required fields from a schema
|
|
18
9
|
const getRequiredFields = (schema, context) => {
|
|
@@ -23,12 +14,8 @@ function generateQueryOptions(operation, spec) {
|
|
|
23
14
|
}
|
|
24
15
|
return schema.required?.map((p) => `'${p}'`) || [];
|
|
25
16
|
};
|
|
26
|
-
const content = requestBody && "content" in requestBody
|
|
27
|
-
|
|
28
|
-
requestBody.content?.["application/json"]?.schema ??
|
|
29
|
-
requestBody.content?.["application/octet-stream"]?.schema)
|
|
30
|
-
: undefined;
|
|
31
|
-
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
17
|
+
const content = requestBody && "content" in requestBody ? (0, utils_1.getContentSchema)(requestBody.content) : undefined;
|
|
18
|
+
const requestBodySchema = content ? (0, utils_1.resolveSchema)(content, spec) : undefined;
|
|
32
19
|
// Check if request body is a primitive type (string, number, boolean)
|
|
33
20
|
const isPrimitiveRequestBody = requestBodySchema &&
|
|
34
21
|
!requestBodySchema.properties &&
|
|
@@ -69,8 +56,9 @@ function generateQueryOptions(operation, spec) {
|
|
|
69
56
|
? "!!data"
|
|
70
57
|
: `hasDefinedProps(${paramsVariable}, ${requiredParams.join(", ")})`
|
|
71
58
|
: "true";
|
|
59
|
+
const deprecatedComment = deprecated ? "/** @deprecated */\n" : "";
|
|
72
60
|
return `
|
|
73
|
-
export const ${namedQueryOptions} = (
|
|
61
|
+
${deprecatedComment}export const ${namedQueryOptions} = (
|
|
74
62
|
${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
|
|
75
63
|
) => {
|
|
76
64
|
${destructuringLine}
|
|
@@ -82,36 +70,11 @@ export const ${namedQueryOptions} = (
|
|
|
82
70
|
};`;
|
|
83
71
|
}
|
|
84
72
|
function generateReactQuery(spec) {
|
|
85
|
-
const operations =
|
|
86
|
-
// Collect operations (same as in clientGenerator)
|
|
87
|
-
Object.entries(spec.paths || {}).forEach(([path, pathItem]) => {
|
|
88
|
-
if (!pathItem)
|
|
89
|
-
return;
|
|
90
|
-
["get", "post", "put", "delete", "patch"].forEach((method) => {
|
|
91
|
-
const operation = pathItem[method];
|
|
92
|
-
if (!operation)
|
|
93
|
-
return;
|
|
94
|
-
operations.push({
|
|
95
|
-
method: method,
|
|
96
|
-
path,
|
|
97
|
-
operationId: (0, utils_1.sanitizeTypeName)(`${operation.operationId || `${path.replace(/\W+/g, "_")}`}`),
|
|
98
|
-
summary: operation.summary,
|
|
99
|
-
description: operation.description,
|
|
100
|
-
parameters: [
|
|
101
|
-
...(pathItem.parameters || []),
|
|
102
|
-
...(operation.parameters || []),
|
|
103
|
-
],
|
|
104
|
-
requestBody: operation.requestBody,
|
|
105
|
-
responses: operation.responses,
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
73
|
+
const operations = (0, utils_1.collectOperations)(spec);
|
|
109
74
|
return `import { queryOptions, skipToken } from '@tanstack/react-query';
|
|
110
75
|
import * as apiClient from './${(0, utils_1.specTitle)(spec)}.client';
|
|
111
|
-
// TEMPORARY: allows for backward compatibility imports
|
|
112
|
-
export * from './${(0, utils_1.specTitle)(spec)}.client';
|
|
113
76
|
|
|
114
|
-
const hasDefinedProps = <T extends { [P in K]?:
|
|
77
|
+
const hasDefinedProps = <T extends { [P in K]?: unknown }, K extends PropertyKey>(
|
|
115
78
|
obj: T,
|
|
116
79
|
...keys: K[]
|
|
117
80
|
): obj is T & { [P in K]-?: Exclude<T[P], undefined> } => {
|
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateTypeDefinitions = generateTypeDefinitions;
|
|
4
4
|
const utils_1 = require("../utils");
|
|
5
|
+
/**
|
|
6
|
+
* Formats a parameter as a TypeScript property string with optional JSDoc.
|
|
7
|
+
*/
|
|
8
|
+
function formatParamProperty(param, forceRequired = false) {
|
|
9
|
+
const safeName = (0, utils_1.sanitizePropertyName)(param.name);
|
|
10
|
+
const isDeprecated = param.deprecated;
|
|
11
|
+
const hasDescription = param.description;
|
|
12
|
+
const isOptional = forceRequired ? false : !param.required;
|
|
13
|
+
const desc = hasDescription || isDeprecated
|
|
14
|
+
? `/**${hasDescription ? `\n * ${param.description}` : ""}${isDeprecated ? "\n * @deprecated" : ""}\n */\n`
|
|
15
|
+
: "";
|
|
16
|
+
return `${desc}${safeName}${isOptional ? "?" : ""}: ${(0, utils_1.getTypeFromSchema)(param.schema)}`;
|
|
17
|
+
}
|
|
5
18
|
function generateTypeDefinition(name, schema) {
|
|
6
19
|
const description = !("$ref" in schema) && schema.description ? `/**\n * ${schema.description}\n */\n` : "";
|
|
7
20
|
const typeValue = (0, utils_1.getTypeFromSchema)(schema);
|
|
@@ -16,17 +29,15 @@ function generateTypeDefinition(name, schema) {
|
|
|
16
29
|
* Generates TypeScript interface definitions from OpenAPI schemas
|
|
17
30
|
*/
|
|
18
31
|
function generateTypeDefinitions(spec) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
generatedTypes: new Set(),
|
|
22
|
-
};
|
|
32
|
+
const schemas = spec.components?.schemas || {};
|
|
33
|
+
const generatedTypes = new Set();
|
|
23
34
|
let output = "/* Generated TypeScript Definitions */\n\n";
|
|
24
35
|
// Generate types for all schema definitions
|
|
25
|
-
for (const [name, schema] of Object.entries(
|
|
26
|
-
if (
|
|
36
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
37
|
+
if (generatedTypes.has(name))
|
|
27
38
|
continue;
|
|
28
39
|
output += generateTypeDefinition(name, schema);
|
|
29
|
-
|
|
40
|
+
generatedTypes.add(name);
|
|
30
41
|
}
|
|
31
42
|
// Generate request/response types
|
|
32
43
|
if (spec.paths) {
|
|
@@ -42,30 +53,32 @@ function generateTypeDefinitions(spec) {
|
|
|
42
53
|
// Generate request body type
|
|
43
54
|
if (requestBody) {
|
|
44
55
|
const content = requestBody.content;
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
content["multipart/form-data"] ??
|
|
48
|
-
content["application/octet-stream"] ??
|
|
49
|
-
content["application/json;charset=UTF-8"];
|
|
50
|
-
if (jsonContent?.schema) {
|
|
56
|
+
const requestSchema = (0, utils_1.getContentSchema)(content);
|
|
57
|
+
if (requestSchema) {
|
|
51
58
|
const typeName = `${operationId}Request`;
|
|
52
|
-
output += generateTypeDefinition(typeName,
|
|
59
|
+
output += generateTypeDefinition(typeName, requestSchema);
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
62
|
// Generate response types
|
|
63
|
+
const errorTypes = [];
|
|
56
64
|
if (responses) {
|
|
57
65
|
for (const [code, response] of Object.entries(responses)) {
|
|
58
66
|
const responseObj = response;
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
responseObj.content?.["application/octet-stream"] ??
|
|
62
|
-
responseObj.content?.["application/json;charset=UTF-8"];
|
|
63
|
-
if (content?.schema) {
|
|
67
|
+
const responseSchema = (0, utils_1.getContentSchema)(responseObj.content);
|
|
68
|
+
if (responseSchema) {
|
|
64
69
|
const typeName = `${operationId}Response${code}`;
|
|
65
|
-
output += generateTypeDefinition(typeName,
|
|
70
|
+
output += generateTypeDefinition(typeName, responseSchema);
|
|
71
|
+
// Track non-2xx responses for error union type
|
|
72
|
+
if (!code.startsWith("2")) {
|
|
73
|
+
errorTypes.push(typeName);
|
|
74
|
+
}
|
|
66
75
|
}
|
|
67
76
|
}
|
|
68
77
|
}
|
|
78
|
+
// Generate error union type if there are error responses
|
|
79
|
+
if (errorTypes.length > 0) {
|
|
80
|
+
output += `export type ${(0, utils_1.pascalCase)(operationId)}Error = ${errorTypes.join(" | ")};\n\n`;
|
|
81
|
+
}
|
|
69
82
|
// Build data type parts
|
|
70
83
|
const dataProps = [];
|
|
71
84
|
const urlParams = (parameters?.filter((p) => "in" in p && p.in === "path") ||
|
|
@@ -74,37 +87,13 @@ function generateTypeDefinitions(spec) {
|
|
|
74
87
|
[]);
|
|
75
88
|
const headerParams = (parameters?.filter((p) => "in" in p && p.in === "header") ||
|
|
76
89
|
[]);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
*/\n`
|
|
85
|
-
: "";
|
|
86
|
-
dataProps.push(`${desc}${safeName}: ${(0, utils_1.getTypeFromSchema)(p.schema)}`);
|
|
87
|
-
});
|
|
88
|
-
queryParams.forEach((p) => {
|
|
89
|
-
const safeName = (0, utils_1.sanitizePropertyName)(p.name);
|
|
90
|
-
const isDeprecated = "deprecated" in p && p.deprecated;
|
|
91
|
-
const hasDescription = "description" in p && p.description;
|
|
92
|
-
const desc = hasDescription || isDeprecated
|
|
93
|
-
? `\n/**${hasDescription ? `\n* ${p.description}` : ""}${isDeprecated ? "\n* @deprecated" : ""}
|
|
94
|
-
*/\n`
|
|
95
|
-
: "";
|
|
96
|
-
dataProps.push(`${desc}${safeName}${p.required ? "" : "?"}: ${(0, utils_1.getTypeFromSchema)(p.schema)}`);
|
|
97
|
-
});
|
|
98
|
-
headerParams.forEach((p) => {
|
|
99
|
-
const safeName = (0, utils_1.sanitizePropertyName)(p.name);
|
|
100
|
-
const isDeprecated = "deprecated" in p && p.deprecated;
|
|
101
|
-
const hasDescription = "description" in p && p.description;
|
|
102
|
-
const desc = hasDescription || isDeprecated
|
|
103
|
-
? `\n/**${hasDescription ? `\n* ${p.description}` : ""}${isDeprecated ? "\n* @deprecated" : ""}
|
|
104
|
-
*/\n`
|
|
105
|
-
: "";
|
|
106
|
-
dataProps.push(`${desc}${safeName}${p.required ? "" : "?"}: ${(0, utils_1.getTypeFromSchema)(p.schema)}`);
|
|
107
|
-
});
|
|
90
|
+
const cookieParams = (parameters?.filter((p) => "in" in p && p.in === "cookie") ||
|
|
91
|
+
[]);
|
|
92
|
+
// Add path, query, header, and cookie parameters
|
|
93
|
+
urlParams.forEach((p) => dataProps.push(formatParamProperty(p, true))); // Path params always required
|
|
94
|
+
queryParams.forEach((p) => dataProps.push(formatParamProperty(p)));
|
|
95
|
+
headerParams.forEach((p) => dataProps.push(formatParamProperty(p)));
|
|
96
|
+
cookieParams.forEach((p) => dataProps.push(formatParamProperty(p)));
|
|
108
97
|
// Add request body type if it exists
|
|
109
98
|
const hasData = (parameters && parameters.length > 0) || requestBody;
|
|
110
99
|
let dataType = "undefined";
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from "openapi-types";
|
|
2
|
+
/**
|
|
3
|
+
* Supported content types in order of preference
|
|
4
|
+
*/
|
|
5
|
+
export declare const CONTENT_TYPES: readonly ["application/ld+json", "application/json", "text/plain", "multipart/form-data", "application/octet-stream", "application/json;charset=UTF-8"];
|
|
6
|
+
/**
|
|
7
|
+
* Resolves a schema reference to its actual schema object.
|
|
8
|
+
* If the schema is already a concrete schema (not a $ref), returns it as-is.
|
|
9
|
+
*/
|
|
10
|
+
export declare function resolveSchema(schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined, spec: OpenAPIV3.Document): OpenAPIV3.SchemaObject | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the schema from a content object, checking supported content types in order.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getContentSchema(content: {
|
|
15
|
+
[media: string]: OpenAPIV3.MediaTypeObject;
|
|
16
|
+
} | undefined): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Operation info extracted from OpenAPI paths
|
|
19
|
+
*/
|
|
20
|
+
export interface OperationInfo {
|
|
21
|
+
method: string;
|
|
22
|
+
path: string;
|
|
23
|
+
operationId: string;
|
|
24
|
+
summary?: string;
|
|
25
|
+
description?: string;
|
|
26
|
+
deprecated?: boolean;
|
|
27
|
+
parameters: OpenAPIV3.ParameterObject[];
|
|
28
|
+
requestBody?: OpenAPIV3.RequestBodyObject;
|
|
29
|
+
responses: OpenAPIV3.ResponsesObject;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Collects all operations from an OpenAPI spec, resolving parameter and request body references.
|
|
33
|
+
*/
|
|
34
|
+
export declare function collectOperations(spec: OpenAPIV3.Document): OperationInfo[];
|
|
2
35
|
export declare function camelCase(str: string): string;
|
|
3
36
|
export declare function pascalCase(str: string): string;
|
|
4
37
|
/**
|
|
@@ -39,6 +72,7 @@ export declare function specTitle(spec: OpenAPIV3.Document): string;
|
|
|
39
72
|
* Handles:
|
|
40
73
|
* - References ($ref) by extracting the type name
|
|
41
74
|
* - Nullable types by appending "| null"
|
|
75
|
+
* - Composition types (allOf → intersection, oneOf/anyOf → union)
|
|
42
76
|
* - Enums by creating union types of the values
|
|
43
77
|
* - OneOf schemas as union types
|
|
44
78
|
* - Basic types (string, number, boolean)
|
|
@@ -48,7 +82,7 @@ export declare function specTitle(spec: OpenAPIV3.Document): string;
|
|
|
48
82
|
* - Objects with additionalProperties as Records
|
|
49
83
|
* - Fallback to "any" for unknown types
|
|
50
84
|
*
|
|
51
|
-
* @param
|
|
85
|
+
* @param schema - The OpenAPI schema/parameter object to convert
|
|
52
86
|
* @returns The TypeScript type as a string
|
|
53
87
|
*/
|
|
54
88
|
export declare function getTypeFromSchema(schema: OpenAPIV3.ParameterObject | OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined): string | undefined;
|
package/dist/utils.js
CHANGED
|
@@ -1,11 +1,98 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONTENT_TYPES = void 0;
|
|
4
|
+
exports.resolveSchema = resolveSchema;
|
|
5
|
+
exports.getContentSchema = getContentSchema;
|
|
6
|
+
exports.collectOperations = collectOperations;
|
|
3
7
|
exports.camelCase = camelCase;
|
|
4
8
|
exports.pascalCase = pascalCase;
|
|
5
9
|
exports.sanitizePropertyName = sanitizePropertyName;
|
|
6
10
|
exports.sanitizeTypeName = sanitizeTypeName;
|
|
7
11
|
exports.specTitle = specTitle;
|
|
8
12
|
exports.getTypeFromSchema = getTypeFromSchema;
|
|
13
|
+
/**
|
|
14
|
+
* Supported content types in order of preference
|
|
15
|
+
*/
|
|
16
|
+
exports.CONTENT_TYPES = [
|
|
17
|
+
"application/ld+json",
|
|
18
|
+
"application/json",
|
|
19
|
+
"text/plain",
|
|
20
|
+
"multipart/form-data",
|
|
21
|
+
"application/octet-stream",
|
|
22
|
+
"application/json;charset=UTF-8",
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Resolves a schema reference to its actual schema object.
|
|
26
|
+
* If the schema is already a concrete schema (not a $ref), returns it as-is.
|
|
27
|
+
*/
|
|
28
|
+
function resolveSchema(schema, spec) {
|
|
29
|
+
if (!schema)
|
|
30
|
+
return undefined;
|
|
31
|
+
if ("$ref" in schema) {
|
|
32
|
+
const index = schema.$ref.split("/").pop();
|
|
33
|
+
return spec.components?.schemas?.[index];
|
|
34
|
+
}
|
|
35
|
+
return schema;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Extracts the schema from a content object, checking supported content types in order.
|
|
39
|
+
*/
|
|
40
|
+
function getContentSchema(content) {
|
|
41
|
+
if (!content)
|
|
42
|
+
return undefined;
|
|
43
|
+
for (const type of exports.CONTENT_TYPES) {
|
|
44
|
+
if (content[type]?.schema) {
|
|
45
|
+
return content[type].schema;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const HTTP_METHODS = ["get", "post", "put", "delete", "patch"];
|
|
51
|
+
/**
|
|
52
|
+
* Collects all operations from an OpenAPI spec, resolving parameter and request body references.
|
|
53
|
+
*/
|
|
54
|
+
function collectOperations(spec) {
|
|
55
|
+
const operations = [];
|
|
56
|
+
const resolveParameters = (parameters) => {
|
|
57
|
+
return parameters.map((p) => {
|
|
58
|
+
if ("$ref" in p) {
|
|
59
|
+
const index = p.$ref.split("/").pop();
|
|
60
|
+
return spec.components?.parameters?.[index];
|
|
61
|
+
}
|
|
62
|
+
return p;
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
const resolveRequestBody = (requestBody) => {
|
|
66
|
+
if (!requestBody)
|
|
67
|
+
return undefined;
|
|
68
|
+
if ("$ref" in requestBody) {
|
|
69
|
+
const index = requestBody.$ref.split("/").pop();
|
|
70
|
+
return spec.components?.requestBodies?.[index];
|
|
71
|
+
}
|
|
72
|
+
return requestBody;
|
|
73
|
+
};
|
|
74
|
+
Object.entries(spec.paths || {}).forEach(([path, pathItem]) => {
|
|
75
|
+
if (!pathItem)
|
|
76
|
+
return;
|
|
77
|
+
HTTP_METHODS.forEach((method) => {
|
|
78
|
+
const operation = pathItem[method];
|
|
79
|
+
if (!operation)
|
|
80
|
+
return;
|
|
81
|
+
operations.push({
|
|
82
|
+
method,
|
|
83
|
+
path,
|
|
84
|
+
operationId: sanitizeTypeName(operation.operationId || path.replace(/\W+/g, "_")),
|
|
85
|
+
summary: operation.summary,
|
|
86
|
+
description: operation.description,
|
|
87
|
+
deprecated: operation.deprecated,
|
|
88
|
+
parameters: resolveParameters([...(pathItem.parameters || []), ...(operation.parameters || [])]),
|
|
89
|
+
requestBody: resolveRequestBody(operation.requestBody),
|
|
90
|
+
responses: operation.responses,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
return operations;
|
|
95
|
+
}
|
|
9
96
|
function camelCase(str) {
|
|
10
97
|
return str
|
|
11
98
|
.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase())
|
|
@@ -64,6 +151,7 @@ function specTitle(spec) {
|
|
|
64
151
|
* Handles:
|
|
65
152
|
* - References ($ref) by extracting the type name
|
|
66
153
|
* - Nullable types by appending "| null"
|
|
154
|
+
* - Composition types (allOf → intersection, oneOf/anyOf → union)
|
|
67
155
|
* - Enums by creating union types of the values
|
|
68
156
|
* - OneOf schemas as union types
|
|
69
157
|
* - Basic types (string, number, boolean)
|
|
@@ -73,7 +161,7 @@ function specTitle(spec) {
|
|
|
73
161
|
* - Objects with additionalProperties as Records
|
|
74
162
|
* - Fallback to "any" for unknown types
|
|
75
163
|
*
|
|
76
|
-
* @param
|
|
164
|
+
* @param schema - The OpenAPI schema/parameter object to convert
|
|
77
165
|
* @returns The TypeScript type as a string
|
|
78
166
|
*/
|
|
79
167
|
function getTypeFromSchema(schema) {
|
|
@@ -86,6 +174,30 @@ function getTypeFromSchema(schema) {
|
|
|
86
174
|
}
|
|
87
175
|
// Add "| null" for nullable types
|
|
88
176
|
const nullable = "nullable" in schema && schema.nullable ? " | null" : "";
|
|
177
|
+
// Handle allOf (intersection types)
|
|
178
|
+
if ("allOf" in schema && schema.allOf) {
|
|
179
|
+
const types = schema.allOf.map((s) => getTypeFromSchema(s)).filter(Boolean);
|
|
180
|
+
if (types.length === 0)
|
|
181
|
+
return "unknown";
|
|
182
|
+
const result = types.length === 1 ? types[0] : `(${types.join(" & ")})`;
|
|
183
|
+
return `${result}${nullable}`;
|
|
184
|
+
}
|
|
185
|
+
// Handle oneOf (union types - exactly one)
|
|
186
|
+
if ("oneOf" in schema && schema.oneOf) {
|
|
187
|
+
const types = schema.oneOf.map((s) => getTypeFromSchema(s)).filter(Boolean);
|
|
188
|
+
if (types.length === 0)
|
|
189
|
+
return "unknown";
|
|
190
|
+
const result = types.length === 1 ? types[0] : `(${types.join(" | ")})`;
|
|
191
|
+
return `${result}${nullable}`;
|
|
192
|
+
}
|
|
193
|
+
// Handle anyOf (union types - one or more)
|
|
194
|
+
if ("anyOf" in schema && schema.anyOf) {
|
|
195
|
+
const types = schema.anyOf.map((s) => getTypeFromSchema(s)).filter(Boolean);
|
|
196
|
+
if (types.length === 0)
|
|
197
|
+
return "unknown";
|
|
198
|
+
const result = types.length === 1 ? types[0] : `(${types.join(" | ")})`;
|
|
199
|
+
return `${result}${nullable}`;
|
|
200
|
+
}
|
|
89
201
|
// Handle enums as union types
|
|
90
202
|
if ("enum" in schema && schema.enum) {
|
|
91
203
|
if (Object.values(schema.enum)?.length > 0) {
|
|
@@ -95,13 +207,6 @@ function getTypeFromSchema(schema) {
|
|
|
95
207
|
}
|
|
96
208
|
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
|
|
97
209
|
}
|
|
98
|
-
// Handle oneOf as union types
|
|
99
|
-
if ("oneOf" in schema && schema.oneOf) {
|
|
100
|
-
const unionTypes = schema.oneOf
|
|
101
|
-
.map((subSchema) => getTypeFromSchema(subSchema))
|
|
102
|
-
.filter((type) => type !== undefined);
|
|
103
|
-
return unionTypes.length > 0 ? `${unionTypes.join(" | ")}${nullable}` : `any${nullable}`;
|
|
104
|
-
}
|
|
105
210
|
// Handle types based on the "type" property
|
|
106
211
|
if ("type" in schema) {
|
|
107
212
|
switch (schema.type) {
|
|
@@ -132,8 +237,7 @@ function getTypeFromSchema(schema) {
|
|
|
132
237
|
const isDeprecated = "deprecated" in prop && prop.deprecated;
|
|
133
238
|
const hasDescription = "description" in prop && prop.description;
|
|
134
239
|
const desc = hasDescription || isDeprecated
|
|
135
|
-
? `/**${hasDescription ? `\n* ${prop.description}` : ""}${isDeprecated ? "\n* @deprecated" : ""}
|
|
136
|
-
*/\n`
|
|
240
|
+
? `/**${hasDescription ? `\n * ${prop.description}` : ""}${isDeprecated ? "\n * @deprecated" : ""}\n */\n`
|
|
137
241
|
: "";
|
|
138
242
|
return `${desc}${safeName}${isRequired ? "" : "?"}: ${propertyType};`;
|
|
139
243
|
})
|
|
@@ -143,16 +247,16 @@ function getTypeFromSchema(schema) {
|
|
|
143
247
|
// Handle objects with additionalProperties
|
|
144
248
|
if (schema.additionalProperties) {
|
|
145
249
|
const valueType = typeof schema.additionalProperties === "boolean"
|
|
146
|
-
? "
|
|
250
|
+
? "unknown"
|
|
147
251
|
: getTypeFromSchema(schema.additionalProperties);
|
|
148
252
|
return `Record<string, ${valueType}>${nullable}`;
|
|
149
253
|
}
|
|
150
254
|
// Default object type when no properties specified
|
|
151
|
-
return `Record<string,
|
|
255
|
+
return `Record<string, unknown>${nullable}`;
|
|
152
256
|
default:
|
|
153
|
-
return `
|
|
257
|
+
return `unknown${nullable}`;
|
|
154
258
|
}
|
|
155
259
|
}
|
|
156
260
|
// Fallback for schemas without a type
|
|
157
|
-
return "
|
|
261
|
+
return "unknown";
|
|
158
262
|
}
|
package/dist/utils.js.map
CHANGED
|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,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAClB,OAAO,GAAG,CAAC;QACb,CAAC;QAED,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChB,KAAK,MAAM,WAAW,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;gBACnD,IACE,WAAW,CAAC,UAAU,CAAC,kBAAkB,CAAC;oBAC1C,WAAW,CAAC,UAAU,CAAC,qBAAqB,CAAC;oBAC7C,WAAW,CAAC,UAAU,CAAC,0BAA0B,CAAC,EAClD,CAAC;oBACD,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAO,CAAC;oBAChD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;gBAC9B,CAAC;YACH,CAAC;YACD,OAAO;QACT,CAAC;QAED,OAAO;IACT,CAAC,CAAC,CACH,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAChB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Generate Axios API clients and React Query options from OpenAPI specifications",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"react-query-lightbase-codegen": "./dist/cli.js"
|
|
9
|
+
},
|
|
7
10
|
"files": ["src", "dist"],
|
|
8
11
|
"author": {
|
|
9
12
|
"name": "Oliver Winter",
|