react-query-lightbase-codegen 2.1.4 → 2.2.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.
- package/dist/generator/clientGenerator.js +9 -4
- package/dist/generator/reactQueryGenerator.js +7 -2
- package/dist/generator/schemaGenerator.js +18 -12
- package/package.json +2 -5
- package/src/generator/clientGenerator.ts +14 -5
- package/src/generator/reactQueryGenerator.ts +8 -2
- package/src/generator/schemaGenerator.ts +22 -13
|
@@ -33,7 +33,9 @@ function generateAxiosMethod(operation, spec) {
|
|
|
33
33
|
const [code, response] = responseDetails;
|
|
34
34
|
const responseObj = response;
|
|
35
35
|
const desc = "description" in responseObj ? responseObj.description : "";
|
|
36
|
-
const contentType = responseObj.content?.["application/json"]?.schema
|
|
36
|
+
const contentType = responseObj.content?.["application/ld+json"]?.schema ??
|
|
37
|
+
responseObj.content?.["application/json"]?.schema ??
|
|
38
|
+
responseObj.content?.["application/octet-stream"]?.schema;
|
|
37
39
|
const typeName = (0, utils_1.pascalCase)(`${operationId}Response${code}`);
|
|
38
40
|
if (contentType) {
|
|
39
41
|
if (desc) {
|
|
@@ -52,9 +54,12 @@ function generateAxiosMethod(operation, spec) {
|
|
|
52
54
|
const formDataSchema = isFormData
|
|
53
55
|
? resolveSchema(requestBody.content["multipart/form-data"].schema, spec)
|
|
54
56
|
: undefined;
|
|
55
|
-
const
|
|
56
|
-
?
|
|
57
|
+
const content = requestBody && "content" in requestBody
|
|
58
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
59
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
60
|
+
requestBody.content?.["application/octet-stream"]?.schema)
|
|
57
61
|
: undefined;
|
|
62
|
+
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
58
63
|
// Build data type parts
|
|
59
64
|
const dataProps = [];
|
|
60
65
|
// Add path and query parameters
|
|
@@ -101,7 +106,7 @@ function generateAxiosMethod(operation, spec) {
|
|
|
101
106
|
requestBodySchema?.properties
|
|
102
107
|
? `const bodyData = {
|
|
103
108
|
${Object.entries(requestBodySchema.properties)
|
|
104
|
-
.map(([key]) =>
|
|
109
|
+
.map(([key]) => `["${key}"]: data["${key}"]`)
|
|
105
110
|
.join(",\n ")}
|
|
106
111
|
};`
|
|
107
112
|
: "",
|
|
@@ -14,11 +14,16 @@ function generateQueryOptions(operation, spec) {
|
|
|
14
14
|
}
|
|
15
15
|
return schema.required?.map((p) => `'${p}'`) || [];
|
|
16
16
|
};
|
|
17
|
+
const content = requestBody && "content" in requestBody
|
|
18
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
19
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
20
|
+
requestBody.content?.["application/octet-stream"]?.schema)
|
|
21
|
+
: undefined;
|
|
17
22
|
// Get required parameter names from both parameters and request body
|
|
18
23
|
const requiredParams = [
|
|
19
24
|
...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
|
|
20
|
-
...(
|
|
21
|
-
? getRequiredFields(
|
|
25
|
+
...(content
|
|
26
|
+
? getRequiredFields(content, {
|
|
22
27
|
schemas: spec.components?.schemas || {},
|
|
23
28
|
})
|
|
24
29
|
: []),
|
|
@@ -12,24 +12,25 @@ function getTypeFromSchema(schema, context) {
|
|
|
12
12
|
const refType = schema.$ref.split("/").pop();
|
|
13
13
|
return (0, utils_1.sanitizeTypeName)(refType);
|
|
14
14
|
}
|
|
15
|
+
const nullable = schema.nullable ? " | null" : "";
|
|
15
16
|
// Handle enum types properly
|
|
16
17
|
if (schema.enum) {
|
|
17
|
-
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ");
|
|
18
|
+
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
|
|
18
19
|
}
|
|
19
20
|
switch (schema.type) {
|
|
20
21
|
case "string":
|
|
21
22
|
if ("format" in schema && schema.format === "binary") {
|
|
22
|
-
return
|
|
23
|
+
return `string | { name?: string; type?: string; uri: string }${nullable}`;
|
|
23
24
|
}
|
|
24
|
-
return
|
|
25
|
+
return `string${nullable}`;
|
|
25
26
|
case "number":
|
|
26
27
|
case "integer":
|
|
27
|
-
return
|
|
28
|
+
return `number${nullable}`;
|
|
28
29
|
case "boolean":
|
|
29
|
-
return
|
|
30
|
+
return `boolean${nullable}`;
|
|
30
31
|
case "array": {
|
|
31
32
|
const itemType = getTypeFromSchema(schema.items, context);
|
|
32
|
-
return `Array<${itemType}
|
|
33
|
+
return `Array<${itemType}>${nullable}`;
|
|
33
34
|
}
|
|
34
35
|
case "object":
|
|
35
36
|
if (schema.properties) {
|
|
@@ -41,17 +42,17 @@ function getTypeFromSchema(schema, context) {
|
|
|
41
42
|
return ` ${safeName}${isRequired ? "" : "?"}: ${propertyType};`;
|
|
42
43
|
})
|
|
43
44
|
.join("\n");
|
|
44
|
-
return `{
|
|
45
|
+
return `{${properties}\n}${nullable}`;
|
|
45
46
|
}
|
|
46
47
|
if (schema.additionalProperties) {
|
|
47
48
|
const valueType = typeof schema.additionalProperties === "boolean"
|
|
48
49
|
? "any"
|
|
49
50
|
: getTypeFromSchema(schema.additionalProperties, context);
|
|
50
|
-
return `Record<string, ${valueType}
|
|
51
|
+
return `Record<string, ${valueType}>${nullable}`;
|
|
51
52
|
}
|
|
52
|
-
return
|
|
53
|
+
return `Record<string, any>${nullable}`;
|
|
53
54
|
default:
|
|
54
|
-
return
|
|
55
|
+
return `any${nullable}`;
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
function generateTypeDefinition(name, schema, context) {
|
|
@@ -93,7 +94,10 @@ function generateTypeDefinitions(spec) {
|
|
|
93
94
|
// Generate request body type
|
|
94
95
|
if (operationObject.requestBody) {
|
|
95
96
|
const content = operationObject.requestBody.content;
|
|
96
|
-
const jsonContent = content["application/json"]
|
|
97
|
+
const jsonContent = content["application/ld+json"] ??
|
|
98
|
+
content["application/json"] ??
|
|
99
|
+
content["multipart/form-data"] ??
|
|
100
|
+
content["application/octet-stream"];
|
|
97
101
|
if (jsonContent?.schema) {
|
|
98
102
|
const typeName = `${operationId}Request`;
|
|
99
103
|
output += generateTypeDefinition(typeName, jsonContent.schema, context);
|
|
@@ -103,7 +107,9 @@ function generateTypeDefinitions(spec) {
|
|
|
103
107
|
if (operationObject.responses) {
|
|
104
108
|
for (const [code, response] of Object.entries(operationObject.responses)) {
|
|
105
109
|
const responseObj = response;
|
|
106
|
-
const content = responseObj.content?.["application/json"]
|
|
110
|
+
const content = responseObj.content?.["application/ld+json"] ??
|
|
111
|
+
responseObj.content?.["application/json"] ??
|
|
112
|
+
responseObj.content?.["application/octet-stream"];
|
|
107
113
|
if (content?.schema) {
|
|
108
114
|
const typeName = `${operationId}Response${code}`;
|
|
109
115
|
output += generateTypeDefinition(typeName, content.schema, context);
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.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",
|
|
7
|
-
"files": [
|
|
8
|
-
"src",
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
7
|
+
"files": ["src", "dist"],
|
|
11
8
|
"author": {
|
|
12
9
|
"name": "Oliver Winter",
|
|
13
10
|
"email": "owinter86@gmail.com"
|
|
@@ -50,7 +50,11 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
50
50
|
const [code, response] = responseDetails;
|
|
51
51
|
const responseObj = response as OpenAPIV3.ResponseObject;
|
|
52
52
|
const desc = "description" in responseObj ? responseObj.description : "";
|
|
53
|
-
const contentType =
|
|
53
|
+
const contentType =
|
|
54
|
+
responseObj.content?.["application/ld+json"]?.schema ??
|
|
55
|
+
responseObj.content?.["application/json"]?.schema ??
|
|
56
|
+
responseObj.content?.["application/octet-stream"]?.schema;
|
|
57
|
+
|
|
54
58
|
const typeName = pascalCase(`${operationId}Response${code}`);
|
|
55
59
|
|
|
56
60
|
if (contentType) {
|
|
@@ -74,9 +78,14 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
74
78
|
? resolveSchema(requestBody.content["multipart/form-data"].schema, spec)
|
|
75
79
|
: undefined;
|
|
76
80
|
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
const content =
|
|
82
|
+
requestBody && "content" in requestBody
|
|
83
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
84
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
85
|
+
requestBody.content?.["application/octet-stream"]?.schema)
|
|
86
|
+
: undefined;
|
|
87
|
+
|
|
88
|
+
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
80
89
|
|
|
81
90
|
// Build data type parts
|
|
82
91
|
const dataProps: string[] = [];
|
|
@@ -129,7 +138,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
129
138
|
requestBodySchema?.properties
|
|
130
139
|
? `const bodyData = {
|
|
131
140
|
${Object.entries(requestBodySchema.properties)
|
|
132
|
-
.map(([key]) =>
|
|
141
|
+
.map(([key]) => `["${key}"]: data["${key}"]`)
|
|
133
142
|
.join(",\n ")}
|
|
134
143
|
};`
|
|
135
144
|
: "",
|
|
@@ -20,11 +20,17 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
|
|
|
20
20
|
return schema.required?.map((p) => `'${p}'`) || [];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
const content =
|
|
24
|
+
requestBody && "content" in requestBody
|
|
25
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
26
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
27
|
+
requestBody.content?.["application/octet-stream"]?.schema)
|
|
28
|
+
: undefined;
|
|
23
29
|
// Get required parameter names from both parameters and request body
|
|
24
30
|
const requiredParams = [
|
|
25
31
|
...(parameters?.filter((p) => p.required).map((p) => `'${p.name}'`) || []),
|
|
26
|
-
...(
|
|
27
|
-
? getRequiredFields(
|
|
32
|
+
...(content
|
|
33
|
+
? getRequiredFields(content, {
|
|
28
34
|
schemas: (spec.components?.schemas as { [key: string]: OpenAPIV3.SchemaObject }) || {},
|
|
29
35
|
})
|
|
30
36
|
: []),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { OpenAPIV3 } from "openapi-types";
|
|
2
|
-
import {
|
|
2
|
+
import { sanitizePropertyName, sanitizeTypeName } from "../utils";
|
|
3
3
|
|
|
4
4
|
interface SchemaContext {
|
|
5
5
|
schemas: { [key: string]: OpenAPIV3.SchemaObject };
|
|
@@ -19,26 +19,28 @@ function getTypeFromSchema(
|
|
|
19
19
|
const refType = schema.$ref.split("/").pop();
|
|
20
20
|
return sanitizeTypeName(refType as string);
|
|
21
21
|
}
|
|
22
|
+
const nullable = schema.nullable ? " | null" : "";
|
|
22
23
|
|
|
23
24
|
// Handle enum types properly
|
|
24
25
|
if (schema.enum) {
|
|
25
|
-
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ");
|
|
26
|
+
return schema.enum.map((e) => (typeof e === "string" ? `'${e}'` : e)).join(" | ") + nullable;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
switch (schema.type) {
|
|
29
30
|
case "string":
|
|
30
31
|
if ("format" in schema && schema.format === "binary") {
|
|
31
|
-
return
|
|
32
|
+
return `string | { name?: string; type?: string; uri: string }${nullable}`;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
+
|
|
35
|
+
return `string${nullable}`;
|
|
34
36
|
case "number":
|
|
35
37
|
case "integer":
|
|
36
|
-
return
|
|
38
|
+
return `number${nullable}`;
|
|
37
39
|
case "boolean":
|
|
38
|
-
return
|
|
40
|
+
return `boolean${nullable}`;
|
|
39
41
|
case "array": {
|
|
40
42
|
const itemType = getTypeFromSchema(schema.items, context);
|
|
41
|
-
return `Array<${itemType}
|
|
43
|
+
return `Array<${itemType}>${nullable}`;
|
|
42
44
|
}
|
|
43
45
|
case "object":
|
|
44
46
|
if (schema.properties) {
|
|
@@ -50,18 +52,18 @@ function getTypeFromSchema(
|
|
|
50
52
|
return ` ${safeName}${isRequired ? "" : "?"}: ${propertyType};`;
|
|
51
53
|
})
|
|
52
54
|
.join("\n");
|
|
53
|
-
return `{
|
|
55
|
+
return `{${properties}\n}${nullable}`;
|
|
54
56
|
}
|
|
55
57
|
if (schema.additionalProperties) {
|
|
56
58
|
const valueType =
|
|
57
59
|
typeof schema.additionalProperties === "boolean"
|
|
58
60
|
? "any"
|
|
59
61
|
: getTypeFromSchema(schema.additionalProperties, context);
|
|
60
|
-
return `Record<string, ${valueType}
|
|
62
|
+
return `Record<string, ${valueType}>${nullable}`;
|
|
61
63
|
}
|
|
62
|
-
return
|
|
64
|
+
return `Record<string, any>${nullable}`;
|
|
63
65
|
default:
|
|
64
|
-
return
|
|
66
|
+
return `any${nullable}`;
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
|
@@ -113,7 +115,11 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
|
|
|
113
115
|
// Generate request body type
|
|
114
116
|
if (operationObject.requestBody) {
|
|
115
117
|
const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
|
|
116
|
-
const jsonContent =
|
|
118
|
+
const jsonContent =
|
|
119
|
+
content["application/ld+json"] ??
|
|
120
|
+
content["application/json"] ??
|
|
121
|
+
content["multipart/form-data"] ??
|
|
122
|
+
content["application/octet-stream"];
|
|
117
123
|
if (jsonContent?.schema) {
|
|
118
124
|
const typeName = `${operationId}Request`;
|
|
119
125
|
output += generateTypeDefinition(typeName, jsonContent.schema as OpenAPIV3.SchemaObject, context);
|
|
@@ -124,7 +130,10 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
|
|
|
124
130
|
if (operationObject.responses) {
|
|
125
131
|
for (const [code, response] of Object.entries(operationObject.responses)) {
|
|
126
132
|
const responseObj = response as OpenAPIV3.ResponseObject;
|
|
127
|
-
const content =
|
|
133
|
+
const content =
|
|
134
|
+
responseObj.content?.["application/ld+json"] ??
|
|
135
|
+
responseObj.content?.["application/json"] ??
|
|
136
|
+
responseObj.content?.["application/octet-stream"];
|
|
128
137
|
if (content?.schema) {
|
|
129
138
|
const typeName = `${operationId}Response${code}`;
|
|
130
139
|
output += generateTypeDefinition(typeName, content.schema as OpenAPIV3.SchemaObject, context);
|