@bitstack/ng-query-codegen-openapi 0.0.31-alpha.1 → 0.0.31-alpha.4
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 +38 -36
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +89 -67
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +89 -67
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/common-types-generator.ts +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.mjs
CHANGED
|
@@ -375,6 +375,9 @@ var FileWriterService = class {
|
|
|
375
375
|
if (sharedFiles.commonTypes) {
|
|
376
376
|
filesToWrite[path3.join(outputDir, "common-types.ts")] = sharedFiles.commonTypes;
|
|
377
377
|
}
|
|
378
|
+
if (sharedFiles.tagTypes) {
|
|
379
|
+
filesToWrite[path3.join(outputDir, "tagTypes.ts")] = sharedFiles.tagTypes;
|
|
380
|
+
}
|
|
378
381
|
if (sharedFiles.doNotModify) {
|
|
379
382
|
filesToWrite[path3.join(outputDir, "DO_NOT_MODIFY.md")] = sharedFiles.doNotModify;
|
|
380
383
|
}
|
|
@@ -481,7 +484,7 @@ export interface IRequestConfig extends RequestOptions {
|
|
|
481
484
|
}
|
|
482
485
|
|
|
483
486
|
export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
|
|
484
|
-
(
|
|
487
|
+
(undefined | {fetchOptions?: IRequestConfig;}):
|
|
485
488
|
{
|
|
486
489
|
variables: TVariables;
|
|
487
490
|
fetchOptions?: IRequestConfig;
|
|
@@ -911,7 +914,7 @@ function generateRtkQueryFile(endpointInfos, options) {
|
|
|
911
914
|
*/
|
|
912
915
|
${info.operationName}Query(${info.argTypeName !== "VoidApiArg" ? `args: IRestFulEndpointsQueryReturn<${info.argTypeName}>` : ""}): Observable<QueryState<${info.responseTypeName}>> {
|
|
913
916
|
return this.ntkQuery.query<${info.responseTypeName}, ${info.argTypeName !== "VoidApiArg" ? `IRestFulEndpointsQueryReturn<${info.argTypeName}>` : "void"}>({
|
|
914
|
-
queryKey: [
|
|
917
|
+
queryKey: [ECacheTagTypes.${info.queryKeyName}${info.argTypeName !== "VoidApiArg" ? ", args.variables" : ""}],
|
|
915
918
|
queryFn: () => {
|
|
916
919
|
return this.apiService.${info.operationName}(${info.argTypeName !== "VoidApiArg" ? "args" : ""});
|
|
917
920
|
},
|
|
@@ -949,7 +952,7 @@ function generateRtkQueryFile(endpointInfos, options) {
|
|
|
949
952
|
trigger: (${info.argTypeName !== "VoidApiArg" ? "args, " : ""}options = {}) => {
|
|
950
953
|
const { skipCache = true } = options;
|
|
951
954
|
if (!skipCache) {
|
|
952
|
-
const cachedResult = this.ntkQuery.getQueryCache<${info.responseTypeName}>([
|
|
955
|
+
const cachedResult = this.ntkQuery.getQueryCache<${info.responseTypeName}>([ECacheTagTypes.${info.queryKeyName}${info.argTypeName !== "VoidApiArg" ? ", args.variables" : ""}]);
|
|
953
956
|
if (cachedResult) {
|
|
954
957
|
return cachedResult;
|
|
955
958
|
}
|
|
@@ -966,8 +969,8 @@ import {Injectable, inject} from '@angular/core';
|
|
|
966
969
|
import {Observable} from 'rxjs';
|
|
967
970
|
import {HttpErrorResponse} from '@angular/common/http';
|
|
968
971
|
import {NtkQueryService, MutationState, QueryState, MutationResponse} from '@core/ntk-query';
|
|
969
|
-
import {
|
|
970
|
-
import {${apiServiceName}} from './
|
|
972
|
+
import {ECacheTagTypes} from '../tagTypes';
|
|
973
|
+
import {${apiServiceName}} from './enhanceEndpoints';
|
|
971
974
|
import {IRestFulEndpointsQueryReturn, RequestOptions} from '../common-types';
|
|
972
975
|
import {${endpointInfos.map((info) => ` ${info.argTypeName}, ${info.responseTypeName}`).join(",")}} from './types';
|
|
973
976
|
|
|
@@ -1071,6 +1074,11 @@ var ApiCodeGenerator = class {
|
|
|
1071
1074
|
const rtkQueryContent = generateRtkQueryFile(endpointInfos, this.options);
|
|
1072
1075
|
const enhanceEndpointsContent = generateRtkEnhanceEndpointsFile(endpointInfos, this.options);
|
|
1073
1076
|
const indexContent = this.generateIndex();
|
|
1077
|
+
const allEndpointCacheKeys = endpointInfos.filter((info) => info.isQuery).map((info) => ({
|
|
1078
|
+
operationName: info.operationName,
|
|
1079
|
+
queryKeyName: info.queryKeyName,
|
|
1080
|
+
groupKey: this.options.groupKey || "_common"
|
|
1081
|
+
}));
|
|
1074
1082
|
const operationNames = endpointInfos.map((info) => info.operationName);
|
|
1075
1083
|
const allTags = /* @__PURE__ */ new Set();
|
|
1076
1084
|
endpointInfos.forEach((info) => {
|
|
@@ -1086,8 +1094,9 @@ var ApiCodeGenerator = class {
|
|
|
1086
1094
|
queryService: rtkQueryContent,
|
|
1087
1095
|
// RTK Query 檔案
|
|
1088
1096
|
index: indexContent,
|
|
1089
|
-
enhanceEndpoints: enhanceEndpointsContent
|
|
1097
|
+
enhanceEndpoints: enhanceEndpointsContent,
|
|
1090
1098
|
// 新增的 enhance endpoints 檔案
|
|
1099
|
+
allEndpointCacheKeys
|
|
1091
1100
|
}
|
|
1092
1101
|
};
|
|
1093
1102
|
}
|
|
@@ -1166,25 +1175,42 @@ export function withoutUndefined(obj?: Record<string, any>) {
|
|
|
1166
1175
|
|
|
1167
1176
|
`;
|
|
1168
1177
|
}
|
|
1178
|
+
function toCamelCase(str) {
|
|
1179
|
+
return str.toLowerCase().replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
1180
|
+
}
|
|
1169
1181
|
|
|
1170
1182
|
// src/generators/tag-types-generator.ts
|
|
1171
1183
|
init_esm_shims();
|
|
1172
|
-
function generateTagTypesFile(
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1184
|
+
function generateTagTypesFile(allEndpointInfos) {
|
|
1185
|
+
const groupedKeys = allEndpointInfos.reduce(
|
|
1186
|
+
(acc, info) => {
|
|
1187
|
+
if (!acc[info.groupKey]) {
|
|
1188
|
+
acc[info.groupKey] = [];
|
|
1189
|
+
}
|
|
1190
|
+
acc[info.groupKey].push({
|
|
1191
|
+
operationName: info.operationName,
|
|
1192
|
+
queryKeyName: info.queryKeyName
|
|
1193
|
+
});
|
|
1194
|
+
return acc;
|
|
1195
|
+
},
|
|
1196
|
+
{}
|
|
1197
|
+
);
|
|
1198
|
+
const enumEntries = Object.entries(groupedKeys).map(([groupKey, keys]) => {
|
|
1199
|
+
const groupComment = ` // ${groupKey.charAt(0).toUpperCase() + groupKey.slice(1)} related cache keys`;
|
|
1200
|
+
const keyEntries = keys.map((key) => ` ${key.queryKeyName} = '${toCamelCase(key.queryKeyName)}',`).join("\n");
|
|
1201
|
+
return `${groupComment}
|
|
1202
|
+
${keyEntries}`;
|
|
1203
|
+
}).join("\n\n");
|
|
1204
|
+
return `// [Warning] Generated automatically - do not edit manually
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Cache keys enum for all API queries
|
|
1208
|
+
*/
|
|
1185
1209
|
export enum ECacheTagTypes {
|
|
1186
1210
|
${enumEntries}
|
|
1187
1211
|
}
|
|
1212
|
+
|
|
1213
|
+
export default ECacheTagTypes;
|
|
1188
1214
|
`;
|
|
1189
1215
|
}
|
|
1190
1216
|
|
|
@@ -1194,6 +1220,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1194
1220
|
openApiService = new OpenApiService();
|
|
1195
1221
|
groupService = new GroupService();
|
|
1196
1222
|
fileWriterService = new FileWriterService();
|
|
1223
|
+
allEndpointCacheKeys = [];
|
|
1197
1224
|
// 內部狀態存儲
|
|
1198
1225
|
openApiDoc = null;
|
|
1199
1226
|
parserService = null;
|
|
@@ -1223,7 +1250,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1223
1250
|
this.generateSchemaContent();
|
|
1224
1251
|
this.generateUtilsContent();
|
|
1225
1252
|
this.generateDoNotModifyContent();
|
|
1226
|
-
this.
|
|
1253
|
+
this.generatECacheTagTypesContent();
|
|
1227
1254
|
return await this.release();
|
|
1228
1255
|
}
|
|
1229
1256
|
/**
|
|
@@ -1237,23 +1264,23 @@ var UnifiedCodeGenerator = class {
|
|
|
1237
1264
|
this._options.schemaFile
|
|
1238
1265
|
);
|
|
1239
1266
|
}
|
|
1240
|
-
this.openApiDoc = await this.openApiService.getDocument(
|
|
1241
|
-
this.actualSchemaFile,
|
|
1242
|
-
this._options.httpResolverOptions
|
|
1243
|
-
);
|
|
1267
|
+
this.openApiDoc = await this.openApiService.getDocument(this.actualSchemaFile, this._options.httpResolverOptions);
|
|
1244
1268
|
this.parserService = new OpenApiParserService(this.openApiDoc, this._options);
|
|
1245
1269
|
this.parserService.initialize();
|
|
1246
1270
|
const apiGen = this.parserService.getApiGenerator();
|
|
1247
|
-
this.schemaInterfaces = apiGen.aliases.reduce(
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1271
|
+
this.schemaInterfaces = apiGen.aliases.reduce(
|
|
1272
|
+
(curr, alias) => {
|
|
1273
|
+
if (ts3.isInterfaceDeclaration(alias) || ts3.isTypeAliasDeclaration(alias)) {
|
|
1274
|
+
const name = alias.name.text;
|
|
1275
|
+
return {
|
|
1276
|
+
...curr,
|
|
1277
|
+
[name]: alias
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
return curr;
|
|
1281
|
+
},
|
|
1282
|
+
{}
|
|
1283
|
+
);
|
|
1257
1284
|
}
|
|
1258
1285
|
/**
|
|
1259
1286
|
* 生成階段:產生所有內容但不寫檔
|
|
@@ -1266,10 +1293,7 @@ var UnifiedCodeGenerator = class {
|
|
|
1266
1293
|
const groupInfos = this.groupService.groupPaths(paths, this._options.outputFiles);
|
|
1267
1294
|
for (const groupInfo of Object.values(groupInfos)) {
|
|
1268
1295
|
try {
|
|
1269
|
-
const groupContent = await this.generateApiGroupContent(
|
|
1270
|
-
this._options,
|
|
1271
|
-
groupInfo
|
|
1272
|
-
);
|
|
1296
|
+
const groupContent = await this.generateApiGroupContent(this._options, groupInfo);
|
|
1273
1297
|
if (groupContent.operationNames.length > 0) {
|
|
1274
1298
|
this.generatedContent.groups.push({
|
|
1275
1299
|
groupKey: groupInfo.groupKey,
|
|
@@ -1285,6 +1309,12 @@ var UnifiedCodeGenerator = class {
|
|
|
1285
1309
|
}
|
|
1286
1310
|
}
|
|
1287
1311
|
}
|
|
1312
|
+
/**
|
|
1313
|
+
* 生成 cache keys
|
|
1314
|
+
*/
|
|
1315
|
+
async generatECacheTagTypesContent() {
|
|
1316
|
+
this.generatedContent.tagTypes = generateTagTypesFile(this.allEndpointCacheKeys);
|
|
1317
|
+
}
|
|
1288
1318
|
/**
|
|
1289
1319
|
* 生成 common types
|
|
1290
1320
|
*/
|
|
@@ -1312,10 +1342,10 @@ var UnifiedCodeGenerator = class {
|
|
|
1312
1342
|
/**
|
|
1313
1343
|
* 生成 Tag Types
|
|
1314
1344
|
*/
|
|
1315
|
-
async generateTagTypesContent() {
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
}
|
|
1345
|
+
// private async generateTagTypesContent(): Promise<void> {
|
|
1346
|
+
// const tagsArray = Array.from(this.allTags);
|
|
1347
|
+
// this.generatedContent.tagTypes = generateTagTypesFile(tagsArray);
|
|
1348
|
+
// }
|
|
1319
1349
|
/**
|
|
1320
1350
|
* 發佈階段:統一寫入所有檔案
|
|
1321
1351
|
*/
|
|
@@ -1328,15 +1358,12 @@ var UnifiedCodeGenerator = class {
|
|
|
1328
1358
|
try {
|
|
1329
1359
|
if (group.content?.files) {
|
|
1330
1360
|
const groupOutputDir = path4.dirname(group.outputPath);
|
|
1331
|
-
const groupResults = await this.fileWriterService.writeGroupFiles(
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
index: group.content.files.index
|
|
1338
|
-
}
|
|
1339
|
-
);
|
|
1361
|
+
const groupResults = await this.fileWriterService.writeGroupFiles(groupOutputDir, {
|
|
1362
|
+
types: group.content.files.types,
|
|
1363
|
+
queryService: group.content.files.queryService,
|
|
1364
|
+
enhanceEndpoints: group.content.files.enhanceEndpoints,
|
|
1365
|
+
index: group.content.files.index
|
|
1366
|
+
});
|
|
1340
1367
|
results.push(...groupResults);
|
|
1341
1368
|
generatedGroups.push(group.groupKey);
|
|
1342
1369
|
}
|
|
@@ -1345,24 +1372,15 @@ var UnifiedCodeGenerator = class {
|
|
|
1345
1372
|
}
|
|
1346
1373
|
}
|
|
1347
1374
|
const outputDir = this.generatedContent.groups[0] ? path4.dirname(path4.dirname(this.generatedContent.groups[0].outputPath)) : "./generated";
|
|
1348
|
-
if (this.generatedContent.commonTypes || this.generatedContent.doNotModify || this.generatedContent.utils) {
|
|
1349
|
-
const sharedResults = await this.fileWriterService.writeSharedFiles(
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
}
|
|
1356
|
-
);
|
|
1375
|
+
if (this.generatedContent.tagTypes || this.generatedContent.commonTypes || this.generatedContent.doNotModify || this.generatedContent.utils) {
|
|
1376
|
+
const sharedResults = await this.fileWriterService.writeSharedFiles(outputDir, {
|
|
1377
|
+
tagTypes: this.generatedContent.tagTypes || void 0,
|
|
1378
|
+
commonTypes: this.generatedContent.commonTypes || void 0,
|
|
1379
|
+
doNotModify: this.generatedContent.doNotModify || void 0,
|
|
1380
|
+
utils: this.generatedContent.utils || void 0
|
|
1381
|
+
});
|
|
1357
1382
|
results.push(...sharedResults);
|
|
1358
1383
|
}
|
|
1359
|
-
if (this.generatedContent.tagTypes) {
|
|
1360
|
-
const tagTypesResult = await this.fileWriterService.writeFile(
|
|
1361
|
-
path4.join(outputDir, "tagTypes.ts"),
|
|
1362
|
-
this.generatedContent.tagTypes
|
|
1363
|
-
);
|
|
1364
|
-
results.push(tagTypesResult);
|
|
1365
|
-
}
|
|
1366
1384
|
if (this.generatedContent.componentSchema) {
|
|
1367
1385
|
const schemaResults = await this.fileWriterService.writeSchemaFile(
|
|
1368
1386
|
outputDir,
|
|
@@ -1405,6 +1423,10 @@ var UnifiedCodeGenerator = class {
|
|
|
1405
1423
|
}
|
|
1406
1424
|
const apiGenerator = new ApiCodeGenerator(this.parserService, groupOptions);
|
|
1407
1425
|
const result = await apiGenerator.generate();
|
|
1426
|
+
if (result.files && "allEndpointCacheKeys" in result.files) {
|
|
1427
|
+
const cacheKeys = result.files.allEndpointCacheKeys;
|
|
1428
|
+
this.allEndpointCacheKeys.push(...cacheKeys);
|
|
1429
|
+
}
|
|
1408
1430
|
return result;
|
|
1409
1431
|
}
|
|
1410
1432
|
/**
|