@tachybase/plugin-api-logs 0.23.43 → 0.23.54

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.
@@ -1,15 +1,15 @@
1
1
  module.exports = {
2
2
  "react": "18.3.1",
3
- "@tachybase/client": "0.23.47",
4
- "@tachybase/schema": "0.23.47",
3
+ "@tachybase/client": "0.23.58",
4
+ "@tachybase/schema": "0.23.58",
5
5
  "@ant-design/icons": "5.6.1",
6
6
  "react-i18next": "15.4.0",
7
- "@tachybase/database": "0.23.47",
8
- "@tachybase/server": "0.23.47",
9
- "@tachybase/utils": "0.23.47",
7
+ "@tachybase/database": "0.23.58",
8
+ "@tachybase/actions": "0.23.58",
9
+ "@tachybase/server": "0.23.58",
10
+ "@tachybase/utils": "0.23.58",
10
11
  "antd": "5.22.5",
11
- "@tachybase/components": "0.23.47",
12
- "@tachybase/actions": "0.23.47",
13
- "@tachybase/data-source": "0.23.47",
14
- "@tachybase/resourcer": "0.23.47"
12
+ "@tachybase/components": "0.23.58",
13
+ "@tachybase/data-source": "0.23.58",
14
+ "@tachybase/resourcer": "0.23.58"
15
15
  };
@@ -73,7 +73,7 @@ class ApiLogsController {
73
73
  __runInitializers(_init, 5, this);
74
74
  }
