react-query-lightbase-codegen 2.5.9 → 2.5.11
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.
|
@@ -2,6 +2,15 @@
|
|
|
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
|
+
}
|
|
5
14
|
function generateQueryOptions(operation, spec) {
|
|
6
15
|
const { operationId, parameters, requestBody, method } = operation;
|
|
7
16
|
const hasData = (parameters && parameters.length > 0) || operation.requestBody;
|
|
@@ -19,6 +28,12 @@ function generateQueryOptions(operation, spec) {
|
|
|
19
28
|
requestBody.content?.["application/json"]?.schema ??
|
|
20
29
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
21
30
|
: undefined;
|
|
31
|
+
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
32
|
+
// Check if request body is a primitive type (string, number, boolean)
|
|
33
|
+
const isPrimitiveRequestBody = requestBodySchema &&
|
|
34
|
+
!requestBodySchema.properties &&
|
|
35
|
+
!requestBodySchema.type?.includes("object") &&
|
|
36
|
+
!requestBodySchema.type?.includes("array");
|
|
22
37
|
// Get required parameter names from both parameters and request body
|
|
23
38
|
const requiredParams = [
|
|
24
39
|
...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
|
|
@@ -35,15 +50,34 @@ function generateQueryOptions(operation, spec) {
|
|
|
35
50
|
];
|
|
36
51
|
const namedQueryOptions = (0, utils_1.camelCase)(`get${operationId}QueryOptions`);
|
|
37
52
|
const namedQuery = (0, utils_1.camelCase)(`${operationId}`);
|
|
53
|
+
// Handle destructuring based on whether we have primitive request body
|
|
54
|
+
const destructuringLine = hasData
|
|
55
|
+
? isPrimitiveRequestBody
|
|
56
|
+
? "const { axiosConfig, data } = props || {};"
|
|
57
|
+
: "const { axiosConfig, ...params } = props || {};"
|
|
58
|
+
: "const { axiosConfig } = props || {};";
|
|
59
|
+
const paramsVariable = hasData ? (isPrimitiveRequestBody ? "data" : "params") : "";
|
|
60
|
+
const queryKeyParams = hasData ? paramsVariable : "";
|
|
61
|
+
const functionCall = hasData
|
|
62
|
+
? isPrimitiveRequestBody
|
|
63
|
+
? "{data, axiosConfig}"
|
|
64
|
+
: "{...params, axiosConfig}"
|
|
65
|
+
: "{axiosConfig}";
|
|
66
|
+
// Handle enabled logic based on request body type
|
|
67
|
+
const enabledLogic = hasData
|
|
68
|
+
? isPrimitiveRequestBody
|
|
69
|
+
? "!!data"
|
|
70
|
+
: `hasDefinedProps(${paramsVariable}, ${requiredParams.join(", ")})`
|
|
71
|
+
: "true";
|
|
38
72
|
return `
|
|
39
73
|
export const ${namedQueryOptions} = (
|
|
40
74
|
${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
|
|
41
75
|
) => {
|
|
42
|
-
${
|
|
43
|
-
const enabled = ${
|
|
76
|
+
${destructuringLine}
|
|
77
|
+
const enabled = ${enabledLogic};
|
|
44
78
|
return queryOptions({
|
|
45
|
-
queryKey: ['${(0, utils_1.camelCase)(operationId)}', ${
|
|
46
|
-
queryFn: enabled ? () => apiClient.${namedQuery}(${
|
|
79
|
+
queryKey: ['${(0, utils_1.camelCase)(operationId)}', ${queryKeyParams}],
|
|
80
|
+
queryFn: enabled ? () => apiClient.${namedQuery}(${functionCall}) : skipToken,
|
|
47
81
|
});
|
|
48
82
|
};`;
|
|
49
83
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export declare function specTitle(spec: OpenAPIV3.Document): string;
|
|
|
40
40
|
* - References ($ref) by extracting the type name
|
|
41
41
|
* - Nullable types by appending "| null"
|
|
42
42
|
* - Enums by creating union types of the values
|
|
43
|
+
* - OneOf schemas as union types
|
|
43
44
|
* - Basic types (string, number, boolean)
|
|
44
45
|
* - Binary format strings as a union with file metadata object
|
|
45
46
|
* - Arrays by recursively getting the item type
|
package/dist/utils.js
CHANGED
|
@@ -65,6 +65,7 @@ function specTitle(spec) {
|
|
|
65
65
|
* - References ($ref) by extracting the type name
|
|
66
66
|
* - Nullable types by appending "| null"
|
|
67
67
|
* - Enums by creating union types of the values
|
|
68
|
+
* - OneOf schemas as union types
|
|
68
69
|
* - Basic types (string, number, boolean)
|
|
69
70
|
* - Binary format strings as a union with file metadata object
|
|
70
71
|
* - Arrays by recursively getting the item type
|
|
@@ -94,6 +95,13 @@ function getTypeFromSchema(schema) {
|
|
|
94
95
|
}
|
|
95
96
|
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
|
|
96
97
|
}
|
|
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
|
+
}
|
|
97
105
|
// Handle types based on the "type" property
|
|
98
106
|
if ("type" in schema) {
|
|
99
107
|
switch (schema.type) {
|
package/package.json
CHANGED
|
@@ -2,6 +2,18 @@ import type { OpenAPIV3 } from "openapi-types";
|
|
|
2
2
|
import { camelCase, sanitizeTypeName, specTitle } from "../utils";
|
|
3
3
|
import type { OperationInfo } from "./clientGenerator";
|
|
4
4
|
|
|
5
|
+
function resolveSchema(
|
|
6
|
+
schema: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject | undefined,
|
|
7
|
+
spec: OpenAPIV3.Document
|
|
8
|
+
): OpenAPIV3.SchemaObject | undefined {
|
|
9
|
+
if (!schema) return undefined;
|
|
10
|
+
if ("$ref" in schema) {
|
|
11
|
+
const index = schema.$ref.split("/").pop();
|
|
12
|
+
return spec.components?.schemas?.[index as string] as OpenAPIV3.SchemaObject;
|
|
13
|
+
}
|
|
14
|
+
return schema;
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document): string {
|
|
6
18
|
const { operationId, parameters, requestBody, method } = operation;
|
|
7
19
|
|
|
@@ -26,6 +38,16 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
|
|
|
26
38
|
requestBody.content?.["application/json"]?.schema ??
|
|
27
39
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
28
40
|
: undefined;
|
|
41
|
+
|
|
42
|
+
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
43
|
+
|
|
44
|
+
// Check if request body is a primitive type (string, number, boolean)
|
|
45
|
+
const isPrimitiveRequestBody =
|
|
46
|
+
requestBodySchema &&
|
|
47
|
+
!requestBodySchema.properties &&
|
|
48
|
+
!requestBodySchema.type?.includes("object") &&
|
|
49
|
+
!requestBodySchema.type?.includes("array");
|
|
50
|
+
|
|
29
51
|
// Get required parameter names from both parameters and request body
|
|
30
52
|
const requiredParams = [
|
|
31
53
|
...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
|
|
@@ -44,15 +66,39 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
|
|
|
44
66
|
const namedQueryOptions = camelCase(`get${operationId}QueryOptions`);
|
|
45
67
|
const namedQuery = camelCase(`${operationId}`);
|
|
46
68
|
|
|
69
|
+
// Handle destructuring based on whether we have primitive request body
|
|
70
|
+
const destructuringLine = hasData
|
|
71
|
+
? isPrimitiveRequestBody
|
|
72
|
+
? "const { axiosConfig, data } = props || {};"
|
|
73
|
+
: "const { axiosConfig, ...params } = props || {};"
|
|
74
|
+
: "const { axiosConfig } = props || {};";
|
|
75
|
+
|
|
76
|
+
const paramsVariable = hasData ? (isPrimitiveRequestBody ? "data" : "params") : "";
|
|
77
|
+
|
|
78
|
+
const queryKeyParams = hasData ? paramsVariable : "";
|
|
79
|
+
|
|
80
|
+
const functionCall = hasData
|
|
81
|
+
? isPrimitiveRequestBody
|
|
82
|
+
? "{data, axiosConfig}"
|
|
83
|
+
: "{...params, axiosConfig}"
|
|
84
|
+
: "{axiosConfig}";
|
|
85
|
+
|
|
86
|
+
// Handle enabled logic based on request body type
|
|
87
|
+
const enabledLogic = hasData
|
|
88
|
+
? isPrimitiveRequestBody
|
|
89
|
+
? "!!data"
|
|
90
|
+
: `hasDefinedProps(${paramsVariable}, ${requiredParams.join(", ")})`
|
|
91
|
+
: "true";
|
|
92
|
+
|
|
47
93
|
return `
|
|
48
94
|
export const ${namedQueryOptions} = (
|
|
49
95
|
${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
|
|
50
96
|
) => {
|
|
51
|
-
${
|
|
52
|
-
const enabled = ${
|
|
97
|
+
${destructuringLine}
|
|
98
|
+
const enabled = ${enabledLogic};
|
|
53
99
|
return queryOptions({
|
|
54
|
-
queryKey: ['${camelCase(operationId)}', ${
|
|
55
|
-
queryFn: enabled ? () => apiClient.${namedQuery}(${
|
|
100
|
+
queryKey: ['${camelCase(operationId)}', ${queryKeyParams}],
|
|
101
|
+
queryFn: enabled ? () => apiClient.${namedQuery}(${functionCall}) : skipToken,
|
|
56
102
|
});
|
|
57
103
|
};`;
|
|
58
104
|
}
|
package/src/utils.ts
CHANGED
|
@@ -64,6 +64,7 @@ export function specTitle(spec: OpenAPIV3.Document): string {
|
|
|
64
64
|
* - References ($ref) by extracting the type name
|
|
65
65
|
* - Nullable types by appending "| null"
|
|
66
66
|
* - Enums by creating union types of the values
|
|
67
|
+
* - OneOf schemas as union types
|
|
67
68
|
* - Basic types (string, number, boolean)
|
|
68
69
|
* - Binary format strings as a union with file metadata object
|
|
69
70
|
* - Arrays by recursively getting the item type
|
|
@@ -99,6 +100,14 @@ export function getTypeFromSchema(
|
|
|
99
100
|
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
|
|
100
101
|
}
|
|
101
102
|
|
|
103
|
+
// Handle oneOf as union types
|
|
104
|
+
if ("oneOf" in schema && schema.oneOf) {
|
|
105
|
+
const unionTypes = schema.oneOf
|
|
106
|
+
.map((subSchema) => getTypeFromSchema(subSchema))
|
|
107
|
+
.filter((type): type is string => type !== undefined);
|
|
108
|
+
return unionTypes.length > 0 ? `${unionTypes.join(" | ")}${nullable}` : `any${nullable}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
102
111
|
// Handle types based on the "type" property
|
|
103
112
|
if ("type" in schema) {
|
|
104
113
|
switch (schema.type) {
|