@accelbyte/codegen 3.0.6 → 3.1.0
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.
|
@@ -89,7 +89,7 @@ const makeNewImportVarMap$1 = () => ({
|
|
|
89
89
|
axios: ["AxiosInstance"],
|
|
90
90
|
"@accelbyte/sdk": ["SDKRequestConfig"]
|
|
91
91
|
});
|
|
92
|
-
const generateImports = (body, importStatements, makeNewImportVarMap2, getImportableVarMap2) => {
|
|
92
|
+
const generateImports$1 = (body, importStatements, makeNewImportVarMap2, getImportableVarMap2) => {
|
|
93
93
|
const usedImportVarMap = makeNewImportVarMap2;
|
|
94
94
|
const importableVarMap = getImportableVarMap2;
|
|
95
95
|
for (const [moduleSource, importableVars] of Object.entries(importableVarMap)) {
|
|
@@ -110,16 +110,55 @@ const templateClass = (className, body, importStatements) => {
|
|
|
110
110
|
return `/**
|
|
111
111
|
* AUTO GENERATED
|
|
112
112
|
*/
|
|
113
|
-
${generateImports(body, importStatements, makeNewImportVarMap$1(), getImportableVarMap$1())}
|
|
113
|
+
${generateImports$1(body, importStatements, makeNewImportVarMap$1(), getImportableVarMap$1())}
|
|
114
114
|
|
|
115
115
|
export class ${className} {
|
|
116
116
|
// @ts-ignore
|
|
117
|
-
constructor(private axiosInstance: AxiosInstance, private namespace: string, private
|
|
117
|
+
constructor(private axiosInstance: AxiosInstance, private namespace: string, private useSchemaValidation = true) {}
|
|
118
118
|
${body}
|
|
119
119
|
}
|
|
120
120
|
`;
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
+
const generateImports = (body, className) => {
|
|
124
|
+
const generatedImports = `import { AccelbyteSDK, ApiArgs, ApiError } from '@accelbyte/sdk'
|
|
125
|
+
import { AxiosError } from 'axios'
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
import { useQuery, UseQueryOptions, UseQueryResult, useMutation, UseMutationOptions, UseMutationResult } from '@tanstack/react-query'
|
|
128
|
+
import { ${className} } from "../${className}.js"
|
|
129
|
+
`;
|
|
130
|
+
return generatedImports;
|
|
131
|
+
};
|
|
132
|
+
const templateQuery = (className, body, importStatements, serviceNameTitle, returnMethods, paramImports) => {
|
|
133
|
+
const classNameWithoutApi = className.replace("Api", "");
|
|
134
|
+
const queryKeys = ParserUtils.createQueryKeys(classNameWithoutApi, returnMethods);
|
|
135
|
+
const generatedImports = generateImports(body, className);
|
|
136
|
+
return `/**
|
|
137
|
+
* AUTO GENERATED
|
|
138
|
+
*/
|
|
139
|
+
/* eslint-disable camelcase */
|
|
140
|
+
${generatedImports}
|
|
141
|
+
${filterUsedImports(paramImports, body)}
|
|
142
|
+
|
|
143
|
+
export enum Key_${classNameWithoutApi} {
|
|
144
|
+
${queryKeys}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
${body}
|
|
148
|
+
`;
|
|
149
|
+
};
|
|
150
|
+
function filterUsedImports(importArr, body) {
|
|
151
|
+
return importArr.filter((path) => {
|
|
152
|
+
const start = path.indexOf("{") + 1;
|
|
153
|
+
const end = path.indexOf("}");
|
|
154
|
+
if (start > 0 && end > start) {
|
|
155
|
+
const importName = path.slice(start, end).trim();
|
|
156
|
+
return body.includes(importName);
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}).map((path) => path).join("\n") + "\n";
|
|
160
|
+
}
|
|
161
|
+
|
|
123
162
|
const getImportableVarMap = () => ({
|
|
124
163
|
"@accelbyte/sdk": ["CodeGenUtil", "IResponse", "Validate", "ApiArgs", "Network", "AccelbyteSDK"]
|
|
125
164
|
});
|
|
@@ -127,19 +166,22 @@ const makeNewImportVarMap = () => ({
|
|
|
127
166
|
"@accelbyte/sdk": ["AccelbyteSDK", "ApiArgs", "ApiUtils"]
|
|
128
167
|
});
|
|
129
168
|
const templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
169
|
+
const $className = className.replace("Api", "$");
|
|
130
170
|
return `/**
|
|
131
171
|
* AUTO GENERATED
|
|
132
172
|
*/
|
|
133
173
|
/* eslint-disable camelcase */
|
|
134
174
|
// @ts-ignore -> ts-expect-error TS6133
|
|
135
|
-
${generateImports(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
175
|
+
${generateImports$1(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
176
|
+
${`import { ${$className} } from './endpoints/${$className}.js'
|
|
177
|
+
`}
|
|
136
178
|
|
|
137
179
|
export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
138
180
|
const sdkAssembly = sdk.assembly()
|
|
139
181
|
|
|
140
182
|
const namespace = args?.namespace ? args?.namespace : sdkAssembly.namespace
|
|
141
183
|
const requestConfig = ApiUtils.mergedConfigs(sdkAssembly.config, args)
|
|
142
|
-
const
|
|
184
|
+
const useSchemaValidation = sdkAssembly.useSchemaValidation
|
|
143
185
|
${body}
|
|
144
186
|
|
|
145
187
|
return {
|
|
@@ -229,8 +271,35 @@ const REMOVED_KEYWORDS = [
|
|
|
229
271
|
"/{namespace}/"
|
|
230
272
|
];
|
|
231
273
|
class ParserUtils {
|
|
232
|
-
static
|
|
233
|
-
|
|
274
|
+
static createQueryKeys(classNameWithoutApi, csvMethodNames) {
|
|
275
|
+
const keys = csvMethodNames.split(",");
|
|
276
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
277
|
+
const enumString = keys.reduce((acc, key, index) => {
|
|
278
|
+
const trimmedKey = key.trim();
|
|
279
|
+
const isDeprecated = key.indexOf("_DEPRECATED") > 0;
|
|
280
|
+
if (trimmedKey && !isDeprecated) {
|
|
281
|
+
const cleanedKey = trimmedKey.replace(/^(get|update|create|patch|delete)[_]?/, "");
|
|
282
|
+
if (!processedKeys.has(cleanedKey)) {
|
|
283
|
+
processedKeys.add(cleanedKey);
|
|
284
|
+
acc += `${cleanedKey} = '${classNameWithoutApi}.${cleanedKey}'`;
|
|
285
|
+
if (index < keys.length - 1) {
|
|
286
|
+
acc += ",\n";
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return acc;
|
|
291
|
+
}, "");
|
|
292
|
+
return enumString;
|
|
293
|
+
}
|
|
294
|
+
static addDeprecatedSuffix(isDeprecated, methodName) {
|
|
295
|
+
if (isDeprecated) {
|
|
296
|
+
return methodName + "_DEPRECATED";
|
|
297
|
+
} else {
|
|
298
|
+
return methodName;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
static replaceAll = (text, search, replace) => {
|
|
302
|
+
return text.split(search).join(replace);
|
|
234
303
|
};
|
|
235
304
|
static generateClassName = (tag, isAdmin) => {
|
|
236
305
|
const className = _.upperFirst(_.camelCase(tag));
|
|
@@ -370,7 +439,7 @@ class ParserUtils {
|
|
|
370
439
|
if (bodyParam?.schema?.type === "array" && bodyParam?.schema?.items?.$ref) {
|
|
371
440
|
return `${ParserUtils.parseRefType(bodyParam.schema.items.$ref)}[]`;
|
|
372
441
|
}
|
|
373
|
-
if (bodyParam?.schema
|
|
442
|
+
if (bodyParam?.schema?.$ref) {
|
|
374
443
|
return ParserUtils.parseRefType(bodyParam.schema.$ref);
|
|
375
444
|
}
|
|
376
445
|
if (bodyParam?.schema?.additionalProperties?.type === "object") {
|
|
@@ -486,6 +555,20 @@ class ParserUtils {
|
|
|
486
555
|
const fileContent = templateClass(apiName, apiBuffer, imports);
|
|
487
556
|
fs.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
488
557
|
}
|
|
558
|
+
static writeAtomFile(distDir, apiName, fileContent) {
|
|
559
|
+
ParserUtils.mkdirIfNotExist(distDir);
|
|
560
|
+
fs.writeFileSync(`${distDir}/${apiName}.atom.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
561
|
+
}
|
|
562
|
+
static writeQueryFile(distDir, apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports) {
|
|
563
|
+
if (apiBuffer.length < 1) {
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
const queryFileName = `${apiName.replace("Api", "")}.query`;
|
|
567
|
+
ParserUtils.mkdirIfNotExist(distDir);
|
|
568
|
+
const fileContent = templateQuery(apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports);
|
|
569
|
+
fs.writeFileSync(`${distDir}/${queryFileName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
570
|
+
return queryFileName;
|
|
571
|
+
}
|
|
489
572
|
static writeXVersion(distDir, xversionJson, apiInfo) {
|
|
490
573
|
if (xversionJson) {
|
|
491
574
|
console.log("x-version:", xversionJson);
|
|
@@ -956,7 +1039,7 @@ const apis = {
|
|
|
956
1039
|
${returnStatement}
|
|
957
1040
|
}
|
|
958
1041
|
|
|
959
|
-
export const ${
|
|
1042
|
+
export const ${serviceNameTitle} = apis
|
|
960
1043
|
`;
|
|
961
1044
|
};
|
|
962
1045
|
|
|
@@ -1015,11 +1098,11 @@ const Endpoint = zod.z.object({
|
|
|
1015
1098
|
})),
|
|
1016
1099
|
parameters: zod.z.array(EndpointParameters).nullish(),
|
|
1017
1100
|
requestBody: zod.z.object({
|
|
1018
|
-
required: zod.z.boolean(),
|
|
1101
|
+
required: zod.z.boolean().nullish(),
|
|
1019
1102
|
content: zod.z.object({
|
|
1020
1103
|
"application/json": zod.z.object({
|
|
1021
1104
|
schema: Schema.nullish()
|
|
1022
|
-
})
|
|
1105
|
+
}).nullish()
|
|
1023
1106
|
}).nullish()
|
|
1024
1107
|
}).nullish(),
|
|
1025
1108
|
"x-security": zod.z.any().nullish()
|
|
@@ -1117,10 +1200,6 @@ const templateMethod = ({
|
|
|
1117
1200
|
methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
|
|
1118
1201
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
1119
1202
|
let methodImpl = "";
|
|
1120
|
-
const isCacheFetch = ["get"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
1121
|
-
const deprecateTag = isCacheFetch ? `/**
|
|
1122
|
-
* @deprecated Use "${classMethod}()" instead.
|
|
1123
|
-
*/` : "";
|
|
1124
1203
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
1125
1204
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1126
1205
|
const generateMethodName = () => `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
@@ -1131,18 +1210,11 @@ const templateMethod = ({
|
|
|
1131
1210
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1132
1211
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1133
1212
|
|
|
1134
|
-
${` return Validate.validateOrReturnResponse(this.
|
|
1213
|
+
${` return Validate.validateOrReturnResponse(this.useSchemaValidation, () => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}')`}
|
|
1135
1214
|
}
|
|
1136
1215
|
`;
|
|
1137
1216
|
if (!isGuardInvoked) {
|
|
1138
|
-
methodImpl =
|
|
1139
|
-
${deprecateTag}
|
|
1140
|
-
TODO_${classMethod}(${methodParams}): Promise<AxiosResponse<${resolvedResponseClass}>> {
|
|
1141
|
-
${queryParamsDefault}
|
|
1142
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1143
|
-
return this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1144
|
-
}
|
|
1145
|
-
`;
|
|
1217
|
+
methodImpl = ``;
|
|
1146
1218
|
}
|
|
1147
1219
|
const res = {
|
|
1148
1220
|
methodImpl,
|
|
@@ -1204,7 +1276,7 @@ const templateApiMethod = ({
|
|
|
1204
1276
|
const methodImpl = `
|
|
1205
1277
|
${descriptionText}
|
|
1206
1278
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1207
|
-
const $ = new ${classGenName}(Network.create(requestConfig), namespace,
|
|
1279
|
+
const $ = new ${classGenName}(Network.create(requestConfig), namespace, useSchemaValidation)
|
|
1208
1280
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
1209
1281
|
if (resp.error) throw resp.error
|
|
1210
1282
|
return resp.response.data
|
|
@@ -1267,6 +1339,151 @@ const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
|
1267
1339
|
return result;
|
|
1268
1340
|
};
|
|
1269
1341
|
|
|
1342
|
+
const templateQueryMethod = ({
|
|
1343
|
+
classMethod,
|
|
1344
|
+
httpMethod,
|
|
1345
|
+
path,
|
|
1346
|
+
pathParams,
|
|
1347
|
+
responseClass,
|
|
1348
|
+
methodParams,
|
|
1349
|
+
apiGenName,
|
|
1350
|
+
isFormUrlEncoded,
|
|
1351
|
+
deprecated
|
|
1352
|
+
}) => {
|
|
1353
|
+
if (isFormUrlEncoded || deprecated) {
|
|
1354
|
+
return "";
|
|
1355
|
+
}
|
|
1356
|
+
const isPostFetch = httpMethod === "post" && (path.includes("/table-query/") || path.endsWith("/list"));
|
|
1357
|
+
const isAdmin = path.indexOf("/admin/") > 0;
|
|
1358
|
+
const isGet = httpMethod === "get" || isPostFetch;
|
|
1359
|
+
let queryMethod = isGet ? "useQuery" : "useMutation";
|
|
1360
|
+
let mParams = "";
|
|
1361
|
+
let mParamsNoTypes = "";
|
|
1362
|
+
let newPath = `'${path}'`;
|
|
1363
|
+
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams, path);
|
|
1364
|
+
for (const pathParam of sortedPathParams) {
|
|
1365
|
+
const type = ParserUtils.parseType(pathParam);
|
|
1366
|
+
if (pathParam.name !== "namespace") {
|
|
1367
|
+
mParams += pathParam.name + `:${type}, `;
|
|
1368
|
+
mParamsNoTypes += pathParam.name + ", ";
|
|
1369
|
+
}
|
|
1370
|
+
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
1371
|
+
if (path.match(`{${pathParam.name}}`)) {
|
|
1372
|
+
if (type === "string") {
|
|
1373
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
1374
|
+
} else {
|
|
1375
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', String(${pName}))`;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
const resolvedResponseClass = responseClass || "unknown";
|
|
1380
|
+
let _methodName = convertMethodName(classMethod, isPostFetch);
|
|
1381
|
+
_methodName = isAdmin ? _methodName.replace("use", "useAdm") : _methodName;
|
|
1382
|
+
const _responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1383
|
+
const _methodParams = methodParams && methodParams.length > 0 ? `& { ${methodParams} }` : "";
|
|
1384
|
+
const _methodParamsImpl = convertToMethodImplArgs(methodParams);
|
|
1385
|
+
const queryMethodImpl = `
|
|
1386
|
+
|
|
1387
|
+
export const ${_methodName} = (
|
|
1388
|
+
sdk: AccelbyteSDK,
|
|
1389
|
+
input: ApiArgs ${_methodParams},
|
|
1390
|
+
options?: Omit<UseQueryOptions<${_responseType}, AxiosError<ApiError>>, 'queryKey'>,
|
|
1391
|
+
callback?: (data: ${_responseType}) => void
|
|
1392
|
+
): UseQueryResult<${_responseType}, AxiosError<ApiError>> => {
|
|
1393
|
+
//
|
|
1394
|
+
const queryFn = (
|
|
1395
|
+
sdk: AccelbyteSDK,
|
|
1396
|
+
input: Parameters<typeof ${_methodName}>[1]
|
|
1397
|
+
) => async () => {
|
|
1398
|
+
const data =
|
|
1399
|
+
(await ${apiGenName}(sdk, { namespace: input.namespace }).
|
|
1400
|
+
${classMethod}(${_methodParamsImpl}))
|
|
1401
|
+
callback && callback(data)
|
|
1402
|
+
return data
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
return ${queryMethod}<${_responseType}, AxiosError<ApiError>>({
|
|
1406
|
+
queryKey: [${createQueryKey(apiGenName, classMethod)}, input],
|
|
1407
|
+
queryFn: queryFn(sdk, input),
|
|
1408
|
+
...options
|
|
1409
|
+
})
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
`;
|
|
1413
|
+
const mutationMethodImpl = `
|
|
1414
|
+
|
|
1415
|
+
export const ${_methodName} = (
|
|
1416
|
+
sdk: AccelbyteSDK,
|
|
1417
|
+
options?: Omit<UseMutationOptions<${_responseType}, AxiosError<ApiError>, ApiArgs ${_methodParams}>, 'mutationKey'>,
|
|
1418
|
+
callback?: (data: ${_responseType}) => void
|
|
1419
|
+
): UseMutationResult<${_responseType}, AxiosError<ApiError>, ApiArgs ${_methodParams}> => {
|
|
1420
|
+
//
|
|
1421
|
+
const mutationFn = async (input: ApiArgs ${_methodParams}) => {
|
|
1422
|
+
const data =
|
|
1423
|
+
(await ${apiGenName}(sdk, { namespace: input.namespace, config: input.config }).
|
|
1424
|
+
${classMethod}(${_methodParamsImpl}))
|
|
1425
|
+
callback && callback(data)
|
|
1426
|
+
return data
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
return useMutation({
|
|
1430
|
+
mutationKey: [${createQueryKey(apiGenName, classMethod)}],
|
|
1431
|
+
mutationFn,
|
|
1432
|
+
...options
|
|
1433
|
+
})
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
`;
|
|
1437
|
+
return isGet ? queryMethodImpl : mutationMethodImpl;
|
|
1438
|
+
};
|
|
1439
|
+
function createQueryKey(className, methodName) {
|
|
1440
|
+
if (methodName.indexOf("_DEPRECATED") > 0) {
|
|
1441
|
+
return "";
|
|
1442
|
+
}
|
|
1443
|
+
const prefixRegex = /^(get|create|update|delete|patch)[_]?/i;
|
|
1444
|
+
const cleanedMethodName = methodName.replace(prefixRegex, "").trim();
|
|
1445
|
+
const finalMethodName = cleanedMethodName.charAt(0).toUpperCase() + cleanedMethodName.slice(1);
|
|
1446
|
+
return `Key_${className.replace("Api", "")}.${finalMethodName}`;
|
|
1447
|
+
}
|
|
1448
|
+
const prefixMappings = {
|
|
1449
|
+
get: "use",
|
|
1450
|
+
create: "useCreate",
|
|
1451
|
+
patch: "usePatch",
|
|
1452
|
+
update: "useUpdate",
|
|
1453
|
+
delete: "useDelete"
|
|
1454
|
+
};
|
|
1455
|
+
function convertMethodName(classMethod, isPostFetch) {
|
|
1456
|
+
for (const [originalPrefix, newPrefix] of Object.entries(prefixMappings)) {
|
|
1457
|
+
if (classMethod.startsWith(originalPrefix)) {
|
|
1458
|
+
if (isPostFetch) {
|
|
1459
|
+
return classMethod.replace(originalPrefix, "useFetch");
|
|
1460
|
+
}
|
|
1461
|
+
const newMethodName = classMethod.replace(originalPrefix, newPrefix);
|
|
1462
|
+
if (originalPrefix === "get") {
|
|
1463
|
+
return newMethodName;
|
|
1464
|
+
} else {
|
|
1465
|
+
return newMethodName + "Mutation";
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
return classMethod;
|
|
1470
|
+
}
|
|
1471
|
+
function convertToMethodImplArgs(methodArgs) {
|
|
1472
|
+
let properties = methodArgs.split(/,\s*(?![^{}]*\})/).map((prop) => prop.trim()).filter(Boolean);
|
|
1473
|
+
let formattedProperties = [];
|
|
1474
|
+
properties.forEach((prop) => {
|
|
1475
|
+
if (prop.includes(": {")) {
|
|
1476
|
+
const propertyName = prop.split(": {")[0].replace("?", "").trim();
|
|
1477
|
+
formattedProperties.push(`input.${propertyName}`);
|
|
1478
|
+
} else {
|
|
1479
|
+
const colonIndex = prop.indexOf(":");
|
|
1480
|
+
const propertyName = prop.substring(0, colonIndex).replace("?", "").trim();
|
|
1481
|
+
formattedProperties.push(`input.${propertyName}`);
|
|
1482
|
+
}
|
|
1483
|
+
});
|
|
1484
|
+
return formattedProperties.join(", ");
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1270
1487
|
const GIT_URL = "https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages";
|
|
1271
1488
|
class SwaggerReaderHelpers {
|
|
1272
1489
|
static getServicePrefix = (servicePaths) => servicePaths[servicePaths.length - 1].split("/")[1];
|
|
@@ -1283,7 +1500,8 @@ class SwaggerReaderHelpers {
|
|
|
1283
1500
|
tagToEndpointClassesRecord: {},
|
|
1284
1501
|
tagToSdkClientRecord: {},
|
|
1285
1502
|
tagToSdkFunctionNamesRecord: {},
|
|
1286
|
-
tagToSdkImportsRecord: {}
|
|
1503
|
+
tagToSdkImportsRecord: {},
|
|
1504
|
+
tagToEndpointQueryRecord: {}
|
|
1287
1505
|
},
|
|
1288
1506
|
public: {
|
|
1289
1507
|
arrayDefinitions: [],
|
|
@@ -1292,7 +1510,8 @@ class SwaggerReaderHelpers {
|
|
|
1292
1510
|
tagToEndpointClassesRecord: {},
|
|
1293
1511
|
tagToSdkClientRecord: {},
|
|
1294
1512
|
tagToSdkFunctionNamesRecord: {},
|
|
1295
|
-
tagToSdkImportsRecord: {}
|
|
1513
|
+
tagToSdkImportsRecord: {},
|
|
1514
|
+
tagToEndpointQueryRecord: {}
|
|
1296
1515
|
}
|
|
1297
1516
|
};
|
|
1298
1517
|
const sortedPathsByLength = new Map(Object.entries(api.paths).sort((a, b) => {
|
|
@@ -1321,7 +1540,8 @@ class SwaggerReaderHelpers {
|
|
|
1321
1540
|
tagToEndpointClassesRecord,
|
|
1322
1541
|
tagToSdkClientRecord,
|
|
1323
1542
|
tagToSdkFunctionNamesRecord,
|
|
1324
|
-
tagToSdkImportsRecord
|
|
1543
|
+
tagToSdkImportsRecord,
|
|
1544
|
+
tagToEndpointQueryRecord
|
|
1325
1545
|
} = picked;
|
|
1326
1546
|
const tagToClassMethodsMapByType = isAdminEndpoint ? tagToClassMethodsMap.admin : tagToClassMethodsMap.public;
|
|
1327
1547
|
const httpMethods = Object.keys(operation);
|
|
@@ -1368,12 +1588,12 @@ class SwaggerReaderHelpers {
|
|
|
1368
1588
|
{
|
|
1369
1589
|
name: "body",
|
|
1370
1590
|
in: "body",
|
|
1371
|
-
schema: endpoint.requestBody.content["application/json"]
|
|
1591
|
+
schema: endpoint.requestBody.content["application/json"]?.schema
|
|
1372
1592
|
}
|
|
1373
1593
|
];
|
|
1374
1594
|
}
|
|
1375
1595
|
const { methodImpl, methodParams, methodParamsNoTypes, importStatements } = templateMethod({
|
|
1376
|
-
classMethod,
|
|
1596
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1377
1597
|
description,
|
|
1378
1598
|
httpMethod,
|
|
1379
1599
|
path: pathWithBase,
|
|
@@ -1385,8 +1605,21 @@ class SwaggerReaderHelpers {
|
|
|
1385
1605
|
deprecated
|
|
1386
1606
|
});
|
|
1387
1607
|
tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
|
|
1608
|
+
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1609
|
+
const queryMethodImpl = templateQueryMethod({
|
|
1610
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1611
|
+
httpMethod,
|
|
1612
|
+
path: pathWithBase,
|
|
1613
|
+
pathParams,
|
|
1614
|
+
responseClass,
|
|
1615
|
+
methodParams,
|
|
1616
|
+
apiGenName,
|
|
1617
|
+
isFormUrlEncoded,
|
|
1618
|
+
deprecated
|
|
1619
|
+
});
|
|
1620
|
+
tagToEndpointQueryRecord[tag] = (tagToEndpointQueryRecord[tag] || "") + queryMethodImpl;
|
|
1388
1621
|
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1389
|
-
classMethod,
|
|
1622
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1390
1623
|
description,
|
|
1391
1624
|
httpMethod,
|
|
1392
1625
|
path: pathWithBase,
|
|
@@ -1400,11 +1633,15 @@ class SwaggerReaderHelpers {
|
|
|
1400
1633
|
xSecurity: endpoint["x-security"]
|
|
1401
1634
|
});
|
|
1402
1635
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
1403
|
-
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
|
|
1636
|
+
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + ParserUtils.addDeprecatedSuffix(deprecated, classMethod) + ",";
|
|
1404
1637
|
tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
|
|
1405
1638
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1406
|
-
const
|
|
1407
|
-
|
|
1639
|
+
const resultSnippet = templateSdkSnippet({
|
|
1640
|
+
serviceNameTitle,
|
|
1641
|
+
apiName: apiGenName,
|
|
1642
|
+
snippetMethod,
|
|
1643
|
+
snippetApiArgs
|
|
1644
|
+
});
|
|
1408
1645
|
const currentSnippetMap = {};
|
|
1409
1646
|
snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
|
|
1410
1647
|
currentSnippetMap.web = resultSnippet;
|
|
@@ -1424,12 +1661,13 @@ class CodeGenerator {
|
|
|
1424
1661
|
static srcFolder = () => CliParser.getOutputPath();
|
|
1425
1662
|
static getGeneratedFolder = (isAdmin) => isAdmin ? `${CodeGenerator.srcFolder()}/generated-admin` : `${CodeGenerator.srcFolder()}/generated-public`;
|
|
1426
1663
|
static getGeneratedSnippetsFolder = () => `${CliParser.getSnippetOutputPath()}/generated-snippets`;
|
|
1427
|
-
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS) => {
|
|
1664
|
+
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES) => {
|
|
1428
1665
|
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1429
1666
|
ParserUtils.mkdirIfNotExist(DIST_DIR(true));
|
|
1430
1667
|
ParserUtils.mkdirIfNotExist(DIST_DIR(false));
|
|
1431
1668
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(true));
|
|
1432
1669
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(false));
|
|
1670
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_QUERIES(true));
|
|
1433
1671
|
};
|
|
1434
1672
|
static main = async (nameArray) => {
|
|
1435
1673
|
const serviceName = nameArray[0];
|
|
@@ -1457,9 +1695,10 @@ class CodeGenerator {
|
|
|
1457
1695
|
}
|
|
1458
1696
|
const DIST_DIR = (isAdmin) => `${CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1459
1697
|
const DIST_DIR_ENDPOINTS = (isAdmin) => path.join(DIST_DIR(isAdmin), "endpoints");
|
|
1698
|
+
const DIST_DIR_QUERIES = (isAdmin) => path.join(DIST_DIR(isAdmin), "queries");
|
|
1460
1699
|
const DIST_DEFINITION_DIR = path.join(CodeGenerator.srcFolder(), "generated-definitions");
|
|
1461
1700
|
const targetSrcFolder = `${CodeGenerator.srcFolder()}/`;
|
|
1462
|
-
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS);
|
|
1701
|
+
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES);
|
|
1463
1702
|
const mainApiList = [];
|
|
1464
1703
|
const generatePublicOrAdmin = (isAdmin) => {
|
|
1465
1704
|
const parsedInformationByType = isAdmin ? parsedInformation.admin : parsedInformation.public;
|
|
@@ -1469,7 +1708,8 @@ class CodeGenerator {
|
|
|
1469
1708
|
tagToEndpointClassesRecord,
|
|
1470
1709
|
tagToSdkClientRecord,
|
|
1471
1710
|
tagToSdkFunctionNamesRecord,
|
|
1472
|
-
tagToSdkImportsRecord
|
|
1711
|
+
tagToSdkImportsRecord,
|
|
1712
|
+
tagToEndpointQueryRecord
|
|
1473
1713
|
} = parsedInformationByType;
|
|
1474
1714
|
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1475
1715
|
const apiList = [];
|
|
@@ -1480,12 +1720,15 @@ class CodeGenerator {
|
|
|
1480
1720
|
const apiImports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1481
1721
|
apiImports.push(`import { ${classGenName} } from './endpoints/${classGenName}.js'`);
|
|
1482
1722
|
ParserUtils.writeClassFile(DIST_DIR_ENDPOINTS(isAdminEndpoint), classGenName, classBuffer, imports);
|
|
1483
|
-
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1484
1723
|
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1485
|
-
|
|
1724
|
+
const queryBuffer = tagToEndpointQueryRecord[tag];
|
|
1725
|
+
const queryFileName = ParserUtils.writeQueryFile(DIST_DIR_QUERIES(isAdminEndpoint), apiGenName, queryBuffer, apiImports, serviceNameTitle, tagToSdkFunctionNamesRecord[tag], imports);
|
|
1726
|
+
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1727
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports, tagToSdkFunctionNamesRecord[tag]);
|
|
1486
1728
|
apiList.push(apiGenName);
|
|
1487
1729
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder));
|
|
1488
1730
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR(isAdminEndpoint), `${apiGenName}`), targetSrcFolder));
|
|
1731
|
+
queryFileName && indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR(isAdminEndpoint), "queries", `${queryFileName}`), targetSrcFolder));
|
|
1489
1732
|
}
|
|
1490
1733
|
mainApiList.push(...apiList);
|
|
1491
1734
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder));
|
|
@@ -1597,7 +1840,7 @@ const generateSdk = async () => {
|
|
|
1597
1840
|
}
|
|
1598
1841
|
});
|
|
1599
1842
|
}
|
|
1600
|
-
const indexImportsArray = Array.from(indexImportsSet);
|
|
1843
|
+
const indexImportsArray = Array.from(indexImportsSet).sort();
|
|
1601
1844
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1602
1845
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1603
1846
|
});
|