@accelbyte/codegen 2.2.1 → 2.3.1

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/.eslintrc.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "plugins": ["unused-imports"],
3
+ "rules": {
4
+ "unused-imports/no-unused-imports": "error"
5
+ }
6
+ }
@@ -941,7 +941,7 @@ const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
941
941
  let imports = "";
942
942
  let returnStatement = "";
943
943
  for (const cl of apiList) {
944
- const dir = cl.toLowerCase().includes("admin") ? "generated-admin" : "generated-public";
944
+ const dir = cl.toLowerCase().includes("admin") && cl !== "AdminApi" ? "generated-admin" : "generated-public";
945
945
  imports += `
946
946
  import { ${cl} } from './${dir}/${cl}.js'`;
947
947
  returnStatement += `
@@ -1021,7 +1021,8 @@ const Endpoint = zod.z.object({
1021
1021
  schema: Schema.nullish()
1022
1022
  })
1023
1023
  }).nullish()
1024
- }).nullish()
1024
+ }).nullish(),
1025
+ "x-security": zod.z.any().nullish()
1025
1026
  });
1026
1027
  const Operation = zod.z.object({
1027
1028
  get: Endpoint.nullish(),
@@ -1171,17 +1172,13 @@ const templateApiMethod = ({
1171
1172
  classGenName,
1172
1173
  methodParams,
1173
1174
  methodParamsNoTypes,
1174
- deprecated
1175
+ deprecated,
1176
+ xSecurity
1175
1177
  }) => {
1176
- let methodSignature = "";
1177
1178
  let newPath = `'${path}'`;
1178
- let snippetSdk = "";
1179
- let snippetShell = "";
1179
+ let snippetMethod = "";
1180
1180
  for (const pathParam of pathParams) {
1181
1181
  const type = ParserUtils.parseType(pathParam);
1182
- if (pathParam.name !== "namespace") {
1183
- methodSignature += pathParam.name + `:${type}, `;
1184
- }
1185
1182
  const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
1186
1183
  if (path.match(`{${pathParam.name}}`)) {
1187
1184
  if (type === "string") {
@@ -1191,16 +1188,21 @@ const templateApiMethod = ({
1191
1188
  }
1192
1189
  }
1193
1190
  }
1194
- snippetShell = `curl --location --request \\
1195
- ${httpMethod} '__DOMAIN__${path}' \\
1196
- --header 'accept: application/json'`;
1191
+ const snippetShellArgs = ["--location --request", `${httpMethod} '__DOMAIN__${path}'`, "--header 'accept: application/json'"];
1192
+ const snippetApiArgs = [];
1193
+ if (xSecurity !== void 0 || path.includes("/admin")) {
1194
+ snippetShellArgs.push("--header 'Authorization: Bearer {access_token}'");
1195
+ snippetApiArgs.push("{ config: { headers: { Authorization: 'Bearer {access_token}' } }}".trim());
1196
+ }
1197
1197
  if (httpMethod !== "get") {
1198
1198
  const curlParams = bodyParams?.map((ob) => {
1199
1199
  return ` "${ob.name}": ""`;
1200
1200
  });
1201
- snippetShell += ` \\
1202
- --data-raw '{ ${curlParams}}'`;
1201
+ if (curlParams.length > 0) {
1202
+ snippetShellArgs.push(`--data-raw '{ ${curlParams}}'`);
1203
+ }
1203
1204
  }
1205
+ const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
1204
1206
  const descriptionText = description ? `
1205
1207
  /**${deprecated ? "\n * @deprecated" : ""}
1206
1208
  * ${description.replace(/\n/g, "\n * ")}
@@ -1217,13 +1219,24 @@ const templateApiMethod = ({
1217
1219
  }
1218
1220
  `;
1219
1221
  const snippetPromiseString = responseType !== "unknown" ? `Promise<${responseType}>` : "Promise";
1220
- snippetSdk += `${classMethod}(${methodParams})
1222
+ snippetMethod += `${classMethod}(${methodParams})
1221
1223
  // return ${snippetPromiseString}`;
1222
- return [methodImpl, snippetSdk, snippetShell];
1224
+ return {
1225
+ generatedMethodString: methodImpl,
1226
+ snippetApiArgs,
1227
+ snippetMethod,
1228
+ snippetShell
1229
+ };
1223
1230
  };
1224
1231
 
1225
- const templateSdkSnippet = (serviceNameTitle, apiName, methodSnippet) => {
1226
- const methodArr = methodSnippet.split("//");
1232
+ const templateSdkSnippet = ({
1233
+ serviceNameTitle,
1234
+ apiName,
1235
+ snippetMethod,
1236
+ snippetApiArgs: snippetApiArgsParam
1237
+ }) => {
1238
+ const methodArr = snippetMethod.split("//");
1239
+ const snippetApiArgs = ["sdk", ...snippetApiArgsParam];
1227
1240
  let normMethod = normalizeMethodSnippet(methodArr[0].trim(), "data:");
1228
1241
  normMethod = normalizeMethodSnippet(normMethod, "queryParams:");
1229
1242
  normMethod = normalizeMethodSnippet(normMethod, "queryParams?:");
@@ -1232,14 +1245,16 @@ const templateSdkSnippet = (serviceNameTitle, apiName, methodSnippet) => {
1232
1245
  import { ${serviceNameTitle} } from '@accelbyte/sdk-${serviceNameTitle.toLowerCase()}'
1233
1246
 
1234
1247
  const sdk = Accelbyte.SDK({
1248
+ options: {
1235
1249
  baseURL: 'https://demo.accelbyte.io',
1236
1250
  clientId: '77f88506b6174c3ea4d925f5b4096ce8',
1237
1251
  namespace: 'accelbyte',
1238
1252
  redirectURI: 'http://localhost:3030'
1253
+ }
1239
1254
  })
1240
1255
 
1241
- ${serviceNameTitle}.${apiName}(sdk)
1242
- .${normMethod}`;
1256
+ ${serviceNameTitle}.${apiName}(${snippetApiArgs.join(", ")})
1257
+ .${normMethod}`;
1243
1258
  return sdkSnippet;
1244
1259
  };
1245
1260
  const normalizeMethodSnippet = (methodInput, splitWord) => {
@@ -1378,7 +1393,7 @@ class SwaggerReaderHelpers {
1378
1393
  deprecated
1379
1394
  });
1380
1395
  tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
1381
- const [generatedMethodString, snippetMethod, snippetShell] = templateApiMethod({
1396
+ const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
1382
1397
  classMethod,
1383
1398
  description,
1384
1399
  httpMethod,
@@ -1389,14 +1404,15 @@ class SwaggerReaderHelpers {
1389
1404
  classGenName,
1390
1405
  methodParams,
1391
1406
  methodParamsNoTypes,
1392
- deprecated
1407
+ deprecated,
1408
+ xSecurity: endpoint["x-security"]
1393
1409
  });
1394
1410
  tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
1395
1411
  tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
1396
1412
  tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
1397
1413
  const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
1398
1414
  const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
1399
- const resultSnippet = templateSdkSnippet(serviceNameTitle, apiGenName, snippetMethod);
1415
+ const resultSnippet = templateSdkSnippet({ serviceNameTitle, apiName: apiGenName, snippetMethod, snippetApiArgs });
1400
1416
  const currentSnippetMap = {};
1401
1417
  snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
1402
1418
  currentSnippetMap.web = resultSnippet;