react-query-lightbase-codegen 2.2.0 → 2.3.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/generator/clientGenerator.js +7 -6
- package/dist/generator/reactQueryGenerator.js +2 -2
- package/dist/generator/schemaGenerator.js +15 -14
- package/package.json +2 -5
- package/src/generator/clientGenerator.ts +6 -5
- package/src/generator/reactQueryGenerator.ts +2 -2
- package/src/generator/schemaGenerator.ts +14 -12
|
@@ -33,8 +33,8 @@ 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 ??
|
|
37
|
-
responseObj.content?.["application/
|
|
36
|
+
const contentType = responseObj.content?.["application/ld+json"]?.schema ??
|
|
37
|
+
responseObj.content?.["application/json"]?.schema ??
|
|
38
38
|
responseObj.content?.["application/octet-stream"]?.schema;
|
|
39
39
|
const typeName = (0, utils_1.pascalCase)(`${operationId}Response${code}`);
|
|
40
40
|
if (contentType) {
|
|
@@ -55,8 +55,8 @@ function generateAxiosMethod(operation, spec) {
|
|
|
55
55
|
? resolveSchema(requestBody.content["multipart/form-data"].schema, spec)
|
|
56
56
|
: undefined;
|
|
57
57
|
const content = requestBody && "content" in requestBody
|
|
58
|
-
? (requestBody.content?.["application/json"]?.schema ??
|
|
59
|
-
requestBody.content?.["application/
|
|
58
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
59
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
60
60
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
61
61
|
: undefined;
|
|
62
62
|
const requestBodySchema = content ? resolveSchema(content, spec) : undefined;
|
|
@@ -106,7 +106,7 @@ function generateAxiosMethod(operation, spec) {
|
|
|
106
106
|
requestBodySchema?.properties
|
|
107
107
|
? `const bodyData = {
|
|
108
108
|
${Object.entries(requestBodySchema.properties)
|
|
109
|
-
.map(([key]) =>
|
|
109
|
+
.map(([key]) => `["${key}"]: data["${key}"]`)
|
|
110
110
|
.join(",\n ")}
|
|
111
111
|
};`
|
|
112
112
|
: "",
|
|
@@ -134,9 +134,10 @@ function generateAxiosMethod(operation, spec) {
|
|
|
134
134
|
.filter(Boolean)
|
|
135
135
|
.join("\n ");
|
|
136
136
|
const requestParms = hasData
|
|
137
|
-
? `props: ${
|
|
137
|
+
? `props: ${(0, utils_1.pascalCase)(operationId)}Params & { axiosConfig?: AxiosRequestConfig; }`
|
|
138
138
|
: "props?: { axiosConfig?: AxiosRequestConfig }";
|
|
139
139
|
return `
|
|
140
|
+
${hasData ? `export type ${(0, utils_1.pascalCase)(operationId)}Params = ${dataType};` : ""}
|
|
140
141
|
${jsDocLines.join("\n ")}
|
|
141
142
|
export async function ${(0, utils_1.camelCase)(operationId)}(${requestParms}): Promise<${responseType}> {
|
|
142
143
|
${methodBody}
|
|
@@ -15,8 +15,8 @@ function generateQueryOptions(operation, spec) {
|
|
|
15
15
|
return schema.required?.map((p) => `'${p}'`) || [];
|
|
16
16
|
};
|
|
17
17
|
const content = requestBody && "content" in requestBody
|
|
18
|
-
? (requestBody.content?.["application/json"]?.schema ??
|
|
19
|
-
requestBody.content?.["application/
|
|
18
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
19
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
20
20
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
21
21
|
: undefined;
|
|
22
22
|
// Get required parameter names from both parameters and request body
|
|
@@ -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,9 +94,9 @@ 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"] ??
|
|
97
99
|
content["multipart/form-data"] ??
|
|
98
|
-
content["application/ld+json"] ??
|
|
99
100
|
content["application/octet-stream"];
|
|
100
101
|
if (jsonContent?.schema) {
|
|
101
102
|
const typeName = `${operationId}Request`;
|
|
@@ -106,8 +107,8 @@ function generateTypeDefinitions(spec) {
|
|
|
106
107
|
if (operationObject.responses) {
|
|
107
108
|
for (const [code, response] of Object.entries(operationObject.responses)) {
|
|
108
109
|
const responseObj = response;
|
|
109
|
-
const content = responseObj.content?.["application/json"] ??
|
|
110
|
-
responseObj.content?.["application/
|
|
110
|
+
const content = responseObj.content?.["application/ld+json"] ??
|
|
111
|
+
responseObj.content?.["application/json"] ??
|
|
111
112
|
responseObj.content?.["application/octet-stream"];
|
|
112
113
|
if (content?.schema) {
|
|
113
114
|
const typeName = `${operationId}Response${code}`;
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.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
|
-
"files": [
|
|
8
|
-
"src",
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
7
|
+
"files": ["src", "dist"],
|
|
11
8
|
"author": {
|
|
12
9
|
"name": "Oliver Winter",
|
|
13
10
|
"email": "owinter86@gmail.com"
|
|
@@ -51,8 +51,8 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
51
51
|
const responseObj = response as OpenAPIV3.ResponseObject;
|
|
52
52
|
const desc = "description" in responseObj ? responseObj.description : "";
|
|
53
53
|
const contentType =
|
|
54
|
-
responseObj.content?.["application/json"]?.schema ??
|
|
55
54
|
responseObj.content?.["application/ld+json"]?.schema ??
|
|
55
|
+
responseObj.content?.["application/json"]?.schema ??
|
|
56
56
|
responseObj.content?.["application/octet-stream"]?.schema;
|
|
57
57
|
|
|
58
58
|
const typeName = pascalCase(`${operationId}Response${code}`);
|
|
@@ -80,8 +80,8 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
80
80
|
|
|
81
81
|
const content =
|
|
82
82
|
requestBody && "content" in requestBody
|
|
83
|
-
? (requestBody.content?.["application/json"]?.schema ??
|
|
84
|
-
requestBody.content?.["application/
|
|
83
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
84
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
85
85
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
86
86
|
: undefined;
|
|
87
87
|
|
|
@@ -138,7 +138,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
138
138
|
requestBodySchema?.properties
|
|
139
139
|
? `const bodyData = {
|
|
140
140
|
${Object.entries(requestBodySchema.properties)
|
|
141
|
-
.map(([key]) =>
|
|
141
|
+
.map(([key]) => `["${key}"]: data["${key}"]`)
|
|
142
142
|
.join(",\n ")}
|
|
143
143
|
};`
|
|
144
144
|
: "",
|
|
@@ -167,10 +167,11 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
|
|
|
167
167
|
.join("\n ");
|
|
168
168
|
|
|
169
169
|
const requestParms = hasData
|
|
170
|
-
? `props: ${
|
|
170
|
+
? `props: ${pascalCase(operationId)}Params & { axiosConfig?: AxiosRequestConfig; }`
|
|
171
171
|
: "props?: { axiosConfig?: AxiosRequestConfig }";
|
|
172
172
|
|
|
173
173
|
return `
|
|
174
|
+
${hasData ? `export type ${pascalCase(operationId)}Params = ${dataType};` : ""}
|
|
174
175
|
${jsDocLines.join("\n ")}
|
|
175
176
|
export async function ${camelCase(operationId)}(${requestParms}): Promise<${responseType}> {
|
|
176
177
|
${methodBody}
|
|
@@ -22,8 +22,8 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
|
|
|
22
22
|
|
|
23
23
|
const content =
|
|
24
24
|
requestBody && "content" in requestBody
|
|
25
|
-
? (requestBody.content?.["application/json"]?.schema ??
|
|
26
|
-
requestBody.content?.["application/
|
|
25
|
+
? (requestBody.content?.["application/ld+json"]?.schema ??
|
|
26
|
+
requestBody.content?.["application/json"]?.schema ??
|
|
27
27
|
requestBody.content?.["application/octet-stream"]?.schema)
|
|
28
28
|
: undefined;
|
|
29
29
|
// Get required parameter names from both parameters and request body
|
|
@@ -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
|
|
|
@@ -114,9 +116,9 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
|
|
|
114
116
|
if (operationObject.requestBody) {
|
|
115
117
|
const content = (operationObject.requestBody as OpenAPIV3.RequestBodyObject).content;
|
|
116
118
|
const jsonContent =
|
|
119
|
+
content["application/ld+json"] ??
|
|
117
120
|
content["application/json"] ??
|
|
118
121
|
content["multipart/form-data"] ??
|
|
119
|
-
content["application/ld+json"] ??
|
|
120
122
|
content["application/octet-stream"];
|
|
121
123
|
if (jsonContent?.schema) {
|
|
122
124
|
const typeName = `${operationId}Request`;
|
|
@@ -129,8 +131,8 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
|
|
|
129
131
|
for (const [code, response] of Object.entries(operationObject.responses)) {
|
|
130
132
|
const responseObj = response as OpenAPIV3.ResponseObject;
|
|
131
133
|
const content =
|
|
132
|
-
responseObj.content?.["application/json"] ??
|
|
133
134
|
responseObj.content?.["application/ld+json"] ??
|
|
135
|
+
responseObj.content?.["application/json"] ??
|
|
134
136
|
responseObj.content?.["application/octet-stream"];
|
|
135
137
|
if (content?.schema) {
|
|
136
138
|
const typeName = `${operationId}Response${code}`;
|