@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.
|
@@ -66,7 +66,7 @@ const makeNewImportVarMap$1 = () => ({
|
|
|
66
66
|
axios: ["AxiosInstance"],
|
|
67
67
|
"@accelbyte/sdk": ["SDKRequestConfig"]
|
|
68
68
|
});
|
|
69
|
-
const generateImports = (body, importStatements, makeNewImportVarMap2, getImportableVarMap2) => {
|
|
69
|
+
const generateImports$1 = (body, importStatements, makeNewImportVarMap2, getImportableVarMap2) => {
|
|
70
70
|
const usedImportVarMap = makeNewImportVarMap2;
|
|
71
71
|
const importableVarMap = getImportableVarMap2;
|
|
72
72
|
for (const [moduleSource, importableVars] of Object.entries(importableVarMap)) {
|
|
@@ -87,16 +87,55 @@ const templateClass = (className, body, importStatements) => {
|
|
|
87
87
|
return `/**
|
|
88
88
|
* AUTO GENERATED
|
|
89
89
|
*/
|
|
90
|
-
${generateImports(body, importStatements, makeNewImportVarMap$1(), getImportableVarMap$1())}
|
|
90
|
+
${generateImports$1(body, importStatements, makeNewImportVarMap$1(), getImportableVarMap$1())}
|
|
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 useSchemaValidation = true) {}
|
|
95
95
|
${body}
|
|
96
96
|
}
|
|
97
97
|
`;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
+
const generateImports = (body, className) => {
|
|
101
|
+
const generatedImports = `import { AccelbyteSDK, ApiArgs, ApiError } from '@accelbyte/sdk'
|
|
102
|
+
import { AxiosError } from 'axios'
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
import { useQuery, UseQueryOptions, UseQueryResult, useMutation, UseMutationOptions, UseMutationResult } from '@tanstack/react-query'
|
|
105
|
+
import { ${className} } from "../${className}.js"
|
|
106
|
+
`;
|
|
107
|
+
return generatedImports;
|
|
108
|
+
};
|
|
109
|
+
const templateQuery = (className, body, importStatements, serviceNameTitle, returnMethods, paramImports) => {
|
|
110
|
+
const classNameWithoutApi = className.replace("Api", "");
|
|
111
|
+
const queryKeys = ParserUtils.createQueryKeys(classNameWithoutApi, returnMethods);
|
|
112
|
+
const generatedImports = generateImports(body, className);
|
|
113
|
+
return `/**
|
|
114
|
+
* AUTO GENERATED
|
|
115
|
+
*/
|
|
116
|
+
/* eslint-disable camelcase */
|
|
117
|
+
${generatedImports}
|
|
118
|
+
${filterUsedImports(paramImports, body)}
|
|
119
|
+
|
|
120
|
+
export enum Key_${classNameWithoutApi} {
|
|
121
|
+
${queryKeys}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
${body}
|
|
125
|
+
`;
|
|
126
|
+
};
|
|
127
|
+
function filterUsedImports(importArr, body) {
|
|
128
|
+
return importArr.filter((path) => {
|
|
129
|
+
const start = path.indexOf("{") + 1;
|
|
130
|
+
const end = path.indexOf("}");
|
|
131
|
+
if (start > 0 && end > start) {
|
|
132
|
+
const importName = path.slice(start, end).trim();
|
|
133
|
+
return body.includes(importName);
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
}).map((path) => path).join("\n") + "\n";
|
|
137
|
+
}
|
|
138
|
+
|
|
100
139
|
const getImportableVarMap = () => ({
|
|
101
140
|
"@accelbyte/sdk": ["CodeGenUtil", "IResponse", "Validate", "ApiArgs", "Network", "AccelbyteSDK"]
|
|
102
141
|
});
|
|
@@ -104,18 +143,22 @@ const makeNewImportVarMap = () => ({
|
|
|
104
143
|
"@accelbyte/sdk": ["AccelbyteSDK", "ApiArgs", "ApiUtils"]
|
|
105
144
|
});
|
|
106
145
|
const templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
146
|
+
const $className = className.replace("Api", "$");
|
|
107
147
|
return `/**
|
|
108
148
|
* AUTO GENERATED
|
|
109
149
|
*/
|
|
110
150
|
/* eslint-disable camelcase */
|
|
111
|
-
|
|
151
|
+
// @ts-ignore -> ts-expect-error TS6133
|
|
152
|
+
${generateImports$1(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
153
|
+
${`import { ${$className} } from './endpoints/${$className}.js'
|
|
154
|
+
`}
|
|
112
155
|
|
|
113
156
|
export function ${className}(sdk: AccelbyteSDK, args?: ApiArgs) {
|
|
114
157
|
const sdkAssembly = sdk.assembly()
|
|
115
158
|
|
|
116
159
|
const namespace = args?.namespace ? args?.namespace : sdkAssembly.namespace
|
|
117
160
|
const requestConfig = ApiUtils.mergedConfigs(sdkAssembly.config, args)
|
|
118
|
-
const
|
|
161
|
+
const useSchemaValidation = sdkAssembly.useSchemaValidation
|
|
119
162
|
${body}
|
|
120
163
|
|
|
121
164
|
return {
|
|
@@ -205,8 +248,35 @@ const REMOVED_KEYWORDS = [
|
|
|
205
248
|
"/{namespace}/"
|
|
206
249
|
];
|
|
207
250
|
class ParserUtils {
|
|
208
|
-
static
|
|
209
|
-
|
|
251
|
+
static createQueryKeys(classNameWithoutApi, csvMethodNames) {
|
|
252
|
+
const keys = csvMethodNames.split(",");
|
|
253
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
254
|
+
const enumString = keys.reduce((acc, key, index) => {
|
|
255
|
+
const trimmedKey = key.trim();
|
|
256
|
+
const isDeprecated = key.indexOf("_DEPRECATED") > 0;
|
|
257
|
+
if (trimmedKey && !isDeprecated) {
|
|
258
|
+
const cleanedKey = trimmedKey.replace(/^(get|update|create|patch|delete)[_]?/, "");
|
|
259
|
+
if (!processedKeys.has(cleanedKey)) {
|
|
260
|
+
processedKeys.add(cleanedKey);
|
|
261
|
+
acc += `${cleanedKey} = '${classNameWithoutApi}.${cleanedKey}'`;
|
|
262
|
+
if (index < keys.length - 1) {
|
|
263
|
+
acc += ",\n";
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return acc;
|
|
268
|
+
}, "");
|
|
269
|
+
return enumString;
|
|
270
|
+
}
|
|
271
|
+
static addDeprecatedSuffix(isDeprecated, methodName) {
|
|
272
|
+
if (isDeprecated) {
|
|
273
|
+
return methodName + "_DEPRECATED";
|
|
274
|
+
} else {
|
|
275
|
+
return methodName;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
static replaceAll = (text, search, replace) => {
|
|
279
|
+
return text.split(search).join(replace);
|
|
210
280
|
};
|
|
211
281
|
static generateClassName = (tag, isAdmin) => {
|
|
212
282
|
const className = _.upperFirst(_.camelCase(tag));
|
|
@@ -346,7 +416,7 @@ class ParserUtils {
|
|
|
346
416
|
if (bodyParam?.schema?.type === "array" && bodyParam?.schema?.items?.$ref) {
|
|
347
417
|
return `${ParserUtils.parseRefType(bodyParam.schema.items.$ref)}[]`;
|
|
348
418
|
}
|
|
349
|
-
if (bodyParam?.schema
|
|
419
|
+
if (bodyParam?.schema?.$ref) {
|
|
350
420
|
return ParserUtils.parseRefType(bodyParam.schema.$ref);
|
|
351
421
|
}
|
|
352
422
|
if (bodyParam?.schema?.additionalProperties?.type === "object") {
|
|
@@ -462,6 +532,20 @@ class ParserUtils {
|
|
|
462
532
|
const fileContent = templateClass(apiName, apiBuffer, imports);
|
|
463
533
|
fs__default.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
464
534
|
}
|
|
535
|
+
static writeAtomFile(distDir, apiName, fileContent) {
|
|
536
|
+
ParserUtils.mkdirIfNotExist(distDir);
|
|
537
|
+
fs__default.writeFileSync(`${distDir}/${apiName}.atom.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
538
|
+
}
|
|
539
|
+
static writeQueryFile(distDir, apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports) {
|
|
540
|
+
if (apiBuffer.length < 1) {
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
const queryFileName = `${apiName.replace("Api", "")}.query`;
|
|
544
|
+
ParserUtils.mkdirIfNotExist(distDir);
|
|
545
|
+
const fileContent = templateQuery(apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports);
|
|
546
|
+
fs__default.writeFileSync(`${distDir}/${queryFileName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
547
|
+
return queryFileName;
|
|
548
|
+
}
|
|
465
549
|
static writeXVersion(distDir, xversionJson, apiInfo) {
|
|
466
550
|
if (xversionJson) {
|
|
467
551
|
console.log("x-version:", xversionJson);
|
|
@@ -932,7 +1016,7 @@ const apis = {
|
|
|
932
1016
|
${returnStatement}
|
|
933
1017
|
}
|
|
934
1018
|
|
|
935
|
-
export const ${
|
|
1019
|
+
export const ${serviceNameTitle} = apis
|
|
936
1020
|
`;
|
|
937
1021
|
};
|
|
938
1022
|
|
|
@@ -991,11 +1075,11 @@ const Endpoint = z.object({
|
|
|
991
1075
|
})),
|
|
992
1076
|
parameters: z.array(EndpointParameters).nullish(),
|
|
993
1077
|
requestBody: z.object({
|
|
994
|
-
required: z.boolean(),
|
|
1078
|
+
required: z.boolean().nullish(),
|
|
995
1079
|
content: z.object({
|
|
996
1080
|
"application/json": z.object({
|
|
997
1081
|
schema: Schema.nullish()
|
|
998
|
-
})
|
|
1082
|
+
}).nullish()
|
|
999
1083
|
}).nullish()
|
|
1000
1084
|
}).nullish(),
|
|
1001
1085
|
"x-security": z.any().nullish()
|
|
@@ -1093,10 +1177,6 @@ const templateMethod = ({
|
|
|
1093
1177
|
methodParams = (queryParamsType ? `${methodParams} ${queryParamsType}` : methodParams).replace(/,\s*$/, "");
|
|
1094
1178
|
methodParamsNoTypes = queryParamsType ? `${methodParamsNoTypes} queryParams` : methodParamsNoTypes;
|
|
1095
1179
|
let methodImpl = "";
|
|
1096
|
-
const isCacheFetch = ["get"].includes(httpMethod) && resolvedResponseClass !== "unknown";
|
|
1097
|
-
const deprecateTag = isCacheFetch ? `/**
|
|
1098
|
-
* @deprecated Use "${classMethod}()" instead.
|
|
1099
|
-
*/` : "";
|
|
1100
1180
|
const isGuardInvoked = ["get", "post", "put", "patch", "delete"].includes(httpMethod);
|
|
1101
1181
|
const responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1102
1182
|
const generateMethodName = () => `${classMethod}(${methodParams}): Promise<${responseSyncType}<${responseType}>>`;
|
|
@@ -1107,18 +1187,11 @@ const templateMethod = ({
|
|
|
1107
1187
|
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1108
1188
|
const resultPromise = this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1109
1189
|
|
|
1110
|
-
${` return Validate.validateOrReturnResponse(this.
|
|
1190
|
+
${` return Validate.validateOrReturnResponse(this.useSchemaValidation, () => resultPromise, ${resolvedResponseClassValidated}, '${resolvedResponseClassValidated}')`}
|
|
1111
1191
|
}
|
|
1112
1192
|
`;
|
|
1113
1193
|
if (!isGuardInvoked) {
|
|
1114
|
-
methodImpl =
|
|
1115
|
-
${deprecateTag}
|
|
1116
|
-
TODO_${classMethod}(${methodParams}): Promise<AxiosResponse<${resolvedResponseClass}>> {
|
|
1117
|
-
${queryParamsDefault}
|
|
1118
|
-
const url = ${newPath} ${formPayloadString} ${isFileUpload ? "\n// TODO file upload not implemented" : ""}
|
|
1119
|
-
return this.axiosInstance.${httpMethod}(url, ${dataPayload})
|
|
1120
|
-
}
|
|
1121
|
-
`;
|
|
1194
|
+
methodImpl = ``;
|
|
1122
1195
|
}
|
|
1123
1196
|
const res = {
|
|
1124
1197
|
methodImpl,
|
|
@@ -1180,7 +1253,7 @@ const templateApiMethod = ({
|
|
|
1180
1253
|
const methodImpl = `
|
|
1181
1254
|
${descriptionText}
|
|
1182
1255
|
async function ${classMethod}(${methodParams}): Promise<${responseType}> {
|
|
1183
|
-
const $ = new ${classGenName}(Network.create(requestConfig), namespace,
|
|
1256
|
+
const $ = new ${classGenName}(Network.create(requestConfig), namespace, useSchemaValidation)
|
|
1184
1257
|
const resp = await $.${classMethod}(${methodParamsNoTypes})
|
|
1185
1258
|
if (resp.error) throw resp.error
|
|
1186
1259
|
return resp.response.data
|
|
@@ -1243,6 +1316,151 @@ const normalizeMethodSnippet = (methodInput, splitWord) => {
|
|
|
1243
1316
|
return result;
|
|
1244
1317
|
};
|
|
1245
1318
|
|
|
1319
|
+
const templateQueryMethod = ({
|
|
1320
|
+
classMethod,
|
|
1321
|
+
httpMethod,
|
|
1322
|
+
path,
|
|
1323
|
+
pathParams,
|
|
1324
|
+
responseClass,
|
|
1325
|
+
methodParams,
|
|
1326
|
+
apiGenName,
|
|
1327
|
+
isFormUrlEncoded,
|
|
1328
|
+
deprecated
|
|
1329
|
+
}) => {
|
|
1330
|
+
if (isFormUrlEncoded || deprecated) {
|
|
1331
|
+
return "";
|
|
1332
|
+
}
|
|
1333
|
+
const isPostFetch = httpMethod === "post" && (path.includes("/table-query/") || path.endsWith("/list"));
|
|
1334
|
+
const isAdmin = path.indexOf("/admin/") > 0;
|
|
1335
|
+
const isGet = httpMethod === "get" || isPostFetch;
|
|
1336
|
+
let queryMethod = isGet ? "useQuery" : "useMutation";
|
|
1337
|
+
let mParams = "";
|
|
1338
|
+
let mParamsNoTypes = "";
|
|
1339
|
+
let newPath = `'${path}'`;
|
|
1340
|
+
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams, path);
|
|
1341
|
+
for (const pathParam of sortedPathParams) {
|
|
1342
|
+
const type = ParserUtils.parseType(pathParam);
|
|
1343
|
+
if (pathParam.name !== "namespace") {
|
|
1344
|
+
mParams += pathParam.name + `:${type}, `;
|
|
1345
|
+
mParamsNoTypes += pathParam.name + ", ";
|
|
1346
|
+
}
|
|
1347
|
+
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
1348
|
+
if (path.match(`{${pathParam.name}}`)) {
|
|
1349
|
+
if (type === "string") {
|
|
1350
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
1351
|
+
} else {
|
|
1352
|
+
newPath = `${newPath}.replace('{${pathParam.name}}', String(${pName}))`;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
const resolvedResponseClass = responseClass || "unknown";
|
|
1357
|
+
let _methodName = convertMethodName(classMethod, isPostFetch);
|
|
1358
|
+
_methodName = isAdmin ? _methodName.replace("use", "useAdm") : _methodName;
|
|
1359
|
+
const _responseType = resolvedResponseClass !== "unknown" ? `${resolvedResponseClass}` : "unknown";
|
|
1360
|
+
const _methodParams = methodParams && methodParams.length > 0 ? `& { ${methodParams} }` : "";
|
|
1361
|
+
const _methodParamsImpl = convertToMethodImplArgs(methodParams);
|
|
1362
|
+
const queryMethodImpl = `
|
|
1363
|
+
|
|
1364
|
+
export const ${_methodName} = (
|
|
1365
|
+
sdk: AccelbyteSDK,
|
|
1366
|
+
input: ApiArgs ${_methodParams},
|
|
1367
|
+
options?: Omit<UseQueryOptions<${_responseType}, AxiosError<ApiError>>, 'queryKey'>,
|
|
1368
|
+
callback?: (data: ${_responseType}) => void
|
|
1369
|
+
): UseQueryResult<${_responseType}, AxiosError<ApiError>> => {
|
|
1370
|
+
//
|
|
1371
|
+
const queryFn = (
|
|
1372
|
+
sdk: AccelbyteSDK,
|
|
1373
|
+
input: Parameters<typeof ${_methodName}>[1]
|
|
1374
|
+
) => async () => {
|
|
1375
|
+
const data =
|
|
1376
|
+
(await ${apiGenName}(sdk, { namespace: input.namespace }).
|
|
1377
|
+
${classMethod}(${_methodParamsImpl}))
|
|
1378
|
+
callback && callback(data)
|
|
1379
|
+
return data
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
return ${queryMethod}<${_responseType}, AxiosError<ApiError>>({
|
|
1383
|
+
queryKey: [${createQueryKey(apiGenName, classMethod)}, input],
|
|
1384
|
+
queryFn: queryFn(sdk, input),
|
|
1385
|
+
...options
|
|
1386
|
+
})
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
`;
|
|
1390
|
+
const mutationMethodImpl = `
|
|
1391
|
+
|
|
1392
|
+
export const ${_methodName} = (
|
|
1393
|
+
sdk: AccelbyteSDK,
|
|
1394
|
+
options?: Omit<UseMutationOptions<${_responseType}, AxiosError<ApiError>, ApiArgs ${_methodParams}>, 'mutationKey'>,
|
|
1395
|
+
callback?: (data: ${_responseType}) => void
|
|
1396
|
+
): UseMutationResult<${_responseType}, AxiosError<ApiError>, ApiArgs ${_methodParams}> => {
|
|
1397
|
+
//
|
|
1398
|
+
const mutationFn = async (input: ApiArgs ${_methodParams}) => {
|
|
1399
|
+
const data =
|
|
1400
|
+
(await ${apiGenName}(sdk, { namespace: input.namespace, config: input.config }).
|
|
1401
|
+
${classMethod}(${_methodParamsImpl}))
|
|
1402
|
+
callback && callback(data)
|
|
1403
|
+
return data
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
return useMutation({
|
|
1407
|
+
mutationKey: [${createQueryKey(apiGenName, classMethod)}],
|
|
1408
|
+
mutationFn,
|
|
1409
|
+
...options
|
|
1410
|
+
})
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
`;
|
|
1414
|
+
return isGet ? queryMethodImpl : mutationMethodImpl;
|
|
1415
|
+
};
|
|
1416
|
+
function createQueryKey(className, methodName) {
|
|
1417
|
+
if (methodName.indexOf("_DEPRECATED") > 0) {
|
|
1418
|
+
return "";
|
|
1419
|
+
}
|
|
1420
|
+
const prefixRegex = /^(get|create|update|delete|patch)[_]?/i;
|
|
1421
|
+
const cleanedMethodName = methodName.replace(prefixRegex, "").trim();
|
|
1422
|
+
const finalMethodName = cleanedMethodName.charAt(0).toUpperCase() + cleanedMethodName.slice(1);
|
|
1423
|
+
return `Key_${className.replace("Api", "")}.${finalMethodName}`;
|
|
1424
|
+
}
|
|
1425
|
+
const prefixMappings = {
|
|
1426
|
+
get: "use",
|
|
1427
|
+
create: "useCreate",
|
|
1428
|
+
patch: "usePatch",
|
|
1429
|
+
update: "useUpdate",
|
|
1430
|
+
delete: "useDelete"
|
|
1431
|
+
};
|
|
1432
|
+
function convertMethodName(classMethod, isPostFetch) {
|
|
1433
|
+
for (const [originalPrefix, newPrefix] of Object.entries(prefixMappings)) {
|
|
1434
|
+
if (classMethod.startsWith(originalPrefix)) {
|
|
1435
|
+
if (isPostFetch) {
|
|
1436
|
+
return classMethod.replace(originalPrefix, "useFetch");
|
|
1437
|
+
}
|
|
1438
|
+
const newMethodName = classMethod.replace(originalPrefix, newPrefix);
|
|
1439
|
+
if (originalPrefix === "get") {
|
|
1440
|
+
return newMethodName;
|
|
1441
|
+
} else {
|
|
1442
|
+
return newMethodName + "Mutation";
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
return classMethod;
|
|
1447
|
+
}
|
|
1448
|
+
function convertToMethodImplArgs(methodArgs) {
|
|
1449
|
+
let properties = methodArgs.split(/,\s*(?![^{}]*\})/).map((prop) => prop.trim()).filter(Boolean);
|
|
1450
|
+
let formattedProperties = [];
|
|
1451
|
+
properties.forEach((prop) => {
|
|
1452
|
+
if (prop.includes(": {")) {
|
|
1453
|
+
const propertyName = prop.split(": {")[0].replace("?", "").trim();
|
|
1454
|
+
formattedProperties.push(`input.${propertyName}`);
|
|
1455
|
+
} else {
|
|
1456
|
+
const colonIndex = prop.indexOf(":");
|
|
1457
|
+
const propertyName = prop.substring(0, colonIndex).replace("?", "").trim();
|
|
1458
|
+
formattedProperties.push(`input.${propertyName}`);
|
|
1459
|
+
}
|
|
1460
|
+
});
|
|
1461
|
+
return formattedProperties.join(", ");
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1246
1464
|
const GIT_URL = "https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages";
|
|
1247
1465
|
class SwaggerReaderHelpers {
|
|
1248
1466
|
static getServicePrefix = (servicePaths) => servicePaths[servicePaths.length - 1].split("/")[1];
|
|
@@ -1259,7 +1477,8 @@ class SwaggerReaderHelpers {
|
|
|
1259
1477
|
tagToEndpointClassesRecord: {},
|
|
1260
1478
|
tagToSdkClientRecord: {},
|
|
1261
1479
|
tagToSdkFunctionNamesRecord: {},
|
|
1262
|
-
tagToSdkImportsRecord: {}
|
|
1480
|
+
tagToSdkImportsRecord: {},
|
|
1481
|
+
tagToEndpointQueryRecord: {}
|
|
1263
1482
|
},
|
|
1264
1483
|
public: {
|
|
1265
1484
|
arrayDefinitions: [],
|
|
@@ -1268,7 +1487,8 @@ class SwaggerReaderHelpers {
|
|
|
1268
1487
|
tagToEndpointClassesRecord: {},
|
|
1269
1488
|
tagToSdkClientRecord: {},
|
|
1270
1489
|
tagToSdkFunctionNamesRecord: {},
|
|
1271
|
-
tagToSdkImportsRecord: {}
|
|
1490
|
+
tagToSdkImportsRecord: {},
|
|
1491
|
+
tagToEndpointQueryRecord: {}
|
|
1272
1492
|
}
|
|
1273
1493
|
};
|
|
1274
1494
|
const sortedPathsByLength = new Map(Object.entries(api.paths).sort((a, b) => {
|
|
@@ -1297,7 +1517,8 @@ class SwaggerReaderHelpers {
|
|
|
1297
1517
|
tagToEndpointClassesRecord,
|
|
1298
1518
|
tagToSdkClientRecord,
|
|
1299
1519
|
tagToSdkFunctionNamesRecord,
|
|
1300
|
-
tagToSdkImportsRecord
|
|
1520
|
+
tagToSdkImportsRecord,
|
|
1521
|
+
tagToEndpointQueryRecord
|
|
1301
1522
|
} = picked;
|
|
1302
1523
|
const tagToClassMethodsMapByType = isAdminEndpoint ? tagToClassMethodsMap.admin : tagToClassMethodsMap.public;
|
|
1303
1524
|
const httpMethods = Object.keys(operation);
|
|
@@ -1344,12 +1565,12 @@ class SwaggerReaderHelpers {
|
|
|
1344
1565
|
{
|
|
1345
1566
|
name: "body",
|
|
1346
1567
|
in: "body",
|
|
1347
|
-
schema: endpoint.requestBody.content["application/json"]
|
|
1568
|
+
schema: endpoint.requestBody.content["application/json"]?.schema
|
|
1348
1569
|
}
|
|
1349
1570
|
];
|
|
1350
1571
|
}
|
|
1351
1572
|
const { methodImpl, methodParams, methodParamsNoTypes, importStatements } = templateMethod({
|
|
1352
|
-
classMethod,
|
|
1573
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1353
1574
|
description,
|
|
1354
1575
|
httpMethod,
|
|
1355
1576
|
path: pathWithBase,
|
|
@@ -1361,8 +1582,21 @@ class SwaggerReaderHelpers {
|
|
|
1361
1582
|
deprecated
|
|
1362
1583
|
});
|
|
1363
1584
|
tagToEndpointClassesRecord[tag] = (tagToEndpointClassesRecord[tag] || "") + methodImpl;
|
|
1585
|
+
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1586
|
+
const queryMethodImpl = templateQueryMethod({
|
|
1587
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1588
|
+
httpMethod,
|
|
1589
|
+
path: pathWithBase,
|
|
1590
|
+
pathParams,
|
|
1591
|
+
responseClass,
|
|
1592
|
+
methodParams,
|
|
1593
|
+
apiGenName,
|
|
1594
|
+
isFormUrlEncoded,
|
|
1595
|
+
deprecated
|
|
1596
|
+
});
|
|
1597
|
+
tagToEndpointQueryRecord[tag] = (tagToEndpointQueryRecord[tag] || "") + queryMethodImpl;
|
|
1364
1598
|
const { generatedMethodString, snippetApiArgs, snippetMethod, snippetShell } = templateApiMethod({
|
|
1365
|
-
classMethod,
|
|
1599
|
+
classMethod: ParserUtils.addDeprecatedSuffix(deprecated, classMethod),
|
|
1366
1600
|
description,
|
|
1367
1601
|
httpMethod,
|
|
1368
1602
|
path: pathWithBase,
|
|
@@ -1376,11 +1610,15 @@ class SwaggerReaderHelpers {
|
|
|
1376
1610
|
xSecurity: endpoint["x-security"]
|
|
1377
1611
|
});
|
|
1378
1612
|
tagToSdkClientRecord[tag] = (tagToSdkClientRecord[tag] || "") + generatedMethodString;
|
|
1379
|
-
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + classMethod + ",";
|
|
1613
|
+
tagToSdkFunctionNamesRecord[tag] = (tagToSdkFunctionNamesRecord[tag] || "") + ParserUtils.addDeprecatedSuffix(deprecated, classMethod) + ",";
|
|
1380
1614
|
tagToSdkImportsRecord[tag] = tagToSdkImportsRecord[tag] ? [.../* @__PURE__ */ new Set([...importStatements, ...tagToSdkImportsRecord[tag]])] : [...new Set(importStatements)];
|
|
1381
1615
|
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1382
|
-
const
|
|
1383
|
-
|
|
1616
|
+
const resultSnippet = templateSdkSnippet({
|
|
1617
|
+
serviceNameTitle,
|
|
1618
|
+
apiName: apiGenName,
|
|
1619
|
+
snippetMethod,
|
|
1620
|
+
snippetApiArgs
|
|
1621
|
+
});
|
|
1384
1622
|
const currentSnippetMap = {};
|
|
1385
1623
|
snippetMap[pathWithBase][httpMethod] = currentSnippetMap;
|
|
1386
1624
|
currentSnippetMap.web = resultSnippet;
|
|
@@ -1400,12 +1638,13 @@ class CodeGenerator {
|
|
|
1400
1638
|
static srcFolder = () => CliParser.getOutputPath();
|
|
1401
1639
|
static getGeneratedFolder = (isAdmin) => isAdmin ? `${CodeGenerator.srcFolder()}/generated-admin` : `${CodeGenerator.srcFolder()}/generated-public`;
|
|
1402
1640
|
static getGeneratedSnippetsFolder = () => `${CliParser.getSnippetOutputPath()}/generated-snippets`;
|
|
1403
|
-
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS) => {
|
|
1641
|
+
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES) => {
|
|
1404
1642
|
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1405
1643
|
ParserUtils.mkdirIfNotExist(DIST_DIR(true));
|
|
1406
1644
|
ParserUtils.mkdirIfNotExist(DIST_DIR(false));
|
|
1407
1645
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(true));
|
|
1408
1646
|
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(false));
|
|
1647
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_QUERIES(true));
|
|
1409
1648
|
};
|
|
1410
1649
|
static main = async (nameArray) => {
|
|
1411
1650
|
const serviceName = nameArray[0];
|
|
@@ -1433,9 +1672,10 @@ class CodeGenerator {
|
|
|
1433
1672
|
}
|
|
1434
1673
|
const DIST_DIR = (isAdmin) => `${CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1435
1674
|
const DIST_DIR_ENDPOINTS = (isAdmin) => path__default.join(DIST_DIR(isAdmin), "endpoints");
|
|
1675
|
+
const DIST_DIR_QUERIES = (isAdmin) => path__default.join(DIST_DIR(isAdmin), "queries");
|
|
1436
1676
|
const DIST_DEFINITION_DIR = path__default.join(CodeGenerator.srcFolder(), "generated-definitions");
|
|
1437
1677
|
const targetSrcFolder = `${CodeGenerator.srcFolder()}/`;
|
|
1438
|
-
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS);
|
|
1678
|
+
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES);
|
|
1439
1679
|
const mainApiList = [];
|
|
1440
1680
|
const generatePublicOrAdmin = (isAdmin) => {
|
|
1441
1681
|
const parsedInformationByType = isAdmin ? parsedInformation.admin : parsedInformation.public;
|
|
@@ -1445,7 +1685,8 @@ class CodeGenerator {
|
|
|
1445
1685
|
tagToEndpointClassesRecord,
|
|
1446
1686
|
tagToSdkClientRecord,
|
|
1447
1687
|
tagToSdkFunctionNamesRecord,
|
|
1448
|
-
tagToSdkImportsRecord
|
|
1688
|
+
tagToSdkImportsRecord,
|
|
1689
|
+
tagToEndpointQueryRecord
|
|
1449
1690
|
} = parsedInformationByType;
|
|
1450
1691
|
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1451
1692
|
const apiList = [];
|
|
@@ -1456,12 +1697,15 @@ class CodeGenerator {
|
|
|
1456
1697
|
const apiImports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1457
1698
|
apiImports.push(`import { ${classGenName} } from './endpoints/${classGenName}.js'`);
|
|
1458
1699
|
ParserUtils.writeClassFile(DIST_DIR_ENDPOINTS(isAdminEndpoint), classGenName, classBuffer, imports);
|
|
1459
|
-
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1460
1700
|
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1461
|
-
|
|
1701
|
+
const queryBuffer = tagToEndpointQueryRecord[tag];
|
|
1702
|
+
const queryFileName = ParserUtils.writeQueryFile(DIST_DIR_QUERIES(isAdminEndpoint), apiGenName, queryBuffer, apiImports, serviceNameTitle, tagToSdkFunctionNamesRecord[tag], imports);
|
|
1703
|
+
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1704
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports, tagToSdkFunctionNamesRecord[tag]);
|
|
1462
1705
|
apiList.push(apiGenName);
|
|
1463
1706
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder));
|
|
1464
1707
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR(isAdminEndpoint), `${apiGenName}`), targetSrcFolder));
|
|
1708
|
+
queryFileName && indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR(isAdminEndpoint), "queries", `${queryFileName}`), targetSrcFolder));
|
|
1465
1709
|
}
|
|
1466
1710
|
mainApiList.push(...apiList);
|
|
1467
1711
|
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder));
|
|
@@ -1573,7 +1817,7 @@ const generateSdk = async () => {
|
|
|
1573
1817
|
}
|
|
1574
1818
|
});
|
|
1575
1819
|
}
|
|
1576
|
-
const indexImportsArray = Array.from(indexImportsSet);
|
|
1820
|
+
const indexImportsArray = Array.from(indexImportsSet).sort();
|
|
1577
1821
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1578
1822
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1579
1823
|
});
|