@lensjs/express 1.0.11 → 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,
@@ -186,6 +200,10 @@ var ExpressAdapter = class extends import_core.LensAdapter {
186
200
  if (!body) {
187
201
  return null;
188
202
  }
189
- return JSON.parse(body);
203
+ try {
204
+ return JSON.parse(body);
205
+ } catch (_e) {
206
+ return body;
207
+ }
190
208
  }
191
209
  };
@@ -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,
@@ -156,7 +172,11 @@ var ExpressAdapter = class extends LensAdapter {
156
172
  if (!body) {
157
173
  return null;
158
174
  }
159
- return JSON.parse(body);
175
+ try {
176
+ return JSON.parse(body);
177
+ } catch (_e) {
178
+ return body;
179
+ }
160
180
  }
161
181
  };
162
182
  export {
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,
@@ -189,7 +203,11 @@ var ExpressAdapter = class extends import_core.LensAdapter {
189
203
  if (!body) {
190
204
  return null;
191
205
  }
192
- return JSON.parse(body);
206
+ try {
207
+ return JSON.parse(body);
208
+ } catch (_e) {
209
+ return body;
210
+ }
193
211
  }
194
212
  };
195
213
 
@@ -200,21 +218,36 @@ var defaultConfig = {
200
218
  path: "/lens",
201
219
  ignoredPaths: [],
202
220
  onlyPaths: [],
203
- requestWatcherEnabled: true
221
+ requestWatcherEnabled: true,
222
+ cacheWatcherEnabled: false
204
223
  };
205
224
  var lens = async (config) => {
206
225
  const adapter = new ExpressAdapter({ app: config.app });
207
226
  const watchers = [];
208
227
  const mergedConfig = {
209
- ...config,
210
- ...defaultConfig
228
+ ...defaultConfig,
229
+ ...config
211
230
  };
212
- if (mergedConfig.requestWatcherEnabled) {
213
- watchers.push(new import_core2.RequestWatcher());
214
- }
215
- if (mergedConfig.queryWatcher?.enabled) {
216
- watchers.push(new import_core2.QueryWatcher());
217
- }
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);
218
251
  const { ignoredPaths, normalizedPath } = import_core2.lensUtils.prepareIgnoredPaths(
219
252
  mergedConfig.path,
220
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,
@@ -164,7 +181,11 @@ var ExpressAdapter = class extends LensAdapter {
164
181
  if (!body) {
165
182
  return null;
166
183
  }
167
- return JSON.parse(body);
184
+ try {
185
+ return JSON.parse(body);
186
+ } catch (_e) {
187
+ return body;
188
+ }
168
189
  }
169
190
  };
170
191
 
@@ -175,21 +196,36 @@ var defaultConfig = {
175
196
  path: "/lens",
176
197
  ignoredPaths: [],
177
198
  onlyPaths: [],
178
- requestWatcherEnabled: true
199
+ requestWatcherEnabled: true,
200
+ cacheWatcherEnabled: false
179
201
  };
180
202
  var lens = async (config) => {
181
203
  const adapter = new ExpressAdapter({ app: config.app });
182
204
  const watchers = [];
183
205
  const mergedConfig = {
184
- ...config,
185
- ...defaultConfig
206
+ ...defaultConfig,
207
+ ...config
186
208
  };
187
- if (mergedConfig.requestWatcherEnabled) {
188
- watchers.push(new RequestWatcher2());
189
- }
190
- if (mergedConfig.queryWatcher?.enabled) {
191
- watchers.push(new QueryWatcher2());
192
- }
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);
193
229
  const { ignoredPaths, normalizedPath } = lensUtils2.prepareIgnoredPaths(
194
230
  mergedConfig.path,
195
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.11",
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",
@@ -19,10 +19,10 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "express": "^5.1.0",
22
- "@lensjs/watchers": "1.0.11",
23
- "@lensjs/core": "1.0.11",
24
- "@lensjs/typescript-config": "1.0.11",
25
- "@lensjs/date": "1.0.11"
22
+ "@lensjs/date": "1.0.12",
23
+ "@lensjs/watchers": "1.0.13",
24
+ "@lensjs/core": "2.0.0",
25
+ "@lensjs/typescript-config": "1.0.12"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/express": "^5.0.3"