@lensjs/express 1.0.12 → 1.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.
package/dist/adapter.cjs CHANGED
@@ -58,6 +58,9 @@ var ExpressAdapter = class extends import_core.LensAdapter {
58
58
  case import_core.WatcherTypeEnum.QUERY:
59
59
  void this.watchQueries(watcher);
60
60
  break;
61
+ case import_core.WatcherTypeEnum.CACHE:
62
+ void this.watchCache(watcher);
63
+ break;
61
64
  }
62
65
  }
63
66
  }
@@ -90,6 +93,12 @@ var ExpressAdapter = class extends import_core.LensAdapter {
90
93
  return res.sendFile(path.join(uiPath, "index.html"));
91
94
  });
92
95
  }
96
+ async watchCache(watcher) {
97
+ if (!this.config.cacheWatcherEnabled) return;
98
+ import_core.lensEmitter.on("cache", async (data) => {
99
+ await watcher?.log(data);
100
+ });
101
+ }
93
102
  async watchQueries(watcher) {
94
103
  if (!this.config.queryWatcher.enabled) return;
95
104
  const handler = this.config.queryWatcher.handler;
@@ -98,7 +107,7 @@ var ExpressAdapter = class extends import_core.LensAdapter {
98
107
  const queryPayload = {
99
108
  query: query.query,
100
109
  duration: query.duration || "0 ms",
101
- createdAt: query.createdAt || (0, import_date.sqlDateTime)(),
110
+ createdAt: query.createdAt || (0, import_date.nowISO)(),
102
111
  type: query.type
103
112
  };
104
113
  await watcher?.log({
@@ -110,13 +119,18 @@ var ExpressAdapter = class extends import_core.LensAdapter {
110
119
  watchRequests(requestWatcher) {
111
120
  if (!this.config.requestWatcherEnabled) return;
112
121
  this.app.use((req, res, next) => {
113
- if (this.shouldIgnorePath(req.path)) return next();
114
- const start = process.hrtime();
115
- this.patchResponseMethods(res);
116
- res.on("finish", async () => {
117
- await this.finalizeRequestLog(req, res, requestWatcher, start);
122
+ const context = {
123
+ requestId: import_core.lensUtils.generateRandomUuid()
124
+ };
125
+ import_core.lensContext.run(context, () => {
126
+ if (this.shouldIgnorePath(req.path)) return next();
127
+ const start = process.hrtime();
128
+ this.patchResponseMethods(res);
129
+ res.on("finish", async () => {
130
+ await this.finalizeRequestLog(req, res, requestWatcher, start);
131
+ });
132
+ next();
118
133
  });
119
- next();
120
134
  });
121
135
  }
122
136
  patchResponseMethods(res) {
@@ -158,7 +172,7 @@ var ExpressAdapter = class extends import_core.LensAdapter {
158
172
  const duration = import_core.lensUtils.prettyHrTime(process.hrtime(start));
159
173
  const logPayload = {
160
174
  request: {
161
- id: import_core.lensUtils.generateRandomUuid(),
175
+ id: import_core.lensContext.getStore()?.requestId || import_core.lensUtils.generateRandomUuid(),
162
176
  method: req.method,
163
177
  duration,
164
178
  path: req.originalUrl,
@@ -13,6 +13,7 @@ declare class ExpressAdapter extends LensAdapter {
13
13
  setup(): void;
14
14
  registerRoutes(routes: RouteDefinition[]): void;
15
15
  serveUI(uiPath: string, spaRoute: string, _dataToInject: Record<string, any>): void;
16
+ private watchCache;
16
17
  private watchQueries;
17
18
  private watchRequests;
18
19
  private patchResponseMethods;
package/dist/adapter.d.ts CHANGED
@@ -13,6 +13,7 @@ declare class ExpressAdapter extends LensAdapter {
13
13
  setup(): void;
14
14
  registerRoutes(routes: RouteDefinition[]): void;
15
15
  serveUI(uiPath: string, spaRoute: string, _dataToInject: Record<string, any>): void;
16
+ private watchCache;
16
17
  private watchQueries;
17
18
  private watchRequests;
18
19
  private patchResponseMethods;
package/dist/adapter.js CHANGED
@@ -2,12 +2,14 @@
2
2
  import {
3
3
  LensAdapter,
4
4
  lensUtils,
5
- WatcherTypeEnum
5
+ WatcherTypeEnum,
6
+ lensContext,
7
+ lensEmitter
6
8
  } from "@lensjs/core";
7
9
  import * as path from "path";
8
10
  import fs from "fs";
9
11
  import express from "express";
10
- import { nowISO, sqlDateTime } from "@lensjs/date";
12
+ import { nowISO } from "@lensjs/date";
11
13
  var ExpressAdapter = class extends LensAdapter {
12
14
  app;
13
15
  config;
@@ -28,6 +30,9 @@ var ExpressAdapter = class extends LensAdapter {
28
30
  case WatcherTypeEnum.QUERY:
29
31
  void this.watchQueries(watcher);
30
32
  break;
33
+ case WatcherTypeEnum.CACHE:
34
+ void this.watchCache(watcher);
35
+ break;
31
36
  }
32
37
  }
33
38
  }
@@ -60,6 +65,12 @@ var ExpressAdapter = class extends LensAdapter {
60
65
  return res.sendFile(path.join(uiPath, "index.html"));
61
66
  });
62
67
  }
68
+ async watchCache(watcher) {
69
+ if (!this.config.cacheWatcherEnabled) return;
70
+ lensEmitter.on("cache", async (data) => {
71
+ await watcher?.log(data);
72
+ });
73
+ }
63
74
  async watchQueries(watcher) {
64
75
  if (!this.config.queryWatcher.enabled) return;
65
76
  const handler = this.config.queryWatcher.handler;
@@ -68,7 +79,7 @@ var ExpressAdapter = class extends LensAdapter {
68
79
  const queryPayload = {
69
80
  query: query.query,
70
81
  duration: query.duration || "0 ms",
71
- createdAt: query.createdAt || sqlDateTime(),
82
+ createdAt: query.createdAt || nowISO(),
72
83
  type: query.type
73
84
  };
74
85
  await watcher?.log({
@@ -80,13 +91,18 @@ var ExpressAdapter = class extends LensAdapter {
80
91
  watchRequests(requestWatcher) {
81
92
  if (!this.config.requestWatcherEnabled) return;
82
93
  this.app.use((req, res, next) => {
83
- if (this.shouldIgnorePath(req.path)) return next();
84
- const start = process.hrtime();
85
- this.patchResponseMethods(res);
86
- res.on("finish", async () => {
87
- await this.finalizeRequestLog(req, res, requestWatcher, start);
94
+ const context = {
95
+ requestId: lensUtils.generateRandomUuid()
96
+ };
97
+ lensContext.run(context, () => {
98
+ if (this.shouldIgnorePath(req.path)) return next();
99
+ const start = process.hrtime();
100
+ this.patchResponseMethods(res);
101
+ res.on("finish", async () => {
102
+ await this.finalizeRequestLog(req, res, requestWatcher, start);
103
+ });
104
+ next();
88
105
  });
89
- next();
90
106
  });
91
107
  }
92
108
  patchResponseMethods(res) {
@@ -128,7 +144,7 @@ var ExpressAdapter = class extends LensAdapter {
128
144
  const duration = lensUtils.prettyHrTime(process.hrtime(start));
129
145
  const logPayload = {
130
146
  request: {
131
- id: lensUtils.generateRandomUuid(),
147
+ id: lensContext.getStore()?.requestId || lensUtils.generateRandomUuid(),
132
148
  method: req.method,
133
149
  duration,
134
150
  path: req.originalUrl,
package/dist/index.cjs CHANGED
@@ -61,6 +61,9 @@ var ExpressAdapter = class extends import_core.LensAdapter {
61
61
  case import_core.WatcherTypeEnum.QUERY:
62
62
  void this.watchQueries(watcher);
63
63
  break;
64
+ case import_core.WatcherTypeEnum.CACHE:
65
+ void this.watchCache(watcher);
66
+ break;
64
67
  }
65
68
  }
66
69
  }
@@ -93,6 +96,12 @@ var ExpressAdapter = class extends import_core.LensAdapter {
93
96
  return res.sendFile(path.join(uiPath, "index.html"));
94
97
  });
95
98
  }
99
+ async watchCache(watcher) {
100
+ if (!this.config.cacheWatcherEnabled) return;
101
+ import_core.lensEmitter.on("cache", async (data) => {
102
+ await watcher?.log(data);
103
+ });
104
+ }
96
105
  async watchQueries(watcher) {
97
106
  if (!this.config.queryWatcher.enabled) return;
98
107
  const handler = this.config.queryWatcher.handler;
@@ -101,7 +110,7 @@ var ExpressAdapter = class extends import_core.LensAdapter {
101
110
  const queryPayload = {
102
111
  query: query.query,
103
112
  duration: query.duration || "0 ms",
104
- createdAt: query.createdAt || (0, import_date.sqlDateTime)(),
113
+ createdAt: query.createdAt || (0, import_date.nowISO)(),
105
114
  type: query.type
106
115
  };
107
116
  await watcher?.log({
@@ -113,13 +122,18 @@ var ExpressAdapter = class extends import_core.LensAdapter {
113
122
  watchRequests(requestWatcher) {
114
123
  if (!this.config.requestWatcherEnabled) return;
115
124
  this.app.use((req, res, next) => {
116
- if (this.shouldIgnorePath(req.path)) return next();
117
- const start = process.hrtime();
118
- this.patchResponseMethods(res);
119
- res.on("finish", async () => {
120
- await this.finalizeRequestLog(req, res, requestWatcher, start);
125
+ const context = {
126
+ requestId: import_core.lensUtils.generateRandomUuid()
127
+ };
128
+ import_core.lensContext.run(context, () => {
129
+ if (this.shouldIgnorePath(req.path)) return next();
130
+ const start = process.hrtime();
131
+ this.patchResponseMethods(res);
132
+ res.on("finish", async () => {
133
+ await this.finalizeRequestLog(req, res, requestWatcher, start);
134
+ });
135
+ next();
121
136
  });
122
- next();
123
137
  });
124
138
  }
125
139
  patchResponseMethods(res) {
@@ -161,7 +175,7 @@ var ExpressAdapter = class extends import_core.LensAdapter {
161
175
  const duration = import_core.lensUtils.prettyHrTime(process.hrtime(start));
162
176
  const logPayload = {
163
177
  request: {
164
- id: import_core.lensUtils.generateRandomUuid(),
178
+ id: import_core.lensContext.getStore()?.requestId || import_core.lensUtils.generateRandomUuid(),
165
179
  method: req.method,
166
180
  duration,
167
181
  path: req.originalUrl,
@@ -204,21 +218,36 @@ var defaultConfig = {
204
218
  path: "/lens",
205
219
  ignoredPaths: [],
206
220
  onlyPaths: [],
207
- requestWatcherEnabled: true
221
+ requestWatcherEnabled: true,
222
+ cacheWatcherEnabled: false
208
223
  };
209
224
  var lens = async (config) => {
210
225
  const adapter = new ExpressAdapter({ app: config.app });
211
226
  const watchers = [];
212
227
  const mergedConfig = {
213
- ...config,
214
- ...defaultConfig
228
+ ...defaultConfig,
229
+ ...config
215
230
  };
216
- if (mergedConfig.requestWatcherEnabled) {
217
- watchers.push(new import_core2.RequestWatcher());
218
- }
219
- if (mergedConfig.queryWatcher?.enabled) {
220
- watchers.push(new import_core2.QueryWatcher());
221
- }
231
+ const defaultWatchers = [
232
+ {
233
+ enabled: mergedConfig.requestWatcherEnabled,
234
+ watcher: new import_core2.RequestWatcher()
235
+ },
236
+ {
237
+ enabled: mergedConfig.cacheWatcherEnabled,
238
+ watcher: new import_core2.CacheWatcher()
239
+ },
240
+ {
241
+ enabled: mergedConfig.queryWatcher?.enabled,
242
+ watcher: new import_core2.QueryWatcher()
243
+ }
244
+ ];
245
+ defaultWatchers.forEach((watcher) => {
246
+ if (watcher.enabled) {
247
+ watchers.push(watcher.watcher);
248
+ }
249
+ });
250
+ console.log("currentWatchers", watchers);
222
251
  const { ignoredPaths, normalizedPath } = import_core2.lensUtils.prepareIgnoredPaths(
223
252
  mergedConfig.path,
224
253
  mergedConfig.ignoredPaths
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  import {
3
+ CacheWatcher as CacheWatcher2,
3
4
  Lens,
4
5
  lensUtils as lensUtils2,
5
6
  QueryWatcher as QueryWatcher2,
@@ -10,12 +11,14 @@ import {
10
11
  import {
11
12
  LensAdapter,
12
13
  lensUtils,
13
- WatcherTypeEnum
14
+ WatcherTypeEnum,
15
+ lensContext,
16
+ lensEmitter
14
17
  } from "@lensjs/core";
15
18
  import * as path from "path";
16
19
  import fs from "fs";
17
20
  import express from "express";
18
- import { nowISO, sqlDateTime } from "@lensjs/date";
21
+ import { nowISO } from "@lensjs/date";
19
22
  var ExpressAdapter = class extends LensAdapter {
20
23
  app;
21
24
  config;
@@ -36,6 +39,9 @@ var ExpressAdapter = class extends LensAdapter {
36
39
  case WatcherTypeEnum.QUERY:
37
40
  void this.watchQueries(watcher);
38
41
  break;
42
+ case WatcherTypeEnum.CACHE:
43
+ void this.watchCache(watcher);
44
+ break;
39
45
  }
40
46
  }
41
47
  }
@@ -68,6 +74,12 @@ var ExpressAdapter = class extends LensAdapter {
68
74
  return res.sendFile(path.join(uiPath, "index.html"));
69
75
  });
70
76
  }
77
+ async watchCache(watcher) {
78
+ if (!this.config.cacheWatcherEnabled) return;
79
+ lensEmitter.on("cache", async (data) => {
80
+ await watcher?.log(data);
81
+ });
82
+ }
71
83
  async watchQueries(watcher) {
72
84
  if (!this.config.queryWatcher.enabled) return;
73
85
  const handler = this.config.queryWatcher.handler;
@@ -76,7 +88,7 @@ var ExpressAdapter = class extends LensAdapter {
76
88
  const queryPayload = {
77
89
  query: query.query,
78
90
  duration: query.duration || "0 ms",
79
- createdAt: query.createdAt || sqlDateTime(),
91
+ createdAt: query.createdAt || nowISO(),
80
92
  type: query.type
81
93
  };
82
94
  await watcher?.log({
@@ -88,13 +100,18 @@ var ExpressAdapter = class extends LensAdapter {
88
100
  watchRequests(requestWatcher) {
89
101
  if (!this.config.requestWatcherEnabled) return;
90
102
  this.app.use((req, res, next) => {
91
- if (this.shouldIgnorePath(req.path)) return next();
92
- const start = process.hrtime();
93
- this.patchResponseMethods(res);
94
- res.on("finish", async () => {
95
- await this.finalizeRequestLog(req, res, requestWatcher, start);
103
+ const context = {
104
+ requestId: lensUtils.generateRandomUuid()
105
+ };
106
+ lensContext.run(context, () => {
107
+ if (this.shouldIgnorePath(req.path)) return next();
108
+ const start = process.hrtime();
109
+ this.patchResponseMethods(res);
110
+ res.on("finish", async () => {
111
+ await this.finalizeRequestLog(req, res, requestWatcher, start);
112
+ });
113
+ next();
96
114
  });
97
- next();
98
115
  });
99
116
  }
100
117
  patchResponseMethods(res) {
@@ -136,7 +153,7 @@ var ExpressAdapter = class extends LensAdapter {
136
153
  const duration = lensUtils.prettyHrTime(process.hrtime(start));
137
154
  const logPayload = {
138
155
  request: {
139
- id: lensUtils.generateRandomUuid(),
156
+ id: lensContext.getStore()?.requestId || lensUtils.generateRandomUuid(),
140
157
  method: req.method,
141
158
  duration,
142
159
  path: req.originalUrl,
@@ -179,21 +196,36 @@ var defaultConfig = {
179
196
  path: "/lens",
180
197
  ignoredPaths: [],
181
198
  onlyPaths: [],
182
- requestWatcherEnabled: true
199
+ requestWatcherEnabled: true,
200
+ cacheWatcherEnabled: false
183
201
  };
184
202
  var lens = async (config) => {
185
203
  const adapter = new ExpressAdapter({ app: config.app });
186
204
  const watchers = [];
187
205
  const mergedConfig = {
188
- ...config,
189
- ...defaultConfig
206
+ ...defaultConfig,
207
+ ...config
190
208
  };
191
- if (mergedConfig.requestWatcherEnabled) {
192
- watchers.push(new RequestWatcher2());
193
- }
194
- if (mergedConfig.queryWatcher?.enabled) {
195
- watchers.push(new QueryWatcher2());
196
- }
209
+ const defaultWatchers = [
210
+ {
211
+ enabled: mergedConfig.requestWatcherEnabled,
212
+ watcher: new RequestWatcher2()
213
+ },
214
+ {
215
+ enabled: mergedConfig.cacheWatcherEnabled,
216
+ watcher: new CacheWatcher2()
217
+ },
218
+ {
219
+ enabled: mergedConfig.queryWatcher?.enabled,
220
+ watcher: new QueryWatcher2()
221
+ }
222
+ ];
223
+ defaultWatchers.forEach((watcher) => {
224
+ if (watcher.enabled) {
225
+ watchers.push(watcher.watcher);
226
+ }
227
+ });
228
+ console.log("currentWatchers", watchers);
197
229
  const { ignoredPaths, normalizedPath } = lensUtils2.prepareIgnoredPaths(
198
230
  mergedConfig.path,
199
231
  mergedConfig.ignoredPaths
@@ -10,6 +10,7 @@ type ExpressAdapterConfig = {
10
10
  ignoredPaths?: RegExp[];
11
11
  onlyPaths?: RegExp[];
12
12
  requestWatcherEnabled?: boolean;
13
+ cacheWatcherEnabled?: boolean;
13
14
  queryWatcher?: {
14
15
  enabled: boolean;
15
16
  handler: QueryWatcherHandler;
@@ -10,6 +10,7 @@ type ExpressAdapterConfig = {
10
10
  ignoredPaths?: RegExp[];
11
11
  onlyPaths?: RegExp[];
12
12
  requestWatcherEnabled?: boolean;
13
+ cacheWatcherEnabled?: boolean;
13
14
  queryWatcher?: {
14
15
  enabled: boolean;
15
16
  handler: QueryWatcherHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lensjs/express",
3
- "version": "1.0.12",
3
+ "version": "1.1.0",
4
4
  "description": "Express adapter for LensJs",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "dependencies": {
21
21
  "express": "^5.1.0",
22
22
  "@lensjs/date": "1.0.12",
23
- "@lensjs/core": "1.0.12",
24
- "@lensjs/watchers": "1.0.12",
23
+ "@lensjs/watchers": "1.0.13",
24
+ "@lensjs/core": "2.0.0",
25
25
  "@lensjs/typescript-config": "1.0.12"
26
26
  },
27
27
  "devDependencies": {