@accelbyte/codegen 1.0.1 → 1.0.2

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.
@@ -9,6 +9,7 @@ var path = require('path');
9
9
  var SwaggerParser = require('@apidevtools/swagger-parser');
10
10
  var fastJsonPatch = require('fast-json-patch');
11
11
  var _ = require('lodash');
12
+ var semver = require('semver');
12
13
  var https = require('https');
13
14
 
14
15
  function _interopNamespaceDefault(e) {
@@ -174,7 +175,14 @@ class ParserUtils {
174
175
  };
175
176
  static parseQueryParamAttributeDefault = (definition) => {
176
177
  const attrName = definition.name.slice(definition.name.lastIndexOf(".") + 1);
177
- const defaultValue = definition.type === "string" ? `'${definition.default}'` : definition.default;
178
+ let defaultValue = definition.default;
179
+ if (definition.type === "array" && Array.isArray(definition.default)) {
180
+ const mappedDefaultValue = definition.default.map((defaultValue2) => typeof defaultValue2 === "string" ? `'${defaultValue2}'` : defaultValue2);
181
+ defaultValue = `[${mappedDefaultValue.join(", ")}]`;
182
+ }
183
+ if (definition.type === "string") {
184
+ defaultValue = `'${definition.default}'`;
185
+ }
178
186
  return `${attrName}: ${defaultValue}`;
179
187
  };
180
188
  static parseType = (pathParam) => {
@@ -403,6 +411,19 @@ class ParserUtils {
403
411
  const fileContent = templateClass(apiName, apiBuffer, imports);
404
412
  fs.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
405
413
  }
414
+ static writeXVersion(distDir, xversionJson, apiInfo) {
415
+ if (xversionJson) {
416
+ console.log("x-version:", xversionJson);
417
+ fs.writeFileSync(`${distDir}/version.json`, JSON.stringify(xversionJson, null, 2));
418
+ } else {
419
+ const customVersion = {
420
+ ...apiInfo,
421
+ gitHash: apiInfo.version
422
+ };
423
+ console.error("!!!! Missing x-version for ", distDir, customVersion);
424
+ fs.writeFileSync(`${distDir}/version.json`, JSON.stringify(customVersion, null, 2));
425
+ }
426
+ }
406
427
  static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethods) {
407
428
  const newImports = [];
408
429
  imports.forEach((el, index) => {
@@ -440,7 +461,7 @@ class ParserUtils {
440
461
  ` + fileContent.trim();
441
462
  fs.writeFileSync(pathToChangelog, fileContent, "utf-8");
442
463
  }
443
- static syncPackageVersion(apiInfo, isAdminWebSdk) {
464
+ static syncPackageVersion(apiInfo, isAdminWebSdk, prereleaseId) {
444
465
  if (isAdminWebSdk) {
445
466
  return;
446
467
  }
@@ -449,9 +470,18 @@ class ParserUtils {
449
470
  let swaggerVersion = apiInfo.version ? apiInfo.version : UNDEFINED_SWAGGER_SEMVER;
450
471
  swaggerVersion = Number(swaggerVersion.replace(".", "").replace(".", ""));
451
472
  swaggerVersion = isNaN(swaggerVersion) ? 0 : swaggerVersion;
452
- const semver = packageJSON.version.split(".").map((numStr) => Number(numStr));
453
- const newSemver = [semver[0], swaggerVersion, ++semver[2]];
454
- packageJSON.version = newSemver[0] + "." + newSemver[1] + "." + newSemver[2];
473
+ const currentSemver = packageJSON.version;
474
+ let nextSemver = [semver.major(currentSemver), swaggerVersion, semver.patch(currentSemver)].join(".");
475
+ if (!prereleaseId) {
476
+ nextSemver = semver.inc(nextSemver, "patch");
477
+ } else {
478
+ const currentPrerelease = semver.prerelease(currentSemver);
479
+ if (currentPrerelease) {
480
+ nextSemver += `-${currentPrerelease}`;
481
+ }
482
+ nextSemver = semver.inc(nextSemver, "prerelease", void 0, prereleaseId);
483
+ }
484
+ packageJSON.version = nextSemver;
455
485
  fs.writeFileSync(pathToPackageJSON, JSON.stringify(packageJSON, null, 2));
456
486
  ParserUtils.syncChangelog(packageJSON.version);
457
487
  }
@@ -626,10 +656,11 @@ const EndpointParameters = zod.z.object({
626
656
  in: EndpointParametersIn,
627
657
  required: zod.z.boolean().nullish(),
628
658
  schema: Schema.nullish(),
629
- default: zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()]).nullish(),
659
+ default: zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number(), zod.z.array(zod.z.any())]).nullish(),
630
660
  enum: zod.z.array(zod.z.union([zod.z.boolean(), zod.z.string(), zod.z.number()])).nullish(),
631
661
  items: zod.z.object({
632
- type: zod.z.string()
662
+ type: zod.z.string(),
663
+ enum: zod.z.array(zod.z.any()).nullish()
633
664
  }).nullish()
634
665
  });
635
666
  const Endpoint = zod.z.object({
@@ -1198,6 +1229,8 @@ class CodeGenerator {
1198
1229
  continue;
1199
1230
  } else if (!CliParser.isAdmin() && isAdminEndpoint) {
1200
1231
  continue;
1232
+ } else if (path2.indexOf("/healthz") >= 0) {
1233
+ continue;
1201
1234
  }
1202
1235
  const httpMethods = Object.keys(operation);
1203
1236
  for (const httpMethod of httpMethods) {
@@ -1307,19 +1340,20 @@ class CodeGenerator {
1307
1340
  const DIST_ENDPOINTS_DIR = path.join(DIST_DIR, "endpoints");
1308
1341
  const DIST_DEFINITION_DIR = path.join(DIST_DIR, "definitions");
1309
1342
  const swaggerFilePath = `${CliParser.getSwaggersOutputPath()}/${swaggerFile}`;
1310
- const swaggerPatchedFilePath = `${CodeGenerator.getPatchedDir()}/${swaggerFile}`;
1311
- const swaggerPatchFilePath = `${swaggerFilePath}patch`;
1312
- ParserUtils.applyPatchIfExists(swaggerFilePath, swaggerPatchFilePath, swaggerPatchedFilePath, CodeGenerator.getPatchedDir());
1313
- const api = await parser.parse(swaggerPatchedFilePath);
1343
+ const api = await parser.parse(swaggerFilePath);
1314
1344
  const indexImportsSet = /* @__PURE__ */ new Set();
1315
1345
  console.log("----------\nGenerating API:", { title: api.info.title, version: api.info.version });
1316
1346
  ParserUtils.mkdirIfNotExist(DIST_DIR);
1317
1347
  ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
1318
1348
  ParserUtils.mkdirIfNotExist(DIST_ENDPOINTS_DIR);
1319
- ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin());
1349
+ ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin(), process.env.PRERELEASE_ID);
1320
1350
  const { apiArgumentsByTag, apiBufferByTag, classBufferByTag, dependenciesByTag, classImports, arrayDefinitions, snippetMap } = await CodeGenerator.iterateApi(api, serviceName);
1321
1351
  if (CliParser.getSnippetOutputPath()) {
1322
- ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify(snippetMap, null, 2));
1352
+ try {
1353
+ ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify(snippetMap, null, 2));
1354
+ } catch (err) {
1355
+ console.log("Generating snippets", err);
1356
+ }
1323
1357
  }
1324
1358
  const targetSrcFolder = `${CliParser.getOutputPath()}/`;
1325
1359
  const apiList = [];
@@ -1369,6 +1403,7 @@ class CodeGenerator {
1369
1403
  ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
1370
1404
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
1371
1405
  }
1406
+ ParserUtils.writeXVersion(DIST_DIR, api["x-version"], api.info);
1372
1407
  console.log("\nCOMPLETED\n----------\n\n");
1373
1408
  return indexImportsSet;
1374
1409
  };
@@ -1403,13 +1438,19 @@ class SwaggerDownloader {
1403
1438
  };
1404
1439
  static downloadFile = (targetFileName, url) => {
1405
1440
  const destFile = SwaggerDownloader.getDestFile(targetFileName);
1406
- const file = fs__namespace.createWriteStream(destFile);
1441
+ let data = "";
1407
1442
  const request = https__namespace.get(url, function(response) {
1408
- response.pipe(file);
1409
- file.on("finish", () => {
1410
- file.close();
1411
- SwaggerDownloader.postSanitizeDownloadedFile(destFile);
1412
- console.log(`SwaggerDownload ${url} completed`);
1443
+ response.on("data", (chunk) => {
1444
+ data += chunk;
1445
+ });
1446
+ response.on("end", () => {
1447
+ if (response.statusCode !== 200) {
1448
+ console.log(`SwaggerDownload error with status code: ${response.statusCode}`);
1449
+ } else {
1450
+ fs__namespace.writeFileSync(destFile, JSON.stringify(JSON.parse(data), null, 2), "utf-8");
1451
+ SwaggerDownloader.postSanitizeDownloadedFile(destFile);
1452
+ console.log(`SwaggerDownload ${url} completed with status code: ${response.statusCode}`);
1453
+ }
1413
1454
  });
1414
1455
  });
1415
1456
  request.on("error", (err) => {
@@ -1423,7 +1464,6 @@ class SwaggerDownloader {
1423
1464
  const url = swaggers[ref][3];
1424
1465
  SwaggerDownloader.downloadFile(targetFileName, url);
1425
1466
  }
1426
- console.log("\n----------\n SwaggerDownloader COMPLETED.\n----------\n\n");
1427
1467
  };
1428
1468
  }
1429
1469
 
@@ -1431,7 +1471,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
1431
1471
  CliParser.createInstance(yargs2);
1432
1472
  SwaggerDownloader.main();
1433
1473
  }).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
1434
- yargs2.check(({ output, swaggersOutput }) => {
1474
+ yargs2.check(({ output }) => {
1435
1475
  if (!output?.trim()) {
1436
1476
  throw new Error("output is required for generate-code");
1437
1477
  }
@@ -1452,7 +1492,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
1452
1492
  }
1453
1493
  const indexImportsArray = Array.from(indexImportsSet);
1454
1494
  const filesToImport = indexImportsArray.map((fileToImport) => {
1455
- return `export * from '${fileToImport.replace("\\", "/")}'`;
1495
+ return `export * from '${fileToImport.replace("\\", "/")}.js'`;
1456
1496
  });
1457
1497
  ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n"), CliParser.isAdmin());
1458
1498
  }).option("config", {