@accelbyte/codegen 1.0.1 → 1.0.4
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.
|
@@ -7,6 +7,7 @@ import path__default from 'path';
|
|
|
7
7
|
import SwaggerParser from '@apidevtools/swagger-parser';
|
|
8
8
|
import { applyPatch } from 'fast-json-patch';
|
|
9
9
|
import _ from 'lodash';
|
|
10
|
+
import semver from 'semver';
|
|
10
11
|
import * as https from 'https';
|
|
11
12
|
|
|
12
13
|
const SwaggersConfig = z.array(z.array(z.string()));
|
|
@@ -151,7 +152,14 @@ class ParserUtils {
|
|
|
151
152
|
};
|
|
152
153
|
static parseQueryParamAttributeDefault = (definition) => {
|
|
153
154
|
const attrName = definition.name.slice(definition.name.lastIndexOf(".") + 1);
|
|
154
|
-
|
|
155
|
+
let defaultValue = definition.default;
|
|
156
|
+
if (definition.type === "array" && Array.isArray(definition.default)) {
|
|
157
|
+
const mappedDefaultValue = definition.default.map((defaultValue2) => typeof defaultValue2 === "string" ? `'${defaultValue2}'` : defaultValue2);
|
|
158
|
+
defaultValue = `[${mappedDefaultValue.join(", ")}]`;
|
|
159
|
+
}
|
|
160
|
+
if (definition.type === "string") {
|
|
161
|
+
defaultValue = `'${definition.default}'`;
|
|
162
|
+
}
|
|
155
163
|
return `${attrName}: ${defaultValue}`;
|
|
156
164
|
};
|
|
157
165
|
static parseType = (pathParam) => {
|
|
@@ -380,6 +388,19 @@ class ParserUtils {
|
|
|
380
388
|
const fileContent = templateClass(apiName, apiBuffer, imports);
|
|
381
389
|
fs__default.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
382
390
|
}
|
|
391
|
+
static writeXVersion(distDir, xversionJson, apiInfo) {
|
|
392
|
+
if (xversionJson) {
|
|
393
|
+
console.log("x-version:", xversionJson);
|
|
394
|
+
fs__default.writeFileSync(`${distDir}/version.json`, JSON.stringify(xversionJson, null, 2));
|
|
395
|
+
} else {
|
|
396
|
+
const customVersion = {
|
|
397
|
+
...apiInfo,
|
|
398
|
+
gitHash: apiInfo.version
|
|
399
|
+
};
|
|
400
|
+
console.error("!!!! Missing x-version for ", distDir, customVersion);
|
|
401
|
+
fs__default.writeFileSync(`${distDir}/version.json`, JSON.stringify(customVersion, null, 2));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
383
404
|
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethods) {
|
|
384
405
|
const newImports = [];
|
|
385
406
|
imports.forEach((el, index) => {
|
|
@@ -417,7 +438,7 @@ class ParserUtils {
|
|
|
417
438
|
` + fileContent.trim();
|
|
418
439
|
fs__default.writeFileSync(pathToChangelog, fileContent, "utf-8");
|
|
419
440
|
}
|
|
420
|
-
static syncPackageVersion(apiInfo, isAdminWebSdk) {
|
|
441
|
+
static syncPackageVersion(apiInfo, isAdminWebSdk, prereleaseId) {
|
|
421
442
|
if (isAdminWebSdk) {
|
|
422
443
|
return;
|
|
423
444
|
}
|
|
@@ -426,9 +447,18 @@ class ParserUtils {
|
|
|
426
447
|
let swaggerVersion = apiInfo.version ? apiInfo.version : UNDEFINED_SWAGGER_SEMVER;
|
|
427
448
|
swaggerVersion = Number(swaggerVersion.replace(".", "").replace(".", ""));
|
|
428
449
|
swaggerVersion = isNaN(swaggerVersion) ? 0 : swaggerVersion;
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
450
|
+
const currentSemver = packageJSON.version;
|
|
451
|
+
let nextSemver = [semver.major(currentSemver), swaggerVersion, semver.patch(currentSemver)].join(".");
|
|
452
|
+
if (!prereleaseId) {
|
|
453
|
+
nextSemver = semver.inc(nextSemver, "patch");
|
|
454
|
+
} else {
|
|
455
|
+
const currentPrerelease = semver.prerelease(currentSemver);
|
|
456
|
+
if (currentPrerelease) {
|
|
457
|
+
nextSemver += `-${currentPrerelease.join(".")}`;
|
|
458
|
+
}
|
|
459
|
+
nextSemver = semver.inc(nextSemver, "prerelease", void 0, prereleaseId);
|
|
460
|
+
}
|
|
461
|
+
packageJSON.version = nextSemver;
|
|
432
462
|
writeFileSync(pathToPackageJSON, JSON.stringify(packageJSON, null, 2));
|
|
433
463
|
ParserUtils.syncChangelog(packageJSON.version);
|
|
434
464
|
}
|
|
@@ -508,6 +538,9 @@ class ParserUtils {
|
|
|
508
538
|
*/
|
|
509
539
|
${content}`;
|
|
510
540
|
};
|
|
541
|
+
static sortPathParamsByPath = (pathParams, path2) => {
|
|
542
|
+
return pathParams.sort((a, b) => path2.indexOf(a.name) - path2.indexOf(b.name));
|
|
543
|
+
};
|
|
511
544
|
}
|
|
512
545
|
const mappedMethod = (httpMethod, isForm) => {
|
|
513
546
|
if (httpMethod === "get") {
|
|
@@ -603,10 +636,11 @@ const EndpointParameters = z.object({
|
|
|
603
636
|
in: EndpointParametersIn,
|
|
604
637
|
required: z.boolean().nullish(),
|
|
605
638
|
schema: Schema.nullish(),
|
|
606
|
-
default: z.union([z.boolean(), z.string(), z.number()]).nullish(),
|
|
639
|
+
default: z.union([z.boolean(), z.string(), z.number(), z.array(z.any())]).nullish(),
|
|
607
640
|
enum: z.array(z.union([z.boolean(), z.string(), z.number()])).nullish(),
|
|
608
641
|
items: z.object({
|
|
609
|
-
type: z.string()
|
|
642
|
+
type: z.string(),
|
|
643
|
+
enum: z.array(z.any()).nullish()
|
|
610
644
|
}).nullish()
|
|
611
645
|
});
|
|
612
646
|
const Endpoint = z.object({
|
|
@@ -679,7 +713,8 @@ const templateMethod = ({
|
|
|
679
713
|
let methodParamsNoTypes = "";
|
|
680
714
|
let newPath = `'${path}'`;
|
|
681
715
|
let importStatements = [];
|
|
682
|
-
|
|
716
|
+
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams, path);
|
|
717
|
+
for (const pathParam of sortedPathParams) {
|
|
683
718
|
const type = ParserUtils.parseType(pathParam);
|
|
684
719
|
if (pathParam.name !== "namespace") {
|
|
685
720
|
methodParams += pathParam.name + `:${type}, `;
|
|
@@ -1175,6 +1210,8 @@ class CodeGenerator {
|
|
|
1175
1210
|
continue;
|
|
1176
1211
|
} else if (!CliParser.isAdmin() && isAdminEndpoint) {
|
|
1177
1212
|
continue;
|
|
1213
|
+
} else if (path2.indexOf("/healthz") >= 0) {
|
|
1214
|
+
continue;
|
|
1178
1215
|
}
|
|
1179
1216
|
const httpMethods = Object.keys(operation);
|
|
1180
1217
|
for (const httpMethod of httpMethods) {
|
|
@@ -1284,19 +1321,20 @@ class CodeGenerator {
|
|
|
1284
1321
|
const DIST_ENDPOINTS_DIR = path__default.join(DIST_DIR, "endpoints");
|
|
1285
1322
|
const DIST_DEFINITION_DIR = path__default.join(DIST_DIR, "definitions");
|
|
1286
1323
|
const swaggerFilePath = `${CliParser.getSwaggersOutputPath()}/${swaggerFile}`;
|
|
1287
|
-
const
|
|
1288
|
-
const swaggerPatchFilePath = `${swaggerFilePath}patch`;
|
|
1289
|
-
ParserUtils.applyPatchIfExists(swaggerFilePath, swaggerPatchFilePath, swaggerPatchedFilePath, CodeGenerator.getPatchedDir());
|
|
1290
|
-
const api = await parser.parse(swaggerPatchedFilePath);
|
|
1324
|
+
const api = await parser.parse(swaggerFilePath);
|
|
1291
1325
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
1292
1326
|
console.log("----------\nGenerating API:", { title: api.info.title, version: api.info.version });
|
|
1293
1327
|
ParserUtils.mkdirIfNotExist(DIST_DIR);
|
|
1294
1328
|
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1295
1329
|
ParserUtils.mkdirIfNotExist(DIST_ENDPOINTS_DIR);
|
|
1296
|
-
ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin());
|
|
1330
|
+
ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin(), process.env.PRERELEASE_ID);
|
|
1297
1331
|
const { apiArgumentsByTag, apiBufferByTag, classBufferByTag, dependenciesByTag, classImports, arrayDefinitions, snippetMap } = await CodeGenerator.iterateApi(api, serviceName);
|
|
1298
1332
|
if (CliParser.getSnippetOutputPath()) {
|
|
1299
|
-
|
|
1333
|
+
try {
|
|
1334
|
+
ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify(snippetMap, null, 2));
|
|
1335
|
+
} catch (err) {
|
|
1336
|
+
console.log("Generating snippets", err);
|
|
1337
|
+
}
|
|
1300
1338
|
}
|
|
1301
1339
|
const targetSrcFolder = `${CliParser.getOutputPath()}/`;
|
|
1302
1340
|
const apiList = [];
|
|
@@ -1346,6 +1384,7 @@ class CodeGenerator {
|
|
|
1346
1384
|
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
|
|
1347
1385
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
|
|
1348
1386
|
}
|
|
1387
|
+
ParserUtils.writeXVersion(DIST_DIR, api["x-version"], api.info);
|
|
1349
1388
|
console.log("\nCOMPLETED\n----------\n\n");
|
|
1350
1389
|
return indexImportsSet;
|
|
1351
1390
|
};
|
|
@@ -1380,13 +1419,19 @@ class SwaggerDownloader {
|
|
|
1380
1419
|
};
|
|
1381
1420
|
static downloadFile = (targetFileName, url) => {
|
|
1382
1421
|
const destFile = SwaggerDownloader.getDestFile(targetFileName);
|
|
1383
|
-
|
|
1422
|
+
let data = "";
|
|
1384
1423
|
const request = https.get(url, function(response) {
|
|
1385
|
-
response.
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1424
|
+
response.on("data", (chunk) => {
|
|
1425
|
+
data += chunk;
|
|
1426
|
+
});
|
|
1427
|
+
response.on("end", () => {
|
|
1428
|
+
if (response.statusCode !== 200) {
|
|
1429
|
+
console.log(`SwaggerDownload error with status code: ${response.statusCode}`);
|
|
1430
|
+
} else {
|
|
1431
|
+
fs.writeFileSync(destFile, JSON.stringify(JSON.parse(data), null, 2), "utf-8");
|
|
1432
|
+
SwaggerDownloader.postSanitizeDownloadedFile(destFile);
|
|
1433
|
+
console.log(`SwaggerDownload ${url} completed with status code: ${response.statusCode}`);
|
|
1434
|
+
}
|
|
1390
1435
|
});
|
|
1391
1436
|
});
|
|
1392
1437
|
request.on("error", (err) => {
|
|
@@ -1400,7 +1445,6 @@ class SwaggerDownloader {
|
|
|
1400
1445
|
const url = swaggers[ref][3];
|
|
1401
1446
|
SwaggerDownloader.downloadFile(targetFileName, url);
|
|
1402
1447
|
}
|
|
1403
|
-
console.log("\n----------\n SwaggerDownloader COMPLETED.\n----------\n\n");
|
|
1404
1448
|
};
|
|
1405
1449
|
}
|
|
1406
1450
|
|
|
@@ -1408,7 +1452,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1408
1452
|
CliParser.createInstance(yargs2);
|
|
1409
1453
|
SwaggerDownloader.main();
|
|
1410
1454
|
}).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
|
|
1411
|
-
yargs2.check(({ output
|
|
1455
|
+
yargs2.check(({ output }) => {
|
|
1412
1456
|
if (!output?.trim()) {
|
|
1413
1457
|
throw new Error("output is required for generate-code");
|
|
1414
1458
|
}
|
|
@@ -1429,7 +1473,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1429
1473
|
}
|
|
1430
1474
|
const indexImportsArray = Array.from(indexImportsSet);
|
|
1431
1475
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1432
|
-
return `export * from '${fileToImport.replace("\\", "/")}'`;
|
|
1476
|
+
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1433
1477
|
});
|
|
1434
1478
|
ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n"), CliParser.isAdmin());
|
|
1435
1479
|
}).option("config", {
|