@accelbyte/codegen 2.2.1 → 3.0.3
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 +46 -39
- package/dist/accelbyte-codegen.js.map +1 -1
- package/dist/accelbyte-codegen.mjs +46 -39
- package/dist/accelbyte-codegen.mjs.map +1 -1
- package/package.json +3 -1
- package/legacy_changelog.md +0 -228
|
@@ -58,7 +58,7 @@ class CliParser {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
const getImportableVarMap$1 = () => ({
|
|
61
|
-
"@accelbyte/sdk": ["CodeGenUtil", "
|
|
61
|
+
"@accelbyte/sdk": ["CodeGenUtil", "IResponse", "Validate"],
|
|
62
62
|
axios: ["AxiosRequestConfig", "AxiosResponse"],
|
|
63
63
|
zod: ["z"]
|
|
64
64
|
});
|
|
@@ -91,14 +91,14 @@ ${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
|
|
94
|
+
constructor(private axiosInstance: AxiosInstance, private namespace: string, private isValidationEnabled = true) {}
|
|
95
95
|
${body}
|
|
96
96
|
}
|
|
97
97
|
`;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
100
|
const getImportableVarMap = () => ({
|
|
101
|
-
"@accelbyte/sdk": ["CodeGenUtil", "
|
|
101
|
+
"@accelbyte/sdk": ["CodeGenUtil", "IResponse", "Validate", "ApiArgs", "Network", "AccelbyteSDK"]
|
|
102
102
|
});
|
|
103
103
|
const makeNewImportVarMap = () => ({
|
|
104
104
|
"@accelbyte/sdk": ["AccelbyteSDK", "ApiArgs", "ApiUtils"]
|
|
@@ -114,7 +114,6 @@ export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
|
114
114
|
const sdkAssembly = sdk.assembly()
|
|
115
115
|
|
|
116
116
|
const namespace = args?.namespace ? args?.namespace : sdkAssembly.namespace
|
|
117
|
-
const cache = args?.cache ? args?.cache : sdkAssembly.cache
|
|
118
117
|
const requestConfig = ApiUtils.mergedConfigs(sdkAssembly.config, args)
|
|
119
118
|
const isValidationEnabled = args?.isValidationEnabled !== false
|
|
120
119
|
${body}
|
|
@@ -918,7 +917,7 @@ const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
|
|
|
918
917
|
let imports = "";
|
|
919
918
|
let returnStatement = "";
|
|
920
919
|
for (const cl of apiList) {
|
|
921
|
-
const dir = cl.toLowerCase().includes("admin") ? "generated-admin" : "generated-public";
|
|
920
|
+
const dir = cl.toLowerCase().includes("admin") && cl !== "AdminApi" ? "generated-admin" : "generated-public";
|
|
922
921
|
imports += `
|
|
923
922
|
import { ${cl} } from './${dir}/${cl}.js'`;
|
|
924
923
|
returnStatement += `
|
|
@@ -998,7 +997,8 @@ const Endpoint = z.object({
|
|
|
998
997
|
schema: Schema.nullish()
|
|
999
998
|
})
|
|
1000
999
|
}).nullish()
|
|
1001
|
-
}).nullish()
|
|
1000
|
+
}).nullish(),
|
|
1001
|
+
"x-security": z.any().nullish()
|
|
1002
1002
|
});
|
|
1003
1003
|
const Operation = z.object({
|
|
1004
1004
|
get: Endpoint.nullish(),
|
|
@@ -1094,28 +1094,20 @@ const templateMethod = ({
|
|
|
1094
1094
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
1095
1095
|
let methodImpl = "";
|
|
1096
1096
|
const isCacheFetch = ["get"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
1097
|
-
const cachedFetchMethod = classMethod;
|
|
1098
1097
|
const deprecateTag = isCacheFetch ? `/**
|
|
1099
1098
|
* @deprecated Use "${classMethod}()" instead.
|
|
1100
1099
|
*/` : "";
|
|
1101
1100
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
1102
|
-
const methodName = httpMethod === "get" ? cachedFetchMethod : ["post", "put", "patch", "delete"].includes(httpMethod) ? classMethod : "";
|
|
1103
1101
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1104
|
-
const generateMethodName = () => `${
|
|
1105
|
-
const responseSyncType =
|
|
1102
|
+
const generateMethodName = () => `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
1103
|
+
const responseSyncType = "IResponse";
|
|
1106
1104
|
methodImpl = `${descriptionText}
|
|
1107
1105
|
${generateMethodName()} {
|
|
1108
1106
|
${queryParamsDefault}
|
|
1109
1107
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1110
1108
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1111
1109
|
|
|
1112
|
-
${
|
|
1113
|
-
|
|
1114
|
-
if (!this.cache) {
|
|
1115
|
-
return SdkCache.withoutCache(res)
|
|
1116
|
-
}
|
|
1117
|
-
const cacheKey = url + CodeGenUtil.hashCode(JSON.stringify({ params }))
|
|
1118
|
-
return SdkCache.withCache(cacheKey, res)` : ""}${["post", "put", "patch", "delete"].includes(httpMethod) ? ` return this.isValidationEnabled ? Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}') : Validate.unsafeResponse(() => resultPromise)` : ""}
|
|
1110
|
+
${` return this.isValidationEnabled ? Validate.responseType(() => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}') : Validate.unsafeResponse(() => resultPromise)`}
|
|
1119
1111
|
}
|
|
1120
1112
|
`;
|
|
1121
1113
|
if (!isGuardInvoked) {
|
|
@@ -1148,17 +1140,13 @@ const templateApiMethod = ({
|
|
|
1148
1140
|
classGenName,
|
|
1149
1141
|
methodParams,
|
|
1150
1142
|
methodParamsNoTypes,
|
|
1151
|
-
deprecated
|
|
1143
|
+
deprecated,
|
|
1144
|
+
xSecurity
|
|
1152
1145
|
}) => {
|
|
1153
|
-
let methodSignature = "";
|
|
1154
1146
|
let newPath = `'${path}'`;
|
|
1155
|
-
let
|
|
1156
|
-
let snippetShell = "";
|
|
1147
|
+
let snippetMethod = "";
|
|
1157
1148
|
for (const pathParam of pathParams) {
|
|
1158
1149
|
const type = ParserUtils.parseType(pathParam);
|
|
1159
|
-
if (pathParam.name !== "namespace") {
|
|
1160
|
-
methodSignature += pathParam.name + `:${type}, `;
|
|
1161
|
-
}
|
|
1162
1150
|
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
1163
1151
|
if (path.match(`{${pathParam.name}}`)) {
|
|
1164
1152
|
if (type === "string") {
|
|
@@ -1168,16 +1156,21 @@ const templateApiMethod = ({
|
|
|
1168
1156
|
}
|
|
1169
1157
|
}
|
|
1170
1158
|
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1159
|
+
const snippetShellArgs = ["--location --request", `${httpMethod} '__DOMAIN__${path}'`, "--header 'accept: application/json'"];
|
|
1160
|
+
const snippetApiArgs = [];
|
|
1161
|
+
if (xSecurity !== void 0 || path.includes("/admin")) {
|
|
1162
|
+
snippetShellArgs.push("--header 'Authorization: Bearer {access_token}'");
|
|
1163
|
+
snippetApiArgs.push("{ config: { headers: { Authorization: 'Bearer {access_token}' } }}".trim());
|
|
1164
|
+
}
|
|
1174
1165
|
if (httpMethod !== "get") {
|
|
1175
1166
|
const curlParams = bodyParams?.map((ob) => {
|
|
1176
1167
|
return ` "${ob.name}": ""`;
|
|
1177
1168
|
});
|
|
1178
|
-
|
|
1179
|
-
|
|
1169
|
+
if (curlParams.length > 0) {
|
|
1170
|
+
snippetShellArgs.push(`--data-raw '{ ${curlParams}}'`);
|
|
1171
|
+
}
|
|
1180
1172
|
}
|
|
1173
|
+
const snippetShell = `curl ${snippetShellArgs.join(" \\\n ")}`;
|
|
1181
1174
|
const descriptionText = description ? `
|
|
1182
1175
|
/**${deprecated ? "\n * @deprecated" : ""}
|
|
1183
1176
|
* ${description.replace(/\n/g, "\n * ")}
|
|
@@ -1187,20 +1180,31 @@ const templateApiMethod = ({
|
|
|
1187
1180
|
const methodImpl = `
|
|
1188
1181
|
${descriptionText}
|
|
1189
1182
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1190
|
-
const $ = new ${classGenName}(Network.create(requestConfig), namespace,
|
|
1183
|
+
const $ = new ${classGenName}(Network.create(requestConfig), namespace, isValidationEnabled)
|
|
1191
1184
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
1192
1185
|
if (resp.error) throw resp.error
|
|
1193
1186
|
return resp.response.data
|
|
1194
1187
|
}
|
|
1195
1188
|
`;
|
|
1196
1189
|
const snippetPromiseString = responseType !== "unknown" ? `Promise<${responseType}>` : "Promise";
|
|
1197
|
-
|
|
1190
|
+
snippetMethod += `${classMethod}(${methodParams})
|
|
1198
1191
|
// return ${snippetPromiseString}`;
|
|
1199
|
-
return
|
|
1192
|
+
return {
|
|
1193
|
+
generatedMethodString: methodImpl,
|
|
1194
|
+
snippetApiArgs,
|
|
1195
|
+
snippetMethod,
|
|
1196
|
+
snippetShell
|
|
1197
|
+
};
|
|
1200
1198
|
};
|
|
1201
1199
|
|
|
1202
|
-
const templateSdkSnippet = (
|
|
1203
|
-
|
|
1200
|
+
const templateSdkSnippet = ({
|
|
1201
|
+
serviceNameTitle,
|
|
1202
|
+
apiName,
|
|
1203
|
+
snippetMethod,
|
|
1204
|
+
snippetApiArgs: snippetApiArgsParam
|
|
1205
|
+
}) => {
|
|
1206
|
+
const methodArr = snippetMethod.split("//");
|
|
1207
|
+
const snippetApiArgs = ["sdk", ...snippetApiArgsParam];
|
|
1204
1208
|
let normMethod = normalizeMethodSnippet(methodArr[0].trim(), "data:");
|
|
1205
1209
|
normMethod = normalizeMethodSnippet(normMethod, "queryParams:");
|
|
1206
1210
|
normMethod = normalizeMethodSnippet(normMethod, "queryParams?:");
|
|
@@ -1209,14 +1213,16 @@ const templateSdkSnippet = (serviceNameTitle, apiName, methodSnippet) => {
|
|
|
1209
1213
|
import { ${serviceNameTitle} } from '@accelbyte/sdk-${serviceNameTitle.toLowerCase()}'
|
|
1210
1214
|
|
|
1211
1215
|
const sdk = Accelbyte.SDK({
|
|
1216
|
+
options: {
|
|
1212
1217
|
baseURL: 'https://demo.accelbyte.io',
|
|
1213
1218
|
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
1214
1219
|
namespace: 'accelbyte',
|
|
1215
1220
|
redirectURI: 'http://localhost:3030'
|
|
1221
|
+
}
|
|
1216
1222
|
})
|
|
1217
1223
|
|
|
1218
|
-
${serviceNameTitle}.${apiName}(
|
|
1219
|
-
|
|
1224
|
+
${serviceNameTitle}.${apiName}(${snippetApiArgs.join(", ")})
|
|
1225
|
+
.${normMethod}`;
|
|
1220
1226
|
return sdkSnippet;
|
|
1221
1227
|
};
|
|
1222
1228
|
const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
@@ -1355,7 +1361,7 @@ class SwaggerReaderHelpers {
|
|
|
1355
1361
|
deprecated
|
|
1356
1362
|
});
|
|
1357
1363
|
tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
|
|
1358
|
-
const
|
|
1364
|
+
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1359
1365
|
classMethod,
|
|
1360
1366
|
description,
|
|
1361
1367
|
httpMethod,
|
|
@@ -1366,14 +1372,15 @@ class SwaggerReaderHelpers {
|
|
|
1366
1372
|
classGenName,
|
|
1367
1373
|
methodParams,
|
|
1368
1374
|
methodParamsNoTypes,
|
|
1369
|
-
deprecated
|
|
1375
|
+
deprecated,
|
|
1376
|
+
xSecurity: endpoint["x-security"]
|
|
1370
1377
|
});
|
|
1371
1378
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
1372
1379
|
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
|
|
1373
1380
|
tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
|
|
1374
1381
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1375
1382
|
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1376
|
-
const resultSnippet = templateSdkSnippet(serviceNameTitle, apiGenName, snippetMethod);
|
|
1383
|
+
const resultSnippet = templateSdkSnippet({ serviceNameTitle, apiName: apiGenName, snippetMethod, snippetApiArgs });
|
|
1377
1384
|
const currentSnippetMap = {};
|
|
1378
1385
|
snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
|
|
1379
1386
|
currentSnippetMap.web = resultSnippet;
|