75
75
  async tableSync(ctx, next) {
76
- var _a, _b, _c, _d;
76
+ var _a, _b, _c, _d, _e;
77
77
  const apiLogsRepo = ctx.db.getRepository("apiLogsConfig");
78
78
  const apiLogsConfigs = await apiLogsRepo.find();
79
79
  const collectionsToInsert = [];
@@ -98,9 +98,11 @@ class ApiLogsController {
98
98
  return;
99
99
  }
100
100
  try {
101
- await apiLogsRepo.createMany({ records: collectionsToInsert });
101
+ await apiLogsRepo.createMany({ records: collectionsToInsert, hooks: false });
102
+ const plugin = ctx.app.pm.get("api-logs");
103
+ await ((_b = plugin.apiFilter) == null ? void 0 : _b.load());
102
104
  } catch (error) {
103
- ctx.throw(((_d = (_c = (_b = error == null ? void 0 : error.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error) == null ? void 0 : _d.message) || "request error");
105
+ ctx.throw(((_e = (_d = (_c = error == null ? void 0 : error.response) == null ? void 0 : _c.data) == null ? void 0 : _d.error) == null ? void 0 : _e.message) || "request error");
104
106
  }
105
107
  await next();
106
108
  }
@@ -1 +1,2 @@
1
- export declare function handleCreate(ctx: any): Promise<void>;
1
+ import { Context } from '@tachybase/actions';
2
+ export declare function handleCreate(ctx: Context, next: any): Promise<void>;
@@ -20,17 +20,21 @@ __export(afterCreate_exports, {
20
20
  handleCreate: () => handleCreate
21
21
  });
22
22
  module.exports = __toCommonJS(afterCreate_exports);
23
- async function handleCreate(ctx) {
24
- var _a;
23
+ async function handleCreate(ctx, next) {
24
+ var _a, _b;
25
+ await next();
25
26
  const { actionName, resourceName, params } = ctx.action;
26
27
  const currentTime = (/* @__PURE__ */ new Date()).toISOString();
27
28
  const apilogsRepo = ctx.db.getRepository("apiLogs");
28
29
  const currentUserId = (_a = ctx.auth) == null ? void 0 : _a.user.id;
29
- const currentRecordId = params.filterByTk;
30
30
  const app = ctx.app;
31
31
  const collection = app.mainDataSource.collectionManager.getCollection(ctx.action.resourceName);
32
+ const currentRecordId = (_b = ctx.body) == null ? void 0 : _b[collection.filterTargetKey];
32
33
  const changes = [];
33
- const changed = params.values;
34
+ const changed = {
35
+ ...params.values || {},
36
+ [collection.filterTargetKey]: currentRecordId
37
+ };
34
38
  if (changed) {
35
39
  Object.keys(changed).forEach((key) => {
36
40
  const field = collection.findField((field2) => {
@@ -47,26 +51,17 @@ async function handleCreate(ctx) {
47
51
  if (!changes.length) {
48
52
  return;
49
53
  }
50
- try {
51
- await apilogsRepo.create({
52
- values: {
53
- action: actionName,
54
- createdAt: currentTime,
55
- collectionName: resourceName,
56
- recordId: currentRecordId,
57
- userId: currentUserId,
58
- changes
59
- }
60
- });
61
- } catch (error) {
62
- ctx.logger.error("Failed to create API log:", {
63
- error,
54
+ apilogsRepo.create({
55
+ values: {
64
56
  action: actionName,
65
- resourceName,
66
- recordId: currentRecordId
67
- });
68
- throw new Error(`Failed to create API log: ${error.message}`);
69
- }
57
+ createdAt: currentTime,
58
+ collectionName: resourceName,
59
+ recordId: currentRecordId,
60
+ userId: currentUserId,
61
+ changes
62
+ },
63
+ hooks: false
64
+ });
70
65
  }
71
66
  // Annotate the CommonJS export names for ESM import in node:
72
67
  0 && (module.exports = {
@@ -1 +1 @@
1
- export declare function handleDestroy(ctx: any): Promise<void>;
1
+ export declare function handleDestroy(ctx: any, next: any): Promise<void>;
@@ -21,16 +21,40 @@ __export(afterDestroy_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(afterDestroy_exports);
23
23
  var import_getFieldChange = require("./getFieldChange");
24
- async function handleDestroy(ctx) {
24
+ async function handleDestroy(ctx, next) {
25
+ var _a, _b;
26
+ const insertValues = [];
27
+ if (Array.isArray((_b = (_a = ctx.action) == null ? void 0 : _a.params) == null ? void 0 : _b.filterByTk)) {
28
+ const promises = ctx.action.params.filterByTk.map((tk) => handleGetInsertValues(ctx, tk, insertValues));
29
+ await Promise.all(promises);
30
+ } else {
31
+ await handleGetInsertValues(ctx, ctx.action.params.filterByTk, insertValues);
32
+ }
33
+ await next();
34
+ if (insertValues.length) {
35
+ insertDestoryValues(ctx.db, insertValues);
36
+ }
37
+ }
38
+ async function handleGetInsertValues(ctx, tk, insertValues) {
39
+ try {
40
+ const values = await getInsertValues(ctx, tk);
41
+ insertValues.push(values);
42
+ } catch (error) {
43
+ ctx.app.logger.error("handleDestroy error, getInsertValues: ", {
44
+ param: ctx.action.params,
45
+ error
46
+ });
47
+ }
48
+ }
49
+ async function getInsertValues(ctx, tk) {
25
50
  var _a;
26
51
  const { actionName, resourceName, params } = ctx.action;
27
- const currentTime = (/* @__PURE__ */ new Date()).toISOString();
28
52
  const apilogsRepo = ctx.db.getRepository("apiLogs");
29
53
  const currentUserId = (_a = ctx.auth) == null ? void 0 : _a.user.id;
30
54
  const app = ctx.app;
31
55
  const collection = app.mainDataSource.collectionManager.getCollection(ctx.action.resourceName);
32
56
  const changes = [];
33
- const { changed, data: dataBefore } = await (0, import_getFieldChange.getChanged)(ctx);
57
+ const { changed, data: dataBefore } = await (0, import_getFieldChange.getChanged)(ctx, tk);
34
58
  Object.keys(dataBefore).forEach((key) => {
35
59
  const field = collection.findField((field2) => {
36
60
  return field2.name === key || field2.options.field === key;
@@ -42,24 +66,24 @@ async function handleDestroy(ctx) {
42
66
  });
43
67
  }
44
68
  });
45
- if (!changes.length) {
46
- return;
47
- }
48
- try {
49
- await apilogsRepo.create({
50
- values: {
51
- action: actionName,
52
- createdAt: currentTime,
53
- collectionName: resourceName,
54
- recordId: params.filterByTk,
55
- userId: currentUserId,
56
- changes
57
- }
58
- });
59
- } catch (error) {
60
- throw new Error("Failed to create API log");
69
+ if (changes.length) {
70
+ return {
71
+ action: actionName,
72
+ createdAt: /* @__PURE__ */ new Date(),
73
+ collectionName: resourceName,
74
+ recordId: tk,
75
+ userId: currentUserId,
76
+ changes
77
+ };
61
78
  }
62
79
  }
80
+ async function insertDestoryValues(db, records) {
81
+ const apilogsRepo = db.getRepository("apiLogs");
82
+ await apilogsRepo.createMany({
83
+ records,
84
+ hooks: false
85
+ });
86
+ }
63
87
  // Annotate the CommonJS export names for ESM import in node:
64
88
  0 && (module.exports = {
65
89
  handleDestroy
@@ -1 +1,2 @@
1
- export declare function handleUpdate(ctx: any): Promise<void>;
1
+ import { Context } from '@tachybase/actions';
2
+ export declare function handleUpdate(ctx: Context, next: any): Promise<any>;
@@ -21,7 +21,7 @@ __export(afterUpdate_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(afterUpdate_exports);
23
23
  var import_getFieldChange = require("./getFieldChange");
24
- async function handleUpdate(ctx) {
24
+ async function handleUpdate(ctx, next) {
25
25
  var _a;
26
26
  const { actionName, resourceName, params } = ctx.action;
27
27
  const currentTime = (/* @__PURE__ */ new Date()).toISOString();
@@ -30,9 +30,9 @@ async function handleUpdate(ctx) {
30
30
  const app = ctx.app;
31
31
  const collection = app.mainDataSource.collectionManager.getCollection(ctx.action.resourceName);
32
32
  const changes = [];
33
- const { changed, data: dataBefore } = await (0, import_getFieldChange.getChanged)(ctx);
33
+ const { changed, data: dataBefore } = await (0, import_getFieldChange.getChanged)(ctx, params.filterByTk);
34
34
  if (!changed) {
35
- return;
35
+ return next();
36
36
  }
37
37
  changed.forEach((key) => {
38
38
  var _a2;
@@ -59,22 +59,20 @@ async function handleUpdate(ctx) {
59
59
  }
60
60
  });
61
61
  if (!changes.length) {
62
- return;
63
- }
64
- try {
65
- await apilogsRepo.create({
66
- values: {
67
- action: actionName,
68
- createdAt: currentTime,
69
- collectionName: resourceName,
70
- recordId: params.filterByTk,
71
- userId: currentUserId,
72
- changes
73
- }
74
- });
75
- } catch (error) {
76
- throw new Error("Failed to create API log");
62
+ return next();
77
63
  }
64
+ await next();
65
+ apilogsRepo.create({
66
+ values: {
67
+ action: actionName,
68
+ createdAt: currentTime,
69
+ collectionName: resourceName,
70
+ recordId: params.filterByTk,
71
+ userId: currentUserId,
72
+ changes
73
+ },
74
+ hooks: false
75
+ });
78
76
  }
79
77
  // Annotate the CommonJS export names for ESM import in node:
80
78
  0 && (module.exports = {
@@ -1,5 +1,5 @@
1
1
  /** 获得真正变动的数据库字段 */
2
- export declare function getChanged(ctx: any): Promise<{
2
+ export declare function getChanged(ctx: any, filterByTk: any): Promise<{
3
3
  changed?: string[];
4
4
  data?: any;
5
5
  error: Error;
@@ -89,13 +89,16 @@ function getDiffKeyExceptAfter(before, after, path = "") {
89
89
  }
90
90
  return [...beforeDiffKeys];
91
91
  }
92
- async function getChanged(ctx) {
92
+ async function getChanged(ctx, filterByTk) {
93
93
  try {
94
94
  const params = import_utils.lodash.cloneDeep(ctx.action.params);
95
- const repo = ctx.db.getRepository(ctx.action.resourceName);
95
+ if (!filterByTk) {
96
+ filterByTk = params.filterByTk;
97
+ }
96
98
  const fieldsObj = {};
97
99
  const app = ctx.app;
98
100
  const collection = app.mainDataSource.collectionManager.getCollection(ctx.action.resourceName);
101
+ const repo = ctx.db.getRepository(collection.name);
99
102
  const fields = collection.getFields();
100
103
  for (const field of fields) {
101
104
  fieldsObj[field.options.name] = field;
@@ -115,9 +118,7 @@ async function getChanged(ctx) {
115
118
  }
116
119
  }
117
120
  let dataBefore = (await repo.findOne({
118
- filter: {
119
- id: params.filterByTk
120
- },
121
+ filterByTk,
121
122
  appends: [...appendSet]
122
123
  })).toJSON();
123
124
  const lostKeys = getLostKey(dataBefore, params.values);
@@ -127,9 +128,7 @@ async function getChanged(ctx) {
127
128
  }
128
129
  }
129
130
  dataBefore = (await repo.findOne({
130
- filter: {
131
- id: params.filterByTk
132
- },
131
+ filterByTk,
133
132
  appends: [...appendSet]
134
133
  })).toJSON();
135
134
  const changed = getDiffKeyExceptAfter(dataBefore, params.values);
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from '@tachybase/server';
2
+ import { ApiFilter } from './ApiFilter';
2
3
  export declare class PluginApiLogsServer extends Plugin {
3
- afterAdd(): Promise<void>;
4
- beforeLoad(): Promise<void>;
4
+ apiFilter: ApiFilter;
5
5
  addApiListener(): Promise<void>;
6
6
  load(): Promise<void>;
7
7
  }
@@ -76,42 +76,38 @@ _PluginApiLogsServer_decorators = [(0, import_server.InjectedPlugin)({
76
76
  Controllers: [import_apiLogsController.ApiLogsController]
77
77
  })];
78
78
  class PluginApiLogsServer extends (_a = import_server.Plugin) {
79
- async afterAdd() {
80
- if (!import_node_worker_threads.isMainThread) {
81
- this.addApiListener();
82
- }
83
- }
84
- async beforeLoad() {
85
- if (import_node_worker_threads.isMainThread) {
86
- this.addApiListener();
87
- }
79
+ constructor() {
80
+ super(...arguments);
81
+ this.apiFilter = null;
88
82
  }
89
83
  async addApiListener() {
90
- const apiFilter = new import_ApiFilter.ApiFilter(this.db);
91
84
  this.app.on("afterStart", async () => {
92
- await apiFilter.load();
85
+ this.apiFilter = new import_ApiFilter.ApiFilter(this.db);
86
+ await this.apiFilter.load();
93
87
  });
94
88
  this.app.resourcer.use(
95
89
  async (ctx, next) => {
90
+ var _a2;
96
91
  const { actionName, resourceName, params } = ctx.action;
97
- if (!apiFilter.check(resourceName, actionName)) {
92
+ if (!((_a2 = this.apiFilter) == null ? void 0 : _a2.check(resourceName, actionName))) {
98
93
  return next();
99
94
  }
100
95
  if (actionName === "update") {
101
- (0, import_hooks.handleUpdate)(ctx);
96
+ return await (0, import_hooks.handleUpdate)(ctx, next);
97
+ } else if (actionName === "create") {
98
+ return await (0, import_hooks.handleCreate)(ctx, next);
99
+ } else if (actionName === "destroy") {
100
+ return await (0, import_hooks.handleDestroy)(ctx, next);
102
101
  }
103
- if (actionName === "create") {
104
- (0, import_hooks.handleCreate)(ctx);
105
- }
106
- if (actionName === "destroy") {
107
- (0, import_hooks.handleDestroy)(ctx);
108
- }
109
- await next();
102
+ return next();
110
103
  },
111
104
  { tag: "apiLogs", after: "acl", before: "dataSource" }
112
105
  );
113
106
  }
114
107
  async load() {
108
+ if (import_node_worker_threads.isMainThread) {
109
+ this.addApiListener();
110
+ }
115
111
  this.app.acl.registerSnippet({
116
112
  name: `pm.system-services.apiLogsConfig`,
117
113
  actions: ["apiLogsConfig:*"]
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tachybase/plugin-api-logs",
3
3
  "displayName": "Api Logs",
4
- "version": "0.23.43",
4
+ "version": "0.23.54",
5
5
  "description": "Provides seamless tracking of API actions through enhanced user interface components, configuration panels, and dashboards",
6
6
  "keywords": [
7
7
  "Logging and monitoring"
@@ -15,16 +15,16 @@
15
15
  "react-router-dom": "6.28.1"
16
16
  },
17
17
  "peerDependencies": {
18
- "@tachybase/actions": "0.23.47",
19
- "@tachybase/client": "0.23.47",
20
- "@tachybase/components": "0.23.47",
21
- "@tachybase/data-source": "0.23.47",
22
- "@tachybase/database": "0.23.47",
23
- "@tachybase/schema": "0.23.47",
24
- "@tachybase/resourcer": "0.23.47",
25
- "@tachybase/server": "0.23.47",
26
- "@tachybase/utils": "0.23.47",
27
- "@tachybase/test": "0.23.47"
18
+ "@tachybase/actions": "0.23.58",
19
+ "@tachybase/components": "0.23.58",
20
+ "@tachybase/data-source": "0.23.58",
21
+ "@tachybase/database": "0.23.58",
22
+ "@tachybase/client": "0.23.58",
23
+ "@tachybase/resourcer": "0.23.58",
24
+ "@tachybase/schema": "0.23.58",
25
+ "@tachybase/test": "0.23.58",
26
+ "@tachybase/server": "0.23.58",
27
+ "@tachybase/utils": "0.23.58"
28
28
  },
29
29
  "description.zh-CN": "Api Logs通过增强的用户界面组件、配置面板和仪表板提供对 API 操作的无缝跟踪",
30
30
  "displayName.zh-CN": "Api 日志"