@lensjs/express 1.0.8 → 1.0.11

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
@@ -26,17 +26,19 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/adapter.ts
29
31
  var adapter_exports = {};
30
32
  __export(adapter_exports, {
31
33
  default: () => ExpressAdapter
32
34
  });
33
35
  module.exports = __toCommonJS(adapter_exports);
34
36
  var import_core = require("@lensjs/core");
35
- var path = __toESM(require("node:path"), 1);
36
- var import_node_fs = __toESM(require("node:fs"), 1);
37
- var import_express2 = __toESM(require("express"), 1);
38
- var import_date = require("@repo/date");
39
- class ExpressAdapter extends import_core.LensAdapter {
37
+ var path = __toESM(require("path"), 1);
38
+ var import_node_fs = __toESM(require("fs"), 1);
39
+ var import_express = __toESM(require("express"), 1);
40
+ var import_date = require("@lensjs/date");
41
+ var ExpressAdapter = class extends import_core.LensAdapter {
40
42
  app;
41
43
  config;
42
44
  constructor({ app }) {
@@ -74,10 +76,10 @@ class ExpressAdapter extends import_core.LensAdapter {
74
76
  });
75
77
  }
76
78
  serveUI(uiPath, spaRoute, _dataToInject) {
77
- this.app.use(this.normalizePath(spaRoute), import_express2.default.static(uiPath));
79
+ this.app.use(this.normalizePath(spaRoute), import_express.default.static(uiPath));
78
80
  this.app.get(
79
- this.normalizePath(`${spaRoute}/favicon.svg`),
80
- (_, res) => res.sendFile(path.join(uiPath, "favicon.svg"))
81
+ this.normalizePath(`${spaRoute}/favicon.ico`),
82
+ (_, res) => res.sendFile(path.join(uiPath, "favicon.ico"))
81
83
  );
82
84
  this.app.get(new RegExp(`^/${spaRoute}(?!/api)(/.*)?$`), (req, res) => {
83
85
  if (import_core.lensUtils.isStaticFile(req.path.split("/"))) {
@@ -167,7 +169,7 @@ class ExpressAdapter extends import_core.LensAdapter {
167
169
  createdAt: (0, import_date.nowISO)()
168
170
  },
169
171
  response: {
170
- json: res._body ?? null,
172
+ json: this.parseBody(res._body),
171
173
  headers: res.getHeaders?.()
172
174
  },
173
175
  user: await this.config.isAuthenticated?.(req) ? await this.config.getUser?.(req) : null
@@ -180,4 +182,10 @@ class ExpressAdapter extends import_core.LensAdapter {
180
182
  normalizePath(pathStr) {
181
183
  return pathStr.startsWith("/") ? pathStr : `/${pathStr}`;
182
184
  }
183
- }
185
+ parseBody(body) {
186
+ if (!body) {
187
+ return null;
188
+ }
189
+ return JSON.parse(body);
190
+ }
191
+ };
@@ -18,6 +18,7 @@ declare class ExpressAdapter extends LensAdapter {
18
18
  private patchResponseMethods;
19
19
  private finalizeRequestLog;
20
20
  private normalizePath;
21
+ private parseBody;
21
22
  }
22
23
 
23
24
  export { ExpressAdapter as default };
package/dist/adapter.d.ts CHANGED
@@ -18,6 +18,7 @@ declare class ExpressAdapter extends LensAdapter {
18
18
  private patchResponseMethods;
19
19
  private finalizeRequestLog;
20
20
  private normalizePath;
21
+ private parseBody;
21
22
  }
22
23
 
23
24
  export { ExpressAdapter as default };
package/dist/adapter.js CHANGED
@@ -1,13 +1,14 @@
1
+ // src/adapter.ts
1
2
  import {
2
3
  LensAdapter,
3
4
  lensUtils,
4
5
  WatcherTypeEnum
5
6
  } from "@lensjs/core";
6
- import * as path from "node:path";
7
- import fs from "node:fs";
7
+ import * as path from "path";
8
+ import fs from "fs";
8
9
  import express from "express";
9
- import { nowISO, sqlDateTime } from "@repo/date";
10
- class ExpressAdapter extends LensAdapter {
10
+ import { nowISO, sqlDateTime } from "@lensjs/date";
11
+ var ExpressAdapter = class extends LensAdapter {
11
12
  app;
12
13
  config;
13
14
  constructor({ app }) {
@@ -47,8 +48,8 @@ class ExpressAdapter extends LensAdapter {
47
48
  serveUI(uiPath, spaRoute, _dataToInject) {
48
49
  this.app.use(this.normalizePath(spaRoute), express.static(uiPath));
49
50
  this.app.get(
50
- this.normalizePath(`${spaRoute}/favicon.svg`),
51
- (_, res) => res.sendFile(path.join(uiPath, "favicon.svg"))
51
+ this.normalizePath(`${spaRoute}/favicon.ico`),
52
+ (_, res) => res.sendFile(path.join(uiPath, "favicon.ico"))
52
53
  );
53
54
  this.app.get(new RegExp(`^/${spaRoute}(?!/api)(/.*)?$`), (req, res) => {
54
55
  if (lensUtils.isStaticFile(req.path.split("/"))) {
@@ -138,7 +139,7 @@ class ExpressAdapter extends LensAdapter {
138
139
  createdAt: nowISO()
139
140
  },
140
141
  response: {
141
- json: res._body ?? null,
142
+ json: this.parseBody(res._body),
142
143
  headers: res.getHeaders?.()
143
144
  },
144
145
  user: await this.config.isAuthenticated?.(req) ? await this.config.getUser?.(req) : null
@@ -151,7 +152,13 @@ class ExpressAdapter extends LensAdapter {
151
152
  normalizePath(pathStr) {
152
153
  return pathStr.startsWith("/") ? pathStr : `/${pathStr}`;
153
154
  }
154
- }
155
+ parseBody(body) {
156
+ if (!body) {
157
+ return null;
158
+ }
159
+ return JSON.parse(body);
160
+ }
161
+ };
155
162
  export {
156
163
  ExpressAdapter as default
157
164
  };
package/dist/index.cjs CHANGED
@@ -26,14 +26,175 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
29
31
  var index_exports = {};
30
32
  __export(index_exports, {
31
33
  lens: () => lens
32
34
  });
33
35
  module.exports = __toCommonJS(index_exports);
36
+ var import_core2 = require("@lensjs/core");
37
+
38
+ // src/adapter.ts
34
39
  var import_core = require("@lensjs/core");
35
- var import_adapter = __toESM(require("./adapter"), 1);
36
- const defaultConfig = {
40
+ var path = __toESM(require("path"), 1);
41
+ var import_node_fs = __toESM(require("fs"), 1);
42
+ var import_express = __toESM(require("express"), 1);
43
+ var import_date = require("@lensjs/date");
44
+ var ExpressAdapter = class extends import_core.LensAdapter {
45
+ app;
46
+ config;
47
+ constructor({ app }) {
48
+ super();
49
+ this.app = app;
50
+ }
51
+ setConfig(config) {
52
+ this.config = config;
53
+ return this;
54
+ }
55
+ setup() {
56
+ for (const watcher of this.getWatchers()) {
57
+ switch (watcher.name) {
58
+ case import_core.WatcherTypeEnum.REQUEST:
59
+ this.watchRequests(watcher);
60
+ break;
61
+ case import_core.WatcherTypeEnum.QUERY:
62
+ void this.watchQueries(watcher);
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ registerRoutes(routes) {
68
+ routes.forEach((route) => {
69
+ this.app[route.method.toLowerCase()](
70
+ this.normalizePath(route.path),
71
+ async (req, res) => {
72
+ const result = await route.handler({
73
+ params: req.params,
74
+ qs: req.query
75
+ });
76
+ return res.json(result);
77
+ }
78
+ );
79
+ });
80
+ }
81
+ serveUI(uiPath, spaRoute, _dataToInject) {
82
+ this.app.use(this.normalizePath(spaRoute), import_express.default.static(uiPath));
83
+ this.app.get(
84
+ this.normalizePath(`${spaRoute}/favicon.ico`),
85
+ (_, res) => res.sendFile(path.join(uiPath, "favicon.ico"))
86
+ );
87
+ this.app.get(new RegExp(`^/${spaRoute}(?!/api)(/.*)?$`), (req, res) => {
88
+ if (import_core.lensUtils.isStaticFile(req.path.split("/"))) {
89
+ return res.download(
90
+ path.join(uiPath, import_core.lensUtils.stripBeforeAssetsPath(req.path))
91
+ );
92
+ }
93
+ return res.sendFile(path.join(uiPath, "index.html"));
94
+ });
95
+ }
96
+ async watchQueries(watcher) {
97
+ if (!this.config.queryWatcher.enabled) return;
98
+ const handler = this.config.queryWatcher.handler;
99
+ await handler({
100
+ onQuery: async (query) => {
101
+ const queryPayload = {
102
+ query: query.query,
103
+ duration: query.duration || "0 ms",
104
+ createdAt: query.createdAt || (0, import_date.sqlDateTime)(),
105
+ type: query.type
106
+ };
107
+ await watcher?.log({
108
+ data: queryPayload
109
+ });
110
+ }
111
+ });
112
+ }
113
+ watchRequests(requestWatcher) {
114
+ if (!this.config.requestWatcherEnabled) return;
115
+ 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);
121
+ });
122
+ next();
123
+ });
124
+ }
125
+ patchResponseMethods(res) {
126
+ const originalJson = res.json.bind(res);
127
+ const originalSend = res.send.bind(res);
128
+ res._body = void 0;
129
+ res.json = function(body) {
130
+ res._body = body;
131
+ return originalJson(body);
132
+ };
133
+ res.send = function(body) {
134
+ let safeBody;
135
+ try {
136
+ if (!body) {
137
+ safeBody = "Purged By Lens";
138
+ } else if (typeof body === "object" && !Buffer.isBuffer(body)) {
139
+ safeBody = body;
140
+ } else if (typeof body === "string") {
141
+ const filePath = path.resolve(body);
142
+ if (import_node_fs.default.existsSync(filePath)) {
143
+ safeBody = "Purged By Lens";
144
+ } else {
145
+ safeBody = body;
146
+ }
147
+ } else if (Buffer.isBuffer(body)) {
148
+ safeBody = "Purged By Lens";
149
+ } else {
150
+ safeBody = "Purged By Lens";
151
+ }
152
+ } catch {
153
+ safeBody = "Purged By Lens";
154
+ }
155
+ res._body = safeBody;
156
+ return originalSend(safeBody);
157
+ };
158
+ }
159
+ async finalizeRequestLog(req, res, requestWatcher, start) {
160
+ try {
161
+ const duration = import_core.lensUtils.prettyHrTime(process.hrtime(start));
162
+ const logPayload = {
163
+ request: {
164
+ id: import_core.lensUtils.generateRandomUuid(),
165
+ method: req.method,
166
+ duration,
167
+ path: req.originalUrl,
168
+ headers: req.headers,
169
+ body: req.body ?? {},
170
+ status: res.statusCode,
171
+ ip: req.socket?.remoteAddress ?? "",
172
+ createdAt: (0, import_date.nowISO)()
173
+ },
174
+ response: {
175
+ json: this.parseBody(res._body),
176
+ headers: res.getHeaders?.()
177
+ },
178
+ user: await this.config.isAuthenticated?.(req) ? await this.config.getUser?.(req) : null
179
+ };
180
+ await requestWatcher.log(logPayload);
181
+ } catch (err) {
182
+ console.error("Error finalizing request log:", err);
183
+ }
184
+ }
185
+ normalizePath(pathStr) {
186
+ return pathStr.startsWith("/") ? pathStr : `/${pathStr}`;
187
+ }
188
+ parseBody(body) {
189
+ if (!body) {
190
+ return null;
191
+ }
192
+ return JSON.parse(body);
193
+ }
194
+ };
195
+
196
+ // src/index.ts
197
+ var defaultConfig = {
37
198
  appName: "Lens",
38
199
  enabled: true,
39
200
  path: "/lens",
@@ -41,25 +202,25 @@ const defaultConfig = {
41
202
  onlyPaths: [],
42
203
  requestWatcherEnabled: true
43
204
  };
44
- const lens = async (config) => {
45
- const adapter = new import_adapter.default({ app: config.app });
205
+ var lens = async (config) => {
206
+ const adapter = new ExpressAdapter({ app: config.app });
46
207
  const watchers = [];
47
208
  const mergedConfig = {
48
209
  ...config,
49
210
  ...defaultConfig
50
211
  };
51
212
  if (mergedConfig.requestWatcherEnabled) {
52
- watchers.push(new import_core.RequestWatcher());
213
+ watchers.push(new import_core2.RequestWatcher());
53
214
  }
54
215
  if (mergedConfig.queryWatcher?.enabled) {
55
- watchers.push(new import_core.QueryWatcher());
216
+ watchers.push(new import_core2.QueryWatcher());
56
217
  }
57
- const { ignoredPaths, normalizedPath } = import_core.lensUtils.prepareIgnoredPaths(
218
+ const { ignoredPaths, normalizedPath } = import_core2.lensUtils.prepareIgnoredPaths(
58
219
  mergedConfig.path,
59
220
  mergedConfig.ignoredPaths
60
221
  );
61
222
  adapter.setConfig(mergedConfig).setIgnoredPaths(ignoredPaths).setOnlyPaths(mergedConfig.onlyPaths);
62
- await import_core.Lens.setAdapter(adapter).setWatchers(watchers).start({
223
+ await import_core2.Lens.setAdapter(adapter).setWatchers(watchers).start({
63
224
  appName: mergedConfig.appName,
64
225
  enabled: mergedConfig.enabled,
65
226
  basePath: normalizedPath
package/dist/index.js CHANGED
@@ -1,11 +1,175 @@
1
+ // src/index.ts
1
2
  import {
2
3
  Lens,
4
+ lensUtils as lensUtils2,
5
+ QueryWatcher as QueryWatcher2,
6
+ RequestWatcher as RequestWatcher2
7
+ } from "@lensjs/core";
8
+
9
+ // src/adapter.ts
10
+ import {
11
+ LensAdapter,
3
12
  lensUtils,
4
- QueryWatcher,
5
- RequestWatcher
13
+ WatcherTypeEnum
6
14
  } from "@lensjs/core";
7
- import ExpressAdapter from "./adapter";
8
- const defaultConfig = {
15
+ import * as path from "path";
16
+ import fs from "fs";
17
+ import express from "express";
18
+ import { nowISO, sqlDateTime } from "@lensjs/date";
19
+ var ExpressAdapter = class extends LensAdapter {
20
+ app;
21
+ config;
22
+ constructor({ app }) {
23
+ super();
24
+ this.app = app;
25
+ }
26
+ setConfig(config) {
27
+ this.config = config;
28
+ return this;
29
+ }
30
+ setup() {
31
+ for (const watcher of this.getWatchers()) {
32
+ switch (watcher.name) {
33
+ case WatcherTypeEnum.REQUEST:
34
+ this.watchRequests(watcher);
35
+ break;
36
+ case WatcherTypeEnum.QUERY:
37
+ void this.watchQueries(watcher);
38
+ break;
39
+ }
40
+ }
41
+ }
42
+ registerRoutes(routes) {
43
+ routes.forEach((route) => {
44
+ this.app[route.method.toLowerCase()](
45
+ this.normalizePath(route.path),
46
+ async (req, res) => {
47
+ const result = await route.handler({
48
+ params: req.params,
49
+ qs: req.query
50
+ });
51
+ return res.json(result);
52
+ }
53
+ );
54
+ });
55
+ }
56
+ serveUI(uiPath, spaRoute, _dataToInject) {
57
+ this.app.use(this.normalizePath(spaRoute), express.static(uiPath));
58
+ this.app.get(
59
+ this.normalizePath(`${spaRoute}/favicon.ico`),
60
+ (_, res) => res.sendFile(path.join(uiPath, "favicon.ico"))
61
+ );
62
+ this.app.get(new RegExp(`^/${spaRoute}(?!/api)(/.*)?$`), (req, res) => {
63
+ if (lensUtils.isStaticFile(req.path.split("/"))) {
64
+ return res.download(
65
+ path.join(uiPath, lensUtils.stripBeforeAssetsPath(req.path))
66
+ );
67
+ }
68
+ return res.sendFile(path.join(uiPath, "index.html"));
69
+ });
70
+ }
71
+ async watchQueries(watcher) {
72
+ if (!this.config.queryWatcher.enabled) return;
73
+ const handler = this.config.queryWatcher.handler;
74
+ await handler({
75
+ onQuery: async (query) => {
76
+ const queryPayload = {
77
+ query: query.query,
78
+ duration: query.duration || "0 ms",
79
+ createdAt: query.createdAt || sqlDateTime(),
80
+ type: query.type
81
+ };
82
+ await watcher?.log({
83
+ data: queryPayload
84
+ });
85
+ }
86
+ });
87
+ }
88
+ watchRequests(requestWatcher) {
89
+ if (!this.config.requestWatcherEnabled) return;
90
+ 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);
96
+ });
97
+ next();
98
+ });
99
+ }
100
+ patchResponseMethods(res) {
101
+ const originalJson = res.json.bind(res);
102
+ const originalSend = res.send.bind(res);
103
+ res._body = void 0;
104
+ res.json = function(body) {
105
+ res._body = body;
106
+ return originalJson(body);
107
+ };
108
+ res.send = function(body) {
109
+ let safeBody;
110
+ try {
111
+ if (!body) {
112
+ safeBody = "Purged By Lens";
113
+ } else if (typeof body === "object" && !Buffer.isBuffer(body)) {
114
+ safeBody = body;
115
+ } else if (typeof body === "string") {
116
+ const filePath = path.resolve(body);
117
+ if (fs.existsSync(filePath)) {
118
+ safeBody = "Purged By Lens";
119
+ } else {
120
+ safeBody = body;
121
+ }
122
+ } else if (Buffer.isBuffer(body)) {
123
+ safeBody = "Purged By Lens";
124
+ } else {
125
+ safeBody = "Purged By Lens";
126
+ }
127
+ } catch {
128
+ safeBody = "Purged By Lens";
129
+ }
130
+ res._body = safeBody;
131
+ return originalSend(safeBody);
132
+ };
133
+ }
134
+ async finalizeRequestLog(req, res, requestWatcher, start) {
135
+ try {
136
+ const duration = lensUtils.prettyHrTime(process.hrtime(start));
137
+ const logPayload = {
138
+ request: {
139
+ id: lensUtils.generateRandomUuid(),
140
+ method: req.method,
141
+ duration,
142
+ path: req.originalUrl,
143
+ headers: req.headers,
144
+ body: req.body ?? {},
145
+ status: res.statusCode,
146
+ ip: req.socket?.remoteAddress ?? "",
147
+ createdAt: nowISO()
148
+ },
149
+ response: {
150
+ json: this.parseBody(res._body),
151
+ headers: res.getHeaders?.()
152
+ },
153
+ user: await this.config.isAuthenticated?.(req) ? await this.config.getUser?.(req) : null
154
+ };
155
+ await requestWatcher.log(logPayload);
156
+ } catch (err) {
157
+ console.error("Error finalizing request log:", err);
158
+ }
159
+ }
160
+ normalizePath(pathStr) {
161
+ return pathStr.startsWith("/") ? pathStr : `/${pathStr}`;
162
+ }
163
+ parseBody(body) {
164
+ if (!body) {
165
+ return null;
166
+ }
167
+ return JSON.parse(body);
168
+ }
169
+ };
170
+
171
+ // src/index.ts
172
+ var defaultConfig = {
9
173
  appName: "Lens",
10
174
  enabled: true,
11
175
  path: "/lens",
@@ -13,7 +177,7 @@ const defaultConfig = {
13
177
  onlyPaths: [],
14
178
  requestWatcherEnabled: true
15
179
  };
16
- const lens = async (config) => {
180
+ var lens = async (config) => {
17
181
  const adapter = new ExpressAdapter({ app: config.app });
18
182
  const watchers = [];
19
183
  const mergedConfig = {
@@ -21,12 +185,12 @@ const lens = async (config) => {
21
185
  ...defaultConfig
22
186
  };
23
187
  if (mergedConfig.requestWatcherEnabled) {
24
- watchers.push(new RequestWatcher());
188
+ watchers.push(new RequestWatcher2());
25
189
  }
26
190
  if (mergedConfig.queryWatcher?.enabled) {
27
- watchers.push(new QueryWatcher());
191
+ watchers.push(new QueryWatcher2());
28
192
  }
29
- const { ignoredPaths, normalizedPath } = lensUtils.prepareIgnoredPaths(
193
+ const { ignoredPaths, normalizedPath } = lensUtils2.prepareIgnoredPaths(
30
194
  mergedConfig.path,
31
195
  mergedConfig.ignoredPaths
32
196
  );
@@ -12,5 +12,7 @@ var __copyProps = (to, from, except, desc) => {
12
12
  return to;
13
13
  };
14
14
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/types/index.ts
15
17
  var types_exports = {};
16
18
  module.exports = __toCommonJS(types_exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lensjs/express",
3
- "version": "1.0.8",
3
+ "version": "1.0.11",
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
- "@repo/date": "0.0.1",
23
- "@repo/typescript-config": "0.0.1",
24
- "@lensjs/core": "1.0.8",
25
- "@lensjs/watchers": "1.0.8"
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"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/express": "^5.0.3"