@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.
@@ -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
- const defaultValue = definition.type === "string" ? `'${definition.default}'` : definition.default;
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 semver = packageJSON.version.split(".").map((numStr) => Number(numStr));
430
- const newSemver = [semver[0], swaggerVersion, ++semver[2]];
431
- packageJSON.version = newSemver[0] + "." + newSemver[1] + "." + newSemver[2];
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}`;
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
  }
@@ -603,10 +633,11 @@ const EndpointParameters = z.object({
603
633
  in: EndpointParametersIn,
604
634
  required: z.boolean().nullish(),
605
635
  schema: Schema.nullish(),
606
- default: z.union([z.boolean(), z.string(), z.number()]).nullish(),
636
+ default: z.union([z.boolean(), z.string(), z.number(), z.array(z.any())]).nullish(),
607
637
  enum: z.array(z.union([z.boolean(), z.string(), z.number()])).nullish(),
608
638
  items: z.object({
609
- type: z.string()
639
+ type: z.string(),
640
+ enum: z.array(z.any()).nullish()
610
641
  }).nullish()
611
642
  });
612
643
  const Endpoint = z.object({
@@ -1175,6 +1206,8 @@ class CodeGenerator {
1175
1206
  continue;
1176
1207
  } else if (!CliParser.isAdmin() && isAdminEndpoint) {
1177
1208
  continue;
1209
+ } else if (path2.indexOf("/healthz") >= 0) {
1210
+ continue;
1178
1211
  }
1179
1212
  const httpMethods = Object.keys(operation);
1180
1213
  for (const httpMethod of httpMethods) {
@@ -1284,19 +1317,20 @@ class CodeGenerator {
1284
1317
  const DIST_ENDPOINTS_DIR = path__default.join(DIST_DIR, "endpoints");
1285
1318
  const DIST_DEFINITION_DIR = path__default.join(DIST_DIR, "definitions");
1286
1319
  const swaggerFilePath = `${CliParser.getSwaggersOutputPath()}/${swaggerFile}`;
1287
- const swaggerPatchedFilePath = `${CodeGenerator.getPatchedDir()}/${swaggerFile}`;
1288
- const swaggerPatchFilePath = `${swaggerFilePath}patch`;
1289
- ParserUtils.applyPatchIfExists(swaggerFilePath, swaggerPatchFilePath, swaggerPatchedFilePath, CodeGenerator.getPatchedDir());
1290
- const api = await parser.parse(swaggerPatchedFilePath);
1320
+ const api = await parser.parse(swaggerFilePath);
1291
1321
  const indexImportsSet = /* @__PURE__ */ new Set();
1292
1322
  console.log("----------\nGenerating API:", { title: api.info.title, version: api.info.version });
1293
1323
  ParserUtils.mkdirIfNotExist(DIST_DIR);
1294
1324
  ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
1295
1325
  ParserUtils.mkdirIfNotExist(DIST_ENDPOINTS_DIR);
1296
- ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin());
1326
+ ParserUtils.syncPackageVersion(api.info, CliParser.isAdmin(), process.env.PRERELEASE_ID);
1297
1327
  const { apiArgumentsByTag, apiBufferByTag, classBufferByTag, dependenciesByTag, classImports, arrayDefinitions, snippetMap } = await CodeGenerator.iterateApi(api, serviceName);
1298
1328
  if (CliParser.getSnippetOutputPath()) {
1299
- ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify(snippetMap, null, 2));
1329
+ try {
1330
+ ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify(snippetMap, null, 2));
1331
+ } catch (err) {
1332
+ console.log("Generating snippets", err);
1333
+ }
1300
1334
  }
1301
1335
  const targetSrcFolder = `${CliParser.getOutputPath()}/`;
1302
1336
  const apiList = [];
@@ -1346,6 +1380,7 @@ class CodeGenerator {
1346
1380
  ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
1347
1381
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
1348
1382
  }
1383
+ ParserUtils.writeXVersion(DIST_DIR, api["x-version"], api.info);
1349
1384
  console.log("\nCOMPLETED\n----------\n\n");
1350
1385
  return indexImportsSet;
1351
1386
  };
@@ -1380,13 +1415,19 @@ class SwaggerDownloader {
1380
1415
  };
1381
1416
  static downloadFile = (targetFileName, url) => {
1382
1417
  const destFile = SwaggerDownloader.getDestFile(targetFileName);
1383
- const file = fs.createWriteStream(destFile);
1418
+ let data = "";
1384
1419
  const request = https.get(url, function(response) {
1385
- response.pipe(file);
1386
- file.on("finish", () => {
1387
- file.close();
1388
- SwaggerDownloader.postSanitizeDownloadedFile(destFile);
1389
- console.log(`SwaggerDownload ${url} completed`);
1420
+ response.on("data", (chunk) => {
1421
+ data += chunk;
1422
+ });
1423
+ response.on("end", () => {
1424
+ if (response.statusCode !== 200) {
1425
+ console.log(`SwaggerDownload error with status code: ${response.statusCode}`);
1426
+ } else {
1427
+ fs.writeFileSync(destFile, JSON.stringify(JSON.parse(data), null, 2), "utf-8");
1428
+ SwaggerDownloader.postSanitizeDownloadedFile(destFile);
1429
+ console.log(`SwaggerDownload ${url} completed with status code: ${response.statusCode}`);
1430
+ }
1390
1431
  });
1391
1432
  });
1392
1433
  request.on("error", (err) => {
@@ -1400,7 +1441,6 @@ class SwaggerDownloader {
1400
1441
  const url = swaggers[ref][3];
1401
1442
  SwaggerDownloader.downloadFile(targetFileName, url);
1402
1443
  }
1403
- console.log("\n----------\n SwaggerDownloader COMPLETED.\n----------\n\n");
1404
1444
  };
1405
1445
  }
1406
1446
 
@@ -1408,7 +1448,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
1408
1448
  CliParser.createInstance(yargs2);
1409
1449
  SwaggerDownloader.main();
1410
1450
  }).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
1411
- yargs2.check(({ output, swaggersOutput }) => {
1451
+ yargs2.check(({ output }) => {
1412
1452
  if (!output?.trim()) {
1413
1453
  throw new Error("output is required for generate-code");
1414
1454
  }
@@ -1429,7 +1469,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
1429
1469
  }
1430
1470
  const indexImportsArray = Array.from(indexImportsSet);
1431
1471
  const filesToImport = indexImportsArray.map((fileToImport) => {
1432
- return `export * from '${fileToImport.replace("\\", "/")}'`;
1472
+ return `export * from '${fileToImport.replace("\\", "/")}.js'`;
1433
1473
  });
1434
1474
  ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n"), CliParser.isAdmin());
1435
1475
  }).option("config", {