react-query-lightbase-codegen 2.1.2 → 2.1.3
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/generator/clientGenerator.js +3 -2
- package/dist/generator/reactQueryGenerator.js +2 -2
- package/dist/generator/schemaGenerator.js +6 -6
- package/dist/utils.js +1 -1
- package/package.json +1 -1
- package/src/generator/clientGenerator.ts +5 -2
- package/src/generator/reactQueryGenerator.ts +3 -3
- package/src/generator/schemaGenerator.ts +6 -8
- package/src/utils.ts +1 -1
|
@@ -85,8 +85,9 @@ function generateAxiosMethod(operation, spec) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
// Get response type from 2xx response
|
|
88
|
-
const
|
|
89
|
-
|
|
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: [
|
|
@@ -29,7 +29,7 @@ function getTypeFromSchema(schema, context) {
|
|
|
29
29
|
return "boolean";
|
|
30
30
|
case "array": {
|
|
31
31
|
const itemType = getTypeFromSchema(schema.items, context);
|
|
32
|
-
return `Array<${itemType}>`;
|
|
32
|
+
return `Array<${(0, utils_1.sanitizeTypeName)(itemType)}>`;
|
|
33
33
|
}
|
|
34
34
|
case "object":
|
|
35
35
|
if (schema.properties) {
|
|
@@ -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 =
|
|
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 =
|
|
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 =
|
|
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
|
@@ -109,8 +109,11 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
// Get response type from 2xx response
|
|
112
|
-
|
|
113
|
-
const responseType =
|
|
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: [
|
|
@@ -38,7 +38,7 @@ function getTypeFromSchema(
|
|
|
38
38
|
return "boolean";
|
|
39
39
|
case "array": {
|
|
40
40
|
const itemType = getTypeFromSchema(schema.items, context);
|
|
41
|
-
return `Array<${itemType}>`;
|
|
41
|
+
return `Array<${sanitizeTypeName(itemType)}>`;
|
|
42
42
|
}
|
|
43
43
|
case "object":
|
|
44
44
|
if (schema.properties) {
|
|
@@ -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 =
|
|
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 =
|
|
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 =
|
|
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 {
|