@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.
|
@@ -124,6 +124,18 @@ var extractDescription = (description, options) => {
|
|
|
124
124
|
indentation
|
|
125
125
|
}) : "";
|
|
126
126
|
};
|
|
127
|
+
var getResponseType = ({
|
|
128
|
+
responseClasses,
|
|
129
|
+
defaultType = "unknown"
|
|
130
|
+
}) => {
|
|
131
|
+
const responseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
|
|
132
|
+
const responseType = responseClass !== "unknown" ? responseClasses?.[0] : defaultType;
|
|
133
|
+
return {
|
|
134
|
+
responseType,
|
|
135
|
+
responseTypeInAxiosResponse: `Promise<AxiosResponse<${responseType}>>`,
|
|
136
|
+
responseTypeInResponse: `Promise<Response<${responseType}>>`
|
|
137
|
+
};
|
|
138
|
+
};
|
|
127
139
|
|
|
128
140
|
// src/templates/template-class.ts
|
|
129
141
|
var getImportableVarMap = () => ({
|
|
@@ -177,6 +189,12 @@ var makeNewImportVarMap2 = () => ({
|
|
|
177
189
|
axios: ["AxiosRequestConfig", "AxiosResponse"]
|
|
178
190
|
});
|
|
179
191
|
var templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
192
|
+
const returnsMethodsWithDescription = Object.keys(returnMethods).reduce((acc, key) => {
|
|
193
|
+
acc += `
|
|
194
|
+
${returnMethods[key]}
|
|
195
|
+
${key},`;
|
|
196
|
+
return acc;
|
|
197
|
+
}, "");
|
|
180
198
|
const $className = className.replace(/Api$/, "$");
|
|
181
199
|
return `/**
|
|
182
200
|
* AUTO GENERATED
|
|
@@ -191,25 +209,35 @@ export function ${className}(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
|
|
|
191
209
|
const sdkAssembly = sdk.assembly()
|
|
192
210
|
|
|
193
211
|
const namespace = args?.coreConfig?.namespace ?? sdkAssembly.coreConfig.namespace
|
|
194
|
-
const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, {...(args?.coreConfig?.baseURL ? {baseURL: args?.coreConfig?.baseURL} : {}), ...args?.axiosConfig?.request})
|
|
195
|
-
const interceptors = args?.axiosConfig?.interceptors ?? sdkAssembly.axiosConfig.interceptors ?? []
|
|
196
212
|
const useSchemaValidation = args?.coreConfig?.useSchemaValidation ?? sdkAssembly.coreConfig.useSchemaValidation
|
|
197
|
-
const axiosInstance = Network.create(requestConfig)
|
|
198
213
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
214
|
+
let axiosInstance = sdkAssembly.axiosInstance
|
|
215
|
+
const requestConfigOverrides = args?.axiosConfig?.request
|
|
216
|
+
const baseURLOverride = args?.coreConfig?.baseURL
|
|
217
|
+
const interceptorsOverride = args?.axiosConfig?.interceptors ?? []
|
|
203
218
|
|
|
204
|
-
|
|
205
|
-
|
|
219
|
+
if (requestConfigOverrides || baseURLOverride || interceptorsOverride.length > 0) {
|
|
220
|
+
const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, {
|
|
221
|
+
...(baseURLOverride ? { baseURL: baseURLOverride } : {}),
|
|
222
|
+
...requestConfigOverrides
|
|
223
|
+
})
|
|
224
|
+
axiosInstance = Network.create(requestConfig)
|
|
225
|
+
|
|
226
|
+
for (const interceptor of interceptorsOverride) {
|
|
227
|
+
if (interceptor.type === 'request') {
|
|
228
|
+
axiosInstance.interceptors.request.use(interceptor.onRequest, interceptor.onError)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (interceptor.type === 'response') {
|
|
232
|
+
axiosInstance.interceptors.response.use(interceptor.onSuccess, interceptor.onError)
|
|
233
|
+
}
|
|
206
234
|
}
|
|
207
235
|
}
|
|
208
236
|
|
|
209
237
|
${body}
|
|
210
238
|
|
|
211
239
|
return {
|
|
212
|
-
${
|
|
240
|
+
${returnsMethodsWithDescription}
|
|
213
241
|
}
|
|
214
242
|
}
|
|
215
243
|
`;
|
|
@@ -594,12 +622,12 @@ var ParserUtils = class _ParserUtils {
|
|
|
594
622
|
fs3.writeFileSync(`${distDir}/version.json`, JSON.stringify(customVersion, null, 2));
|
|
595
623
|
}
|
|
596
624
|
}
|
|
597
|
-
static writeApiFile(distDir, apiName, apiBuffer, imports,
|
|
625
|
+
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethodsDescription) {
|
|
598
626
|
const newImports = [];
|
|
599
627
|
imports.forEach((el) => {
|
|
600
628
|
newImports.push(el.replace("../../generated-definitions", "../generated-definitions"));
|
|
601
629
|
});
|
|
602
|
-
const fileContent = templateApiClass(apiName, apiBuffer, newImports,
|
|
630
|
+
const fileContent = templateApiClass(apiName, apiBuffer, newImports, returnMethodsDescription);
|
|
603
631
|
fs3.writeFileSync(`${distDir}/${apiName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
604
632
|
}
|
|
605
633
|
static writeApiMainFile(distDir, serviceName, fileContent) {
|
|
@@ -862,7 +890,6 @@ var OpenApiSpec = z2.object({
|
|
|
862
890
|
// src/templates/template-api-method.ts
|
|
863
891
|
var templateApiMethod = ({
|
|
864
892
|
classMethod,
|
|
865
|
-
description,
|
|
866
893
|
httpMethod,
|
|
867
894
|
path: path7,
|
|
868
895
|
pathParams,
|
|
@@ -871,7 +898,6 @@ var templateApiMethod = ({
|
|
|
871
898
|
classGenName,
|
|
872
899
|
methodParams,
|
|
873
900
|
methodParamsNoTypes,
|
|
874
|
-
deprecated,
|
|
875
901
|
xSecurity
|
|
876
902
|
}) => {
|
|
877
903
|
let newPath = `'${path7}'`;
|
|
@@ -902,12 +928,9 @@ var templateApiMethod = ({
|
|
|
902
928
|
}
|
|
903
929
|
}
|
|
904
930
|
const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
|
|
905
|
-
const
|
|
906
|
-
const resolvedResponseClass = responseClasses.length === 1 ? responseClasses?.[0] : "unknown";
|
|
907
|
-
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
931
|
+
const { responseType, responseTypeInAxiosResponse } = getResponseType({ responseClasses });
|
|
908
932
|
const methodImpl = `
|
|
909
|
-
${
|
|
910
|
-
async function ${classMethod}(${methodParams}): Promise<AxiosResponse<${responseType}>> {
|
|
933
|
+
async function ${classMethod}(${methodParams}): ${responseTypeInAxiosResponse} {
|
|
911
934
|
const $ = new ${classGenName}(axiosInstance, namespace, useSchemaValidation)
|
|
912
935
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
913
936
|
if (resp.error) throw resp.error
|
|
@@ -983,14 +1006,12 @@ var templateMethod = ({
|
|
|
983
1006
|
dataPayload = dataType ? `{data, params}` : "{params}";
|
|
984
1007
|
}
|
|
985
1008
|
const isFileUpload = methodParams.indexOf("data: {file") > -1;
|
|
986
|
-
const
|
|
987
|
-
const resolvedResponseClassValidated =
|
|
1009
|
+
const { responseType, responseTypeInResponse } = getResponseType({ responseClasses });
|
|
1010
|
+
const resolvedResponseClassValidated = responseType !== "unknown" ? `${responseType}` : "z.unknown()";
|
|
988
1011
|
methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
|
|
989
1012
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
990
1013
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
991
|
-
const
|
|
992
|
-
const responseSyncType = "Response";
|
|
993
|
-
const generatedMethodName = `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
1014
|
+
const generatedMethodName = `${classMethod}(${methodParams}): ${responseTypeInResponse}`;
|
|
994
1015
|
let methodImpl = `${descriptionText}
|
|
995
1016
|
${generatedMethodName} {
|
|
996
1017
|
${queryParamsDefault}
|
|
@@ -1048,9 +1069,8 @@ var templateQueryMethod = ({
|
|
|
1048
1069
|
}
|
|
1049
1070
|
}
|
|
1050
1071
|
}
|
|
1051
|
-
const
|
|
1052
|
-
let _methodName = convertMethodNameToHook({ classMethod, apiGenName,
|
|
1053
|
-
const _responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1072
|
+
const { responseType } = getResponseType({ responseClasses });
|
|
1073
|
+
let _methodName = convertMethodNameToHook({ classMethod, apiGenName, isGet });
|
|
1054
1074
|
const _methodParams = methodParams && methodParams.length > 0 ? `& { ${methodParams} }` : "";
|
|
1055
1075
|
const _methodParamsImpl = convertToMethodImplArgs(methodParams);
|
|
1056
1076
|
const queryKey = createQueryKey(apiGenName, classMethod);
|
|
@@ -1065,9 +1085,9 @@ ${descriptionText}
|
|
|
1065
1085
|
export const ${_methodName} = (
|
|
1066
1086
|
sdk: AccelByteSDK,
|
|
1067
1087
|
input: SdkSetConfigParam ${_methodParams},
|
|
1068
|
-
options?: Omit<UseQueryOptions<${
|
|
1069
|
-
callback?: (data: AxiosResponse<${
|
|
1070
|
-
): UseQueryResult<${
|
|
1088
|
+
options?: Omit<UseQueryOptions<${responseType}, AxiosError<ApiError>>, 'queryKey'>,
|
|
1089
|
+
callback?: (data: AxiosResponse<${responseType}>) => void
|
|
1090
|
+
): UseQueryResult<${responseType}, AxiosError<ApiError>> => {
|
|
1071
1091
|
|
|
1072
1092
|
const queryFn = (
|
|
1073
1093
|
sdk: AccelByteSDK,
|
|
@@ -1080,7 +1100,7 @@ export const ${_methodName} = (
|
|
|
1080
1100
|
return response.data
|
|
1081
1101
|
}
|
|
1082
1102
|
|
|
1083
|
-
return ${queryMethod}<${
|
|
1103
|
+
return ${queryMethod}<${responseType}, AxiosError<ApiError>>({
|
|
1084
1104
|
queryKey: [${queryKey}, input],
|
|
1085
1105
|
queryFn: queryFn(sdk, input),
|
|
1086
1106
|
...options
|
|
@@ -1093,9 +1113,9 @@ export const ${_methodName} = (
|
|
|
1093
1113
|
${descriptionText}
|
|
1094
1114
|
export const ${_methodName} = (
|
|
1095
1115
|
sdk: AccelByteSDK,
|
|
1096
|
-
options?: Omit<UseMutationOptions<${
|
|
1097
|
-
callback?: (data: ${
|
|
1098
|
-
): UseMutationResult<${
|
|
1116
|
+
options?: Omit<UseMutationOptions<${responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}>, 'mutationKey'>,
|
|
1117
|
+
callback?: (data: ${responseType}) => void
|
|
1118
|
+
): UseMutationResult<${responseType}, AxiosError<ApiError>, SdkSetConfigParam ${_methodParams}> => {
|
|
1099
1119
|
|
|
1100
1120
|
const mutationFn = async (input: SdkSetConfigParam ${_methodParams}) => {
|
|
1101
1121
|
const response =
|
|
@@ -1115,6 +1135,13 @@ export const ${_methodName} = (
|
|
|
1115
1135
|
`;
|
|
1116
1136
|
return isGet ? queryMethodImpl : mutationMethodImpl;
|
|
1117
1137
|
};
|
|
1138
|
+
function versionMutationSuffixMethodName(baseMethodName) {
|
|
1139
|
+
const parts = baseMethodName.split(/(_v\d+)$/);
|
|
1140
|
+
const name = parts[0];
|
|
1141
|
+
const versionSuffix = parts[1] || "";
|
|
1142
|
+
const res = `${name}Mutation${versionSuffix}`;
|
|
1143
|
+
return res;
|
|
1144
|
+
}
|
|
1118
1145
|
function createQueryKey(className, methodName) {
|
|
1119
1146
|
const prefixRegex = /^(get|create|update|delete|patch|post|fetch)[_]?/i;
|
|
1120
1147
|
const cleanedMethodName = methodName.replace(prefixRegex, "").trim();
|
|
@@ -1130,15 +1157,11 @@ var prefixMappings = {
|
|
|
1130
1157
|
post: "usePost",
|
|
1131
1158
|
fetch: "useFetch"
|
|
1132
1159
|
};
|
|
1133
|
-
function convertMethodNameToHook({
|
|
1134
|
-
|
|
1135
|
-
apiGenName,
|
|
1136
|
-
isPostFetch,
|
|
1137
|
-
isFetch
|
|
1138
|
-
}) {
|
|
1139
|
-
for (const [originalPrefix, newPrefix] of Object.entries(prefixMappings)) {
|
|
1160
|
+
function convertMethodNameToHook({ classMethod, apiGenName, isGet }) {
|
|
1161
|
+
for (const [originalPrefix] of Object.entries(prefixMappings)) {
|
|
1140
1162
|
if (classMethod.startsWith(originalPrefix)) {
|
|
1141
|
-
|
|
1163
|
+
const methodName = !isGet ? versionMutationSuffixMethodName(classMethod) : classMethod;
|
|
1164
|
+
return `use${apiGenName}_${capitalize(methodName)}`;
|
|
1142
1165
|
}
|
|
1143
1166
|
}
|
|
1144
1167
|
return classMethod;
|
|
@@ -1224,7 +1247,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1224
1247
|
tagToSdkClientRecord: {},
|
|
1225
1248
|
tagToSdkFunctionNamesRecord: {},
|
|
1226
1249
|
tagToSdkImportsRecord: {},
|
|
1227
|
-
tagToEndpointQueryRecord: {}
|
|
1250
|
+
tagToEndpointQueryRecord: {},
|
|
1251
|
+
tagToSdkFunctionDescription: {}
|
|
1228
1252
|
},
|
|
1229
1253
|
public: {
|
|
1230
1254
|
arrayDefinitions: [],
|
|
@@ -1234,7 +1258,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1234
1258
|
tagToSdkClientRecord: {},
|
|
1235
1259
|
tagToSdkFunctionNamesRecord: {},
|
|
1236
1260
|
tagToSdkImportsRecord: {},
|
|
1237
|
-
tagToEndpointQueryRecord: {}
|
|
1261
|
+
tagToEndpointQueryRecord: {},
|
|
1262
|
+
tagToSdkFunctionDescription: {}
|
|
1238
1263
|
}
|
|
1239
1264
|
};
|
|
1240
1265
|
const sortedPathsByLength = new Map(
|
|
@@ -1266,7 +1291,8 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1266
1291
|
tagToSdkClientRecord,
|
|
1267
1292
|
tagToSdkFunctionNamesRecord,
|
|
1268
1293
|
tagToSdkImportsRecord,
|
|
1269
|
-
tagToEndpointQueryRecord
|
|
1294
|
+
tagToEndpointQueryRecord,
|
|
1295
|
+
tagToSdkFunctionDescription
|
|
1270
1296
|
} = picked;
|
|
1271
1297
|
const tagToClassMethodsMapByType = isAdminEndpoint ? tagToClassMethodsMap.admin : tagToClassMethodsMap.public;
|
|
1272
1298
|
const generatedMethods = {};
|
|
@@ -1346,9 +1372,14 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1346
1372
|
description
|
|
1347
1373
|
});
|
|
1348
1374
|
tagToEndpointQueryRecord[tag] = (tagToEndpointQueryRecord[tag] || "") + queryMethodImpl;
|
|
1375
|
+
const fnDescription = extractDescription(description, { isDeprecated: deprecated, responseClasses });
|
|
1376
|
+
const tagFnDescriptions = tagToSdkFunctionDescription[tag];
|
|
1377
|
+
tagToSdkFunctionDescription[tag] = {
|
|
1378
|
+
...tagFnDescriptions,
|
|
1379
|
+
[classMethod]: fnDescription
|
|
1380
|
+
};
|
|
1349
1381
|
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1350
1382
|
classMethod,
|
|
1351
|
-
description,
|
|
1352
1383
|
httpMethod,
|
|
1353
1384
|
path: pathWithBase,
|
|
1354
1385
|
pathParams,
|
|
@@ -1357,7 +1388,6 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1357
1388
|
classGenName,
|
|
1358
1389
|
methodParams,
|
|
1359
1390
|
methodParamsNoTypes,
|
|
1360
|
-
deprecated,
|
|
1361
1391
|
xSecurity: endpoint["x-security"]
|
|
1362
1392
|
});
|
|
1363
1393
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
@@ -1729,7 +1759,8 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1729
1759
|
tagToSdkClientRecord,
|
|
1730
1760
|
tagToSdkFunctionNamesRecord,
|
|
1731
1761
|
tagToSdkImportsRecord,
|
|
1732
|
-
tagToEndpointQueryRecord
|
|
1762
|
+
tagToEndpointQueryRecord,
|
|
1763
|
+
tagToSdkFunctionDescription
|
|
1733
1764
|
} = parsedInformationByType;
|
|
1734
1765
|
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1735
1766
|
const apiList = [];
|
|
@@ -1753,7 +1784,7 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1753
1784
|
sdkName
|
|
1754
1785
|
);
|
|
1755
1786
|
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1756
|
-
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports,
|
|
1787
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports, tagToSdkFunctionDescription[tag]);
|
|
1757
1788
|
apiList.push(apiGenName);
|
|
1758
1789
|
indexImportsSet.add(
|
|
1759
1790
|
ParserUtils.getRelativePathToWebSdkSrcFolder(path4.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder)
|