@accelbyte/codegen 1.0.0-alpha.3 → 1.0.0-alpha.6
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/LICENSE +19 -0
- package/dist/accelbyte-codegen.js +246 -214
- package/dist/accelbyte-codegen.js.map +1 -1
- package/dist/accelbyte-codegen.mjs +212 -170
- package/dist/accelbyte-codegen.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -53,6 +53,86 @@ class CliParser {
|
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
const Schema = z.object({
|
|
57
|
+
$ref: z.string().nullish(),
|
|
58
|
+
type: z.union([
|
|
59
|
+
z.literal("array"),
|
|
60
|
+
z.literal("object"),
|
|
61
|
+
z.literal("file"),
|
|
62
|
+
z.literal("string"),
|
|
63
|
+
z.literal("boolean")
|
|
64
|
+
]).nullish(),
|
|
65
|
+
items: z.object({
|
|
66
|
+
$ref: z.string().nullish(),
|
|
67
|
+
type: z.string().nullish()
|
|
68
|
+
}).nullish(),
|
|
69
|
+
properties: z.array(z.string()).nullish(),
|
|
70
|
+
description: z.string().nullish(),
|
|
71
|
+
additionalProperties: z.object({
|
|
72
|
+
type: z.string().nullish()
|
|
73
|
+
}).nullish()
|
|
74
|
+
});
|
|
75
|
+
const Definition = z.object({
|
|
76
|
+
required: z.array(z.string()).nullish(),
|
|
77
|
+
properties: z.record(z.object({
|
|
78
|
+
type: z.string()
|
|
79
|
+
})).nullish()
|
|
80
|
+
});
|
|
81
|
+
const Definitions = z.record(Definition);
|
|
82
|
+
const EndpointParametersType = z.enum(["apiKey", "boolean", "int", "integer", "number", "string", "array", "file"]);
|
|
83
|
+
const EndpointParametersIn = z.enum(["body", "formData", "header", "path", "query"]);
|
|
84
|
+
const EndpointParameters = z.object({
|
|
85
|
+
type: EndpointParametersType.nullish(),
|
|
86
|
+
description: z.string().nullish(),
|
|
87
|
+
name: z.string(),
|
|
88
|
+
in: EndpointParametersIn,
|
|
89
|
+
required: z.boolean().nullish(),
|
|
90
|
+
schema: Schema.nullish(),
|
|
91
|
+
default: z.union([z.boolean(), z.string(), z.number()]).nullish(),
|
|
92
|
+
enum: z.array(z.union([z.boolean(), z.string(), z.number()])).nullish(),
|
|
93
|
+
items: z.object({
|
|
94
|
+
type: z.string()
|
|
95
|
+
}).nullish()
|
|
96
|
+
});
|
|
97
|
+
const Endpoint = z.object({
|
|
98
|
+
description: z.string().nullish(),
|
|
99
|
+
consumes: z.array(z.string()).nullish(),
|
|
100
|
+
produces: z.array(z.string()).nullish(),
|
|
101
|
+
tags: z.array(z.string()).nullish(),
|
|
102
|
+
summary: z.string().nullish(),
|
|
103
|
+
operationId: z.string(),
|
|
104
|
+
deprecated: z.boolean().nullish(),
|
|
105
|
+
responses: z.record(z.object({
|
|
106
|
+
description: z.string().nullish(),
|
|
107
|
+
schema: Schema.nullish()
|
|
108
|
+
})),
|
|
109
|
+
parameters: z.array(EndpointParameters).nullish()
|
|
110
|
+
});
|
|
111
|
+
const Operation = z.object({
|
|
112
|
+
get: Endpoint.nullish(),
|
|
113
|
+
post: Endpoint.nullish(),
|
|
114
|
+
patch: Endpoint.nullish(),
|
|
115
|
+
delete: Endpoint.nullish(),
|
|
116
|
+
put: Endpoint.nullish()
|
|
117
|
+
});
|
|
118
|
+
const Paths = z.record(Operation);
|
|
119
|
+
z.object({
|
|
120
|
+
paths: Paths,
|
|
121
|
+
definitions: Definitions,
|
|
122
|
+
basePath: z.string(),
|
|
123
|
+
info: z.object({
|
|
124
|
+
description: z.string(),
|
|
125
|
+
title: z.string(),
|
|
126
|
+
contact: z.object({
|
|
127
|
+
name: z.string(),
|
|
128
|
+
url: z.string(),
|
|
129
|
+
email: z.string()
|
|
130
|
+
}),
|
|
131
|
+
version: z.string()
|
|
132
|
+
}),
|
|
133
|
+
schemes: z.array(z.string()).nullish()
|
|
134
|
+
});
|
|
135
|
+
|
|
56
136
|
const getImportableVarMap = () => ({
|
|
57
137
|
"@accelbyte/sdk": ["CodeGenUtil", "SdkCache", "IResponse", "IResponseWithSync", "Validate"],
|
|
58
138
|
axios: ["AxiosRequestConfig", "AxiosResponse"],
|
|
@@ -98,44 +178,30 @@ ${body}
|
|
|
98
178
|
`.replace(/, \)/g, ")").trim();
|
|
99
179
|
|
|
100
180
|
class ParserUtils {
|
|
101
|
-
static parseQueryParamAttributeDefault = (
|
|
102
|
-
const attrName = name.slice(name.lastIndexOf(".") + 1);
|
|
181
|
+
static parseQueryParamAttributeDefault = (definition) => {
|
|
182
|
+
const attrName = definition.name.slice(definition.name.lastIndexOf(".") + 1);
|
|
103
183
|
const defaultValue = definition.type === "string" ? `'${definition.default}'` : definition.default;
|
|
104
184
|
return `${attrName}: ${defaultValue}`;
|
|
105
185
|
};
|
|
106
|
-
static parseType = (
|
|
107
|
-
if (type === "integer")
|
|
186
|
+
static parseType = (pathParam) => {
|
|
187
|
+
if (pathParam.type === "int" || pathParam.type === "integer")
|
|
108
188
|
return "number";
|
|
109
|
-
if (type === "array")
|
|
110
|
-
return "any[]
|
|
111
|
-
return type;
|
|
189
|
+
if (pathParam.type === "array")
|
|
190
|
+
return `${pathParam.items.type ?? "any"}[]`;
|
|
191
|
+
return pathParam.type;
|
|
112
192
|
};
|
|
113
193
|
static parseQueryParamsType = (queryParams) => {
|
|
114
|
-
return queryParams.map((
|
|
194
|
+
return queryParams.map((queryParam) => ParserUtils.parseAttributeType(queryParam)).join(",");
|
|
115
195
|
};
|
|
116
196
|
static isAnyQueryParamRequired = (queryParams) => {
|
|
117
|
-
queryParams.
|
|
118
|
-
if (p.required) {
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
return false;
|
|
197
|
+
return queryParams.some((queryParam) => queryParam.required);
|
|
123
198
|
};
|
|
124
199
|
static parseQueryParamsDefault = (queryParams) => {
|
|
125
|
-
const result = queryParams.filter((
|
|
200
|
+
const result = queryParams.filter((queryParam) => !!queryParam.default && !queryParam.required).map(ParserUtils.parseQueryParamAttributeDefault).join(",");
|
|
126
201
|
return result ? `${result},` : "";
|
|
127
202
|
};
|
|
128
203
|
static parseBodyParamsImports = (bodyParams) => {
|
|
129
|
-
|
|
130
|
-
if (p?.schema?.$ref)
|
|
131
|
-
return ParserUtils.parseRefImport(p.schema.$ref, p);
|
|
132
|
-
if (p?.schema?.items?.$ref)
|
|
133
|
-
return ParserUtils.parseRefImport(p.schema.items.$ref, p);
|
|
134
|
-
if (p?.$ref)
|
|
135
|
-
return ParserUtils.parseRefImport(p.$ref, p);
|
|
136
|
-
return null;
|
|
137
|
-
}).filter((p) => !!p);
|
|
138
|
-
return ret;
|
|
204
|
+
return bodyParams.map(ParserUtils.parseRefImport).filter(Boolean);
|
|
139
205
|
};
|
|
140
206
|
static parseImportDir = ($ref) => {
|
|
141
207
|
let ref = $ref.replace(".", "/");
|
|
@@ -148,7 +214,11 @@ class ParserUtils {
|
|
|
148
214
|
return ref.slice(0, ref.lastIndexOf("/")).replace("#", ".");
|
|
149
215
|
}
|
|
150
216
|
};
|
|
151
|
-
static parseRefImport = (
|
|
217
|
+
static parseRefImport = (bodyParam) => {
|
|
218
|
+
const $ref = bodyParam?.schema?.$ref || bodyParam?.schema?.items?.$ref;
|
|
219
|
+
if (!$ref) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
152
222
|
const type = ParserUtils.parseRefType($ref);
|
|
153
223
|
return `import { ${type} } from './definitions/${type}'`;
|
|
154
224
|
};
|
|
@@ -158,16 +228,16 @@ class ParserUtils {
|
|
|
158
228
|
ref = ref.slice(0, -1);
|
|
159
229
|
}
|
|
160
230
|
const val = ref.slice(ref.lastIndexOf("/") + 1);
|
|
161
|
-
return _.upperFirst(_.camelCase(val));
|
|
231
|
+
return _.upperFirst(_.camelCase(val)).replace(/( \w)/g, (group) => group.replace(" ", "").toUpperCase());
|
|
162
232
|
};
|
|
163
|
-
static parseAttributeType = (
|
|
233
|
+
static parseAttributeType = (definition) => {
|
|
164
234
|
const required = definition.required ? "" : "?";
|
|
165
|
-
const attrName = name.slice(name.lastIndexOf(".") + 1);
|
|
166
|
-
if (definition.
|
|
167
|
-
const enums = definition.
|
|
235
|
+
const attrName = definition.name.slice(definition.name.lastIndexOf(".") + 1);
|
|
236
|
+
if (definition.enum) {
|
|
237
|
+
const enums = definition.enum.map((enm) => definition.type === "string" ? `'${enm}'` : enm).join(" | ");
|
|
168
238
|
return `${attrName}${required}: ${enums}`;
|
|
169
239
|
}
|
|
170
|
-
if (definition.type && definition
|
|
240
|
+
if (definition.type && ParserUtils.parseType(definition) === "number") {
|
|
171
241
|
return `${attrName}${required}: number`;
|
|
172
242
|
}
|
|
173
243
|
if (definition.type && definition.type === "array") {
|
|
@@ -179,29 +249,29 @@ class ParserUtils {
|
|
|
179
249
|
return `${attrName}${required}: any`;
|
|
180
250
|
};
|
|
181
251
|
static parseBodyParamsType = (bodyParams) => {
|
|
182
|
-
const [
|
|
183
|
-
if (!
|
|
252
|
+
const [bodyParam] = bodyParams;
|
|
253
|
+
if (!bodyParam)
|
|
184
254
|
return null;
|
|
185
|
-
if (bodyParams.length > 0 &&
|
|
186
|
-
let retBodyParams = `{${bodyParams.map((
|
|
255
|
+
if (bodyParams.length > 0 && bodyParam?.name !== "body" && !bodyParam?.schema) {
|
|
256
|
+
let retBodyParams = `{${bodyParams.map((bodyParam2) => ParserUtils.parseAttributeType(bodyParam2)).join(",")}}`;
|
|
187
257
|
retBodyParams = retBodyParams.replace("file?: file", "file?: File");
|
|
188
258
|
return retBodyParams;
|
|
189
259
|
}
|
|
190
|
-
if (
|
|
191
|
-
return `${
|
|
260
|
+
if (bodyParam?.schema?.type === "array" && !bodyParam?.schema?.items?.$ref) {
|
|
261
|
+
return `${bodyParam.schema.items.type ?? "any"}[]`;
|
|
192
262
|
}
|
|
193
|
-
if (
|
|
194
|
-
return `${ParserUtils.parseRefType(
|
|
263
|
+
if (bodyParam?.schema?.type === "array" && bodyParam?.schema?.items?.$ref) {
|
|
264
|
+
return `${ParserUtils.parseRefType(bodyParam.schema.items.$ref)}[]`;
|
|
195
265
|
}
|
|
196
|
-
if (
|
|
197
|
-
return ParserUtils.parseRefType(
|
|
266
|
+
if (bodyParam?.schema.$ref) {
|
|
267
|
+
return ParserUtils.parseRefType(bodyParam.schema.$ref);
|
|
198
268
|
}
|
|
199
|
-
if (
|
|
269
|
+
if (bodyParam?.schema?.additionalProperties?.type === "object") {
|
|
200
270
|
return "any";
|
|
201
271
|
}
|
|
202
272
|
return null;
|
|
203
273
|
};
|
|
204
|
-
static get2xxResponse(methodEntity
|
|
274
|
+
static get2xxResponse(methodEntity) {
|
|
205
275
|
const keys = Object.keys(methodEntity);
|
|
206
276
|
let responseClass = null;
|
|
207
277
|
keys.forEach((key) => {
|
|
@@ -226,24 +296,23 @@ class ParserUtils {
|
|
|
226
296
|
}
|
|
227
297
|
return contentTypes.includes("application/x-www-form-urlencoded");
|
|
228
298
|
}
|
|
229
|
-
static
|
|
230
|
-
if (!
|
|
299
|
+
static filterPathParams(parameters) {
|
|
300
|
+
if (!parameters) {
|
|
231
301
|
return [];
|
|
232
302
|
}
|
|
233
|
-
|
|
234
|
-
for (const p of parametersArray) {
|
|
235
|
-
if (p.in === "path") {
|
|
236
|
-
res.push(p);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return res;
|
|
303
|
+
return parameters.filter((parameter) => parameter.in === "path");
|
|
240
304
|
}
|
|
241
|
-
static generateClassMethod(
|
|
305
|
+
static generateClassMethod({
|
|
306
|
+
path: path2,
|
|
307
|
+
endpoint,
|
|
308
|
+
httpMethod,
|
|
309
|
+
className
|
|
310
|
+
}) {
|
|
242
311
|
let replacedIdsPath = path2;
|
|
243
|
-
if (
|
|
244
|
-
for (const
|
|
245
|
-
if (
|
|
246
|
-
replacedIdsPath = replacedIdsPath.replace("{" +
|
|
312
|
+
if (endpoint.parameters) {
|
|
313
|
+
for (const parameter of endpoint.parameters) {
|
|
314
|
+
if (parameter.in === "path") {
|
|
315
|
+
replacedIdsPath = replacedIdsPath.replace("{" + parameter.name + "}", "By" + ParserUtils.toTitleCaseWord(parameter.name));
|
|
247
316
|
replacedIdsPath = replacedIdsPath.replace("/iam", "");
|
|
248
317
|
replacedIdsPath = replacedIdsPath.replace("/odin-config", "");
|
|
249
318
|
}
|
|
@@ -261,24 +330,17 @@ class ParserUtils {
|
|
|
261
330
|
}
|
|
262
331
|
return classMethod;
|
|
263
332
|
}
|
|
264
|
-
static
|
|
265
|
-
if (
|
|
266
|
-
return
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return parametersArray.filter((p) => p.in === "body" || p.in === "formData");
|
|
333
|
+
static filterBodyParams(parameters) {
|
|
334
|
+
if (Array.isArray(parameters) && parameters.length > 0) {
|
|
335
|
+
return parameters.filter((parameter) => parameter.in === "body" || parameter.in === "formData");
|
|
336
|
+
}
|
|
337
|
+
return [];
|
|
270
338
|
}
|
|
271
|
-
static
|
|
272
|
-
if (!
|
|
339
|
+
static filterQueryParameters(parameters) {
|
|
340
|
+
if (!parameters) {
|
|
273
341
|
return [];
|
|
274
342
|
}
|
|
275
|
-
|
|
276
|
-
for (const p of parametersArray) {
|
|
277
|
-
if (p.in === "query") {
|
|
278
|
-
res.push(p);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return res;
|
|
343
|
+
return parameters.filter((parameter) => parameter.in === "query");
|
|
282
344
|
}
|
|
283
345
|
static mkdirIfNotExist(dirToCreate) {
|
|
284
346
|
if (!fs__default.existsSync(dirToCreate)) {
|
|
@@ -337,13 +399,6 @@ class ParserUtils {
|
|
|
337
399
|
const { newDocument } = applyPatch(swaggerContent, swaggerPatchFileContent);
|
|
338
400
|
fs__default.writeFileSync(swaggerPatchedFilePath, JSON.stringify(newDocument, null, 2));
|
|
339
401
|
}
|
|
340
|
-
static mapKeys(map_) {
|
|
341
|
-
const methods_ = [];
|
|
342
|
-
for (const m in map_) {
|
|
343
|
-
methods_.push(m);
|
|
344
|
-
}
|
|
345
|
-
return methods_;
|
|
346
|
-
}
|
|
347
402
|
static getRelativePathToWebSdkSrcFolder(srcFolder, targetSrcFolder) {
|
|
348
403
|
const replaced = srcFolder.replace(/\\/g, "/");
|
|
349
404
|
return replaced.replace(/\\/g, "/").replace(targetSrcFolder, "./");
|
|
@@ -359,7 +414,7 @@ ${content}`;
|
|
|
359
414
|
};
|
|
360
415
|
}
|
|
361
416
|
|
|
362
|
-
const templateJsdocMethod = (classMethod, httpMethod, path, pathParams, bodyParams, queryParams) => {
|
|
417
|
+
const templateJsdocMethod = ({ classMethod, httpMethod, path, pathParams, bodyParams, queryParams }) => {
|
|
363
418
|
let jsdoc = "";
|
|
364
419
|
let methodSignature = "";
|
|
365
420
|
let newPath = path;
|
|
@@ -396,21 +451,31 @@ const templateJsdocMethod = (classMethod, httpMethod, path, pathParams, bodyPara
|
|
|
396
451
|
`;
|
|
397
452
|
};
|
|
398
453
|
|
|
399
|
-
const templateMethod = (
|
|
454
|
+
const templateMethod = ({
|
|
455
|
+
classMethod,
|
|
456
|
+
description,
|
|
457
|
+
httpMethod,
|
|
458
|
+
path,
|
|
459
|
+
pathParams,
|
|
460
|
+
bodyParams,
|
|
461
|
+
queryParams,
|
|
462
|
+
isFormUrlEncoded,
|
|
463
|
+
responseClass
|
|
464
|
+
}) => {
|
|
400
465
|
let methodSignature = "";
|
|
401
466
|
let newPath = `'${path}'`;
|
|
402
467
|
let dependencies = [];
|
|
403
|
-
for (const
|
|
404
|
-
const type = ParserUtils.parseType(
|
|
405
|
-
if (
|
|
406
|
-
methodSignature +=
|
|
468
|
+
for (const pathParam of pathParams) {
|
|
469
|
+
const type = ParserUtils.parseType(pathParam);
|
|
470
|
+
if (pathParam.name !== "namespace") {
|
|
471
|
+
methodSignature += pathParam.name + `:${type}, `;
|
|
407
472
|
}
|
|
408
|
-
const pName =
|
|
409
|
-
if (path.match(`{${
|
|
473
|
+
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
474
|
+
if (path.match(`{${pathParam.name}}`)) {
|
|
410
475
|
if (type === "string") {
|
|
411
|
-
newPath = `${newPath}.replace('{${
|
|
476
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
412
477
|
} else {
|
|
413
|
-
newPath = `${newPath}.replace('{${
|
|
478
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', String(${pName}))`;
|
|
414
479
|
}
|
|
415
480
|
}
|
|
416
481
|
}
|
|
@@ -420,8 +485,8 @@ const templateMethod = (classMethod, description, httpMethod, path, pathParams,
|
|
|
420
485
|
dependencies = ParserUtils.parseBodyParamsImports(bodyParams);
|
|
421
486
|
methodSignature += dataType ? `data: ${dataType},` : "";
|
|
422
487
|
}
|
|
423
|
-
ParserUtils.isAnyQueryParamRequired(queryParams);
|
|
424
|
-
const queryParamsType = queryParams.length ? `queryParams${"?"}: {${ParserUtils.parseQueryParamsType(queryParams)}}` : "";
|
|
488
|
+
const isAnyRequired = ParserUtils.isAnyQueryParamRequired(queryParams);
|
|
489
|
+
const queryParamsType = queryParams.length ? `queryParams${isAnyRequired ? "" : "?"}: {${ParserUtils.parseQueryParamsType(queryParams)}}` : "";
|
|
425
490
|
const queryParamsDefault = queryParams.length ? `const params = {${ParserUtils.parseQueryParamsDefault(queryParams)} ...queryParams} as SDKRequestConfig` : "const params = {} as SDKRequestConfig";
|
|
426
491
|
const isPostPutPatch = ["post", "put", "patch"].includes(httpMethod);
|
|
427
492
|
const isDelete = ["delete"].includes(httpMethod);
|
|
@@ -446,74 +511,30 @@ const templateMethod = (classMethod, description, httpMethod, path, pathParams,
|
|
|
446
511
|
const parameters = (queryParamsType ? `${methodSignature} ${queryParamsType}` : methodSignature).replace(/,\s*$/, "");
|
|
447
512
|
let methodImpl = "";
|
|
448
513
|
const isCacheFetch = ["get"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
449
|
-
const isCacheFetchUnknown = ["get"].includes(httpMethod) && resolvedResponseClass === "unknown";
|
|
450
514
|
const cachedFetchMethod = classMethod.replace("get", "fetch");
|
|
451
515
|
const deprecateTag = isCacheFetch ? `/**
|
|
452
516
|
* @deprecated Use "${cachedFetchMethod}()" instead.
|
|
453
517
|
*/` : "";
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
518
|
+
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
519
|
+
const methodName = httpMethod === "get" ? cachedFetchMethod : ["post", "put", "patch", "delete"].includes(httpMethod) ? classMethod : "";
|
|
520
|
+
const methodGenerics = resolvedResponseClass !== "unknown" ? `<T = ${resolvedResponseClass}>` : "";
|
|
521
|
+
const responseType = resolvedResponseClass !== "unknown" ? `T` : "unknown";
|
|
522
|
+
const responseSyncType = httpMethod === "get" ? "IResponseWithSync" : ["post", "put", "patch", "delete"].includes(httpMethod) ? "IResponse" : "";
|
|
523
|
+
methodImpl = `${descriptionText}
|
|
524
|
+
${methodName}${methodGenerics}(${parameters}): Promise<${responseSyncType}<${responseType}>> {
|
|
458
525
|
${queryParamsDefault}
|
|
459
526
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
460
527
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
461
|
-
|
|
462
|
-
const res = () => Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated})
|
|
463
528
|
|
|
464
|
-
|
|
465
|
-
return SdkCache.withoutCache(res)
|
|
466
|
-
}
|
|
467
|
-
const cacheKey = url + CodeGenUtil.hashCode(JSON.stringify({ params }))
|
|
468
|
-
return SdkCache.withCache(cacheKey, res)
|
|
469
|
-
}
|
|
470
|
-
`;
|
|
471
|
-
isGuardInvoked = true;
|
|
472
|
-
}
|
|
473
|
-
if (isCacheFetchUnknown) {
|
|
474
|
-
methodImpl = `${descriptionText}
|
|
475
|
-
${cachedFetchMethod}(${parameters}): Promise<IResponseWithSync<unknown>> {
|
|
476
|
-
${queryParamsDefault}
|
|
477
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
478
|
-
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
479
|
-
|
|
480
|
-
const res = () => Validate.responseType(() => resultPromise, z.unknown())
|
|
529
|
+
${httpMethod === "get" ? `const res = () => Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated})
|
|
481
530
|
|
|
482
531
|
if (!this.cache) {
|
|
483
532
|
return SdkCache.withoutCache(res)
|
|
484
533
|
}
|
|
485
534
|
const cacheKey = url + CodeGenUtil.hashCode(JSON.stringify({ params }))
|
|
486
|
-
return SdkCache.withCache(cacheKey, res)
|
|
487
|
-
}
|
|
488
|
-
`;
|
|
489
|
-
isGuardInvoked = true;
|
|
490
|
-
}
|
|
491
|
-
const withTypeGuard = ["post", "put", "patch", "delete"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
492
|
-
if (withTypeGuard) {
|
|
493
|
-
methodImpl = `${descriptionText}
|
|
494
|
-
${classMethod}<T = ${resolvedResponseClass}>(${parameters}): Promise<IResponse<T>> {
|
|
495
|
-
${queryParamsDefault}
|
|
496
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
497
|
-
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
498
|
-
|
|
499
|
-
return Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated})
|
|
500
|
-
}
|
|
501
|
-
`;
|
|
502
|
-
isGuardInvoked = true;
|
|
503
|
-
}
|
|
504
|
-
const withTypeGuardUnknown = ["post", "put", "patch", "delete"].includes(httpMethod) && resolvedResponseClass === "unknown";
|
|
505
|
-
if (withTypeGuardUnknown) {
|
|
506
|
-
methodImpl = `${descriptionText}
|
|
507
|
-
${classMethod}(${parameters}): Promise<IResponse<unknown>> {
|
|
508
|
-
${queryParamsDefault}
|
|
509
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
510
|
-
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
511
|
-
|
|
512
|
-
return Validate.responseType(() => resultPromise, z.unknown())
|
|
535
|
+
return SdkCache.withCache(cacheKey, res)` : ""}${["post", "put", "patch", "delete"].includes(httpMethod) ? `return Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated})` : ""}
|
|
513
536
|
}
|
|
514
537
|
`;
|
|
515
|
-
isGuardInvoked = true;
|
|
516
|
-
}
|
|
517
538
|
if (!isGuardInvoked) {
|
|
518
539
|
methodImpl = `${descriptionText}
|
|
519
540
|
${deprecateTag}
|
|
@@ -531,12 +552,12 @@ class TemplateZod {
|
|
|
531
552
|
duplicates;
|
|
532
553
|
duplicateFound = false;
|
|
533
554
|
importClasses = /* @__PURE__ */ new Set();
|
|
534
|
-
render = (
|
|
555
|
+
render = (fileName, definition, duplicates) => {
|
|
535
556
|
this.duplicates = duplicates;
|
|
536
557
|
const content = this.parseToZodSchema(definition, definition.required || []);
|
|
537
|
-
const containsRecursiveType = this.importClasses.has(
|
|
558
|
+
const containsRecursiveType = this.importClasses.has(fileName);
|
|
538
559
|
if (containsRecursiveType) {
|
|
539
|
-
this.importClasses.delete(
|
|
560
|
+
this.importClasses.delete(fileName);
|
|
540
561
|
}
|
|
541
562
|
let imports = "";
|
|
542
563
|
for (const cl of Array.from(this.importClasses).sort()) {
|
|
@@ -547,18 +568,18 @@ class TemplateZod {
|
|
|
547
568
|
let exportedTypeString;
|
|
548
569
|
if (containsRecursiveType) {
|
|
549
570
|
exportedVariableString = `
|
|
550
|
-
export const ${
|
|
571
|
+
export const ${fileName}: z.ZodType<${fileName}> = z.lazy(() =>
|
|
551
572
|
${content.schemaString}
|
|
552
573
|
)
|
|
553
574
|
`;
|
|
554
575
|
exportedTypeString = `
|
|
555
|
-
export type ${
|
|
576
|
+
export type ${fileName} = {
|
|
556
577
|
${content.typeString}
|
|
557
578
|
}
|
|
558
579
|
`;
|
|
559
580
|
} else {
|
|
560
|
-
exportedVariableString = `export const ${
|
|
561
|
-
exportedTypeString = `export type ${
|
|
581
|
+
exportedVariableString = `export const ${fileName} = ${content.schemaString}`;
|
|
582
|
+
exportedTypeString = `export type ${fileName} = z.TypeOf<typeof ${fileName}>`;
|
|
562
583
|
}
|
|
563
584
|
const template = `import { z } from 'zod'
|
|
564
585
|
${imports}
|
|
@@ -750,7 +771,7 @@ class CodeGenerator {
|
|
|
750
771
|
static getPatchedDir = () => path__default.join(CliParser.getSwaggersOutputPath(), "patched");
|
|
751
772
|
static getGeneratedPublicFolder = () => `${CliParser.getOutputPath()}/generated-public`;
|
|
752
773
|
static getGeneratedAdminFolder = () => `${CliParser.getOutputPath()}/generated-admin`;
|
|
753
|
-
static iterateApi = (api) => {
|
|
774
|
+
static iterateApi = async (api) => {
|
|
754
775
|
const apiBufferByTag = {};
|
|
755
776
|
const jsDocApiBufferByTag = {};
|
|
756
777
|
const dependenciesByTag = {};
|
|
@@ -763,14 +784,20 @@ class CodeGenerator {
|
|
|
763
784
|
} else if (!CliParser.isAdmin() && isAdminEndpoint) {
|
|
764
785
|
continue;
|
|
765
786
|
}
|
|
766
|
-
const
|
|
767
|
-
const
|
|
768
|
-
for (const httpMethod of
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
787
|
+
const operation = api.paths[path2];
|
|
788
|
+
const httpMethods = Object.keys(operation);
|
|
789
|
+
for (const httpMethod of httpMethods) {
|
|
790
|
+
const endpoint = await Endpoint.parseAsync(operation[httpMethod]).catch((error) => {
|
|
791
|
+
console.error(JSON.stringify({ path: path2, httpMethod }, null, 2));
|
|
792
|
+
throw error;
|
|
793
|
+
});
|
|
794
|
+
if (!endpoint.tags) {
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
const [tag] = endpoint.tags;
|
|
798
|
+
const description = endpoint.description;
|
|
799
|
+
const isDeprecated = endpoint.deprecated;
|
|
800
|
+
const responseClass = ParserUtils.get2xxResponse(endpoint.responses);
|
|
774
801
|
const className = _.upperFirst(_.camelCase(tag));
|
|
775
802
|
classImports[className] = classImports[className] ? classImports[className] : {};
|
|
776
803
|
if (!isDeprecated) {
|
|
@@ -781,15 +808,30 @@ class CodeGenerator {
|
|
|
781
808
|
if (responseClass && responseClass.endsWith("Array")) {
|
|
782
809
|
arrayDefinitions.push(responseClass);
|
|
783
810
|
}
|
|
784
|
-
const isFormUrlEncoded = ParserUtils.isFormUrlEncoded(httpMethod,
|
|
785
|
-
const queryParams = ParserUtils.
|
|
786
|
-
const pathParams = ParserUtils.
|
|
787
|
-
const bodyParams = ParserUtils.
|
|
788
|
-
const classMethod = ParserUtils.generateClassMethod(
|
|
789
|
-
|
|
790
|
-
|
|
811
|
+
const isFormUrlEncoded = ParserUtils.isFormUrlEncoded(httpMethod, endpoint.consumes);
|
|
812
|
+
const queryParams = ParserUtils.filterQueryParameters(endpoint.parameters);
|
|
813
|
+
const pathParams = ParserUtils.filterPathParams(endpoint.parameters);
|
|
814
|
+
const bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
|
|
815
|
+
const classMethod = ParserUtils.generateClassMethod({
|
|
816
|
+
path: path2,
|
|
817
|
+
endpoint,
|
|
818
|
+
httpMethod,
|
|
819
|
+
className
|
|
820
|
+
});
|
|
821
|
+
const pathWithBase = `${api.basePath ?? ""}${path2}`;
|
|
822
|
+
const [generatedMethodString, importStatements] = templateMethod({
|
|
823
|
+
classMethod,
|
|
824
|
+
description,
|
|
825
|
+
httpMethod,
|
|
826
|
+
path: pathWithBase,
|
|
827
|
+
pathParams,
|
|
828
|
+
bodyParams,
|
|
829
|
+
queryParams,
|
|
830
|
+
isFormUrlEncoded,
|
|
831
|
+
responseClass
|
|
832
|
+
});
|
|
791
833
|
apiBufferByTag[tag] = (apiBufferByTag[tag] || "") + generatedMethodString;
|
|
792
|
-
jsDocApiBufferByTag[tag] = (jsDocApiBufferByTag[tag] || "") + templateJsdocMethod(classMethod, httpMethod, path2, pathParams, bodyParams, queryParams);
|
|
834
|
+
jsDocApiBufferByTag[tag] = (jsDocApiBufferByTag[tag] || "") + templateJsdocMethod({ classMethod, httpMethod, path: path2, pathParams, bodyParams, queryParams });
|
|
793
835
|
dependenciesByTag[tag] = dependenciesByTag[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...dependenciesByTag[tag]])] : [...new Set(importStatements)];
|
|
794
836
|
}
|
|
795
837
|
}
|
|
@@ -815,7 +857,7 @@ class CodeGenerator {
|
|
|
815
857
|
ParserUtils.mkdirIfNotExist(DIST_DIR);
|
|
816
858
|
ParserUtils.mkdirIfNotExist(DIST_DOCS_DIR);
|
|
817
859
|
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
818
|
-
const { apiBufferByTag, dependenciesByTag, classImports, arrayDefinitions } = CodeGenerator.iterateApi(api);
|
|
860
|
+
const { apiBufferByTag, dependenciesByTag, classImports, arrayDefinitions } = await CodeGenerator.iterateApi(api);
|
|
819
861
|
const targetSrcFolder = `${CliParser.getOutputPath()}/`;
|
|
820
862
|
for (const tag in apiBufferByTag) {
|
|
821
863
|
const className = _.upperFirst(_.camelCase(tag));
|