@lensjs/core 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.
Files changed (85) hide show
  1. package/dist/abstracts/adapter.cjs +70 -5
  2. package/dist/abstracts/adapter.js +37 -5
  3. package/dist/abstracts/store.cjs +26 -6
  4. package/dist/abstracts/store.js +3 -4
  5. package/dist/context/container.cjs +57 -6
  6. package/dist/context/container.js +34 -4
  7. package/dist/context/context.cjs +71 -7
  8. package/dist/context/context.js +42 -6
  9. package/dist/core/api_controller.cjs +148 -7
  10. package/dist/core/api_controller.js +121 -7
  11. package/dist/core/lens.cjs +404 -12
  12. package/dist/core/lens.js +370 -12
  13. package/dist/core/watcher.cjs +26 -6
  14. package/dist/core/watcher.js +3 -4
  15. package/dist/index.cjs +611 -51
  16. package/dist/index.js +556 -38
  17. package/dist/stores/better_sqlite.cjs +149 -6
  18. package/dist/stores/better_sqlite.js +116 -6
  19. package/dist/stores/index.cjs +153 -7
  20. package/dist/stores/index.js +116 -7
  21. package/dist/types/index.cjs +33 -6
  22. package/dist/types/index.js +6 -4
  23. package/dist/ui/assets/QueriesContainer-CAAsjjW5.js +2 -0
  24. package/dist/ui/assets/{QueryDetailsContainer-CtNxjUvF.js → QueryDetailsContainer-E7P-IO7f.js} +3 -3
  25. package/dist/ui/assets/{QueryTable-B6L0F5jL.js → QueryTable-BcrtUssT.js} +1 -1
  26. package/dist/ui/assets/{RequestDetails-BfxFQmp1.js → RequestDetails-C2DZBu5H.js} +1 -1
  27. package/dist/ui/assets/{RequestDetailsContainer-C0s7VLpw.js → RequestDetailsContainer-DuDo-IqS.js} +2 -2
  28. package/dist/ui/assets/{RequestsContainer-rWbSc4Py.js → RequestsContainer-DK3hQVz9.js} +2 -2
  29. package/dist/ui/assets/{RequetsTable-D2qBWqFN.js → RequetsTable-DgP8p60b.js} +1 -1
  30. package/dist/ui/assets/{StatusCode-BZGwjlgz.js → StatusCode-FQEjz7gK.js} +1 -1
  31. package/dist/ui/assets/{TabbedDataViewer-CmztxF70.js → TabbedDataViewer-cBDdPwIz.js} +1 -1
  32. package/dist/ui/assets/{Table-DF4sxTNg.js → Table-DYaXk80S.js} +2 -2
  33. package/dist/ui/assets/favicon-CwRXoGYE.ico +0 -0
  34. package/dist/ui/assets/{index-Dzy6B_ZD.js → index-BS8XxorB.js} +11 -11
  35. package/dist/ui/assets/{index-byVaIWe8.js → index-CpP2Ap5X.js} +1 -1
  36. package/dist/ui/assets/{useLensApi-CkgX8mtY.js → useLensApi-DG6atd6d.js} +1 -1
  37. package/dist/ui/assets/{useLoadMore-BRca6vvh.js → useLoadMore-CJltToLI.js} +1 -1
  38. package/dist/ui/assets/{useQueries-CVt1cLYV.js → useQueries-C8mDDDc6.js} +1 -1
  39. package/dist/ui/favicon.ico +0 -0
  40. package/dist/ui/index.html +2 -2
  41. package/dist/utils/event_emitter.cjs +42 -6
  42. package/dist/utils/event_emitter.js +5 -4
  43. package/dist/utils/index.cjs +163 -23
  44. package/dist/utils/index.js +109 -12
  45. package/dist/watchers/index.cjs +105 -10
  46. package/dist/watchers/index.js +79 -12
  47. package/dist/watchers/query_watcher.cjs +78 -8
  48. package/dist/watchers/query_watcher.js +55 -8
  49. package/dist/watchers/request_watcher.cjs +90 -8
  50. package/dist/watchers/request_watcher.js +67 -8
  51. package/package.json +3 -3
  52. package/dist/chunk-2HRVJRKV.cjs +0 -10
  53. package/dist/chunk-45JBO7ME.cjs +0 -120
  54. package/dist/chunk-4HLDYZJA.js +0 -34
  55. package/dist/chunk-66RDYK2I.js +0 -9
  56. package/dist/chunk-75ZPJI57.cjs +0 -9
  57. package/dist/chunk-7EKM5HB5.cjs +0 -1
  58. package/dist/chunk-7FBZAVQ3.cjs +0 -138
  59. package/dist/chunk-7MOTQS6N.cjs +0 -32
  60. package/dist/chunk-BL4Z6JFH.cjs +0 -34
  61. package/dist/chunk-CGPET47D.cjs +0 -129
  62. package/dist/chunk-CQ2Z4TCR.js +0 -7
  63. package/dist/chunk-EHOWCXZV.cjs +0 -7
  64. package/dist/chunk-EKG3FVLV.cjs +0 -7
  65. package/dist/chunk-FKMBNWX4.js +0 -7
  66. package/dist/chunk-FLOVBBFU.js +0 -10
  67. package/dist/chunk-GI7SJDNQ.cjs +0 -38
  68. package/dist/chunk-HLBMQ3ID.js +0 -120
  69. package/dist/chunk-HMBQ2DF7.js +0 -129
  70. package/dist/chunk-JTRKNE5Q.js +0 -32
  71. package/dist/chunk-JZ2JLQXL.cjs +0 -87
  72. package/dist/chunk-MLKGABMK.js +0 -9
  73. package/dist/chunk-NUSVID2Q.cjs +0 -9
  74. package/dist/chunk-QRZUNYF6.cjs +0 -16
  75. package/dist/chunk-SJXZARBJ.cjs +0 -22
  76. package/dist/chunk-TJDE6AZM.js +0 -87
  77. package/dist/chunk-UF5HFNMI.js +0 -22
  78. package/dist/chunk-VD5IMUWL.js +0 -0
  79. package/dist/chunk-XXYRWHSQ.js +0 -0
  80. package/dist/chunk-XYXPHWEI.js +0 -16
  81. package/dist/chunk-XZFXXD3A.cjs +0 -1
  82. package/dist/chunk-YC6T4XWI.js +0 -38
  83. package/dist/chunk-YXSPADWE.js +0 -138
  84. package/dist/ui/assets/QueriesContainer-DhnU1ndx.js +0 -2
  85. package/dist/ui/assets/favicon-D0UnoCWN.ico +0 -0
