@accelbyte/codegen 3.0.5 → 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,18 +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
|
|
175
|
+
${generateImports$1(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
176
|
+
${`import { ${$className} } from './endpoints/${$className}.js'
|
|
177
|
+
`}
|
|
135
178
|
|
|
136
179
|
export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
137
180
|
const sdkAssembly = sdk.assembly()
|
|
138
181
|
|
|
139
182
|
const namespace = args?.namespace ? args?.namespace : sdkAssembly.namespace
|
|
140
183
|
const requestConfig = ApiUtils.mergedConfigs(sdkAssembly.config, args)
|
|
141
|
-
const
|
|
184
|
+
const useSchemaValidation = sdkAssembly.useSchemaValidation
|
|
142
185
|
${body}
|
|
143
186
|
|
|
144
187
|
return {
|
|
@@ -228,8 +271,35 @@ const REMOVED_KEYWORDS = [
|
|
|
228
271
|
"/{namespace}/"
|
|
229
272
|
];
|
|
230
273
|
class ParserUtils {
|
|
231
|
-
static
|
|
232
|
-
|
|
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);
|
|
233
303
|
};
|
|
234
304
|
static generateClassName = (tag, isAdmin) => {
|
|
235
305
|
const className = _.upperFirst(_.camelCase(tag));
|
|
@@ -369,7 +439,7 @@ class ParserUtils {
|
|
|
369
439
|
if (bodyParam?.schema?.type === "array" && bodyParam?.schema?.items?.$ref) {
|
|
370
440
|
return `${ParserUtils.parseRefType(bodyParam.schema.items.$ref)}[]`;
|
|
371
441
|
}
|
|
372
|
-
if (bodyParam?.schema
|
|
442
|
+
if (bodyParam?.schema?.$ref) {
|
|
373
443
|
return ParserUtils.parseRefType(bodyParam.schema.$ref);
|
|
374
444
|
}
|
|
375
445
|
if (bodyParam?.schema?.additionalProperties?.type === "object") {
|
|
@@ -485,6 +555,20 @@ class ParserUtils {
|
|
|
485
555
|
const fileContent = templateClass(apiName, apiBuffer, imports);
|
|
486
556
|
fs.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
487
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
|
+
}
|
|
488
572
|
static writeXVersion(distDir, xversionJson, apiInfo) {
|
|
489
573
|
if (xversionJson) {
|
|
490
574
|
console.log("x-version:", xversionJson);
|
|
@@ -955,7 +1039,7 @@ const apis = {
|
|
|
955
1039
|
${returnStatement}
|
|
956
1040
|
}
|
|
957
1041
|
|
|
958
|
-
export const ${
|
|
1042
|
+
export const ${serviceNameTitle} = apis
|
|
959
1043
|
`;
|
|
960
1044
|
};
|
|
961
1045
|
|
|
@@ -1014,11 +1098,11 @@ const Endpoint = zod.z.object({
|
|
|
1014
1098
|
})),
|
|
1015
1099
|
parameters: zod.z.array(EndpointParameters).nullish(),
|
|
1016
1100
|
requestBody: zod.z.object({
|
|
1017
|
-
required: zod.z.boolean(),
|
|
1101
|
+
required: zod.z.boolean().nullish(),
|
|
1018
1102
|
content: zod.z.object({
|
|
1019
1103
|
"application/json": zod.z.object({
|
|
1020
1104
|
schema: Schema.nullish()
|
|
1021
|
-
})
|
|
1105
|
+
}).nullish()
|
|
1022
1106
|
}).nullish()
|
|
1023
1107
|
}).nullish(),
|
|
1024
1108
|
"x-security": zod.z.any().nullish()
|
|
@@ -1116,10 +1200,6 @@ const templateMethod = ({
|
|
|
1116
1200
|
methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
|
|
1117
1201
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
1118
1202
|
let methodImpl = "";
|
|
1119
|
-
const isCacheFetch = ["get"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
1120
|
-
const deprecateTag = isCacheFetch ? `/**
|
|
1121
|
-
* @deprecated Use "${classMethod}()" instead.
|
|
1122
|
-
*/` : "";
|
|
1123
1203
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
1124
1204
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1125
1205
|
const generateMethodName = () => `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
@@ -1130,18 +1210,11 @@ const templateMethod = ({
|
|
|
1130
1210
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1131
1211
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1132
1212
|
|
|
1133
|
-
${` return Validate.validateOrReturnResponse(this.
|
|
1213
|
+
${` return Validate.validateOrReturnResponse(this.useSchemaValidation, () => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}')`}
|
|
1134
1214
|
}
|
|
1135
1215
|
`;
|
|
1136
1216
|
if (!isGuardInvoked) {
|
|
1137
|
-
methodImpl =
|
|
1138
|
-
${deprecateTag}
|
|
1139
|
-
TODO_${classMethod}(${methodParams}): Promise<AxiosResponse<${resolvedResponseClass}>> {
|
|
1140
|
-
${queryParamsDefault}
|
|
1141
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1142
|
-
return this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1143
|
-
}
|
|
1144
|
-
`;
|
|
1217
|
+
methodImpl = ``;
|
|
1145
1218
|
}
|
|
1146
1219
|
const res = {
|
|
1147
1220
|
methodImpl,
|
|
@@ -1203,7 +1276,7 @@ const templateApiMethod = ({
|
|
|
1203
1276
|
const methodImpl = `
|
|
1204
1277
|
${descriptionText}
|
|
1205
1278
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1206
|
-
const $ = new ${classGenName}(Network.create(requestConfig), namespace,
|
|
1279
|
+
const $ = new ${classGenName}(Network.create(requestConfig), namespace, useSchemaValidation)
|
|
1207
1280
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
1208
1281
|
if (resp.error) throw resp.error
|
|
1209
1282
|
return resp.response.data
|
|
@@ -1266,6 +1339,151 @@ const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
|
1266
1339
|
return result;
|
|
1267
1340
|
};
|
|
1268
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
|
+
|
|
1269
1487
|
const GIT_URL = "https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages";
|
|
1270
1488
|
class SwaggerReaderHelpers {
|
|
1271
1489
|
static getServicePrefix = (servicePaths) => servicePaths[servicePaths.length - 1].split("/")[1];
|
|
@@ -1282,7 +1500,8 @@ class SwaggerReaderHelpers {
|
|
|
1282
1500
|
tagToEndpointClassesRecord: {},
|
|
1283
1501
|
tagToSdkClientRecord: {},
|
|
1284
1502
|
tagToSdkFunctionNamesRecord: {},
|
|
1285
|
-
tagToSdkImportsRecord: {}
|
|
1503
|
+
tagToSdkImportsRecord: {},
|
|
1504
|
+
tagToEndpointQueryRecord: {}
|
|
1286
1505
|
},
|
|
1287
1506
|
public: {
|
|
1288
1507
|
arrayDefinitions: [],
|
|
@@ -1291,7 +1510,8 @@ class SwaggerReaderHelpers {
|
|
|
1291
1510
|
tagToEndpointClassesRecord: {},
|
|
1292
1511
|
tagToSdkClientRecord: {},
|
|
1293
1512
|
tagToSdkFunctionNamesRecord: {},
|
|
1294
|
-
tagToSdkImportsRecord: {}
|
|
1513
|
+
tagToSdkImportsRecord: {},
|
|
1514
|
+
tagToEndpointQueryRecord: {}
|
|
1295
1515
|
}
|
|
1296
1516
|
};
|
|
1297
1517
|
const sortedPathsByLength = new Map(Object.entries(api.paths).sort((a, b) => {
|
|
@@ -1320,7 +1540,8 @@ class SwaggerReaderHelpers {
|
|
|
1320
1540
|
tagToEndpointClassesRecord,
|
|
1321
1541
|
tagToSdkClientRecord,
|
|
1322
1542
|
tagToSdkFunctionNamesRecord,
|
|
1323
|
-
tagToSdkImportsRecord
|
|
1543
|
+
tagToSdkImportsRecord,
|
|
1544
|
+
tagToEndpointQueryRecord
|
|
1324
1545
|
} = picked;
|
|
1325
1546
|
const tagToClassMethodsMapByType = isAdminEndpoint ? tagToClassMethodsMap.admin : tagToClassMethodsMap.public;
|
|
1326
1547
|
const httpMethods = Object.keys(operation);
|
|
@@ -1367,12 +1588,12 @@ class SwaggerReaderHelpers {
|
|
|
1367
1588
|
{
|
|
1368
1589
|
name: "body",
|
|
1369
1590
|
in: "body",
|
|
1370
|
-
schema: endpoint.requestBody.content["application/json"]
|
|
1591
|
+
schema: endpoint.requestBody.content["application/json"]?.schema
|
|
1371
1592
|
}
|
|
1372
1593
|
];
|
|
1373
1594
|
}
|
|
1374
1595
|
const { methodImpl, methodParams, methodParamsNoTypes, importStatements } = templateMethod({
|
|
1375
|
-
classMethod,
|
|
1596
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1376
1597
|
description,
|
|
1377
1598
|
httpMethod,
|
|
1378
1599
|
path: pathWithBase,
|
|
@@ -1384,8 +1605,21 @@ class SwaggerReaderHelpers {
|
|
|
1384
1605
|
deprecated
|
|
1385
1606
|
});
|
|
1386
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;
|
|
1387
1621
|
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1388
|
-
classMethod,
|
|
1622
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1389
1623
|
description,
|
|
1390
1624
|
httpMethod,
|
|
1391
1625
|
path: pathWithBase,
|
|
@@ -1399,11 +1633,15 @@ class SwaggerReaderHelpers {
|
|
|
1399
1633
|
xSecurity: endpoint["x-security"]
|
|
1400
1634
|
});
|
|
1401
1635
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
1402
|
-
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
|
|
1636
|
+
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + ParserUtils.addDeprecatedSuffix(deprecated, classMethod) + ",";
|
|
1403
1637
|
tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
|
|
1404
1638
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1405
|
-
const
|
|
1406
|
-
|
|
1639
|
+
const resultSnippet = templateSdkSnippet({
|
|
1640
|
+
serviceNameTitle,
|
|
1641
|
+
apiName: apiGenName,
|
|
1642
|
+
snippetMethod,
|
|
1643
|
+
snippetApiArgs
|
|
1644
|
+
});
|
|
1407
1645
|
const currentSnippetMap = {};
|
|
1408
1646
|
snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
|
|
1409
1647
|
currentSnippetMap.web = resultSnippet;
|
|
@@ -1423,12 +1661,13 @@ class CodeGenerator {
|
|
|
1423
1661
|
static srcFolder = () => CliParser.getOutputPath();
|
|
1424
1662
|
static getGeneratedFolder = (isAdmin) => isAdmin ? `${CodeGenerator.srcFolder()}/generated-admin` : `${CodeGenerator.srcFolder()}/generated-public`;
|
|
1425
1663
|
static getGeneratedSnippetsFolder = () => `${CliParser.getSnippetOutputPath()}/generated-snippets`;
|
|
1426
|
-
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS) => {
|
|
1664
|
+
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES) => {
|
|
1427
1665
|
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1428
1666
|
ParserUtils.mkdirIfNotExist(DIST_DIR(true));
|
|
1429
1667
|
ParserUtils.mkdirIfNotExist(DIST_DIR(false));
|
|
1430
1668
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(true));
|
|
1431
1669
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(false));
|
|
1670
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_QUERIES(true));
|
|
1432
1671
|
};
|
|
1433
1672
|
static main = async (nameArray) => {
|
|
1434
1673
|
const serviceName = nameArray[0];
|
|
@@ -1456,9 +1695,10 @@ class CodeGenerator {
|
|
|
1456
1695
|
}
|
|
1457
1696
|
const DIST_DIR = (isAdmin) => `${CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1458
1697
|
const DIST_DIR_ENDPOINTS = (isAdmin) => path.join(DIST_DIR(isAdmin), "endpoints");
|
|
1698
|
+
const DIST_DIR_QUERIES = (isAdmin) => path.join(DIST_DIR(isAdmin), "queries");
|
|
1459
1699
|
const DIST_DEFINITION_DIR = path.join(CodeGenerator.srcFolder(), "generated-definitions");
|
|
1460
1700
|
const targetSrcFolder = `${CodeGenerator.srcFolder()}/`;
|
|
1461
|
-
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS);
|
|
1701
|
+
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES);
|
|
1462
1702
|
const mainApiList = [];
|
|
1463
1703
|
const generatePublicOrAdmin = (isAdmin) => {
|
|
1464
1704
|
const parsedInformationByType = isAdmin ? parsedInformation.admin : parsedInformation.public;
|
|
@@ -1468,7 +1708,8 @@ class CodeGenerator {
|
|
|
1468
1708
|
tagToEndpointClassesRecord,
|
|
1469
1709
|
tagToSdkClientRecord,
|
|
1470
1710
|
tagToSdkFunctionNamesRecord,
|
|
1471
|
-
tagToSdkImportsRecord
|
|
1711
|
+
tagToSdkImportsRecord,
|
|
1712
|
+
tagToEndpointQueryRecord
|
|
1472
1713
|
} = parsedInformationByType;
|
|
1473
1714
|
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1474
1715
|
const apiList = [];
|
|
@@ -1479,12 +1720,15 @@ class CodeGenerator {
|
|
|
1479
1720
|
const apiImports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1480
1721
|
apiImports.push(`import { ${classGenName} } from './endpoints/${classGenName}.js'`);
|
|
1481
1722
|
ParserUtils.writeClassFile(DIST_DIR_ENDPOINTS(isAdminEndpoint), classGenName, classBuffer, imports);
|
|
1482
|
-
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1483
1723
|
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1484
|
-
|
|
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]);
|
|
1485
1728
|
apiList.push(apiGenName);
|
|
1486
1729
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder));
|
|
1487
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));
|
|
1488
1732
|
}
|
|
1489
1733
|
mainApiList.push(...apiList);
|
|
1490
1734
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder));
|
|
@@ -1596,7 +1840,7 @@ const generateSdk = async () => {
|
|
|
1596
1840
|
}
|
|
1597
1841
|
});
|
|
1598
1842
|
}
|
|
1599
|
-
const indexImportsArray = Array.from(indexImportsSet);
|
|
1843
|
+
const indexImportsArray = Array.from(indexImportsSet).sort();
|
|
1600
1844
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1601
1845
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1602
1846
|
});
|