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