@accelbyte/codegen 2.1.0 → 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 +6 -0
- package/dist/accelbyte-codegen.js +57 -36
- package/dist/accelbyte-codegen.js.map +1 -1
- package/dist/accelbyte-codegen.mjs +57 -36
- package/dist/accelbyte-codegen.mjs.map +1 -1
- package/package.json +3 -1
- package/legacy_changelog.md +0 -228
|
@@ -91,7 +91,7 @@ ${generateImports(body, importStatements, makeNewImportVarMap$1(), getImportable
|
|
|
91
91
|
|
|
92
92
|
export class ${className} {
|
|
93
93
|
// @ts-ignore
|
|
94
|
-
constructor(private axiosInstance: AxiosInstance, private namespace: string, private cache = false) {}
|
|
94
|
+
constructor(private axiosInstance: AxiosInstance, private namespace: string, private cache = false, private isValidationEnabled = true) {}
|
|
95
95
|
${body}
|
|
96
96
|
}
|
|
97
97
|
`;
|
|
@@ -116,6 +116,7 @@ export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
|
116
116
|
const namespace = args?.namespace ? args?.namespace : sdkAssembly.namespace
|
|
117
117
|
const cache = args?.cache ? args?.cache : sdkAssembly.cache
|
|
118
118
|
const requestConfig = ApiUtils.mergedConfigs(sdkAssembly.config, args)
|
|
119
|
+
const isValidationEnabled = args?.isValidationEnabled !== false
|
|
119
120
|
${body}
|
|
120
121
|
|
|
121
122
|
return {
|
|
@@ -917,7 +918,7 @@ const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
|
|
|
917
918
|
let imports = "";
|
|
918
919
|
let returnStatement = "";
|
|
919
920
|
for (const cl of apiList) {
|
|
920
|
-
const dir = cl.toLowerCase().includes("admin") ? "generated-admin" : "generated-public";
|
|
921
|
+
const dir = cl.toLowerCase().includes("admin") && cl !== "AdminApi" ? "generated-admin" : "generated-public";
|
|
921
922
|
imports += `
|
|
922
923
|
import { ${cl} } from './${dir}/${cl}.js'`;
|
|
923
924
|
returnStatement += `
|
|
@@ -997,7 +998,8 @@ const Endpoint = z.object({
|
|
|
997
998
|
schema: Schema.nullish()
|
|
998
999
|
})
|
|
999
1000
|
}).nullish()
|
|
1000
|
-
}).nullish()
|
|
1001
|
+
}).nullish(),
|
|
1002
|
+
"x-security": z.any().nullish()
|
|
1001
1003
|
});
|
|
1002
1004
|
const Operation = z.object({
|
|
1003
1005
|
get: Endpoint.nullish(),
|
|
@@ -1036,7 +1038,8 @@ const templateMethod = ({
|
|
|
1036
1038
|
bodyParams,
|
|
1037
1039
|
queryParams,
|
|
1038
1040
|
isFormUrlEncoded,
|
|
1039
|
-
responseClass
|
|
1041
|
+
responseClass,
|
|
1042
|
+
deprecated
|
|
1040
1043
|
}) => {
|
|
1041
1044
|
let methodParams = "";
|
|
1042
1045
|
let methodParamsNoTypes = "";
|
|
@@ -1072,7 +1075,7 @@ const templateMethod = ({
|
|
|
1072
1075
|
const isDelete = ["delete"].includes(httpMethod);
|
|
1073
1076
|
let dataPayload = "{params}";
|
|
1074
1077
|
const descriptionText = description ? `
|
|
1075
|
-
|
|
1078
|
+
/**${deprecated ? "\n * @deprecated" : ""}
|
|
1076
1079
|
* ${description.replace(/\n/g, "\n * ")}
|
|
1077
1080
|
*/` : "";
|
|
1078
1081
|
let formPayloadString = "";
|
|
@@ -1107,13 +1110,13 @@ const templateMethod = ({
|
|
|
1107
1110
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1108
1111
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1109
1112
|
|
|
1110
|
-
${httpMethod === "get" ? ` const res = () => Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}')
|
|
1113
|
+
${httpMethod === "get" ? ` const res = () => this.isValidationEnabled ? Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}') : Validate.unsafeResponse(() => resultPromise)
|
|
1111
1114
|
|
|
1112
1115
|
if (!this.cache) {
|
|
1113
1116
|
return SdkCache.withoutCache(res)
|
|
1114
1117
|
}
|
|
1115
1118
|
const cacheKey = url + CodeGenUtil.hashCode(JSON.stringify({ params }))
|
|
1116
|
-
return SdkCache.withCache(cacheKey, res)` : ""}${["post", "put", "patch", "delete"].includes(httpMethod) ? ` return Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}')` : ""}
|
|
1119
|
+
return SdkCache.withCache(cacheKey, res)` : ""}${["post", "put", "patch", "delete"].includes(httpMethod) ? ` return this.isValidationEnabled ? Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}') : Validate.unsafeResponse(() => resultPromise)` : ""}
|
|
1117
1120
|
}
|
|
1118
1121
|
`;
|
|
1119
1122
|
if (!isGuardInvoked) {
|
|
@@ -1145,17 +1148,14 @@ const templateApiMethod = ({
|
|
|
1145
1148
|
responseClass,
|
|
1146
1149
|
classGenName,
|
|
1147
1150
|
methodParams,
|
|
1148
|
-
methodParamsNoTypes
|
|
1151
|
+
methodParamsNoTypes,
|
|
1152
|
+
deprecated,
|
|
1153
|
+
xSecurity
|
|
1149
1154
|
}) => {
|
|
1150
|
-
let methodSignature = "";
|
|
1151
1155
|
let newPath = `'${path}'`;
|
|
1152
|
-
let
|
|
1153
|
-
let snippetShell = "";
|
|
1156
|
+
let snippetMethod = "";
|
|
1154
1157
|
for (const pathParam of pathParams) {
|
|
1155
1158
|
const type = ParserUtils.parseType(pathParam);
|
|
1156
|
-
if (pathParam.name !== "namespace") {
|
|
1157
|
-
methodSignature += pathParam.name + `:${type}, `;
|
|
1158
|
-
}
|
|
1159
1159
|
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
1160
1160
|
if (path.match(`{${pathParam.name}}`)) {
|
|
1161
1161
|
if (type === "string") {
|
|
@@ -1165,18 +1165,23 @@ const templateApiMethod = ({
|
|
|
1165
1165
|
}
|
|
1166
1166
|
}
|
|
1167
1167
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1168
|
+
const snippetShellArgs = ["--location --request", `${httpMethod} '__DOMAIN__${path}'`, "--header 'accept: application/json'"];
|
|
1169
|
+
const snippetApiArgs = [];
|
|
1170
|
+
if (xSecurity !== void 0 || path.includes("/admin")) {
|
|
1171
|
+
snippetShellArgs.push("--header 'Authorization: Bearer {access_token}'");
|
|
1172
|
+
snippetApiArgs.push("{ config: { headers: { Authorization: 'Bearer {access_token}' } }}".trim());
|
|
1173
|
+
}
|
|
1171
1174
|
if (httpMethod !== "get") {
|
|
1172
1175
|
const curlParams = bodyParams?.map((ob) => {
|
|
1173
1176
|
return ` "${ob.name}": ""`;
|
|
1174
1177
|
});
|
|
1175
|
-
|
|
1176
|
-
|
|
1178
|
+
if (curlParams.length > 0) {
|
|
1179
|
+
snippetShellArgs.push(`--data-raw '{ ${curlParams}}'`);
|
|
1180
|
+
}
|
|
1177
1181
|
}
|
|
1182
|
+
const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
|
|
1178
1183
|
const descriptionText = description ? `
|
|
1179
|
-
|
|
1184
|
+
/**${deprecated ? "\n * @deprecated" : ""}
|
|
1180
1185
|
* ${description.replace(/\n/g, "\n * ")}
|
|
1181
1186
|
*/` : "";
|
|
1182
1187
|
const resolvedResponseClass = responseClass || "unknown";
|
|
@@ -1184,20 +1189,31 @@ const templateApiMethod = ({
|
|
|
1184
1189
|
const methodImpl = `
|
|
1185
1190
|
${descriptionText}
|
|
1186
1191
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1187
|
-
const $ = new ${classGenName}(Network.create(requestConfig), namespace, cache)
|
|
1192
|
+
const $ = new ${classGenName}(Network.create(requestConfig), namespace, cache, isValidationEnabled)
|
|
1188
1193
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
1189
1194
|
if (resp.error) throw resp.error
|
|
1190
1195
|
return resp.response.data
|
|
1191
1196
|
}
|
|
1192
1197
|
`;
|
|
1193
1198
|
const snippetPromiseString = responseType !== "unknown" ? `Promise<${responseType}>` : "Promise";
|
|
1194
|
-
|
|
1199
|
+
snippetMethod += `${classMethod}(${methodParams})
|
|
1195
1200
|
// return ${snippetPromiseString}`;
|
|
1196
|
-
return
|
|
1201
|
+
return {
|
|
1202
|
+
generatedMethodString: methodImpl,
|
|
1203
|
+
snippetApiArgs,
|
|
1204
|
+
snippetMethod,
|
|
1205
|
+
snippetShell
|
|
1206
|
+
};
|
|
1197
1207
|
};
|
|
1198
1208
|
|
|
1199
|
-
const templateSdkSnippet = (
|
|
1200
|
-
|
|
1209
|
+
const templateSdkSnippet = ({
|
|
1210
|
+
serviceNameTitle,
|
|
1211
|
+
apiName,
|
|
1212
|
+
snippetMethod,
|
|
1213
|
+
snippetApiArgs: snippetApiArgsParam
|
|
1214
|
+
}) => {
|
|
1215
|
+
const methodArr = snippetMethod.split("//");
|
|
1216
|
+
const snippetApiArgs = ["sdk", ...snippetApiArgsParam];
|
|
1201
1217
|
let normMethod = normalizeMethodSnippet(methodArr[0].trim(), "data:");
|
|
1202
1218
|
normMethod = normalizeMethodSnippet(normMethod, "queryParams:");
|
|
1203
1219
|
normMethod = normalizeMethodSnippet(normMethod, "queryParams?:");
|
|
@@ -1206,14 +1222,16 @@ const templateSdkSnippet = (serviceNameTitle, apiName, methodSnippet) => {
|
|
|
1206
1222
|
import { ${serviceNameTitle} } from '@accelbyte/sdk-${serviceNameTitle.toLowerCase()}'
|
|
1207
1223
|
|
|
1208
1224
|
const sdk = Accelbyte.SDK({
|
|
1225
|
+
options: {
|
|
1209
1226
|
baseURL: 'https://demo.accelbyte.io',
|
|
1210
1227
|
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
1211
1228
|
namespace: 'accelbyte',
|
|
1212
1229
|
redirectURI: 'http://localhost:3030'
|
|
1230
|
+
}
|
|
1213
1231
|
})
|
|
1214
1232
|
|
|
1215
|
-
${serviceNameTitle}.${apiName}(
|
|
1216
|
-
|
|
1233
|
+
${serviceNameTitle}.${apiName}(${snippetApiArgs.join(", ")})
|
|
1234
|
+
.${normMethod}`;
|
|
1217
1235
|
return sdkSnippet;
|
|
1218
1236
|
};
|
|
1219
1237
|
const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
@@ -1297,7 +1315,7 @@ class SwaggerReaderHelpers {
|
|
|
1297
1315
|
console.error(JSON.stringify({ path, httpMethod }, null, 2));
|
|
1298
1316
|
throw error;
|
|
1299
1317
|
});
|
|
1300
|
-
if (!endpoint.tags
|
|
1318
|
+
if (!endpoint.tags)
|
|
1301
1319
|
continue;
|
|
1302
1320
|
const [tag] = endpoint.tags;
|
|
1303
1321
|
const pathWithBase = `${api.basePath ?? ""}${path}`;
|
|
@@ -1329,6 +1347,7 @@ class SwaggerReaderHelpers {
|
|
|
1329
1347
|
const isFormUrlEncoded = ParserUtils.isFormUrlEncoded(httpMethod, endpoint.consumes);
|
|
1330
1348
|
const pathParams = ParserUtils.filterPathParams(endpoint.parameters);
|
|
1331
1349
|
let bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
|
|
1350
|
+
const deprecated = !!endpoint.deprecated;
|
|
1332
1351
|
if (endpoint.requestBody) {
|
|
1333
1352
|
bodyParams = [
|
|
1334
1353
|
{
|
|
@@ -1347,10 +1366,11 @@ class SwaggerReaderHelpers {
|
|
|
1347
1366
|
bodyParams,
|
|
1348
1367
|
queryParams,
|
|
1349
1368
|
isFormUrlEncoded,
|
|
1350
|
-
responseClass
|
|
1369
|
+
responseClass,
|
|
1370
|
+
deprecated
|
|
1351
1371
|
});
|
|
1352
1372
|
tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
|
|
1353
|
-
const
|
|
1373
|
+
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1354
1374
|
classMethod,
|
|
1355
1375
|
description,
|
|
1356
1376
|
httpMethod,
|
|
@@ -1360,20 +1380,21 @@ class SwaggerReaderHelpers {
|
|
|
1360
1380
|
responseClass,
|
|
1361
1381
|
classGenName,
|
|
1362
1382
|
methodParams,
|
|
1363
|
-
methodParamsNoTypes
|
|
1383
|
+
methodParamsNoTypes,
|
|
1384
|
+
deprecated,
|
|
1385
|
+
xSecurity: endpoint["x-security"]
|
|
1364
1386
|
});
|
|
1365
1387
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
1366
1388
|
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
|
|
1367
1389
|
tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
|
|
1368
1390
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1369
1391
|
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1370
|
-
const resultSnippet = templateSdkSnippet(serviceNameTitle, apiGenName, snippetMethod);
|
|
1392
|
+
const resultSnippet = templateSdkSnippet({ serviceNameTitle, apiName: apiGenName, snippetMethod, snippetApiArgs });
|
|
1371
1393
|
const currentSnippetMap = {};
|
|
1372
1394
|
snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
}
|
|
1395
|
+
currentSnippetMap.web = resultSnippet;
|
|
1396
|
+
const generatedDirName = isAdminEndpoint ? "generated-admin" : "generated-public";
|
|
1397
|
+
currentSnippetMap.webGit = GIT_URL + `/sdk-${sdkName}/src/${generatedDirName}/${apiGenName}.ts`;
|
|
1377
1398
|
currentSnippetMap.shell = snippetShell;
|
|
1378
1399
|
}
|
|
1379
1400
|
}
|