@accelbyte/codegen 0.0.0-dev-20240906023252 → 0.0.0-dev-20240911051006
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.
|
@@ -146,6 +146,18 @@ var extractDescription = (description, options) => {
|
|
|
146
146
|
indentation
|
|
147
147
|
}) : "";
|
|
148
148
|
};
|
|
149
|
+
var getResponseType = ({
|
|
150
|
+
responseClasses,
|
|
151
|
+
defaultType = "unknown"
|
|
152
|
+
}) => {
|
|
153
|
+
const responseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
|
|
154
|
+
const responseType = responseClass !== "unknown" ? responseClasses?.[0] : defaultType;
|
|
155
|
+
return {
|
|
156
|
+
responseType,
|
|
157
|
+
responseTypeInAxiosResponse: `Promise<AxiosResponse<${responseType}>>`,
|
|
158
|
+
responseTypeInResponse: `Promise<Response<${responseType}>>`
|
|
159
|
+
};
|
|
160
|
+
};
|
|
149
161
|
|
|
150
162
|
// src/templates/template-class.ts
|
|
151
163
|
var getImportableVarMap = () => ({
|
|
@@ -199,6 +211,12 @@ var makeNewImportVarMap2 = () => ({
|
|
|
199
211
|
axios: ["AxiosRequestConfig", "AxiosResponse"]
|
|
200
212
|
});
|
|
201
213
|
var templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
214
|
+
const returnsMethodsWithDescription = Object.keys(returnMethods).reduce((acc, key) => {
|
|
215
|
+
acc += `
|
|
216
|
+
${returnMethods[key]}
|
|
217
|
+
${key},`;
|
|
218
|
+
return acc;
|
|
219
|
+
}, "");
|
|
202
220
|
const $className = className.replace(/Api$/, "$");
|
|
203
221
|
return `/**
|
|
204
222
|
* AUTO GENERATED
|
|
@@ -213,25 +231,35 @@ export function ${className}(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
|
|
|
213
231
|
const sdkAssembly = sdk.assembly()
|
|
214
232
|
|
|
215
233
|
const namespace = args?.coreConfig?.namespace ?? sdkAssembly.coreConfig.namespace
|
|
216
|
-
const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, {...(args?.coreConfig?.baseURL ? {baseURL: args?.coreConfig?.baseURL} : {}), ...args?.axiosConfig?.request})
|
|
217
|
-
const interceptors = args?.axiosConfig?.interceptors ?? sdkAssembly.axiosConfig.interceptors ?? []
|
|
218
234
|
const useSchemaValidation = args?.coreConfig?.useSchemaValidation ?? sdkAssembly.coreConfig.useSchemaValidation
|
|
219
|
-
const axiosInstance = Network.create(requestConfig)
|
|
220
235
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
236
|
+
let axiosInstance = sdkAssembly.axiosInstance
|
|
237
|
+
const requestConfigOverrides = args?.axiosConfig?.request
|
|
238
|
+
const baseURLOverride = args?.coreConfig?.baseURL
|
|
239
|
+
const interceptorsOverride = args?.axiosConfig?.interceptors ?? []
|
|
225
240
|
|
|
226
|
-
|
|
227
|
-
|
|
241
|
+
if (requestConfigOverrides || baseURLOverride || interceptorsOverride.length > 0) {
|
|
242
|
+
const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, {
|
|
243
|
+
...(baseURLOverride ? { baseURL: baseURLOverride } : {}),
|
|
244
|
+
...requestConfigOverrides
|
|
245
|
+
})
|
|
246
|
+
axiosInstance = Network.create(requestConfig)
|
|
247
|
+
|
|
248
|
+
for (const interceptor of interceptorsOverride) {
|
|
249
|
+
if (interceptor.type === 'request') {
|
|
250
|
+
axiosInstance.interceptors.request.use(interceptor.onRequest, interceptor.onError)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (interceptor.type === 'response') {
|
|
254
|
+
axiosInstance.interceptors.response.use(interceptor.onSuccess, interceptor.onError)
|
|
255
|
+
}
|
|
228
256
|
}
|
|
229
257
|
}
|
|
230
258
|
|
|
231
259
|
${body}
|
|
232
260
|
|
|
233
261
|
return {
|
|
234
|
-
${
|
|
262
|
+
${returnsMethodsWithDescription}
|
|
235
263
|
}
|
|
236
264
|
}
|
|
237
265
|
`;
|
|
@@ -616,12 +644,12 @@ var ParserUtils = class _ParserUtils {
|
|
|
616
644
|
import_fs3.default.writeFileSync(`${distDir}/version.json`, JSON.stringify(customVersion, null, 2));
|
|
617
645
|
}
|
|
618
646
|
}
|
|
619
|
-
static writeApiFile(distDir, apiName, apiBuffer, imports,
|
|
647
|
+
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethodsDescription) {
|
|
620
648
|
const newImports = [];
|
|
621
649
|
imports.forEach((el) => {
|
|
622
650
|
newImports.push(el.replace("../../generated-definitions", "../generated-definitions"));
|
|
623
651
|
});
|
|
624
|
-
const fileContent = templateApiClass(apiName, apiBuffer, newImports,
|
|
652
|
+
const fileContent = templateApiClass(apiName, apiBuffer, newImports, returnMethodsDescription);
|
|
625
653
|
import_fs3.default.writeFileSync(`${distDir}/${apiName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
626
654
|
}
|
|
627
655
|
static writeApiMainFile(distDir, serviceName, fileContent) {
|
|
@@ -884,7 +912,6 @@ var OpenApiSpec = import_zod2.z.object({
|
|
|
884
912
|
// src/templates/template-api-method.ts
|
|
885
913
|
var templateApiMethod = ({
|
|
886
914
|
classMethod,
|
|
887
|
-
description,
|
|
888
915
|
httpMethod,
|
|
889
916
|
path: path7,
|
|
890
917
|
pathParams,
|
|
@@ -893,7 +920,6 @@ var templateApiMethod = ({
|
|
|
893
920
|
classGenName,
|
|
894
921
|
methodParams,
|
|
895
922
|
methodParamsNoTypes,
|
|
896
|
-
deprecated,
|
|
897
923
|
xSecurity
|
|
898
924
|
}) => {
|
|
899
925
|
let newPath = `'${path7}'`;
|
|
@@ -924,12 +950,9 @@ var templateApiMethod = ({
|
|
|
924
950
|
}
|
|
925
951
|
}
|
|
926
952
|
const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
|
|
927
|
-
const
|
|
928
|
-
const resolvedResponseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
|
|
929
|
-
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
953
|
+
const { responseType, responseTypeInAxiosResponse } = getResponseType({ responseClasses });
|
|
930
954
|
const methodImpl = `
|
|
931
|
-
${
|
|
932
|
-
async function ${classMethod}(${methodParams}): Promise<AxiosResponse<${responseType}>> {
|
|
955
|
+
async function ${classMethod}(${methodParams}): ${responseTypeInAxiosResponse} {
|
|
933
956
|
const $ = new ${classGenName}(axiosInstance, namespace, useSchemaValidation)
|
|
934
957
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
935
958
|
if (resp.error) throw resp.error
|
|
@@ -1005,14 +1028,12 @@ var templateMethod = ({
|
|
|
1005
1028
|
dataPayload = dataType ? `{data, params}` : "{params}";
|
|
1006
1029
|
}
|
|
1007
1030
|
const isFileUpload = methodParams.indexOf("data: {file") > -1;
|
|
1008
|
-
const
|
|
1009
|
-
const resolvedResponseClassValidated =
|
|
1031
|
+
const { responseType, responseTypeInResponse } = getResponseType({ responseClasses });
|
|
1032
|
+
const resolvedResponseClassValidated = responseType !== "unknown" ? `${responseType}` : "z.unknown()";
|
|
1010
1033
|
methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
|
|
1011
1034
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
1012
1035
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
1013
|
-
const
|
|
1014
|
-
const responseSyncType = "Response";
|
|
1015
|
-
const generatedMethodName = `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
1036
|
+
const generatedMethodName = `${classMethod}(${methodParams}): ${responseTypeInResponse}`;
|
|
1016
1037
|
let methodImpl = `${descriptionText}
|
|
1017
1038
|
${generatedMethodName} {
|
|
1018
1039
|
${queryParamsDefault}
|
|
@@ -1070,9 +1091,8 @@ var templateQueryMethod = ({
|
|
|
1070
1091
|
}
|
|
1071
1092
|
}
|
|
1072
1093
|
}
|
|
1073
|
-
const
|
|
1074
|
-
let _methodName = convertMethodNameToHook({ classMethod, apiGenName,
|
|
1075
|
-
const _responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1094
|
+
const { responseType } = getResponseType({ responseClasses });
|
|
1095
|
+
let _methodName = convertMethodNameToHook({ classMethod, apiGenName, isGet });
|
|
1076
1096
|
const _methodParams = methodParams && methodParams.length > 0 ? `& { ${methodParams} }` : "";
|
|
1077
1097
|
const _methodParamsImpl = convertToMethodImplArgs(methodParams);
|
|
1078
1098
|
const queryKey = createQueryKey(apiGenName, classMethod);
|
|
@@ -1087,9 +1107,9 @@ ${descriptionText}
|
|
|
1087
1107
|
export const ${_methodName} = (
|
|
1088
1108
|
sdk: AccelByteSDK,
|
|
1089
1109
|
input: SdkSetConfigParam ${_methodParams},
|
|
1090
|
-
options?: Omit<UseQueryOptions<${
|
|
1091
|
-
callback?: (data: AxiosResponse<${
|
|
1092
|
-
): UseQueryResult<${
|
|
1110
|
+
options?: Omit<UseQueryOptions<${responseType}, AxiosError<ApiError>>, 'queryKey'>,
|
|
1111
|
+
callback?: (data: AxiosResponse<${responseType}>) => void
|
|
1112
|
+
): UseQueryResult<${responseType}, AxiosError<ApiError>> => {
|
|
1093
1113
|
|
|
1094
1114
|
const queryFn = (
|
|
1095
1115
|
sdk: AccelByteSDK,
|
|
@@ -1102,7 +1122,7 @@ export const ${_methodName} = (
|
|
|
1102
1122
|
return response.data
|
|
1103
1123
|
}
|
|
1104
1124
|
|
|
1105
|
-
return ${queryMethod}<${
|
|
1125
|
+
return ${queryMethod}<${responseType}, AxiosError<ApiError>>({
|
|
1106
1126
|
queryKey: [${queryKey}, input],
|
|
1107
1127
|
queryFn: queryFn(sdk, input),
|
|
1108
1128
|
...options
|
|
@@ -1115,9 +1135,9 @@ export const ${_methodName} = (
|
|
|
1115
1135
|
${descriptionText}
|
|
1116
1136
|
export const ${_methodName} = (
|
|
1117
1137
|
sdk: AccelByteSDK,
|
|
1118
|
-
options?: Omit<UseMutationOptions<${
|
|
1119
|
-
callback?: (data: ${
|
|
1120
|
-
): UseMutationResult<${
|
|
1138
|
+
options?: Omit<UseMutationOptions<${responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}>, 'mutationKey'>,
|
|
1139
|
+
callback?: (data: ${responseType}) => void
|
|
1140
|
+
): UseMutationResult<${responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}> => {
|
|
1121
1141
|
|
|
1122
1142
|
const mutationFn = async (input: SdkSetConfigParam ${_methodParams}) => {
|
|
1123
1143
|
const response =
|
|
@@ -1137,6 +1157,13 @@ export const ${_methodName} = (
|
|
|
1137
1157
|
`;
|
|
1138
1158
|
return isGet ? queryMethodImpl : mutationMethodImpl;
|
|
1139
1159
|
};
|
|
1160
|
+
function versionMutationSuffixMethodName(baseMethodName) {
|
|
1161
|
+
const parts = baseMethodName.split(/(_v\d+)$/);
|
|
1162
|
+
const name = parts[0];
|
|
1163
|
+
const versionSuffix = parts[1] || "";
|
|
1164
|
+
const res = `${name}Mutation${versionSuffix}`;
|
|
1165
|
+
return res;
|
|
1166
|
+
}
|
|
1140
1167
|
function createQueryKey(className, methodName) {
|
|
1141
1168
|
const prefixRegex = /^(get|create|update|delete|patch|post|fetch)[_]?/i;
|
|
1142
1169
|
const cleanedMethodName = methodName.replace(prefixRegex, "").trim();
|
|
@@ -1152,15 +1179,11 @@ var prefixMappings = {
|
|
|
1152
1179
|
post: "usePost",
|
|
1153
1180
|
fetch: "useFetch"
|
|
1154
1181
|
};
|
|
1155
|
-
function convertMethodNameToHook({
|
|
1156
|
-
|
|
1157
|
-
apiGenName,
|
|
1158
|
-
isPostFetch,
|
|
1159
|
-
isFetch
|
|
1160
|
-
}) {
|
|
1161
|
-
for (const [originalPrefix, newPrefix] of Object.entries(prefixMappings)) {
|
|
1182
|
+
function convertMethodNameToHook({ classMethod, apiGenName, isGet }) {
|
|
1183
|
+
for (const [originalPrefix] of Object.entries(prefixMappings)) {
|
|
1162
1184
|
if (classMethod.startsWith(originalPrefix)) {
|
|
1163
|
-
|
|
1185
|
+
const methodName = !isGet ? versionMutationSuffixMethodName(classMethod) : classMethod;
|
|
1186
|
+
return `use${apiGenName}_${capitalize(methodName)}`;
|
|
1164
1187
|
}
|
|
1165
1188
|
}
|
|
1166
1189
|
return classMethod;
|
|
@@ -1246,7 +1269,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1246
1269
|
tagToSdkClientRecord: {},
|
|
1247
1270
|
tagToSdkFunctionNamesRecord: {},
|
|
1248
1271
|
tagToSdkImportsRecord: {},
|
|
1249
|
-
tagToEndpointQueryRecord: {}
|
|
1272
|
+
tagToEndpointQueryRecord: {},
|
|
1273
|
+
tagToSdkFunctionDescription: {}
|
|
1250
1274
|
},
|
|
1251
1275
|
public: {
|
|
1252
1276
|
arrayDefinitions: [],
|
|
@@ -1256,7 +1280,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1256
1280
|
tagToSdkClientRecord: {},
|
|
1257
1281
|
tagToSdkFunctionNamesRecord: {},
|
|
1258
1282
|
tagToSdkImportsRecord: {},
|
|
1259
|
-
tagToEndpointQueryRecord: {}
|
|
1283
|
+
tagToEndpointQueryRecord: {},
|
|
1284
|
+
tagToSdkFunctionDescription: {}
|
|
1260
1285
|
}
|
|
1261
1286
|
};
|
|
1262
1287
|
const sortedPathsByLength = new Map(
|
|
@@ -1288,7 +1313,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1288
1313
|
tagToSdkClientRecord,
|
|
1289
1314
|
tagToSdkFunctionNamesRecord,
|
|
1290
1315
|
tagToSdkImportsRecord,
|
|
1291
|
-
tagToEndpointQueryRecord
|
|
1316
|
+
tagToEndpointQueryRecord,
|
|
1317
|
+
tagToSdkFunctionDescription
|
|
1292
1318
|
} = picked;
|
|
1293
1319
|
const tagToClassMethodsMapByType = isAdminEndpoint ? tagToClassMethodsMap.admin : tagToClassMethodsMap.public;
|
|
1294
1320
|
const generatedMethods = {};
|
|
@@ -1368,9 +1394,14 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1368
1394
|
description
|
|
1369
1395
|
});
|
|
1370
1396
|
tagToEndpointQueryRecord[tag] = (tagToEndpointQueryRecord[tag] || "") + queryMethodImpl;
|
|
1397
|
+
const fnDescription = extractDescription(description, { isDeprecated: deprecated, responseClasses });
|
|
1398
|
+
const tagFnDescriptions = tagToSdkFunctionDescription[tag];
|
|
1399
|
+
tagToSdkFunctionDescription[tag] = {
|
|
1400
|
+
...tagFnDescriptions,
|
|
1401
|
+
[classMethod]: fnDescription
|
|
1402
|
+
};
|
|
1371
1403
|
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1372
1404
|
classMethod,
|
|
1373
|
-
description,
|
|
1374
1405
|
httpMethod,
|
|
1375
1406
|
path: pathWithBase,
|
|
1376
1407
|
pathParams,
|
|
@@ -1379,7 +1410,6 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1379
1410
|
classGenName,
|
|
1380
1411
|
methodParams,
|
|
1381
1412
|
methodParamsNoTypes,
|
|
1382
|
-
deprecated,
|
|
1383
1413
|
xSecurity: endpoint["x-security"]
|
|
1384
1414
|
});
|
|
1385
1415
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
@@ -1751,7 +1781,8 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1751
1781
|
tagToSdkClientRecord,
|
|
1752
1782
|
tagToSdkFunctionNamesRecord,
|
|
1753
1783
|
tagToSdkImportsRecord,
|
|
1754
|
-
tagToEndpointQueryRecord
|
|
1784
|
+
tagToEndpointQueryRecord,
|
|
1785
|
+
tagToSdkFunctionDescription
|
|
1755
1786
|
} = parsedInformationByType;
|
|
1756
1787
|
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1757
1788
|
const apiList = [];
|
|
@@ -1775,7 +1806,7 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1775
1806
|
sdkName
|
|
1776
1807
|
);
|
|
1777
1808
|
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1778
|
-
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports,
|
|
1809
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports, tagToSdkFunctionDescription[tag]);
|
|
1779
1810
|
apiList.push(apiGenName);
|
|
1780
1811
|
indexImportsSet.add(
|
|
1781
1812
|
ParserUtils.getRelativePathToWebSdkSrcFolder(import_path3.default.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder)
|