@accelbyte/codegen 0.0.0-dev-20240828053809 → 0.0.0-dev-20240904015634

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.
@@ -126,12 +126,26 @@ var getPermissionType = (permission) => {
126
126
  return type || null;
127
127
  };
128
128
  var removeAdminPrefix = (name) => name.startsWith("Admin") && name !== "Admin" ? name.replace(/^Admin/, "") : name;
129
- function extractDescription(description, options) {
130
- return description ? `
131
- /**${options?.isDeprecated ? "\n * @deprecated" : ""}
132
- * ${description.replace(/\*\//g, "*\\/").replace(/\n/g, "\n * ")}
133
- */` : "";
134
- }
129
+ var escapeComment = (string) => string.replace(/\*\//g, "*\\/").replace(/\n/g, "\n * ");
130
+ var generateJsDoc = ({ indentation = 4, lines }) => {
131
+ const indent = " ".repeat(indentation);
132
+ const formattedLines = lines.filter(Boolean).map((line) => `${indent} * ${line}`).join("\n");
133
+ return `${indent}/**
134
+ ${formattedLines}
135
+ ${indent} */`;
136
+ };
137
+ var extractDescription = (description, options) => {
138
+ const { indentation = 2, responseClasses, other, isDeprecated } = options;
139
+ return description ? generateJsDoc({
140
+ lines: [
141
+ isDeprecated ? "@deprecated" : "",
142
+ escapeComment(description),
143
+ ...responseClasses.length > 1 ? [" ", "#### Response type:", ...responseClasses.map((val) => `- \`${val}\``)] : "",
144
+ ...options?.other ? other : ""
145
+ ],
146
+ indentation
147
+ }) : "";
148
+ };
135
149
 
136
150
  // src/templates/template-class.ts
137
151
  var getImportableVarMap = () => ({
@@ -182,7 +196,7 @@ var getImportableVarMap2 = () => ({
182
196
  });
183
197
  var makeNewImportVarMap2 = () => ({
184
198
  "@accelbyte/sdk": ["AccelByteSDK", "SdkSetConfigParam", "ApiUtils", "Network"],
185
- axios: ["AxiosResponse"]
199
+ axios: ["AxiosRequestConfig", "AxiosResponse"]
186
200
  });
187
201
  var templateApiClass = (className, body, importStatements, returnMethods) => {
188
202
  const $className = className.replace(/Api$/, "$");
@@ -199,7 +213,7 @@ export function ${className}(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
199
213
  const sdkAssembly = sdk.assembly()
200
214
 
201
215
  const namespace = args?.coreConfig?.namespace ?? sdkAssembly.coreConfig.namespace
202
- const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosConfig.request, args?.axiosConfig?.request)
216
+ const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, args?.axiosConfig?.request)
203
217
  const interceptors = args?.axiosConfig?.interceptors ?? sdkAssembly.axiosConfig.interceptors ?? []
204
218
  const useSchemaValidation = sdkAssembly.coreConfig.useSchemaValidation
205
219
  const axiosInstance = Network.create(requestConfig)
@@ -459,25 +473,23 @@ var ParserUtils = class _ParserUtils {
459
473
  }
460
474
  return null;
461
475
  };
462
- static get2xxResponse(methodEntity) {
476
+ static get2xxResponses(methodEntity) {
463
477
  const keys = Object.keys(methodEntity);
464
- let responseClass = null;
465
- keys.forEach((key) => {
466
- if (String(key).startsWith("2")) {
467
- const sch = methodEntity[key].schema;
468
- const schV3 = methodEntity[key].content && methodEntity[key].content["application/json"].schema;
469
- if (sch?.$ref) {
470
- responseClass = _ParserUtils.parseRefType(sch.$ref);
471
- } else if (sch?.type === "array" && sch.items?.$ref) {
472
- responseClass = _ParserUtils.parseRefType(sch.items.$ref);
473
- responseClass = `${responseClass}Array`;
474
- } else if (schV3?.$ref) {
475
- responseClass = _ParserUtils.parseRefType(schV3.$ref);
476
- } else {
477
- }
478
+ const responseClasses = [];
479
+ const statusCodes = keys.filter((key) => String(key).startsWith("2"));
480
+ statusCodes.forEach((key) => {
481
+ const sch = methodEntity[key].schema;
482
+ const schV3 = methodEntity[key].content && methodEntity[key].content["application/json"].schema;
483
+ if (sch?.$ref) {
484
+ responseClasses.push(_ParserUtils.parseRefType(sch.$ref));
485
+ } else if (sch?.type === "array" && sch.items?.$ref) {
486
+ responseClasses.push(`${_ParserUtils.parseRefType(sch.items.$ref)}Array`);
487
+ } else if (schV3?.$ref) {
488
+ responseClasses.push(_ParserUtils.parseRefType(schV3.$ref));
489
+ } else {
478
490
  }
479
491
  });
480
- return responseClass;
492
+ return responseClasses;
481
493
  }
482
494
  static isFormUrlEncoded(httpMethod, contentTypes) {
483
495
  if (!contentTypes || contentTypes.length < 1) {
@@ -877,7 +889,7 @@ var templateApiMethod = ({
877
889
  path: path7,
878
890
  pathParams,
879
891
  bodyParams,
880
- responseClass,
892
+ responseClasses,
881
893
  classGenName,
882
894
  methodParams,
883
895
  methodParamsNoTypes,
@@ -912,8 +924,8 @@ var templateApiMethod = ({
912
924
  }
913
925
  }
914
926
  const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
915
- const descriptionText = extractDescription(description, { isDeprecated: deprecated });
916
- const resolvedResponseClass = responseClass || "unknown";
927
+ const descriptionText = extractDescription(description, { isDeprecated: deprecated, responseClasses });
928
+ const resolvedResponseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
917
929
  const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
918
930
  const methodImpl = `
919
931
  ${descriptionText}
@@ -945,7 +957,7 @@ var templateMethod = ({
945
957
  bodyParams,
946
958
  queryParams,
947
959
  isFormUrlEncoded,
948
- responseClass,
960
+ responseClasses,
949
961
  deprecated
950
962
  }) => {
951
963
  let methodParams = "";
@@ -981,7 +993,7 @@ var templateMethod = ({
981
993
  const isPostPutPatch = ["post", "put", "patch"].includes(httpMethod);
982
994
  const isDelete = ["delete"].includes(httpMethod);
983
995
  let dataPayload = "{params}";
984
- const descriptionText = extractDescription(description, { isDeprecated: deprecated });
996
+ const descriptionText = extractDescription(description, { isDeprecated: deprecated, responseClasses });
985
997
  let formPayloadString = "";
986
998
  if (isFormUrlEncoded) {
987
999
  formPayloadString = ``;
@@ -993,8 +1005,8 @@ var templateMethod = ({
993
1005
  dataPayload = dataType ? `{data, params}` : "{params}";
994
1006
  }
995
1007
  const isFileUpload = methodParams.indexOf("data: {file") > -1;
996
- const resolvedResponseClass = responseClass || "unknown";
997
- const resolvedResponseClassValidated = responseClass || "z.unknown()";
1008
+ const resolvedResponseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
1009
+ const resolvedResponseClassValidated = responseClasses.length === 1 ? responseClasses?.[0] : "z.unknown()";
998
1010
  methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
999
1011
  methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
1000
1012
  const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
@@ -1029,9 +1041,11 @@ var templateQueryMethod = ({
1029
1041
  httpMethod,
1030
1042
  path: path7,
1031
1043
  pathParams,
1032
- responseClass,
1044
+ responseClasses,
1033
1045
  methodParams,
1034
- apiGenName
1046
+ apiGenName,
1047
+ description,
1048
+ deprecated
1035
1049
  }) => {
1036
1050
  const isPostFetch = httpMethod === "post" && (POST_FETCH_INCLUDES_PATH.some((p) => path7.includes(p)) || path7.endsWith("/list"));
1037
1051
  const isFetch = classMethod.startsWith("fetch");
@@ -1056,20 +1070,27 @@ var templateQueryMethod = ({
1056
1070
  }
1057
1071
  }
1058
1072
  }
1059
- const resolvedResponseClass = responseClass || "unknown";
1073
+ const resolvedResponseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
1060
1074
  let _methodName = convertMethodNameToHook({ classMethod, apiGenName, isPostFetch, isFetch });
1061
1075
  const _responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
1062
1076
  const _methodParams = methodParams && methodParams.length > 0 ? `& { ${methodParams} }` : "";
1063
1077
  const _methodParamsImpl = convertToMethodImplArgs(methodParams);
1078
+ const queryKey = createQueryKey(apiGenName, classMethod);
1079
+ const descriptionText = extractDescription(description, {
1080
+ isDeprecated: deprecated,
1081
+ responseClasses,
1082
+ other: [" ", "#### Default Query Options", "The default options include:", "```", "{", ` queryKey: [${queryKey}, input]`, "}", "```"]
1083
+ });
1064
1084
  const queryMethodImpl = `
1065
1085
 
1086
+ ${descriptionText}
1066
1087
  export const ${_methodName} = (
1067
1088
  sdk: AccelByteSDK,
1068
1089
  input: SdkSetConfigParam ${_methodParams},
1069
1090
  options?: Omit<UseQueryOptions<${_responseType}, AxiosError<ApiError>>, 'queryKey'>,
1070
1091
  callback?: (data: AxiosResponse<${_responseType}>) => void
1071
1092
  ): UseQueryResult<${_responseType}, AxiosError<ApiError>> => {
1072
- //
1093
+
1073
1094
  const queryFn = (
1074
1095
  sdk: AccelByteSDK,
1075
1096
  input: Parameters<typeof ${_methodName}>[1]
@@ -1082,7 +1103,7 @@ export const ${_methodName} = (
1082
1103
  }
1083
1104
 
1084
1105
  return ${queryMethod}<${_responseType}, AxiosError<ApiError>>({
1085
- queryKey: [${createQueryKey(apiGenName, classMethod)}, input],
1106
+ queryKey: [${queryKey}, input],
1086
1107
  queryFn: queryFn(sdk, input),
1087
1108
  ...options
1088
1109
  })
@@ -1091,12 +1112,13 @@ export const ${_methodName} = (
1091
1112
  `;
1092
1113
  const mutationMethodImpl = `
1093
1114
 
1115
+ ${descriptionText}
1094
1116
  export const ${_methodName} = (
1095
1117
  sdk: AccelByteSDK,
1096
1118
  options?: Omit<UseMutationOptions<${_responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}>, 'mutationKey'>,
1097
1119
  callback?: (data: ${_responseType}) => void
1098
1120
  ): UseMutationResult<${_responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}> => {
1099
- //
1121
+
1100
1122
  const mutationFn = async (input: SdkSetConfigParam ${_methodParams}) => {
1101
1123
  const response =
1102
1124
  (await ${apiGenName}(sdk, { coreConfig: input.coreConfig, axiosConfig: input.axiosConfig }).
@@ -1106,7 +1128,7 @@ export const ${_methodName} = (
1106
1128
  }
1107
1129
 
1108
1130
  return useMutation({
1109
- mutationKey: [${createQueryKey(apiGenName, classMethod)}],
1131
+ mutationKey: [${queryKey}],
1110
1132
  mutationFn,
1111
1133
  ...options
1112
1134
  })
@@ -1296,12 +1318,12 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
1296
1318
  snippetMap[pathWithBase] = {};
1297
1319
  }
1298
1320
  const description = endpoint.description?.replace(/\s+/g, " ") || "";
1299
- const responseClass = ParserUtils.get2xxResponse(endpoint.responses);
1321
+ const responseClasses = ParserUtils.get2xxResponses(endpoint.responses);
1322
+ const responseClass = responseClasses.length > 1 ? null : responseClasses?.[0];
1300
1323
  const { className, classGenName } = ParserUtils.generateClassName(tag, isAdminEndpoint);
1301
1324
  tagToClassImportsRecord[className] = tagToClassImportsRecord[className] ? tagToClassImportsRecord[className] : {};
1302
1325
  if (responseClass) {
1303
- const importTypeClass = ParserUtils.parseRefType(responseClass);
1304
- tagToClassImportsRecord[className][importTypeClass] = `import { ${importTypeClass} } from '../../generated-definitions/${importTypeClass}.js'`;
1326
+ tagToClassImportsRecord[className][responseClass] = `import { ${responseClass} } from '../../generated-definitions/${responseClass}.js'`;
1305
1327
  }
1306
1328
  if (responseClass && responseClass.endsWith("Array")) {
1307
1329
  arrayDefinitions.push(responseClass);
@@ -1329,7 +1351,7 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
1329
1351
  bodyParams,
1330
1352
  queryParams,
1331
1353
  isFormUrlEncoded,
1332
- responseClass,
1354
+ responseClasses,
1333
1355
  deprecated
1334
1356
  });
1335
1357
  tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
@@ -1339,9 +1361,11 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
1339
1361
  httpMethod,
1340
1362
  path: pathWithBase,
1341
1363
  pathParams,
1342
- responseClass,
1364
+ responseClasses,
1343
1365
  methodParams,
1344
- apiGenName
1366
+ apiGenName,
1367
+ deprecated,
1368
+ description
1345
1369
  });
1346
1370
  tagToEndpointQueryRecord[tag] = (tagToEndpointQueryRecord[tag] || "") + queryMethodImpl;
1347
1371
  const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
@@ -1351,7 +1375,7 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
1351
1375
  path: pathWithBase,
1352
1376
  pathParams,
1353
1377
  bodyParams,
1354
- responseClass,
1378
+ responseClasses,
1355
1379
  classGenName,
1356
1380
  methodParams,
1357
1381
  methodParamsNoTypes,