@bitstack/ng-query-codegen-openapi 0.0.31-alpha.1 → 0.0.31-alpha.3
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/lib/bin/cli.mjs +37 -35
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +88 -66
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +88 -66
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/rtk-query-generator.ts +4 -4
- package/src/generators/tag-types-generator.ts +37 -21
- package/src/services/api-code-generator.ts +10 -0
- package/src/services/file-writer-service.ts +10 -12
- package/src/services/unified-code-generator.ts +73 -80
- package/src/types.ts +5 -0
package/lib/index.js
CHANGED
|
@@ -390,6 +390,9 @@ var FileWriterService = class {
|
|
|
390
390
|
if (sharedFiles.commonTypes) {
|
|
391
391
|
filesToWrite[import_node_path3.default.join(outputDir, "common-types.ts")] = sharedFiles.commonTypes;
|
|
392
392
|
}
|
|
393
|
+
if (sharedFiles.tagTypes) {
|
|
394
|
+
filesToWrite[import_node_path3.default.join(outputDir, "tagTypes.ts")] = sharedFiles.tagTypes;
|
|
395
|
+
}
|
|
393
396
|
if (sharedFiles.doNotModify) {
|
|
394
397
|
filesToWrite[import_node_path3.default.join(outputDir, "DO_NOT_MODIFY.md")] = sharedFiles.doNotModify;
|
|
395
398
|
}
|
|
@@ -926,7 +929,7 @@ function generateRtkQueryFile(endpointInfos, options) {
|
|
|
926
929
|
*/
|
|
927
930
|
${info.operationName}Query(${info.argTypeName !== "VoidApiArg" ? `args: IRestFulEndpointsQueryReturn<${info.argTypeName}>` : ""}): Observable<QueryState<${info.responseTypeName}>> {
|
|
928
931
|
return this.ntkQuery.query<${info.responseTypeName}, ${info.argTypeName !== "VoidApiArg" ? `IRestFulEndpointsQueryReturn<${info.argTypeName}>` : "void"}>({
|
|
929
|
-
queryKey: [
|
|
932
|
+
queryKey: [ECacheTagTypes.${info.queryKeyName}${info.argTypeName !== "VoidApiArg" ? ", args.variables" : ""}],
|
|
930
933
|
queryFn: () => {
|
|
931
934
|
return this.apiService.${info.operationName}(${info.argTypeName !== "VoidApiArg" ? "args" : ""});
|
|
932
935
|
},
|
|
@@ -964,7 +967,7 @@ function generateRtkQueryFile(endpointInfos, options) {
|
|
|
964
967
|
trigger: (${info.argTypeName !== "VoidApiArg" ? "args, " : ""}options = {}) => {
|
|
965
968
|
const { skipCache = true } = options;
|
|
966
969
|
if (!skipCache) {
|
|
967
|
-
const cachedResult = this.ntkQuery.getQueryCache<${info.responseTypeName}>([
|
|
970
|
+
const cachedResult = this.ntkQuery.getQueryCache<${info.responseTypeName}>([ECacheTagTypes.${info.queryKeyName}${info.argTypeName !== "VoidApiArg" ? ", args.variables" : ""}]);
|
|
968
971
|
if (cachedResult) {
|
|
969
972
|
return cachedResult;
|
|
970
973
|
}
|
|
@@ -981,8 +984,8 @@ import {Injectable, inject} from '@angular/core';
|
|
|
981
984
|
import {Observable} from 'rxjs';
|
|
982
985
|
import {HttpErrorResponse} from '@angular/common/http';
|
|
983
986
|
import {NtkQueryService, MutationState, QueryState, MutationResponse} from '@core/ntk-query';
|
|
984
|
-
import {
|
|
985
|
-
import {${apiServiceName}} from './
|
|
987
|
+
import {ECacheTagTypes} from '../tagTypes';
|
|
988
|
+
import {${apiServiceName}} from './enhanceEndpoints';
|
|
986
989
|
import {IRestFulEndpointsQueryReturn, RequestOptions} from '../common-types';
|
|
987
990
|
import {${endpointInfos.map((info) => ` ${info.argTypeName}, ${info.responseTypeName}`).join(",")}} from './types';
|
|
988
991
|
|
|
@@ -1086,6 +1089,11 @@ var ApiCodeGenerator = class {
|
|
|
1086
1089
|
const rtkQueryContent = generateRtkQueryFile(endpointInfos, this.options);
|
|
1087
1090
|
const enhanceEndpointsContent = generateRtkEnhanceEndpointsFile(endpointInfos, this.options);
|
|
1088
1091
|
const indexContent = this.generateIndex();
|
|
1092
|
+
const allEndpointCacheKeys = endpointInfos.filter((info) => info.isQuery).map((info) => ({
|
|
1093
|
+
operationName: info.operationName,
|
|
1094
|
+
queryKeyName: info.queryKeyName,
|
|
1095
|
+
groupKey: this.options.groupKey || "_common"
|
|
1096
|
+
}));
|
|
1089
1097
|
const operationNames = endpointInfos.map((info) => info.operationName);
|
|
1090
1098
|
const allTags = /* @__PURE__ */ new Set();
|
|
1091
1099
|
endpointInfos.forEach((info) => {
|
|
@@ -1101,8 +1109,9 @@ var ApiCodeGenerator = class {
|
|
|
1101
1109
|
queryService: rtkQueryContent,
|
|
1102
1110
|
// RTK Query 檔案
|
|
1103
1111
|
index: indexContent,
|
|
1104
|
-
enhanceEndpoints: enhanceEndpointsContent
|
|
1112
|
+
enhanceEndpoints: enhanceEndpointsContent,
|
|
1105
1113
|
// 新增的 enhance endpoints 檔案
|
|
1114
|
+
allEndpointCacheKeys
|
|
1106
1115
|
}
|
|
1107
1116
|
};
|
|
1108
1117
|
}
|
|
@@ -1181,25 +1190,42 @@ export function withoutUndefined(obj?: Record<string, any>) {
|
|
|
1181
1190
|
|
|
1182
1191
|
`;
|
|
1183
1192
|
}
|
|
1193
|
+
function toCamelCase(str) {
|
|
1194
|
+
return str.toLowerCase().replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
1195
|
+
}
|
|
1184
1196
|
|
|
1185
1197
|
// src/generators/tag-types-generator.ts
|
|
1186
1198
|
init_cjs_shims();
|
|
1187
|
-
function generateTagTypesFile(
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1199
|
+
function generateTagTypesFile(allEndpointInfos) {
|
|
1200
|
+
const groupedKeys = allEndpointInfos.reduce(
|
|
1201
|
+
(acc, info) => {
|
|
1202
|
+
if (!acc[info.groupKey]) {
|
|
1203
|
+
acc[info.groupKey] = [];
|
|
1204
|
+
}
|
|
1205
|
+
acc[info.groupKey].push({
|
|
1206
|
+
operationName: info.operationName,
|
|
1207
|
+
queryKeyName: info.queryKeyName
|
|
1208
|
+
});
|
|
1209
|
+
return acc;
|
|
1210
|
+
},
|
|
1211
|
+
{}
|
|
1212
|
+
);
|
|
1213
|
+
const enumEntries = Object.entries(groupedKeys).map(([groupKey, keys]) => {
|
|
1214
|
+
const groupComment = ` // ${groupKey.charAt(0).toUpperCase() + groupKey.slice(1)} related cache keys`;
|
|
1215
|
+
const keyEntries = keys.map((key) => ` ${key.queryKeyName} = '${toCamelCase(key.queryKeyName)}',`).join("\n");
|
|
1216
|
+
return `${groupComment}
|
|
1217
|
+
${keyEntries}`;
|
|
1218
|
+
}).join("\n\n");
|
|
1219
|
+
return `// [Warning] Generated automatically - do not edit manually
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* Cache keys enum for all API queries
|
|
1223
|
+
*/
|
|
1200
1224
|
export enum ECacheTagTypes {
|
|
1201
1225
|
${enumEntries}
|
|
1202
1226
|
}
|
|
1227
|
+
|
|
1228
|
+
export default ECacheTagTypes;
|
|
1203
1229
|
`;
|
|
1204
1230
|
}
|
|
1205
1231
|
|
|
@@ -1209,6 +1235,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1209
1235
|
openApiService = new OpenApiService();
|
|
1210
1236
|
groupService = new GroupService();
|
|
1211
1237
|
fileWriterService = new FileWriterService();
|
|
1238
|
+
allEndpointCacheKeys = [];
|
|
1212
1239
|
// 內部狀態存儲
|
|
1213
1240
|
openApiDoc = null;
|
|
1214
1241
|
parserService = null;
|
|
@@ -1238,7 +1265,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1238
1265
|
this.generateSchemaContent();
|
|
1239
1266
|
this.generateUtilsContent();
|
|
1240
1267
|
this.generateDoNotModifyContent();
|
|
1241
|
-
this.
|
|
1268
|
+
this.generatECacheTagTypesContent();
|
|
1242
1269
|
return await this.release();
|
|
1243
1270
|
}
|
|
1244
1271
|
/**
|
|
@@ -1252,23 +1279,23 @@ var UnifiedCodeGenerator = class {
|
|
|
1252
1279
|
this._options.schemaFile
|
|
1253
1280
|
);
|
|
1254
1281
|
}
|
|
1255
|
-
this.openApiDoc = await this.openApiService.getDocument(
|
|
1256
|
-
this.actualSchemaFile,
|
|
1257
|
-
this._options.httpResolverOptions
|
|
1258
|
-
);
|
|
1282
|
+
this.openApiDoc = await this.openApiService.getDocument(this.actualSchemaFile, this._options.httpResolverOptions);
|
|
1259
1283
|
this.parserService = new OpenApiParserService(this.openApiDoc, this._options);
|
|
1260
1284
|
this.parserService.initialize();
|
|
1261
1285
|
const apiGen = this.parserService.getApiGenerator();
|
|
1262
|
-
this.schemaInterfaces = apiGen.aliases.reduce(
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1286
|
+
this.schemaInterfaces = apiGen.aliases.reduce(
|
|
1287
|
+
(curr, alias) => {
|
|
1288
|
+
if (import_typescript3.default.isInterfaceDeclaration(alias) || import_typescript3.default.isTypeAliasDeclaration(alias)) {
|
|
1289
|
+
const name = alias.name.text;
|
|
1290
|
+
return {
|
|
1291
|
+
...curr,
|
|
1292
|
+
[name]: alias
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
return curr;
|
|
1296
|
+
},
|
|
1297
|
+
{}
|
|
1298
|
+
);
|
|
1272
1299
|
}
|
|
1273
1300
|
/**
|
|
1274
1301
|
* 生成階段:產生所有內容但不寫檔
|
|
@@ -1281,10 +1308,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1281
1308
|
const groupInfos = this.groupService.groupPaths(paths, this._options.outputFiles);
|
|
1282
1309
|
for (const groupInfo of Object.values(groupInfos)) {
|
|
1283
1310
|
try {
|
|
1284
|
-
const groupContent = await this.generateApiGroupContent(
|
|
1285
|
-
this._options,
|
|
1286
|
-
groupInfo
|
|
1287
|
-
);
|
|
1311
|
+
const groupContent = await this.generateApiGroupContent(this._options, groupInfo);
|
|
1288
1312
|
if (groupContent.operationNames.length > 0) {
|
|
1289
1313
|
this.generatedContent.groups.push({
|
|
1290
1314
|
groupKey: groupInfo.groupKey,
|
|
@@ -1300,6 +1324,12 @@ var UnifiedCodeGenerator = class {
|
|
|
1300
1324
|
}
|
|
1301
1325
|
}
|
|
1302
1326
|
}
|
|
1327
|
+
/**
|
|
1328
|
+
* 生成 cache keys
|
|
1329
|
+
*/
|
|
1330
|
+
async generatECacheTagTypesContent() {
|
|
1331
|
+
this.generatedContent.tagTypes = generateTagTypesFile(this.allEndpointCacheKeys);
|
|
1332
|
+
}
|
|
1303
1333
|
/**
|
|
1304
1334
|
* 生成 common types
|
|
1305
1335
|
*/
|
|
@@ -1327,10 +1357,10 @@ var UnifiedCodeGenerator = class {
|
|
|
1327
1357
|
/**
|
|
1328
1358
|
* 生成 Tag Types
|
|
1329
1359
|
*/
|
|
1330
|
-
async generateTagTypesContent() {
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
}
|
|
1360
|
+
// private async generateTagTypesContent(): Promise<void> {
|
|
1361
|
+
// const tagsArray = Array.from(this.allTags);
|
|
1362
|
+
// this.generatedContent.tagTypes = generateTagTypesFile(tagsArray);
|
|
1363
|
+
// }
|
|
1334
1364
|
/**
|
|
1335
1365
|
* 發佈階段:統一寫入所有檔案
|
|
1336
1366
|
*/
|
|
@@ -1343,15 +1373,12 @@ var UnifiedCodeGenerator = class {
|
|
|
1343
1373
|
try {
|
|
1344
1374
|
if (group.content?.files) {
|
|
1345
1375
|
const groupOutputDir = import_node_path4.default.dirname(group.outputPath);
|
|
1346
|
-
const groupResults = await this.fileWriterService.writeGroupFiles(
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
index: group.content.files.index
|
|
1353
|
-
}
|
|
1354
|
-
);
|
|
1376
|
+
const groupResults = await this.fileWriterService.writeGroupFiles(groupOutputDir, {
|
|
1377
|
+
types: group.content.files.types,
|
|
1378
|
+
queryService: group.content.files.queryService,
|
|
1379
|
+
enhanceEndpoints: group.content.files.enhanceEndpoints,
|
|
1380
|
+
index: group.content.files.index
|
|
1381
|
+
});
|
|
1355
1382
|
results.push(...groupResults);
|
|
1356
1383
|
generatedGroups.push(group.groupKey);
|
|
1357
1384
|
}
|
|
@@ -1360,24 +1387,15 @@ var UnifiedCodeGenerator = class {
|
|
|
1360
1387
|
}
|
|
1361
1388
|
}
|
|
1362
1389
|
const outputDir = this.generatedContent.groups[0] ? import_node_path4.default.dirname(import_node_path4.default.dirname(this.generatedContent.groups[0].outputPath)) : "./generated";
|
|
1363
|
-
if (this.generatedContent.commonTypes || this.generatedContent.doNotModify || this.generatedContent.utils) {
|
|
1364
|
-
const sharedResults = await this.fileWriterService.writeSharedFiles(
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
}
|
|
1371
|
-
);
|
|
1390
|
+
if (this.generatedContent.tagTypes || this.generatedContent.commonTypes || this.generatedContent.doNotModify || this.generatedContent.utils) {
|
|
1391
|
+
const sharedResults = await this.fileWriterService.writeSharedFiles(outputDir, {
|
|
1392
|
+
tagTypes: this.generatedContent.tagTypes || void 0,
|
|
1393
|
+
commonTypes: this.generatedContent.commonTypes || void 0,
|
|
1394
|
+
doNotModify: this.generatedContent.doNotModify || void 0,
|
|
1395
|
+
utils: this.generatedContent.utils || void 0
|
|
1396
|
+
});
|
|
1372
1397
|
results.push(...sharedResults);
|
|
1373
1398
|
}
|
|
1374
|
-
if (this.generatedContent.tagTypes) {
|
|
1375
|
-
const tagTypesResult = await this.fileWriterService.writeFile(
|
|
1376
|
-
import_node_path4.default.join(outputDir, "tagTypes.ts"),
|
|
1377
|
-
this.generatedContent.tagTypes
|
|
1378
|
-
);
|
|
1379
|
-
results.push(tagTypesResult);
|
|
1380
|
-
}
|
|
1381
1399
|
if (this.generatedContent.componentSchema) {
|
|
1382
1400
|
const schemaResults = await this.fileWriterService.writeSchemaFile(
|
|
1383
1401
|
outputDir,
|
|
@@ -1420,6 +1438,10 @@ var UnifiedCodeGenerator = class {
|
|
|
1420
1438
|
}
|
|
1421
1439
|
const apiGenerator = new ApiCodeGenerator(this.parserService, groupOptions);
|
|
1422
1440
|
const result = await apiGenerator.generate();
|
|
1441
|
+
if (result.files && "allEndpointCacheKeys" in result.files) {
|
|
1442
|
+
const cacheKeys = result.files.allEndpointCacheKeys;
|
|
1443
|
+
this.allEndpointCacheKeys.push(...cacheKeys);
|
|
1444
|
+
}
|
|
1423
1445
|
return result;
|
|
1424
1446
|
}
|
|
1425
1447
|
/**
|