@accelbyte/codegen 1.0.0-beta.12 → 1.0.0-beta.16
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.
|
@@ -65,9 +65,9 @@ const makeNewImportVarMap$1 = () => ({
|
|
|
65
65
|
axios: ["AxiosInstance"],
|
|
66
66
|
"@accelbyte/sdk": ["SDKRequestConfig"]
|
|
67
67
|
});
|
|
68
|
-
const generateImports
|
|
69
|
-
const usedImportVarMap =
|
|
70
|
-
const importableVarMap =
|
|
68
|
+
const generateImports = (body, importStatements, makeNewImportVarMap2, getImportableVarMap2) => {
|
|
69
|
+
const usedImportVarMap = makeNewImportVarMap2;
|
|
70
|
+
const importableVarMap = getImportableVarMap2;
|
|
71
71
|
for (const [moduleSource, importableVars] of Object.entries(importableVarMap)) {
|
|
72
72
|
for (const importableVar of importableVars) {
|
|
73
73
|
const importVarRegex = new RegExp(`(?<![\\d\\w_])${importableVar}(?![\\d\\w_])`);
|
|
@@ -84,7 +84,7 @@ const templateClass = (className, body, importStatements) => {
|
|
|
84
84
|
return `/**
|
|
85
85
|
* AUTO GENERATED
|
|
86
86
|
*/
|
|
87
|
-
${generateImports
|
|
87
|
+
${generateImports(body, importStatements, makeNewImportVarMap$1(), getImportableVarMap$1())}
|
|
88
88
|
|
|
89
89
|
export class ${className} {
|
|
90
90
|
// @ts-ignore
|
|
@@ -100,27 +100,12 @@ const getImportableVarMap = () => ({
|
|
|
100
100
|
const makeNewImportVarMap = () => ({
|
|
101
101
|
"@accelbyte/sdk": ["AccelbyteSDK", "ApiArgs", "ApiUtils"]
|
|
102
102
|
});
|
|
103
|
-
const generateImports = (body, importStatements) => {
|
|
104
|
-
const usedImportVarMap = makeNewImportVarMap();
|
|
105
|
-
const importableVarMap = getImportableVarMap();
|
|
106
|
-
for (const [moduleSource, importableVars] of Object.entries(importableVarMap)) {
|
|
107
|
-
for (const importableVar of importableVars) {
|
|
108
|
-
const importVarRegex = new RegExp(`(?<![\\d\\w_])${importableVar}(?![\\d\\w_])`);
|
|
109
|
-
if (body.match(importVarRegex)) {
|
|
110
|
-
usedImportVarMap[moduleSource] = [...usedImportVarMap[moduleSource] || [], importableVar];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const generatedImports = Object.keys(usedImportVarMap).sort().map((moduleSource) => `import { ${usedImportVarMap[moduleSource].sort().join(", ")} } from '${moduleSource}'`).join("\n");
|
|
115
|
-
return `${generatedImports}
|
|
116
|
-
${importStatements.sort().join("\n")}`;
|
|
117
|
-
};
|
|
118
103
|
const templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
119
104
|
return `/**
|
|
120
105
|
* AUTO GENERATED
|
|
121
106
|
*/
|
|
122
107
|
/* eslint-disable camelcase */
|
|
123
|
-
${generateImports(body, importStatements)}
|
|
108
|
+
${generateImports(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
124
109
|
|
|
125
110
|
export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
126
111
|
const sdkAssembly = sdk.assembly()
|
|
@@ -170,12 +155,18 @@ class ParserUtils {
|
|
|
170
155
|
return `${attrName}: ${defaultValue}`;
|
|
171
156
|
};
|
|
172
157
|
static parseType = (pathParam) => {
|
|
173
|
-
if (pathParam.type
|
|
158
|
+
if (isSwaggerIntegerType(pathParam.type || pathParam?.schema?.type))
|
|
174
159
|
return "number";
|
|
175
|
-
if (pathParam.type === "array")
|
|
160
|
+
if (pathParam.type === "array") {
|
|
161
|
+
if (isSwaggerIntegerType(pathParam.items.type))
|
|
162
|
+
return "number[]";
|
|
176
163
|
return `${pathParam.items.type ?? "any"}[]`;
|
|
177
|
-
|
|
164
|
+
}
|
|
165
|
+
if (pathParam?.schema?.type === "array") {
|
|
166
|
+
if (isSwaggerIntegerType(pathParam.schema.items.type))
|
|
167
|
+
return "number[]";
|
|
178
168
|
return `${pathParam.schema.items.type ?? "any"}[]`;
|
|
169
|
+
}
|
|
179
170
|
if (pathParam?.schema?.type)
|
|
180
171
|
return pathParam.schema.type;
|
|
181
172
|
return pathParam.type;
|
|
@@ -266,6 +257,8 @@ class ParserUtils {
|
|
|
266
257
|
return retBodyParams;
|
|
267
258
|
}
|
|
268
259
|
if (bodyParam?.schema?.type === "array" && !bodyParam?.schema?.items?.$ref) {
|
|
260
|
+
if (isSwaggerIntegerType(bodyParam.schema.items.type))
|
|
261
|
+
return "number[]";
|
|
269
262
|
return `${bodyParam.schema.items.type ?? "any"}[]`;
|
|
270
263
|
}
|
|
271
264
|
if (bodyParam?.schema?.type === "array" && bodyParam?.schema?.items?.$ref) {
|
|
@@ -577,6 +570,9 @@ const testConflict = (path2, generatedMethod, existingMethods) => {
|
|
|
577
570
|
existingMethods: ${JSON.stringify(existingMethods, null, 2)}`);
|
|
578
571
|
}
|
|
579
572
|
};
|
|
573
|
+
const isSwaggerIntegerType = (type) => {
|
|
574
|
+
return type === "integer" || type === "int";
|
|
575
|
+
};
|
|
580
576
|
|
|
581
577
|
const Schema = z.object({
|
|
582
578
|
$ref: z.string().nullish(),
|
|
@@ -683,8 +679,6 @@ const templateMethod = ({
|
|
|
683
679
|
let methodParamsNoTypes = "";
|
|
684
680
|
let newPath = `'${path}'`;
|
|
685
681
|
let importStatements = [];
|
|
686
|
-
let snippetString = "";
|
|
687
|
-
let snippetMethodSignature = "";
|
|
688
682
|
for (const pathParam of pathParams) {
|
|
689
683
|
const type = ParserUtils.parseType(pathParam);
|
|
690
684
|
if (pathParam.name !== "namespace") {
|
|
@@ -706,13 +700,6 @@ const templateMethod = ({
|
|
|
706
700
|
importStatements = ParserUtils.parseBodyParamsImports(bodyParams);
|
|
707
701
|
methodParams += dataType ? `data: ${dataType},` : "";
|
|
708
702
|
methodParamsNoTypes += dataType ? `data,` : "";
|
|
709
|
-
const snippetParams = bodyParams?.map((ob) => {
|
|
710
|
-
return ` <span class='sn-purple'>${ob.name}</span>`;
|
|
711
|
-
});
|
|
712
|
-
snippetMethodSignature += snippetParams ? `data: { ${snippetParams} }` : "";
|
|
713
|
-
bodyParams?.map((ob) => {
|
|
714
|
-
return ` <span class='sn-purple'>"${ob.name}": ""</span>`;
|
|
715
|
-
});
|
|
716
703
|
}
|
|
717
704
|
const isAnyRequired = ParserUtils.isAnyQueryParamRequired(queryParams);
|
|
718
705
|
const queryParamsType = queryParams.length ? `queryParams${isAnyRequired ? "" : "?"}: {${ParserUtils.parseQueryParamsType(queryParams)}}` : "";
|
|
@@ -749,8 +736,6 @@ const templateMethod = ({
|
|
|
749
736
|
const methodName = httpMethod === "get" ? cachedFetchMethod : ["post", "put", "patch", "delete"].includes(httpMethod) ? classMethod : "";
|
|
750
737
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
751
738
|
const generateMethodName = () => `${methodName}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
752
|
-
const generateSnippetMethodName = () => `${methodName}(${snippetMethodSignature})`;
|
|
753
|
-
snippetString += `${generateSnippetMethodName()}`;
|
|
754
739
|
const responseSyncType = httpMethod === "get" ? "IResponseWithSync" : ["post", "put", "patch", "delete"].includes(httpMethod) ? "IResponse" : "";
|
|
755
740
|
methodImpl = `${descriptionText}
|
|
756
741
|
${generateMethodName()} {
|
|
@@ -781,8 +766,7 @@ ${httpMethod === "get" ? ` const res = () => Validate.responseType(() => resu
|
|
|
781
766
|
methodImpl,
|
|
782
767
|
methodParams,
|
|
783
768
|
methodParamsNoTypes,
|
|
784
|
-
importStatements
|
|
785
|
-
snippetString
|
|
769
|
+
importStatements
|
|
786
770
|
};
|
|
787
771
|
return res;
|
|
788
772
|
};
|
|
@@ -1046,7 +1030,6 @@ const templateApiMethod = ({
|
|
|
1046
1030
|
path,
|
|
1047
1031
|
pathParams,
|
|
1048
1032
|
bodyParams,
|
|
1049
|
-
queryParams,
|
|
1050
1033
|
responseClass,
|
|
1051
1034
|
classGenName,
|
|
1052
1035
|
methodParams,
|
|
@@ -1054,7 +1037,6 @@ const templateApiMethod = ({
|
|
|
1054
1037
|
}) => {
|
|
1055
1038
|
let methodSignature = "";
|
|
1056
1039
|
let newPath = `'${path}'`;
|
|
1057
|
-
let dependencies = [];
|
|
1058
1040
|
let snippetSdk = "";
|
|
1059
1041
|
let snippetShell = "";
|
|
1060
1042
|
for (const pathParam of pathParams) {
|
|
@@ -1071,30 +1053,24 @@ const templateApiMethod = ({
|
|
|
1071
1053
|
}
|
|
1072
1054
|
}
|
|
1073
1055
|
}
|
|
1074
|
-
snippetShell =
|
|
1075
|
-
|
|
1056
|
+
snippetShell = `curl --location --request \\
|
|
1057
|
+
${httpMethod} '__DOMAIN__${path}' \\
|
|
1058
|
+
--header 'accept: application/json'`;
|
|
1076
1059
|
if (httpMethod !== "get") {
|
|
1077
|
-
dataType = ParserUtils.parseBodyParamsType(bodyParams);
|
|
1078
|
-
dependencies = ParserUtils.parseBodyParamsImports(bodyParams);
|
|
1079
|
-
methodSignature += dataType ? `data: ${dataType},` : "";
|
|
1080
|
-
bodyParams?.map((ob) => {
|
|
1081
|
-
return ` <span class='sn-purple'>${ob.name}</span>`;
|
|
1082
|
-
});
|
|
1083
1060
|
const curlParams = bodyParams?.map((ob) => {
|
|
1084
|
-
return `
|
|
1061
|
+
return ` "${ob.name}": ""`;
|
|
1085
1062
|
});
|
|
1086
|
-
snippetShell += `
|
|
1063
|
+
snippetShell += ` \\
|
|
1064
|
+
--data-raw '{ ${curlParams}}'`;
|
|
1087
1065
|
}
|
|
1088
|
-
const
|
|
1089
|
-
queryParams.length ? `queryParams${isAnyRequired ? "" : "?"}: {${ParserUtils.parseQueryParamsType(queryParams)}}` : "";
|
|
1090
|
-
queryParams.length ? `const params = {${ParserUtils.parseQueryParamsDefault(queryParams)} ...queryParams} as SDKRequestConfig` : "const params = {} as SDKRequestConfig";
|
|
1091
|
-
description ? `
|
|
1066
|
+
const descriptionText = description ? `
|
|
1092
1067
|
/**
|
|
1093
1068
|
* ${description.replace(/\n/g, "\n * ")}
|
|
1094
1069
|
*/` : "";
|
|
1095
1070
|
const resolvedResponseClass = responseClass || "unknown";
|
|
1096
1071
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1097
1072
|
const methodImpl = `
|
|
1073
|
+
${descriptionText}
|
|
1098
1074
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1099
1075
|
const $ = new ${classGenName}(Network.create(requestConfig), namespace, cache)
|
|
1100
1076
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
@@ -1102,8 +1078,10 @@ const templateApiMethod = ({
|
|
|
1102
1078
|
return resp.response.data
|
|
1103
1079
|
}
|
|
1104
1080
|
`;
|
|
1105
|
-
|
|
1106
|
-
|
|
1081
|
+
const snippetPromiseString = responseType !== "unknown" ? `Promise<${responseType}>` : "Promise";
|
|
1082
|
+
snippetSdk += `${classMethod}(${methodParams})
|
|
1083
|
+
// return ${snippetPromiseString}`;
|
|
1084
|
+
return [methodImpl, snippetSdk, snippetShell];
|
|
1107
1085
|
};
|
|
1108
1086
|
|
|
1109
1087
|
const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
|
|
@@ -1129,26 +1107,44 @@ export const ${ParserUtils.convertDashesToTitleCase(serviceNameTitle)} = apis
|
|
|
1129
1107
|
};
|
|
1130
1108
|
|
|
1131
1109
|
const templateSdkSnippet = (serviceNameTitle, apiName, methodSnippet) => {
|
|
1132
|
-
const
|
|
1133
|
-
|
|
1110
|
+
const methodArr = methodSnippet.split("//");
|
|
1111
|
+
let normMethod = normalizeMethodSnippet(methodArr[0].trim(), "data:");
|
|
1112
|
+
normMethod = normalizeMethodSnippet(normMethod, "queryParams:");
|
|
1113
|
+
normMethod = normalizeMethodSnippet(normMethod, "queryParams?:");
|
|
1114
|
+
normMethod += "\n\n//" + methodArr[1];
|
|
1115
|
+
const sdkSnippet = `import { Accelbyte } from '@accelbyte/sdk'
|
|
1134
1116
|
import { ${serviceNameTitle} } from '@accelbyte/sdk-${serviceNameTitle.toLowerCase()}'
|
|
1135
1117
|
|
|
1136
|
-
const
|
|
1118
|
+
const sdk = Accelbyte.SDK({
|
|
1137
1119
|
baseURL: 'https://demo.accelbyte.io',
|
|
1138
1120
|
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
1139
1121
|
namespace: 'accelbyte',
|
|
1140
1122
|
redirectURI: 'http://localhost:3030'
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
const sdk = Accelbyte.SDK({
|
|
1144
|
-
options: SDK_CONFIG
|
|
1145
1123
|
})
|
|
1146
1124
|
|
|
1147
|
-
${serviceNameTitle}.${apiName}(sdk)
|
|
1148
|
-
`;
|
|
1125
|
+
${serviceNameTitle}.${apiName}(sdk)
|
|
1126
|
+
.${normMethod}`;
|
|
1149
1127
|
return sdkSnippet;
|
|
1150
1128
|
};
|
|
1129
|
+
const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
1130
|
+
const split1 = methodInput.split(splitWord);
|
|
1131
|
+
if (!split1[1]) {
|
|
1132
|
+
return methodInput;
|
|
1133
|
+
}
|
|
1134
|
+
let split2 = split1[1].trim();
|
|
1135
|
+
split2 = ParserUtils.replaceAll(split2, "{", "");
|
|
1136
|
+
split2 = ParserUtils.replaceAll(split2, "})", "");
|
|
1137
|
+
split2 = split2.split(",");
|
|
1138
|
+
let params = "";
|
|
1139
|
+
split2.forEach((p) => {
|
|
1140
|
+
params += "\n " + ParserUtils.replaceAll(p.trim(), ")", "") + ",";
|
|
1141
|
+
});
|
|
1142
|
+
params = params.slice(0, -1);
|
|
1143
|
+
const result = split1[0] + splitWord + " {" + params + "\n })";
|
|
1144
|
+
return result;
|
|
1145
|
+
};
|
|
1151
1146
|
|
|
1147
|
+
const GIT_URL = "https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages";
|
|
1152
1148
|
class CodeGenerator {
|
|
1153
1149
|
static getPatchedDir = () => path__default.join(CliParser.getSwaggersOutputPath(), "patched");
|
|
1154
1150
|
static getGeneratedPublicFolder = () => `${CliParser.getOutputPath()}/generated-public`;
|
|
@@ -1231,7 +1227,7 @@ class CodeGenerator {
|
|
|
1231
1227
|
}
|
|
1232
1228
|
];
|
|
1233
1229
|
}
|
|
1234
|
-
const { methodImpl, methodParams, methodParamsNoTypes, importStatements
|
|
1230
|
+
const { methodImpl, methodParams, methodParamsNoTypes, importStatements } = templateMethod({
|
|
1235
1231
|
classMethod,
|
|
1236
1232
|
description,
|
|
1237
1233
|
httpMethod,
|
|
@@ -1243,14 +1239,13 @@ class CodeGenerator {
|
|
|
1243
1239
|
responseClass
|
|
1244
1240
|
});
|
|
1245
1241
|
classBufferByTag[tag] = (classBufferByTag[tag] || "") + methodImpl;
|
|
1246
|
-
const [generatedMethodString1,
|
|
1242
|
+
const [generatedMethodString1, snippetMethod, snippetShell] = templateApiMethod({
|
|
1247
1243
|
classMethod,
|
|
1248
1244
|
description,
|
|
1249
1245
|
httpMethod,
|
|
1250
1246
|
path: pathWithBase,
|
|
1251
1247
|
pathParams,
|
|
1252
1248
|
bodyParams,
|
|
1253
|
-
queryParams,
|
|
1254
1249
|
responseClass,
|
|
1255
1250
|
classGenName,
|
|
1256
1251
|
methodParams,
|
|
@@ -1261,12 +1256,10 @@ class CodeGenerator {
|
|
|
1261
1256
|
dependenciesByTag[tag] = dependenciesByTag[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...dependenciesByTag[tag]])] : [...new Set(importStatements)];
|
|
1262
1257
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1263
1258
|
const { apiGenName } = ParserUtils.generateApiName(tag);
|
|
1264
|
-
|
|
1265
|
-
resultSnippet = ParserUtils.replaceAll(resultSnippet, "<", "<");
|
|
1266
|
-
resultSnippet = ParserUtils.replaceAll(resultSnippet, ">", ">");
|
|
1267
|
-
resultSnippet = ParserUtils.replaceAll(resultSnippet, "\n", "<br/>");
|
|
1259
|
+
const resultSnippet = templateSdkSnippet(serviceNameTitle, apiGenName, snippetMethod);
|
|
1268
1260
|
snippetMap[path2][httpMethod] = {
|
|
1269
1261
|
web: resultSnippet,
|
|
1262
|
+
webGit: GIT_URL + `/sdk-${serviceName}/src/generated-public/${serviceName}/${apiGenName}.ts`,
|
|
1270
1263
|
shell: snippetShell
|
|
1271
1264
|
};
|
|
1272
1265
|
}
|
|
@@ -1369,6 +1362,22 @@ class SwaggerDownloader {
|
|
|
1369
1362
|
fs.writeFileSync(destFile, "");
|
|
1370
1363
|
return destFile;
|
|
1371
1364
|
};
|
|
1365
|
+
static postSanitizeDownloadedFile = (filePath) => {
|
|
1366
|
+
const searchStr = ["%5B", "%5D", "%20", "%7B", "%7D"];
|
|
1367
|
+
fs.readFile(filePath, "utf8", (err, data) => {
|
|
1368
|
+
if (err)
|
|
1369
|
+
throw err;
|
|
1370
|
+
let result = data;
|
|
1371
|
+
searchStr.forEach((s) => {
|
|
1372
|
+
result = result.replace(new RegExp(s, "g"), " ");
|
|
1373
|
+
});
|
|
1374
|
+
fs.writeFile(filePath, result, "utf8", (err2) => {
|
|
1375
|
+
if (err2)
|
|
1376
|
+
throw err2;
|
|
1377
|
+
console.log("File updated successfully.");
|
|
1378
|
+
});
|
|
1379
|
+
});
|
|
1380
|
+
};
|
|
1372
1381
|
static downloadFile = (targetFileName, url) => {
|
|
1373
1382
|
const destFile = SwaggerDownloader.getDestFile(targetFileName);
|
|
1374
1383
|
const file = fs.createWriteStream(destFile);
|
|
@@ -1376,6 +1385,7 @@ class SwaggerDownloader {
|
|
|
1376
1385
|
response.pipe(file);
|
|
1377
1386
|
file.on("finish", () => {
|
|
1378
1387
|
file.close();
|
|
1388
|
+
SwaggerDownloader.postSanitizeDownloadedFile(destFile);
|
|
1379
1389
|
console.log(`SwaggerDownload ${url} completed`);
|
|
1380
1390
|
});
|
|
1381
1391
|
});
|