package/dist/core/lens.js CHANGED
@@ -1,15 +1,373 @@
1
- import {
2
- Lens
3
- } from "../chunk-HMBQ2DF7.js";
4
- import "../chunk-VD5IMUWL.js";
5
- import "../chunk-HLBMQ3ID.js";
6
- import "../chunk-YXSPADWE.js";
7
- import "../chunk-FKMBNWX4.js";
8
- import "../chunk-TJDE6AZM.js";
9
- import "../chunk-FLOVBBFU.js";
10
- import "../chunk-XYXPHWEI.js";
11
- import "../chunk-YC6T4XWI.js";
12
- import "../chunk-MLKGABMK.js";
1
+ // src/context/container.ts
2
+ var Container = class {
3
+ static bindings = /* @__PURE__ */ new Map();
4
+ static singletons = /* @__PURE__ */ new Map();
5
+ static instances = /* @__PURE__ */ new Map();
6
+ static bind(key, factory) {
7
+ this.bindings.set(key, factory);
8
+ }
9
+ static singleton(key, factory) {
10
+ this.singletons.set(key, factory);
11
+ }
12
+ static make(key) {
13
+ if (this.instances.has(key)) {
14
+ return this.instances.get(key);
15
+ }
16
+ if (this.singletons.has(key)) {
17
+ const instance = this.singletons.get(key)();
18
+ this.instances.set(key, instance);
19
+ return instance;
20
+ }
21
+ if (this.bindings.has(key)) {
22
+ return this.bindings.get(key)();
23
+ }
24
+ throw new Error(`Service "${key}" is not bound in the container`);
25
+ }
26
+ static has(key) {
27
+ return this.bindings.has(key) || this.singletons.has(key) || this.instances.has(key);
28
+ }
29
+ static clear() {
30
+ this.bindings.clear();
31
+ this.singletons.clear();
32
+ this.instances.clear();
33
+ }
34
+ };
35
+
36
+ // src/context/context.ts
37
+ var getStore = () => {
38
+ return Container.make("store");
39
+ };
40
+ var getUiConfig = () => {
41
+ return Container.make("uiConfig");
42
+ };
43
+
44
+ // src/core/api_controller.ts
45
+ var ApiController = class {
46
+ static async getRequests({ qs }) {
47
+ return this.paginatedResponse(
48
+ await getStore().getAllRequests(this.extractPaginationParams(qs))
49
+ );
50
+ }
51
+ static async getRequest({
52
+ params
53
+ }) {
54
+ const request = await getStore().find("request" /* REQUEST */, params.id);
55
+ if (!request) {
56
+ return this.notFoundResponse();
57
+ }
58
+ return this.resourceResponse(request);
59
+ }
60
+ static async getQueries({
61
+ qs
62
+ }) {
63
+ const queries = await getStore().getAllQueries(
64
+ this.extractPaginationParams(qs)
65
+ );
66
+ return this.paginatedResponse(queries);
67
+ }
68
+ static async getQuery({
69
+ params
70
+ }) {
71
+ const query = await getStore().find("query" /* QUERY */, params.id);
72
+ if (!query) {
73
+ return this.notFoundResponse();
74
+ }
75
+ return this.resourceResponse(query);
76
+ }
77
+ static async truncate() {
78
+ await getStore().truncate();
79
+ return this.baseResponse({}, 200, "All entries cleared");
80
+ }
81
+ static fetchUiConfig() {
82
+ return getUiConfig();
83
+ }
84
+ static extractPaginationParams(qs) {
85
+ if (!qs || Object.keys(qs).length === 0) {
86
+ return { page: 1, perPage: 100 };
87
+ }
88
+ let page = Number(qs.page);
89
+ let perPage = Number(qs.perPage);
90
+ if (!Number.isInteger(perPage) || perPage > 100 || perPage < 5) {
91
+ perPage = 100;
92
+ }
93
+ if (!Number.isInteger(page) || page < 1) {
94
+ page = 1;
95
+ }
96
+ return { page, perPage };
97
+ }
98
+ static resourceResponse(data) {
99
+ return this.baseResponse(data, 200, "Data fetched successfully");
100
+ }
101
+ static notFoundResponse(message = "Could not find the requested resource") {
102
+ return this.baseResponse(null, 404, message);
103
+ }
104
+ static paginatedResponse(data) {
105
+ return this.baseResponse(data, 200, "Data fetched successfully");
106
+ }
107
+ static baseResponse(data, status, message) {
108
+ if (!data) {
109
+ return { status, message, data: null };
110
+ }
111
+ if ("meta" in data) {
112
+ return {
113
+ status,
114
+ message,
115
+ data: data.data,
116
+ meta: data.meta
117
+ };
118
+ }
119
+ return { status, message, data };
120
+ }
121
+ };
122
+
123
+ // src/core/lens.ts
124
+ import * as path2 from "path";
125
+
126
+ // src/abstracts/store.ts
127
+ var Store = class {
128
+ };
129
+
130
+ // src/stores/better_sqlite.ts
131
+ import { randomUUID } from "crypto";
132
+ import Database from "libsql";
133
+ import { sqlDateTime } from "@lensjs/date";
134
+ var TABLE_NAME = "lens_entries";
135
+ var BetterSqliteStore = class extends Store {
136
+ connection;
137
+ async initialize() {
138
+ this.connection = new Database("lens.db");
139
+ this.setupSchema();
140
+ console.log("Connected to Lens (SQLite) database.");
141
+ }
142
+ async truncate() {
143
+ this.connection.prepare(`DELETE FROM ${TABLE_NAME};`).run();
144
+ }
145
+ async save(entry) {
146
+ this.connection.prepare(
147
+ `INSERT INTO ${TABLE_NAME} (id, data, type, created_at, lens_entry_id, minimal_data) values($id, $data, $type, $created_at, $lens_entry_id, $minimalData)`
148
+ ).run({
149
+ id: entry.id ?? randomUUID(),
150
+ data: JSON.stringify(entry.data),
151
+ type: entry.type,
152
+ created_at: entry.timestamp ?? sqlDateTime(),
153
+ lens_entry_id: entry.requestId || null,
154
+ minimalData: JSON.stringify(entry.minimal_data ?? {})
155
+ });
156
+ }
157
+ async getAllQueries(pagination) {
158
+ return await this.paginate("query" /* QUERY */, pagination);
159
+ }
160
+ async getAllRequests(pagination) {
161
+ return await this.paginate("request" /* REQUEST */, pagination, false);
162
+ }
163
+ async allByRequestId(requestId, type) {
164
+ const rows = this.connection.prepare(
165
+ `${this.getSelectedColumns()} FROM ${TABLE_NAME} WHERE type = $type AND lens_entry_id = $requestId ORDER BY created_at DESC`
166
+ ).all({ type, requestId });
167
+ return this.mapRows(rows);
168
+ }
169
+ async paginate(type, { page, perPage }, includeFullData = true) {
170
+ const offset = (page - 1) * perPage;
171
+ const query = `${this.getSelectedColumns(
172
+ includeFullData
173
+ )} FROM ${TABLE_NAME} WHERE type = ? ORDER BY created_at DESC LIMIT ? OFFSET ?`;
174
+ const count = await this.count(type);
175
+ const rows = this.connection.prepare(query).all(type, perPage, offset);
176
+ const mappedRows = this.mapRows(rows, includeFullData);
177
+ return {
178
+ meta: {
179
+ total: count,
180
+ lastPage: Math.ceil(count / perPage),
181
+ currentPage: page
182
+ },
183
+ data: mappedRows
184
+ };
185
+ }
186
+ async count(type) {
187
+ const result = this.connection.prepare(`SELECT count(*) as count FROM ${TABLE_NAME} WHERE type = ?`).get(type);
188
+ return Number(result.count);
189
+ }
190
+ async find(type, id) {
191
+ const row = this.connection.prepare(
192
+ `${this.getSelectedColumns()} FROM ${TABLE_NAME} WHERE id = ? AND type = ? LIMIT 1`
193
+ ).get(id, type);
194
+ if (!row) {
195
+ return null;
196
+ }
197
+ return this.mapRow(row, true);
198
+ }
199
+ setupSchema() {
200
+ const createTable = `
201
+ CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
202
+ id TEXT PRIMARY KEY,
203
+ minimal_data TEXT,
204
+ data TEXT NOT NULL,
205
+ type TEXT NOT NULL,
206
+ created_at TEXT NOT NULL,
207
+ updated_at TEXT,
208
+ lens_entry_id TEXT NULL
209
+ );
210
+ `;
211
+ const createIndex = `
212
+ CREATE INDEX IF NOT EXISTS lens_entries_id_type_index
213
+ ON ${TABLE_NAME} (id, type);
214
+ `;
215
+ this.connection.exec(createTable);
216
+ this.connection.exec(createIndex);
217
+ }
218
+ mapRow(row, includeFullData = true) {
219
+ let data = includeFullData ? JSON.parse(row.data) : {};
220
+ if (!includeFullData) {
221
+ data = JSON.parse(row.minimal_data);
222
+ }
223
+ return {
224
+ id: row.id,
225
+ type: row.type,
226
+ created_at: row.created_at,
227
+ lens_entry_id: row.lens_entry_id,
228
+ data
229
+ };
230
+ }
231
+ mapRows(rows, includeFullData = true) {
232
+ let mappedRows = [];
233
+ for (const row of rows) {
234
+ mappedRows.push(this.mapRow(row, includeFullData));
235
+ }
236
+ return mappedRows;
237
+ }
238
+ getSelectedColumns(includeFullData = true) {
239
+ return `SELECT id, minimal_data, type, created_at, lens_entry_id ${includeFullData ? ",data" : ""}`;
240
+ }
241
+ };
242
+
243
+ // src/utils/index.ts
244
+ import { DateTime } from "luxon";
245
+ import { format } from "sql-formatter";
246
+ import { randomUUID as randomUUID2 } from "crypto";
247
+ import { fileURLToPath } from "url";
248
+ import * as path from "path";
249
+ function getMeta(metaUrl) {
250
+ const isESM = typeof __dirname === "undefined";
251
+ if (isESM) {
252
+ if (!metaUrl) {
253
+ throw new Error("In ESM, you must pass import.meta.url to getMeta()");
254
+ }
255
+ const __filename2 = fileURLToPath(metaUrl);
256
+ const __dirname2 = path.dirname(__filename2);
257
+ return { __filename: __filename2, __dirname: __dirname2 };
258
+ } else {
259
+ return { __filename, __dirname };
260
+ }
261
+ }
262
+
263
+ // src/core/lens.ts
264
+ var Lens = class {
265
+ static watchers = /* @__PURE__ */ new Map();
266
+ static store;
267
+ static adapter;
268
+ static watch(watcher) {
269
+ this.watchers.set(watcher.name, watcher);
270
+ return this;
271
+ }
272
+ static setWatchers(watchers) {
273
+ this.watchers = new Map(watchers.map((watcher) => [watcher.name, watcher]));
274
+ return this;
275
+ }
276
+ static async start(config = {
277
+ basePath: "lens",
278
+ appName: "Lens",
279
+ enabled: true
280
+ }) {
281
+ if (!config.enabled) {
282
+ return;
283
+ }
284
+ await this.bindContainerDeps(config);
285
+ let adapter = this.getAdapter();
286
+ adapter.setWatchers(Array.from(this.watchers.values())).setup();
287
+ const { apiRoutes } = this.getRoutes({
288
+ basePath: config.basePath
289
+ });
290
+ adapter.registerRoutes(apiRoutes);
291
+ const { __dirname: __dirname2 } = getMeta(import.meta.url);
292
+ const uiPath = path2.resolve(this.normalizeDirName(__dirname2), "ui");
293
+ adapter.serveUI(uiPath, config.basePath, getUiConfig());
294
+ }
295
+ static setStore(store) {
296
+ this.store = store;
297
+ return this;
298
+ }
299
+ static async getStore() {
300
+ return this.store ?? await this.getDefaultStore();
301
+ }
302
+ static setAdapter(adapter) {
303
+ this.adapter = adapter;
304
+ return this;
305
+ }
306
+ static getAdapter() {
307
+ if (!this.adapter) {
308
+ throw new Error("No adapter has been set");
309
+ }
310
+ return this.adapter;
311
+ }
312
+ static async bindContainerDeps(config) {
313
+ const dbStore = await this.getStore();
314
+ Container.singleton("store", () => dbStore);
315
+ Container.singleton("uiConfig", () => {
316
+ return {
317
+ appName: config.appName,
318
+ path: `/${config.basePath}`,
319
+ api: {
320
+ requests: `/${config.basePath}/api/requests`,
321
+ queries: `/${config.basePath}/api/queries`,
322
+ truncate: `/${config.basePath}/api/truncate`
323
+ }
324
+ };
325
+ });
326
+ }
327
+ static getRoutes({ basePath }) {
328
+ const apiRoutes = [
329
+ {
330
+ method: "GET",
331
+ path: `/lens-config`,
332
+ handler: () => ApiController.fetchUiConfig()
333
+ },
334
+ {
335
+ method: "GET",
336
+ path: `${basePath}/api/requests`,
337
+ handler: async (data) => await ApiController.getRequests(data)
338
+ },
339
+ {
340
+ method: "GET",
341
+ path: `${basePath}/api/requests/:id`,
342
+ handler: async (data) => await ApiController.getRequest(data)
343
+ },
344
+ {
345
+ method: "GET",
346
+ path: `${basePath}/api/queries`,
347
+ handler: async (data) => await ApiController.getQueries(data)
348
+ },
349
+ {
350
+ method: "GET",
351
+ path: `${basePath}/api/queries/:id`,
352
+ handler: async (data) => await ApiController.getQuery(data)
353
+ },
354
+ {
355
+ method: "DELETE",
356
+ path: `${basePath}/api/truncate`,
357
+ handler: async () => await ApiController.truncate()
358
+ }
359
+ ];
360
+ return { apiRoutes };
361
+ }
362
+ static async getDefaultStore() {
363
+ const store = new BetterSqliteStore();
364
+ await store.initialize();
365
+ return store;
366
+ }
367
+ static normalizeDirName(path3) {
368
+ return path3.replace(/(\/packages\/)[^/]+(?=\/dist)/, "$1core");
369
+ }
370
+ };
13
371
  export {
14
372
  Lens as default
15
373
  };
@@ -1,7 +1,27 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
19
 
3
- var _chunkEKG3FVLVcjs = require('../chunk-EKG3FVLV.cjs');
4
- require('../chunk-75ZPJI57.cjs');
5
-
6
-
7
- exports.default = _chunkEKG3FVLVcjs.Watcher;
20
+ // src/core/watcher.ts
21
+ var watcher_exports = {};
22
+ __export(watcher_exports, {
23
+ default: () => Watcher
24
+ });
25
+ module.exports = __toCommonJS(watcher_exports);
26
+ var Watcher = class {
27
+ };
@@ -1,7 +1,6 @@
1
- import {
2
- Watcher
3
- } from "../chunk-CQ2Z4TCR.js";
4
- import "../chunk-MLKGABMK.js";
1
+ // src/core/watcher.ts
2
+ var Watcher = class {
3
+ };
5
4
  export {
6
5
  Watcher as default
7
6
  };