@accelbyte/codegen 0.0.0-dev-20250530101210 → 0.0.0-dev-20260326093546
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -3
- package/dist/accelbyte-codegen.d.mts +22 -0
- package/dist/accelbyte-codegen.d.ts +22 -0
- package/dist/accelbyte-codegen.js +255 -149
- package/dist/accelbyte-codegen.js.map +1 -1
- package/dist/accelbyte-codegen.mjs +252 -149
- package/dist/accelbyte-codegen.mjs.map +1 -1
- package/package.json +6 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/cli.ts
|
|
4
|
-
import
|
|
4
|
+
import path8 from "path";
|
|
5
5
|
import yargs from "yargs";
|
|
6
6
|
|
|
7
7
|
// src/CliParser.ts
|
|
@@ -65,18 +65,52 @@ var CliParser = class _CliParser {
|
|
|
65
65
|
|
|
66
66
|
// src/CodeGenerator.ts
|
|
67
67
|
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
68
|
-
import
|
|
69
|
-
import
|
|
68
|
+
import fs5 from "fs";
|
|
69
|
+
import path5 from "path";
|
|
70
|
+
|
|
71
|
+
// src/CodegenConfig.ts
|
|
72
|
+
import fs2 from "fs";
|
|
73
|
+
import path2 from "path";
|
|
74
|
+
import { pathToFileURL } from "url";
|
|
75
|
+
var CodegenConfig = class _CodegenConfig {
|
|
76
|
+
static config = {};
|
|
77
|
+
static async loadConfig(configDir) {
|
|
78
|
+
const configPath = path2.join(configDir, "abcodegen.config.ts");
|
|
79
|
+
if (!fs2.existsSync(configPath)) {
|
|
80
|
+
_CodegenConfig.config = {};
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const loaded = await import(pathToFileURL(configPath).href);
|
|
84
|
+
_CodegenConfig.config = loaded.default ?? loaded ?? {};
|
|
85
|
+
}
|
|
86
|
+
static shouldProduceIndexFiles() {
|
|
87
|
+
return _CodegenConfig.config.unstable_shouldProduceIndexFiles ?? true;
|
|
88
|
+
}
|
|
89
|
+
static getBasePath() {
|
|
90
|
+
return _CodegenConfig.config.basePath;
|
|
91
|
+
}
|
|
92
|
+
static getOverrideAsAny() {
|
|
93
|
+
return _CodegenConfig.config.unstable_overrideAsAny;
|
|
94
|
+
}
|
|
95
|
+
/** Reset to defaults — used for testing */
|
|
96
|
+
static reset() {
|
|
97
|
+
_CodegenConfig.config = {};
|
|
98
|
+
}
|
|
99
|
+
/** Set config directly — used for testing */
|
|
100
|
+
static setConfig(config) {
|
|
101
|
+
_CodegenConfig.config = config;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
70
104
|
|
|
71
105
|
// src/ParserUtils.ts
|
|
72
106
|
import { applyPatch } from "fast-json-patch";
|
|
73
|
-
import
|
|
107
|
+
import fs4 from "fs";
|
|
74
108
|
import _ from "lodash";
|
|
75
|
-
import
|
|
109
|
+
import path4 from "path";
|
|
76
110
|
|
|
77
111
|
// src/helpers/utils.ts
|
|
78
|
-
import
|
|
79
|
-
import
|
|
112
|
+
import fs3 from "fs";
|
|
113
|
+
import path3 from "path";
|
|
80
114
|
var capitalize = (string) => {
|
|
81
115
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
82
116
|
};
|
|
@@ -153,10 +187,10 @@ var getImportableVarMap = () => ({
|
|
|
153
187
|
zod: ["z"]
|
|
154
188
|
});
|
|
155
189
|
var makeNewImportVarMap = () => ({
|
|
156
|
-
axios: ["AxiosInstance", "AxiosRequestConfig"]
|
|
157
|
-
"@accelbyte/sdk": ["SDKRequestConfig"]
|
|
190
|
+
axios: ["AxiosInstance", "AxiosRequestConfig"]
|
|
158
191
|
});
|
|
159
|
-
var
|
|
192
|
+
var CLASS_TYPE_ONLY_VARS = /* @__PURE__ */ new Set(["AxiosInstance", "AxiosRequestConfig", "AxiosResponse", "SdkSetConfigParam", "Response"]);
|
|
193
|
+
var generateImports = (body, importStatements, makeNewImportVarMap3, getImportableVarMap3, typeOnlyVars = /* @__PURE__ */ new Set()) => {
|
|
160
194
|
const usedImportVarMap = makeNewImportVarMap3;
|
|
161
195
|
const importableVarMap = getImportableVarMap3;
|
|
162
196
|
for (const [moduleSource, importableVars] of Object.entries(importableVarMap)) {
|
|
@@ -167,22 +201,45 @@ var generateImports = (body, importStatements, makeNewImportVarMap3, getImportab
|
|
|
167
201
|
}
|
|
168
202
|
}
|
|
169
203
|
}
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
204
|
+
const importLines = [];
|
|
205
|
+
for (const moduleSource of Object.keys(usedImportVarMap).sort()) {
|
|
206
|
+
const allVars = usedImportVarMap[moduleSource];
|
|
207
|
+
const valueVars = allVars.filter((v) => !typeOnlyVars.has(v)).sort();
|
|
208
|
+
const typeVars = allVars.filter((v) => typeOnlyVars.has(v)).sort();
|
|
209
|
+
if (valueVars.length > 0) {
|
|
210
|
+
importLines.push(`import { ${valueVars.join(", ")} } from '${moduleSource}'`);
|
|
211
|
+
}
|
|
212
|
+
if (typeVars.length > 0) {
|
|
213
|
+
importLines.push(`import type { ${typeVars.join(", ")} } from '${moduleSource}'`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const generatedImports = importLines.join("\n");
|
|
173
217
|
return `${generatedImports}
|
|
174
218
|
${importStatements.sort().join("\n")}`;
|
|
175
219
|
};
|
|
176
220
|
var templateClass = (className, body, importStatements) => {
|
|
221
|
+
const attributes = {
|
|
222
|
+
definitions: ["private axiosInstance: AxiosInstance", "private namespace: string", "private useSchemaValidation: boolean"],
|
|
223
|
+
assignments: ["this.axiosInstance = axiosInstance", "this.namespace = namespace", "this.useSchemaValidation = useSchemaValidation"]
|
|
224
|
+
};
|
|
225
|
+
let namespaceConstructor = "namespace";
|
|
226
|
+
if (!body.includes(".replace('{namespace}', this.namespace)")) {
|
|
227
|
+
namespaceConstructor = "_namespace";
|
|
228
|
+
attributes.definitions.splice(1, 1);
|
|
229
|
+
attributes.assignments.splice(1, 1);
|
|
230
|
+
}
|
|
177
231
|
return `/**
|
|
178
232
|
* AUTO GENERATED
|
|
179
233
|
*/
|
|
180
|
-
${generateImports(body, importStatements, makeNewImportVarMap(), getImportableVarMap())}
|
|
234
|
+
${generateImports(body, importStatements, makeNewImportVarMap(), getImportableVarMap(), CLASS_TYPE_ONLY_VARS)}
|
|
181
235
|
|
|
182
236
|
export class ${className} {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
constructor(
|
|
237
|
+
${attributes.definitions.join("\n")}
|
|
238
|
+
|
|
239
|
+
constructor(axiosInstance: AxiosInstance, ${namespaceConstructor}: string, useSchemaValidation = true) {
|
|
240
|
+
${attributes.assignments.join("\n")}
|
|
241
|
+
}
|
|
242
|
+
|
|
186
243
|
${body}
|
|
187
244
|
}
|
|
188
245
|
`;
|
|
@@ -197,6 +254,15 @@ var makeNewImportVarMap2 = () => ({
|
|
|
197
254
|
"@accelbyte/sdk": ["AccelByteSDK", "SdkSetConfigParam", "ApiUtils", "Network"],
|
|
198
255
|
axios: ["AxiosRequestConfig", "AxiosResponse"]
|
|
199
256
|
});
|
|
257
|
+
var API_TYPE_ONLY_VARS = /* @__PURE__ */ new Set([
|
|
258
|
+
"AxiosRequestConfig",
|
|
259
|
+
"AxiosResponse",
|
|
260
|
+
"AxiosDefaults",
|
|
261
|
+
"HeadersDefaults",
|
|
262
|
+
"AccelByteSDK",
|
|
263
|
+
"SdkSetConfigParam",
|
|
264
|
+
"Response"
|
|
265
|
+
]);
|
|
200
266
|
var templateApiClass = (className, body, importStatements, returnMethods) => {
|
|
201
267
|
const returnsMethodsWithDescription = Object.keys(returnMethods).reduce((acc, key) => {
|
|
202
268
|
acc += `
|
|
@@ -208,43 +274,45 @@ ${key},`;
|
|
|
208
274
|
return `/**
|
|
209
275
|
* AUTO GENERATED
|
|
210
276
|
*/
|
|
211
|
-
|
|
212
|
-
// @ts-ignore -> ts-expect-error TS6133
|
|
213
|
-
${generateImports(body, importStatements, makeNewImportVarMap2(), getImportableVarMap2())}
|
|
277
|
+
${generateImports(body, importStatements, makeNewImportVarMap2(), getImportableVarMap2(), API_TYPE_ONLY_VARS)}
|
|
214
278
|
${`import { ${$className} } from './endpoints/${$className}.js'
|
|
215
279
|
`}
|
|
216
280
|
|
|
217
281
|
export function ${className}(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
|
|
218
282
|
const sdkAssembly = sdk.assembly()
|
|
219
|
-
|
|
283
|
+
|
|
220
284
|
const namespace = args?.coreConfig?.namespace ?? sdkAssembly.coreConfig.namespace
|
|
221
285
|
const useSchemaValidation = args?.coreConfig?.useSchemaValidation ?? sdkAssembly.coreConfig.useSchemaValidation
|
|
222
|
-
|
|
286
|
+
|
|
223
287
|
let axiosInstance = sdkAssembly.axiosInstance
|
|
224
288
|
const requestConfigOverrides = args?.axiosConfig?.request
|
|
225
289
|
const baseURLOverride = args?.coreConfig?.baseURL
|
|
226
|
-
const interceptorsOverride = args?.axiosConfig?.interceptors
|
|
290
|
+
const interceptorsOverride = args?.axiosConfig?.interceptors
|
|
227
291
|
|
|
228
|
-
if (requestConfigOverrides || baseURLOverride || interceptorsOverride
|
|
292
|
+
if (requestConfigOverrides || baseURLOverride || interceptorsOverride) {
|
|
229
293
|
const requestConfig = ApiUtils.mergeAxiosConfigs(sdkAssembly.axiosInstance.defaults as AxiosRequestConfig, {
|
|
230
294
|
...(baseURLOverride ? { baseURL: baseURLOverride } : {}),
|
|
231
295
|
...requestConfigOverrides
|
|
232
296
|
})
|
|
233
297
|
axiosInstance = Network.create(requestConfig)
|
|
234
298
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
299
|
+
if (interceptorsOverride) {
|
|
300
|
+
for (const interceptor of interceptorsOverride) {
|
|
301
|
+
if (interceptor.type === 'request') {
|
|
302
|
+
axiosInstance.interceptors.request.use(interceptor.onRequest, interceptor.onError)
|
|
303
|
+
}
|
|
239
304
|
|
|
240
|
-
|
|
241
|
-
|
|
305
|
+
if (interceptor.type === 'response') {
|
|
306
|
+
axiosInstance.interceptors.response.use(interceptor.onSuccess, interceptor.onError)
|
|
307
|
+
}
|
|
242
308
|
}
|
|
309
|
+
} else {
|
|
310
|
+
axiosInstance.interceptors = sdkAssembly.axiosInstance.interceptors
|
|
243
311
|
}
|
|
244
312
|
}
|
|
245
313
|
|
|
246
314
|
${body}
|
|
247
|
-
|
|
315
|
+
|
|
248
316
|
return {
|
|
249
317
|
${returnsMethodsWithDescription}
|
|
250
318
|
}
|
|
@@ -255,7 +323,7 @@ export function ${className}(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
|
|
|
255
323
|
// src/ParserQueryUtils.ts
|
|
256
324
|
var ParserQueryUtils = class {
|
|
257
325
|
/**
|
|
258
|
-
* convert csv 'aa,bb' into "aa='aa', bb='bb'"
|
|
326
|
+
* convert csv 'aa,bb' into "aa = 'Sdk.Class.aa', bb = 'Sdk.Class.bb'"
|
|
259
327
|
*/
|
|
260
328
|
static createQueryKeys(classNameWithoutApi, csvMethodNames, sdkName) {
|
|
261
329
|
const keys = csvMethodNames.split(",");
|
|
@@ -266,7 +334,7 @@ var ParserQueryUtils = class {
|
|
|
266
334
|
const cleanedKey = trimmedKey.replace(/^(get|update|create|patch|delete|post|fetch)[_]?/, "");
|
|
267
335
|
if (!processedKeys.has(cleanedKey)) {
|
|
268
336
|
processedKeys.add(cleanedKey);
|
|
269
|
-
acc += `${cleanedKey}
|
|
337
|
+
acc += `${cleanedKey}: '${capitalize(sdkName)}.${classNameWithoutApi}.${cleanedKey}'`;
|
|
270
338
|
if (index < keys.length - 1) {
|
|
271
339
|
acc += ",\n";
|
|
272
340
|
}
|
|
@@ -280,42 +348,51 @@ var ParserQueryUtils = class {
|
|
|
280
348
|
|
|
281
349
|
// src/templates/template-query.ts
|
|
282
350
|
var generateImports2 = (body, className) => {
|
|
283
|
-
const
|
|
284
|
-
import {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
import { ${
|
|
288
|
-
|
|
289
|
-
|
|
351
|
+
const lines = [];
|
|
352
|
+
lines.push("import type { AccelByteSDK, ApiError, SdkSetConfigParam } from '@accelbyte/sdk'");
|
|
353
|
+
const axiosTypes = ["AxiosError", "AxiosResponse"].filter((t) => body.includes(t));
|
|
354
|
+
if (axiosTypes.length) {
|
|
355
|
+
lines.push(`import type { ${axiosTypes.join(", ")} } from 'axios'`);
|
|
356
|
+
}
|
|
357
|
+
const rqValues = ["useMutation", "useQuery"].filter((t) => body.includes(t));
|
|
358
|
+
if (rqValues.length) {
|
|
359
|
+
lines.push(`import { ${rqValues.join(", ")} } from '@tanstack/react-query'`);
|
|
360
|
+
}
|
|
361
|
+
const rqTypes = ["UseMutationOptions", "UseMutationResult", "UseQueryOptions", "UseQueryResult"].filter((t) => body.includes(t));
|
|
362
|
+
if (rqTypes.length) {
|
|
363
|
+
lines.push(`import type { ${rqTypes.join(", ")} } from '@tanstack/react-query'`);
|
|
364
|
+
}
|
|
365
|
+
lines.push(`import { ${className} } from "../${className}.js"`);
|
|
366
|
+
return lines.join("\n") + "\n";
|
|
290
367
|
};
|
|
291
368
|
var templateQuery = (className, body, importStatements, serviceNameTitle, returnMethods, paramImports, sdkName) => {
|
|
292
369
|
const classNameWithoutApi = className.replace("Api", "");
|
|
293
370
|
const queryKeys = ParserQueryUtils.createQueryKeys(classNameWithoutApi, returnMethods, sdkName);
|
|
294
371
|
const generatedImports = generateImports2(body, className);
|
|
372
|
+
const queryKeyBlock = `export const Key_${classNameWithoutApi} = {
|
|
373
|
+
${queryKeys}
|
|
374
|
+
} as const`;
|
|
295
375
|
return `/**
|
|
296
376
|
* AUTO GENERATED
|
|
297
377
|
*/
|
|
298
|
-
/* eslint-disable camelcase */
|
|
299
378
|
${generatedImports}
|
|
300
379
|
${filterUsedImports(paramImports, body)}
|
|
301
380
|
|
|
302
|
-
|
|
303
|
-
${queryKeys}
|
|
304
|
-
}
|
|
381
|
+
${queryKeyBlock}
|
|
305
382
|
|
|
306
383
|
${body}
|
|
307
384
|
`;
|
|
308
385
|
};
|
|
309
386
|
function filterUsedImports(importArr, body) {
|
|
310
|
-
return importArr.filter((
|
|
311
|
-
const start =
|
|
312
|
-
const end =
|
|
387
|
+
return importArr.filter((path9) => {
|
|
388
|
+
const start = path9.indexOf("{") + 1;
|
|
389
|
+
const end = path9.indexOf("}");
|
|
313
390
|
if (start > 0 && end > start) {
|
|
314
|
-
const importName =
|
|
391
|
+
const importName = path9.slice(start, end).trim();
|
|
315
392
|
return body.includes(importName);
|
|
316
393
|
}
|
|
317
394
|
return false;
|
|
318
|
-
}).map((
|
|
395
|
+
}).map((path9) => path9).join("\n") + "\n";
|
|
319
396
|
}
|
|
320
397
|
|
|
321
398
|
// src/ParserUtils.ts
|
|
@@ -332,8 +409,8 @@ var REMOVED_KEYWORDS = [
|
|
|
332
409
|
"/{namespace}/"
|
|
333
410
|
];
|
|
334
411
|
var ParserUtils = class _ParserUtils {
|
|
335
|
-
static getVersionSuffixFromPath(
|
|
336
|
-
const version2_3_4_etc =
|
|
412
|
+
static getVersionSuffixFromPath(path9) {
|
|
413
|
+
const version2_3_4_etc = path9.match(/\/v([2-9]|[1-9]\d+)\/+/);
|
|
337
414
|
const methodSuffix = version2_3_4_etc ? `_v${version2_3_4_etc[1]}` : "";
|
|
338
415
|
return methodSuffix;
|
|
339
416
|
}
|
|
@@ -528,14 +605,14 @@ var ParserUtils = class _ParserUtils {
|
|
|
528
605
|
* to this
|
|
529
606
|
* `createGenerateByRequestIdByUserId`
|
|
530
607
|
*/
|
|
531
|
-
static generateNaturalLangMethod = ({ servicePrefix, path:
|
|
532
|
-
let path_ =
|
|
608
|
+
static generateNaturalLangMethod = ({ servicePrefix, path: path9, httpMethod, isForm, existingMethods, permissionType }) => {
|
|
609
|
+
let path_ = path9;
|
|
533
610
|
path_ = path_.replace(`/${servicePrefix}/`, "/");
|
|
534
611
|
REMOVED_KEYWORDS.forEach((prefix) => {
|
|
535
612
|
path_ = path_.replace(prefix, "/");
|
|
536
613
|
});
|
|
537
614
|
path_ = path_.substring(1);
|
|
538
|
-
const isPlural = httpMethod === "get" && !(
|
|
615
|
+
const isPlural = httpMethod === "get" && !(path9.slice(-1) === "}");
|
|
539
616
|
if (!isPlural) {
|
|
540
617
|
path_ = _ParserUtils.replaceAll(path_, "ies/", "y/");
|
|
541
618
|
path_ = _ParserUtils.replaceAll(path_, "s/", "/");
|
|
@@ -578,9 +655,9 @@ var ParserUtils = class _ParserUtils {
|
|
|
578
655
|
const genPath = _.upperFirst(lastWords) + "/" + listBeforeLastWords.join("/") + listByParams.reverse().join("/");
|
|
579
656
|
let generatedMethod = _.camelCase(mappedMethod(httpMethod, isForm, permissionType) + genPath);
|
|
580
657
|
generatedMethod = _ParserUtils.replaceAll(generatedMethod, "Byword", "_By");
|
|
581
|
-
const testedGeneratedMethod = generatedMethod + _ParserUtils.getVersionSuffixFromPath(
|
|
582
|
-
generatedMethod = resolveConflicts({ path:
|
|
583
|
-
generatedMethod = generatedMethod + _ParserUtils.getVersionSuffixFromPath(
|
|
658
|
+
const testedGeneratedMethod = generatedMethod + _ParserUtils.getVersionSuffixFromPath(path9);
|
|
659
|
+
generatedMethod = resolveConflicts({ path: path9, generatedMethod, testedGeneratedMethod, existingMethods });
|
|
660
|
+
generatedMethod = generatedMethod + _ParserUtils.getVersionSuffixFromPath(path9);
|
|
584
661
|
return generatedMethod;
|
|
585
662
|
};
|
|
586
663
|
static filterBodyParams(parameters) {
|
|
@@ -596,17 +673,17 @@ var ParserUtils = class _ParserUtils {
|
|
|
596
673
|
return parameters.filter((parameter) => parameter.in === "query");
|
|
597
674
|
}
|
|
598
675
|
static mkdirIfNotExist(dirToCreate) {
|
|
599
|
-
if (!
|
|
600
|
-
|
|
676
|
+
if (!fs4.existsSync(dirToCreate)) {
|
|
677
|
+
fs4.mkdirSync(dirToCreate, { recursive: true });
|
|
601
678
|
}
|
|
602
679
|
}
|
|
603
680
|
static writeClassFile(distDir, apiName, apiBuffer, imports) {
|
|
604
681
|
const fileContent = templateClass(apiName, apiBuffer, imports);
|
|
605
|
-
|
|
682
|
+
fs4.writeFileSync(`${distDir}/${apiName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
606
683
|
}
|
|
607
684
|
static writeAtomFile(distDir, apiName, fileContent) {
|
|
608
685
|
_ParserUtils.mkdirIfNotExist(distDir);
|
|
609
|
-
|
|
686
|
+
fs4.writeFileSync(`${distDir}/${apiName}.atom.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
610
687
|
}
|
|
611
688
|
static writeQueryFile(distDir, apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports, sdkName) {
|
|
612
689
|
if (apiBuffer.length < 1) {
|
|
@@ -615,20 +692,21 @@ var ParserUtils = class _ParserUtils {
|
|
|
615
692
|
const queryFileName = `${apiName.replace("Api", "")}.query`;
|
|
616
693
|
_ParserUtils.mkdirIfNotExist(distDir);
|
|
617
694
|
const fileContent = templateQuery(apiName, apiBuffer, imports, serviceNameTitle, returnMethods, paramImports, sdkName);
|
|
618
|
-
|
|
695
|
+
fs4.writeFileSync(`${distDir}/${queryFileName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
619
696
|
return queryFileName;
|
|
620
697
|
}
|
|
621
698
|
static writeXVersion(distDir, xversionJson, apiInfo) {
|
|
699
|
+
fs4.mkdirSync(distDir, { recursive: true });
|
|
622
700
|
if (xversionJson) {
|
|
623
701
|
console.log("x-version:", xversionJson);
|
|
624
|
-
|
|
702
|
+
fs4.writeFileSync(`${distDir}/version.json`, JSON.stringify(xversionJson, null, 2));
|
|
625
703
|
} else {
|
|
626
704
|
const customVersion = {
|
|
627
705
|
...apiInfo,
|
|
628
706
|
gitHash: apiInfo.version
|
|
629
707
|
};
|
|
630
708
|
console.error(`!!!! Missing x-version for ${distDir} ${customVersion}`);
|
|
631
|
-
|
|
709
|
+
fs4.writeFileSync(`${distDir}/version.json`, JSON.stringify(customVersion, null, 2));
|
|
632
710
|
}
|
|
633
711
|
}
|
|
634
712
|
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethodsDescription) {
|
|
@@ -637,28 +715,28 @@ var ParserUtils = class _ParserUtils {
|
|
|
637
715
|
newImports.push(el.replace("../../generated-definitions", "../generated-definitions"));
|
|
638
716
|
});
|
|
639
717
|
const fileContent = templateApiClass(apiName, apiBuffer, newImports, returnMethodsDescription);
|
|
640
|
-
|
|
718
|
+
fs4.writeFileSync(`${distDir}/${apiName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
641
719
|
}
|
|
642
720
|
static writeApiMainFile(distDir, serviceName, fileContent) {
|
|
643
|
-
|
|
721
|
+
fs4.writeFileSync(`${distDir}/${serviceName}.ts`, _ParserUtils.prependCopyrightHeader(fileContent));
|
|
644
722
|
}
|
|
645
723
|
static writeSnippetFile(distDir, name, docBuffer) {
|
|
646
724
|
let snippetFileName = _ParserUtils.replaceAll(name, " ", "-").toLowerCase();
|
|
647
725
|
snippetFileName = snippetFileName.replace("justice-", "");
|
|
648
726
|
snippetFileName = "snippet-" + snippetFileName + ".json";
|
|
649
|
-
|
|
727
|
+
fs4.writeFileSync(`${distDir}/${snippetFileName}`, docBuffer);
|
|
650
728
|
}
|
|
651
729
|
static writeDefinitionFile(distDir, name, buffer) {
|
|
652
730
|
_ParserUtils.mkdirIfNotExist(distDir);
|
|
653
|
-
|
|
731
|
+
fs4.writeFileSync(path4.join(distDir, `${name}.ts`), _ParserUtils.prependCopyrightHeader(buffer));
|
|
654
732
|
}
|
|
655
733
|
static writeAllImportsFile(distDir, buffer) {
|
|
656
734
|
_ParserUtils.mkdirIfNotExist(distDir);
|
|
657
|
-
|
|
735
|
+
fs4.writeFileSync(path4.join(distDir, "all-imports.ts"), _ParserUtils.prependCopyrightHeader(buffer));
|
|
658
736
|
}
|
|
659
737
|
static writeAllQueryImportsFile(distDir, buffer) {
|
|
660
738
|
_ParserUtils.mkdirIfNotExist(distDir);
|
|
661
|
-
|
|
739
|
+
fs4.writeFileSync(path4.join(distDir, "all-query-imports.ts"), _ParserUtils.prependCopyrightHeader(buffer));
|
|
662
740
|
}
|
|
663
741
|
static toCamelCase(str) {
|
|
664
742
|
return str.split("/").map(function(word, index) {
|
|
@@ -684,15 +762,15 @@ var ParserUtils = class _ParserUtils {
|
|
|
684
762
|
});
|
|
685
763
|
}
|
|
686
764
|
static applyPatchIfExists(swaggerFilePath, possibleSwaggerPatchFilePath, swaggerPatchedFilePath, swaggerPatchedDir) {
|
|
687
|
-
if (!
|
|
688
|
-
|
|
765
|
+
if (!fs4.existsSync(swaggerPatchedDir)) {
|
|
766
|
+
fs4.mkdirSync(swaggerPatchedDir, { recursive: true });
|
|
689
767
|
}
|
|
690
|
-
if (!
|
|
691
|
-
|
|
768
|
+
if (!fs4.existsSync(possibleSwaggerPatchFilePath)) {
|
|
769
|
+
fs4.copyFileSync(swaggerFilePath, swaggerPatchedFilePath);
|
|
692
770
|
return;
|
|
693
771
|
}
|
|
694
|
-
const swaggerContent = JSON.parse(
|
|
695
|
-
const swaggerPatchFileContent = JSON.parse(
|
|
772
|
+
const swaggerContent = JSON.parse(fs4.readFileSync(swaggerFilePath, "utf8"));
|
|
773
|
+
const swaggerPatchFileContent = JSON.parse(fs4.readFileSync(possibleSwaggerPatchFilePath, "utf8"));
|
|
696
774
|
for (const patchEntry of swaggerPatchFileContent) {
|
|
697
775
|
const segments = patchEntry.path.split("/").filter(Boolean);
|
|
698
776
|
let currentNode = swaggerContent;
|
|
@@ -720,7 +798,7 @@ var ParserUtils = class _ParserUtils {
|
|
|
720
798
|
}
|
|
721
799
|
}
|
|
722
800
|
const { newDocument } = applyPatch(swaggerContent, swaggerPatchFileContent);
|
|
723
|
-
|
|
801
|
+
fs4.writeFileSync(swaggerPatchedFilePath, JSON.stringify(newDocument, null, 2));
|
|
724
802
|
}
|
|
725
803
|
static getRelativePathToWebSdkSrcFolder(srcFolder, targetSrcFolder) {
|
|
726
804
|
const replaced = srcFolder.replace(/\\/g, "/");
|
|
@@ -735,8 +813,8 @@ var ParserUtils = class _ParserUtils {
|
|
|
735
813
|
*/
|
|
736
814
|
${content}`;
|
|
737
815
|
};
|
|
738
|
-
static sortPathParamsByPath = (pathParams,
|
|
739
|
-
const params =
|
|
816
|
+
static sortPathParamsByPath = (pathParams, path9) => {
|
|
817
|
+
const params = path9.match(/{\w*}/g) || [];
|
|
740
818
|
const cleanParams = params.map((param) => param.replace("{", "").replace("}", ""));
|
|
741
819
|
return pathParams.sort((a, b) => cleanParams.indexOf(a.name) - cleanParams.indexOf(b.name));
|
|
742
820
|
};
|
|
@@ -760,30 +838,30 @@ var mappedMethod = (httpMethod, isForm, permissionType) => {
|
|
|
760
838
|
return "delete";
|
|
761
839
|
}
|
|
762
840
|
};
|
|
763
|
-
var resolveConflicts = ({ path:
|
|
841
|
+
var resolveConflicts = ({ path: path9, generatedMethod, testedGeneratedMethod, existingMethods }) => {
|
|
764
842
|
let _testedGenMethod = testedGeneratedMethod;
|
|
765
843
|
try {
|
|
766
|
-
testConflict(
|
|
844
|
+
testConflict(path9, _testedGenMethod, existingMethods);
|
|
767
845
|
} catch (e) {
|
|
768
|
-
if (
|
|
846
|
+
if (path9.indexOf("/namespaces/") >= 0) {
|
|
769
847
|
generatedMethod += "_ByNS";
|
|
770
848
|
_testedGenMethod += "_ByNS";
|
|
771
849
|
}
|
|
772
850
|
}
|
|
773
851
|
try {
|
|
774
|
-
testConflict(
|
|
852
|
+
testConflict(path9, _testedGenMethod, existingMethods);
|
|
775
853
|
} catch (e) {
|
|
776
|
-
if (
|
|
854
|
+
if (path9.indexOf("/admin/") >= 0) {
|
|
777
855
|
generatedMethod += "_admin";
|
|
778
856
|
_testedGenMethod += "_admin";
|
|
779
857
|
}
|
|
780
858
|
}
|
|
781
|
-
testConflict(
|
|
859
|
+
testConflict(path9, _testedGenMethod, existingMethods);
|
|
782
860
|
return generatedMethod;
|
|
783
861
|
};
|
|
784
|
-
var testConflict = (
|
|
862
|
+
var testConflict = (path9, generatedMethod, existingMethods) => {
|
|
785
863
|
if (existingMethods[generatedMethod]) {
|
|
786
|
-
const conflictingMethod = { path:
|
|
864
|
+
const conflictingMethod = { path: path9, generatedMethod };
|
|
787
865
|
throw Error(
|
|
788
866
|
`Duplicate method conflict in ${JSON.stringify(conflictingMethod)},
|
|
789
867
|
existingMethods: ${JSON.stringify(existingMethods, null, 2)}`
|
|
@@ -900,7 +978,7 @@ var OpenApiSpec = z2.object({
|
|
|
900
978
|
var templateApiMethod = ({
|
|
901
979
|
classMethod,
|
|
902
980
|
httpMethod,
|
|
903
|
-
path:
|
|
981
|
+
path: path9,
|
|
904
982
|
pathParams,
|
|
905
983
|
bodyParams,
|
|
906
984
|
responseClasses,
|
|
@@ -909,12 +987,12 @@ var templateApiMethod = ({
|
|
|
909
987
|
methodParamsNoTypes,
|
|
910
988
|
xSecurity
|
|
911
989
|
}) => {
|
|
912
|
-
let newPath = `'${
|
|
990
|
+
let newPath = `'${path9}'`;
|
|
913
991
|
let snippetMethod = "";
|
|
914
992
|
for (const pathParam of pathParams) {
|
|
915
993
|
const type = ParserUtils.parseType(pathParam);
|
|
916
994
|
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
917
|
-
if (
|
|
995
|
+
if (path9.match(`{${pathParam.name}}`)) {
|
|
918
996
|
if (type === "string") {
|
|
919
997
|
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
920
998
|
} else {
|
|
@@ -922,9 +1000,9 @@ var templateApiMethod = ({
|
|
|
922
1000
|
}
|
|
923
1001
|
}
|
|
924
1002
|
}
|
|
925
|
-
const snippetShellArgs = ["--location --request", `${httpMethod} '__DOMAIN__${
|
|
1003
|
+
const snippetShellArgs = ["--location --request", `${httpMethod} '__DOMAIN__${path9}'`, "--header 'accept: application/json'"];
|
|
926
1004
|
const snippetApiArgs = [];
|
|
927
|
-
if (xSecurity !== void 0 ||
|
|
1005
|
+
if (xSecurity !== void 0 || path9.includes("/admin")) {
|
|
928
1006
|
snippetShellArgs.push("--header 'Authorization: Bearer {access_token}'");
|
|
929
1007
|
snippetApiArgs.push("{ axiosConfig: { request: { headers: { Authorization: 'Bearer {access_token}' } } } }".trim());
|
|
930
1008
|
}
|
|
@@ -962,7 +1040,7 @@ var templateMethod = ({
|
|
|
962
1040
|
classMethod,
|
|
963
1041
|
description,
|
|
964
1042
|
httpMethod,
|
|
965
|
-
path:
|
|
1043
|
+
path: path9,
|
|
966
1044
|
pathParams,
|
|
967
1045
|
bodyParams,
|
|
968
1046
|
queryParams,
|
|
@@ -972,9 +1050,9 @@ var templateMethod = ({
|
|
|
972
1050
|
}) => {
|
|
973
1051
|
let methodParams = "";
|
|
974
1052
|
let methodParamsNoTypes = "";
|
|
975
|
-
let newPath = `'${
|
|
1053
|
+
let newPath = `'${path9}'`;
|
|
976
1054
|
let importStatements = [];
|
|
977
|
-
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams,
|
|
1055
|
+
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams, path9);
|
|
978
1056
|
for (const pathParam of sortedPathParams) {
|
|
979
1057
|
const type = ParserUtils.parseType(pathParam);
|
|
980
1058
|
if (pathParam.name !== "namespace") {
|
|
@@ -982,7 +1060,7 @@ var templateMethod = ({
|
|
|
982
1060
|
methodParamsNoTypes += pathParam.name + ", ";
|
|
983
1061
|
}
|
|
984
1062
|
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
985
|
-
if (
|
|
1063
|
+
if (path9.match(`{${pathParam.name}}`)) {
|
|
986
1064
|
if (type === "string") {
|
|
987
1065
|
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
988
1066
|
} else {
|
|
@@ -1047,7 +1125,7 @@ var POST_FETCH_INCLUDES_PATH = ["/table-query/"];
|
|
|
1047
1125
|
var templateQueryMethod = ({
|
|
1048
1126
|
classMethod,
|
|
1049
1127
|
httpMethod,
|
|
1050
|
-
path:
|
|
1128
|
+
path: path9,
|
|
1051
1129
|
pathParams,
|
|
1052
1130
|
responseClasses,
|
|
1053
1131
|
methodParams,
|
|
@@ -1055,14 +1133,14 @@ var templateQueryMethod = ({
|
|
|
1055
1133
|
description,
|
|
1056
1134
|
deprecated
|
|
1057
1135
|
}) => {
|
|
1058
|
-
const isPostFetch = httpMethod === "post" && (POST_FETCH_INCLUDES_PATH.some((p) =>
|
|
1136
|
+
const isPostFetch = httpMethod === "post" && (POST_FETCH_INCLUDES_PATH.some((p) => path9.includes(p)) || path9.endsWith("/list"));
|
|
1059
1137
|
const isFetch = classMethod.startsWith("fetch");
|
|
1060
1138
|
const isGet = httpMethod === "get" || isPostFetch || isFetch;
|
|
1061
1139
|
const queryMethod = isGet ? "useQuery" : "useMutation";
|
|
1062
1140
|
let mParams = "";
|
|
1063
1141
|
let mParamsNoTypes = "";
|
|
1064
|
-
let newPath = `'${
|
|
1065
|
-
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams,
|
|
1142
|
+
let newPath = `'${path9}'`;
|
|
1143
|
+
const sortedPathParams = ParserUtils.sortPathParamsByPath(pathParams, path9);
|
|
1066
1144
|
for (const pathParam of sortedPathParams) {
|
|
1067
1145
|
const type = ParserUtils.parseType(pathParam);
|
|
1068
1146
|
if (pathParam.name !== "namespace") {
|
|
@@ -1070,7 +1148,7 @@ var templateQueryMethod = ({
|
|
|
1070
1148
|
mParamsNoTypes += pathParam.name + ", ";
|
|
1071
1149
|
}
|
|
1072
1150
|
const pName = pathParam.name === "namespace" ? "this.namespace" : pathParam.name;
|
|
1073
|
-
if (
|
|
1151
|
+
if (path9.match(`{${pathParam.name}}`)) {
|
|
1074
1152
|
if (type === "string") {
|
|
1075
1153
|
newPath = `${newPath}.replace('{${pathParam.name}}', ${pName})`;
|
|
1076
1154
|
} else {
|
|
@@ -1105,7 +1183,7 @@ export const ${_methodName} = (
|
|
|
1105
1183
|
const response =
|
|
1106
1184
|
(await ${apiGenName}(sdk, { coreConfig: input.coreConfig, axiosConfig: input.axiosConfig }).
|
|
1107
1185
|
${classMethod}(${_methodParamsImpl}))
|
|
1108
|
-
callback
|
|
1186
|
+
callback?.(response)
|
|
1109
1187
|
return response.data
|
|
1110
1188
|
}
|
|
1111
1189
|
|
|
@@ -1130,7 +1208,7 @@ export const ${_methodName} = (
|
|
|
1130
1208
|
const response =
|
|
1131
1209
|
(await ${apiGenName}(sdk, { coreConfig: input.coreConfig, axiosConfig: input.axiosConfig }).
|
|
1132
1210
|
${classMethod}(${_methodParamsImpl}))
|
|
1133
|
-
callback
|
|
1211
|
+
callback?.(response.data)
|
|
1134
1212
|
return response.data
|
|
1135
1213
|
}
|
|
1136
1214
|
|
|
@@ -1309,11 +1387,11 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1309
1387
|
admin: {},
|
|
1310
1388
|
public: {}
|
|
1311
1389
|
};
|
|
1312
|
-
for (const [
|
|
1313
|
-
if (
|
|
1390
|
+
for (const [path9, operation] of sortedPathsByLength) {
|
|
1391
|
+
if (path9.indexOf("/healthz") >= 0) {
|
|
1314
1392
|
continue;
|
|
1315
1393
|
}
|
|
1316
|
-
const isAdminEndpoint =
|
|
1394
|
+
const isAdminEndpoint = path9.indexOf("/admin/") > -1;
|
|
1317
1395
|
const picked = isAdminEndpoint ? result.admin : result.public;
|
|
1318
1396
|
const {
|
|
1319
1397
|
arrayDefinitions,
|
|
@@ -1331,25 +1409,27 @@ var SwaggerReaderHelpers = class _SwaggerReaderHelpers {
|
|
|
1331
1409
|
const httpMethods = Object.keys(operation);
|
|
1332
1410
|
for (const httpMethod of httpMethods) {
|
|
1333
1411
|
const endpoint = await Endpoint.parseAsync(operation[httpMethod]).catch((error) => {
|
|
1334
|
-
console.error(JSON.stringify({ path:
|
|
1412
|
+
console.error(JSON.stringify({ path: path9, httpMethod }, null, 2));
|
|
1335
1413
|
throw error;
|
|
1336
1414
|
});
|
|
1337
1415
|
if (!endpoint.tags) continue;
|
|
1338
1416
|
const [tag] = endpoint.tags;
|
|
1339
|
-
const
|
|
1417
|
+
const configBasePath = CodegenConfig.getBasePath();
|
|
1418
|
+
const effectiveBasePath = configBasePath !== void 0 ? configBasePath : api.basePath ?? "";
|
|
1419
|
+
const pathWithBase = `${effectiveBasePath}${path9}`;
|
|
1340
1420
|
const permissionType = getPermissionType(getPermission(endpoint));
|
|
1341
1421
|
tagToClassMethodsMapByType[tag] = tagToClassMethodsMapByType[tag] ? tagToClassMethodsMapByType[tag] : {};
|
|
1342
1422
|
const isForm = endpoint.consumes && endpoint.consumes[0] === "application/x-www-form-urlencoded";
|
|
1343
1423
|
const classMethod = ParserUtils.generateNaturalLangMethod({
|
|
1344
1424
|
servicePrefix,
|
|
1345
|
-
path:
|
|
1425
|
+
path: path9,
|
|
1346
1426
|
httpMethod,
|
|
1347
1427
|
isForm,
|
|
1348
1428
|
existingMethods: tagToClassMethodsMapByType[tag],
|
|
1349
1429
|
permissionType
|
|
1350
1430
|
});
|
|
1351
|
-
tagToClassMethodsMapByType[tag][classMethod] = `${
|
|
1352
|
-
generatedMethods[`${
|
|
1431
|
+
tagToClassMethodsMapByType[tag][classMethod] = `${path9} ${httpMethod}`;
|
|
1432
|
+
generatedMethods[`${path9} ${httpMethod}`] = `${classMethod}`;
|
|
1353
1433
|
if (!snippetMap[pathWithBase]) {
|
|
1354
1434
|
snippetMap[pathWithBase] = {};
|
|
1355
1435
|
}
|
|
@@ -1504,6 +1584,20 @@ var TemplateZod = class {
|
|
|
1504
1584
|
// --
|
|
1505
1585
|
render = (fileName, definition, duplicates) => {
|
|
1506
1586
|
this.duplicates = duplicates;
|
|
1587
|
+
const overrideAsAny = CodegenConfig.getOverrideAsAny();
|
|
1588
|
+
const override = overrideAsAny?.[fileName];
|
|
1589
|
+
if (override !== void 0) {
|
|
1590
|
+
const shouldOverride = typeof override === "function" ? override(definition) : override;
|
|
1591
|
+
if (shouldOverride) {
|
|
1592
|
+
const template2 = `import { z } from 'zod'
|
|
1593
|
+
|
|
1594
|
+
export const ${fileName} = z.any()
|
|
1595
|
+
|
|
1596
|
+
export interface ${fileName} extends z.TypeOf<typeof ${fileName}> {}
|
|
1597
|
+
`;
|
|
1598
|
+
return { buffer: template2, duplicateFound: false };
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1507
1601
|
const content = this.parseToZodSchema(definition, definition.required || []);
|
|
1508
1602
|
const containsRecursiveType = this.importClasses.has(fileName);
|
|
1509
1603
|
if (containsRecursiveType) {
|
|
@@ -1806,9 +1900,9 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1806
1900
|
return;
|
|
1807
1901
|
}
|
|
1808
1902
|
const DIST_DIR = (isAdmin) => `${_CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1809
|
-
const DIST_DIR_ENDPOINTS = (isAdmin) =>
|
|
1810
|
-
const DIST_DIR_QUERIES = (isAdmin) =>
|
|
1811
|
-
const DIST_DEFINITION_DIR =
|
|
1903
|
+
const DIST_DIR_ENDPOINTS = (isAdmin) => path5.join(DIST_DIR(isAdmin), "endpoints");
|
|
1904
|
+
const DIST_DIR_QUERIES = (isAdmin) => path5.join(DIST_DIR(isAdmin), "queries");
|
|
1905
|
+
const DIST_DEFINITION_DIR = path5.join(_CodeGenerator.srcFolder(), "generated-definitions");
|
|
1812
1906
|
const targetSrcFolder = `${_CodeGenerator.srcFolder()}/`;
|
|
1813
1907
|
_CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS, DIST_DIR_QUERIES);
|
|
1814
1908
|
const mainApiList = [];
|
|
@@ -1850,22 +1944,24 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1850
1944
|
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, imports, tagToSdkFunctionDescription[tag]);
|
|
1851
1945
|
apiList.push(apiGenName);
|
|
1852
1946
|
indexImportsSet.add(
|
|
1853
|
-
ParserUtils.getRelativePathToWebSdkSrcFolder(
|
|
1947
|
+
ParserUtils.getRelativePathToWebSdkSrcFolder(path5.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder)
|
|
1854
1948
|
);
|
|
1855
1949
|
indexImportsSet.add(
|
|
1856
|
-
ParserUtils.getRelativePathToWebSdkSrcFolder(
|
|
1950
|
+
ParserUtils.getRelativePathToWebSdkSrcFolder(path5.join(DIST_DIR(isAdminEndpoint), `${apiGenName}`), targetSrcFolder)
|
|
1857
1951
|
);
|
|
1858
1952
|
queryFileName && queryImportsSet.add(
|
|
1859
1953
|
ParserUtils.getRelativePathToWebSdkSrcFolder(
|
|
1860
|
-
|
|
1954
|
+
path5.join(DIST_DIR(isAdminEndpoint), "queries", `${queryFileName}`),
|
|
1861
1955
|
targetSrcFolder
|
|
1862
1956
|
)
|
|
1863
1957
|
);
|
|
1864
1958
|
}
|
|
1865
1959
|
mainApiList.push(...apiList);
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1960
|
+
if (CodegenConfig.shouldProduceIndexFiles()) {
|
|
1961
|
+
indexImportsSet.add(
|
|
1962
|
+
ParserUtils.getRelativePathToWebSdkSrcFolder(path5.join(_CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder)
|
|
1963
|
+
);
|
|
1964
|
+
}
|
|
1869
1965
|
};
|
|
1870
1966
|
const writeDefinitions = (api2) => {
|
|
1871
1967
|
const duplicates = /* @__PURE__ */ new Map();
|
|
@@ -1873,7 +1969,7 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1873
1969
|
for (const ref in definitions) {
|
|
1874
1970
|
const definition = definitions[ref];
|
|
1875
1971
|
const fileName = ParserUtils.parseRefType(ref);
|
|
1876
|
-
const fileExist =
|
|
1972
|
+
const fileExist = fs5.existsSync(path5.join(DIST_DEFINITION_DIR, `${fileName}.ts`));
|
|
1877
1973
|
if (fileExist) {
|
|
1878
1974
|
const duplicateName = ParserUtils.toCamelCaseWord(ref).replace(".", "").replace(".", "");
|
|
1879
1975
|
duplicates.set(ref, duplicateName);
|
|
@@ -1881,13 +1977,13 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1881
1977
|
const { buffer } = new TemplateZod().render(fileName, definition, /* @__PURE__ */ new Map());
|
|
1882
1978
|
generatedDefinitions.push(fileName);
|
|
1883
1979
|
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, fileName, buffer);
|
|
1884
|
-
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(
|
|
1980
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path5.join(DIST_DEFINITION_DIR, fileName), targetSrcFolder));
|
|
1885
1981
|
}
|
|
1886
1982
|
for (const arrayClass of arrayDefinitions) {
|
|
1887
1983
|
const buffer = new TemplateZodArray().render(arrayClass);
|
|
1888
1984
|
generatedDefinitions.push(arrayClass);
|
|
1889
1985
|
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
|
|
1890
|
-
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(
|
|
1986
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path5.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
|
|
1891
1987
|
}
|
|
1892
1988
|
};
|
|
1893
1989
|
writeApiEndpointFiles(isAdmin);
|
|
@@ -1895,9 +1991,11 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1895
1991
|
};
|
|
1896
1992
|
generatePublicOrAdmin(true);
|
|
1897
1993
|
generatePublicOrAdmin(false);
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1994
|
+
if (CodegenConfig.shouldProduceIndexFiles()) {
|
|
1995
|
+
const isGenerateWebSocket = CliParser.isGenerateWebSocket();
|
|
1996
|
+
const apiIndexBuff = templateApiIndex(serviceNameTitle, mainApiList, isGenerateWebSocket);
|
|
1997
|
+
ParserUtils.writeApiMainFile(_CodeGenerator.srcFolder(), serviceNameTitle, apiIndexBuff);
|
|
1998
|
+
}
|
|
1901
1999
|
console.log("\nCOMPLETED\n----------\n\n");
|
|
1902
2000
|
return { indexImports: indexImportsSet, queryImports: queryImportsSet };
|
|
1903
2001
|
};
|
|
@@ -1905,29 +2003,29 @@ var CodeGenerator = class _CodeGenerator {
|
|
|
1905
2003
|
};
|
|
1906
2004
|
|
|
1907
2005
|
// src/SwaggerDownloader.ts
|
|
2006
|
+
import * as fs6 from "fs";
|
|
1908
2007
|
import * as https from "https";
|
|
1909
|
-
import * as
|
|
1910
|
-
import * as path5 from "path";
|
|
2008
|
+
import * as path6 from "path";
|
|
1911
2009
|
var SwaggerDownloader = class _SwaggerDownloader {
|
|
1912
2010
|
static getDestFile = (targetFileName) => {
|
|
1913
2011
|
const destPath = CliParser.getResolvedSwaggersOutputPath();
|
|
1914
|
-
const destFile =
|
|
1915
|
-
if (
|
|
1916
|
-
if (!
|
|
1917
|
-
|
|
2012
|
+
const destFile = path6.join(destPath, targetFileName);
|
|
2013
|
+
if (fs6.existsSync(destFile)) return destFile;
|
|
2014
|
+
if (!fs6.existsSync(destPath)) fs6.mkdirSync(destPath);
|
|
2015
|
+
fs6.writeFileSync(destFile, "");
|
|
1918
2016
|
return destFile;
|
|
1919
2017
|
};
|
|
1920
2018
|
// session-api.json contains illegal URL encoded character that breaks the codegen
|
|
1921
2019
|
// e.g. "$ref": "#/definitions/map%5Bstring%5Dinterface%20%7B%7D"
|
|
1922
2020
|
static postSanitizeDownloadedFile = (filePath) => {
|
|
1923
2021
|
const searchStr = ["%5B", "%5D", "%20", "%7B", "%7D"];
|
|
1924
|
-
|
|
2022
|
+
fs6.readFile(filePath, "utf8", (err, data) => {
|
|
1925
2023
|
if (err) throw err;
|
|
1926
2024
|
let result = data;
|
|
1927
2025
|
searchStr.forEach((s) => {
|
|
1928
2026
|
result = result.replace(new RegExp(s, "g"), " ");
|
|
1929
2027
|
});
|
|
1930
|
-
|
|
2028
|
+
fs6.writeFile(filePath, result, "utf8", (err2) => {
|
|
1931
2029
|
if (err2) throw err2;
|
|
1932
2030
|
console.log("File updated successfully.");
|
|
1933
2031
|
});
|
|
@@ -1945,7 +2043,7 @@ var SwaggerDownloader = class _SwaggerDownloader {
|
|
|
1945
2043
|
if (response.statusCode !== 200) {
|
|
1946
2044
|
console.log(`SwaggerDownload error with status code: ${response.statusCode}`);
|
|
1947
2045
|
} else {
|
|
1948
|
-
|
|
2046
|
+
fs6.writeFileSync(destFile, JSON.stringify(JSON.parse(data), null, 2), "utf-8");
|
|
1949
2047
|
_SwaggerDownloader.postSanitizeDownloadedFile(destFile);
|
|
1950
2048
|
console.log(`SwaggerDownload ${url} completed with status code: ${response.statusCode}`);
|
|
1951
2049
|
}
|
|
@@ -1968,8 +2066,8 @@ var SwaggerDownloader = class _SwaggerDownloader {
|
|
|
1968
2066
|
};
|
|
1969
2067
|
|
|
1970
2068
|
// src/WebsocketGenerator.ts
|
|
1971
|
-
import
|
|
1972
|
-
import
|
|
2069
|
+
import fs7 from "fs";
|
|
2070
|
+
import path7 from "path";
|
|
1973
2071
|
|
|
1974
2072
|
// src/templates/template-ws-class.ts
|
|
1975
2073
|
var definitionToFunctionName = (type) => {
|
|
@@ -1984,7 +2082,7 @@ var renderSendFunction = (name, definition) => {
|
|
|
1984
2082
|
send({ type: '${name}', ...data })
|
|
1985
2083
|
}`;
|
|
1986
2084
|
};
|
|
1987
|
-
var templateWebsocketClass = (name,
|
|
2085
|
+
var templateWebsocketClass = (name, path9, definitions) => {
|
|
1988
2086
|
const requestDefinitions = Object.keys(definitions).filter((key) => {
|
|
1989
2087
|
const val = definitions[key];
|
|
1990
2088
|
return val["x-type"] == "request";
|
|
@@ -2043,7 +2141,7 @@ const messageSerializer = (data: Record<string, any>) => {
|
|
|
2043
2141
|
export function WebSocketClass(sdk: AccelByteSDK, args?: SdkSetConfigParam) {
|
|
2044
2142
|
const sdkAssembly = sdk.assembly()
|
|
2045
2143
|
const baseURL = (args?.coreConfig?.baseURL ?? sdkAssembly.coreConfig.baseURL).replace('http', 'ws')
|
|
2046
|
-
const path = '${
|
|
2144
|
+
const path = '${path9}'
|
|
2047
2145
|
const url = baseURL + path
|
|
2048
2146
|
let ws: WebSocket | null = null
|
|
2049
2147
|
let isDisconnectManually = false
|
|
@@ -2292,9 +2390,9 @@ ${renderUnion(["response", "notification"], definitions)}
|
|
|
2292
2390
|
// src/WebsocketGenerator.ts
|
|
2293
2391
|
var WebsocketGenerator = class {
|
|
2294
2392
|
static srcFolder = () => CliParser.getOutputPath();
|
|
2295
|
-
static outputFolder = () =>
|
|
2393
|
+
static outputFolder = () => path7.join(this.srcFolder(), "generated-websocket");
|
|
2296
2394
|
static schemaContent = () => {
|
|
2297
|
-
const fileContent = JSON.parse(
|
|
2395
|
+
const fileContent = JSON.parse(fs7.readFileSync(CliParser.getWebSocketSchemaPath(), "utf8"));
|
|
2298
2396
|
return fileContent;
|
|
2299
2397
|
};
|
|
2300
2398
|
static prepareDirs = () => {
|
|
@@ -2304,11 +2402,11 @@ var WebsocketGenerator = class {
|
|
|
2304
2402
|
const { name, path: wsPath, definitions } = this.schemaContent();
|
|
2305
2403
|
const templateDefinitions = templateWebsocketDefinitions(definitions);
|
|
2306
2404
|
this.prepareDirs();
|
|
2307
|
-
const filePath =
|
|
2308
|
-
|
|
2405
|
+
const filePath = path7.join(this.outputFolder(), "WebSocketDefinitions.ts");
|
|
2406
|
+
fs7.writeFileSync(filePath, templateDefinitions, "utf8");
|
|
2309
2407
|
const templateClass2 = templateWebsocketClass(name, wsPath, definitions);
|
|
2310
|
-
const filePathClass =
|
|
2311
|
-
|
|
2408
|
+
const filePathClass = path7.join(this.outputFolder(), "WebSocketClass.ts");
|
|
2409
|
+
fs7.writeFileSync(filePathClass, templateClass2, "utf8");
|
|
2312
2410
|
};
|
|
2313
2411
|
};
|
|
2314
2412
|
|
|
@@ -2321,24 +2419,28 @@ var generateSdk = async () => {
|
|
|
2321
2419
|
if (CliParser.isGenerateWebSocket()) {
|
|
2322
2420
|
WebsocketGenerator.main();
|
|
2323
2421
|
}
|
|
2422
|
+
if (!CodegenConfig.shouldProduceIndexFiles()) {
|
|
2423
|
+
return;
|
|
2424
|
+
}
|
|
2324
2425
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
2325
2426
|
const queryImportsSet = /* @__PURE__ */ new Set();
|
|
2326
2427
|
const filenamesSet = /* @__PURE__ */ new Set();
|
|
2327
2428
|
for (const set of arrayOfSets) {
|
|
2328
2429
|
set.indexImports.forEach((value) => {
|
|
2329
|
-
const fileName =
|
|
2430
|
+
const fileName = path8.basename(value);
|
|
2330
2431
|
if (!filenamesSet.has(fileName)) {
|
|
2331
2432
|
indexImportsSet.add(value);
|
|
2332
2433
|
filenamesSet.add(fileName);
|
|
2333
2434
|
}
|
|
2334
2435
|
});
|
|
2335
2436
|
set.queryImports.forEach((value) => {
|
|
2336
|
-
const fileName =
|
|
2437
|
+
const fileName = path8.basename(value);
|
|
2337
2438
|
if (!filenamesSet.has(fileName)) {
|
|
2338
2439
|
queryImportsSet.add(value);
|
|
2339
2440
|
}
|
|
2340
2441
|
});
|
|
2341
2442
|
}
|
|
2443
|
+
const outputPath = CliParser.getOutputPath();
|
|
2342
2444
|
const indexImportsArray = Array.from(indexImportsSet).sort();
|
|
2343
2445
|
const queryImportsArray = Array.from(queryImportsSet).sort();
|
|
2344
2446
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
@@ -2347,8 +2449,8 @@ var generateSdk = async () => {
|
|
|
2347
2449
|
const queryFilesToImport = queryImportsArray.map((fileToImport) => {
|
|
2348
2450
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
2349
2451
|
});
|
|
2350
|
-
ParserUtils.writeAllImportsFile(
|
|
2351
|
-
ParserUtils.writeAllQueryImportsFile(
|
|
2452
|
+
ParserUtils.writeAllImportsFile(outputPath, filesToImport.join("\n"));
|
|
2453
|
+
ParserUtils.writeAllQueryImportsFile(outputPath, queryFilesToImport.join("\n"));
|
|
2352
2454
|
};
|
|
2353
2455
|
yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
2354
2456
|
CliParser.createInstance(yargs2);
|
|
@@ -2364,6 +2466,7 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
2364
2466
|
return true;
|
|
2365
2467
|
});
|
|
2366
2468
|
CliParser.createInstance(yargs2);
|
|
2469
|
+
await CodegenConfig.loadConfig(path8.dirname(path8.resolve(CliParser.getConfigPath())));
|
|
2367
2470
|
await generateSdk();
|
|
2368
2471
|
}).option("config", {
|
|
2369
2472
|
description: "Path to the config file with backend service URLs.",
|