@tezx/devtools 1.0.9 → 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/html/env.js ADDED
@@ -0,0 +1,165 @@
1
+ export function EnvInspector(ctx) {
2
+ const env = ctx.env;
3
+ const entries = Object.entries(env);
4
+ const tableRows = entries.length
5
+ ? entries
6
+ .map(([key, value], i) => {
7
+ const safeKey = key.replace(/"/g, """);
8
+ const safeValue = value.replace(/`/g, "\\`").replace(/"/g, """);
9
+ return `
10
+ <tr data-key="${safeKey.toLowerCase()}" data-value="${value.toLowerCase()}">
11
+ <td>${i + 1}</td>
12
+ <td><code>${safeKey}</code></td>
13
+ <td><code>${value}</code></td>
14
+ <td>
15
+ <button class="copy-btn" onclick="copyRowEnv('${safeKey}', \`${safeValue}\`)" title="Copy">
16
+ <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
17
+ <path d="M10 1.5A1.5 1.5 0 0 1 11.5 3v1h-1V3a.5.5 0 0 0-.5-.5H4A1.5 1.5 0 0 0 2.5 4v8A1.5 1.5 0 0 0 4 13.5H5v1H4A2.5 2.5 0 0 1 1.5 12V4A2.5 2.5 0 0 1 4 1.5h6z"/>
18
+ <path d="M5.5 5A1.5 1.5 0 0 0 4 6.5v7A1.5 1.5 0 0 0 5.5 15h6A1.5 1.5 0 0 0 13 13.5v-7A1.5 1.5 0 0 0 11.5 5h-6zM5 6.5A.5.5 0 0 1 5.5 6h6a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-.5.5h-6a.5.5 0 0 1-.5-.5v-7z"/>
19
+ </svg>
20
+ </button>
21
+ </td>
22
+ </tr>`;
23
+ })
24
+ .join("")
25
+ : `
26
+ <tr>
27
+ <td colspan="4">
28
+ <p>⚠️ <strong>No environment variables found.</strong></p>
29
+ <pre class="code-block">
30
+ const env = loadEnv();
31
+
32
+ export const app = new TezX({
33
+ env: env,
34
+ debugMode: true,
35
+ allowDuplicateMw: true,
36
+ });
37
+ </pre>
38
+ </td>
39
+ </tr>`;
40
+ return `
41
+ <style>
42
+ .tabs {
43
+ margin-bottom: 1rem;
44
+ }
45
+ .tabs a {
46
+ display: inline-block;
47
+ margin-right: 1rem;
48
+ text-decoration: none;
49
+ font-weight: 500;
50
+ color: #0f172a;
51
+ cursor: pointer;
52
+ }
53
+
54
+ table {
55
+ width: 100%;
56
+ border-collapse: collapse;
57
+ }
58
+
59
+ table th, table td {
60
+ padding: 0.5rem;
61
+ border: 1px solid #e2e8f0;
62
+ text-align: left;
63
+ }
64
+
65
+ table td button.copy-btn {
66
+ background: #f8fafc;
67
+ border: none;
68
+ padding: 0.3rem 0.4rem;
69
+ border-radius: 0.375rem;
70
+ cursor: pointer;
71
+ color: #334155;
72
+ transition: background 0.2s ease;
73
+ }
74
+
75
+ .search-container {
76
+ margin-bottom: 1rem;
77
+ }
78
+
79
+ .search-container input {
80
+ padding: 0.5rem;
81
+ width: 100%;
82
+ max-width: 400px;
83
+ border: 1px solid #cbd5e1;
84
+ border-radius: 0.375rem;
85
+ }
86
+
87
+ .code-block {
88
+ background: #f1f5f9;
89
+ padding: 1rem;
90
+ border-radius: 0.5rem;
91
+ font-size: 0.875rem;
92
+ }
93
+
94
+ </style>
95
+
96
+ <div class="tabs">
97
+ <a onclick="exportEnv()">📤 Export JSON</a>
98
+ <a onclick="copyAllEnv()">📋 Copy All</a>
99
+ </div>
100
+
101
+ <div class="search-container">
102
+ <input type="text" id="env-search" placeholder="🔍 Search environment variables..." oninput="filterEnvTable()" />
103
+ </div>
104
+
105
+ <div class="table-container">
106
+ <table>
107
+ <thead>
108
+ <tr>
109
+ <th>#</th>
110
+ <th>Key</th>
111
+ <th>Value</th>
112
+ <th>Copy</th>
113
+ </tr>
114
+ </thead>
115
+ <tbody id="env-body">
116
+ ${tableRows}
117
+ </tbody>
118
+ </table>
119
+ </div>
120
+
121
+ <pre id="json-output">${JSON.stringify(env, null, 2)}</pre>
122
+
123
+ <script>
124
+ function exportEnv() {
125
+ const output = document.getElementById('json-output');
126
+ const blob = new Blob([output.textContent], { type: 'application/json' });
127
+ const url = URL.createObjectURL(blob);
128
+ const a = document.createElement('a');
129
+ a.href = url;
130
+ a.download = 'env.json';
131
+ a.click();
132
+ URL.revokeObjectURL(url);
133
+ }
134
+
135
+ function copyAllEnv() {
136
+ const output = document.getElementById('json-output');
137
+ navigator.clipboard.writeText(output.textContent).then(() => {
138
+ showToast('✅ All environment variables copied!');
139
+ }).catch(err => {
140
+ alert('❌ Failed to copy: ' + err);
141
+ });
142
+ }
143
+
144
+ function copyRowEnv(key, value) {
145
+ const text = \`\${key}=\${value}\`;
146
+ navigator.clipboard.writeText(text).then(() => {
147
+ showToast(\`✅ Copied: \${key}\`);
148
+ }).catch(err => {
149
+ alert('❌ Failed to copy: ' + err);
150
+ });
151
+ }
152
+
153
+ function filterEnvTable() {
154
+ const input = document.getElementById('env-search').value.toLowerCase();
155
+ const rows = document.querySelectorAll('#env-body tr');
156
+
157
+ rows.forEach(row => {
158
+ const key = row.getAttribute('data-key') || '';
159
+ const value = row.getAttribute('data-value') || '';
160
+ row.style.display = (key.includes(input) || value.includes(input)) ? '' : 'none';
161
+ });
162
+ }
163
+ </script>
164
+ `;
165
+ }
@@ -0,0 +1,9 @@
1
+ import { Context, TezX } from "tezx";
2
+ export type Tab = "cookies" | "routes" | ".env" | "middlewares";
3
+ export type TabType = {
4
+ doc_title: string;
5
+ label: string;
6
+ tab: Tab | string;
7
+ content: string;
8
+ }[];
9
+ export declare function html(ctx: Context, app: TezX): TabType;
package/html/index.js ADDED
@@ -0,0 +1,33 @@
1
+ import { CookiesInspector } from "./cookies.js";
2
+ import { EnvInspector } from "./env.js";
3
+ import { Routes } from "./routes.js";
4
+ import { StaticFile } from "./StaticFile.js";
5
+ export function html(ctx, app) {
6
+ let tabDb = [
7
+ {
8
+ doc_title: "DevTools - Route Inspector",
9
+ label: "Routes",
10
+ tab: "routes",
11
+ content: Routes(ctx, app),
12
+ },
13
+ {
14
+ doc_title: "Static File",
15
+ label: "Static File",
16
+ tab: "static-file",
17
+ content: StaticFile(ctx, app),
18
+ },
19
+ {
20
+ tab: "cookies",
21
+ label: "Cookies",
22
+ doc_title: "DevTools - Cookie Inspector",
23
+ content: CookiesInspector(ctx),
24
+ },
25
+ {
26
+ tab: ".env",
27
+ label: "Environment",
28
+ doc_title: "DevTools - Environment",
29
+ content: EnvInspector(ctx),
30
+ },
31
+ ];
32
+ return tabDb;
33
+ }
@@ -0,0 +1,2 @@
1
+ import { Context, TezX } from "tezx";
2
+ export declare function Routes(ctx: Context, app: TezX): string;
package/html/routes.js ADDED
@@ -0,0 +1,157 @@
1
+ export function Routes(ctx, app) {
2
+ const routeMap = app.route.reduce((acc, curr) => {
3
+ const { pattern, method, handlers } = curr;
4
+ if (!acc[pattern])
5
+ acc[pattern] = {};
6
+ acc[pattern][method] = handlers;
7
+ return acc;
8
+ }, {});
9
+ const allRoutes = Object.entries(routeMap).map(([pattern, middlewareMap]) => ({
10
+ pattern,
11
+ handlers: Object.entries(middlewareMap).map(([method, handlers]) => ({
12
+ method,
13
+ handlerNames: handlers.map((fn) => fn.name || "[anonymous]"),
14
+ })),
15
+ }));
16
+ const rawJSON = JSON.stringify(allRoutes, null, 2);
17
+ const toCSV = (data) => {
18
+ const headers = ["Path", "Middleware", "Handler"];
19
+ const rows = data.flatMap((route) => route.handlers.flatMap((mw) => mw.handlerNames.map((h) => [
20
+ JSON.stringify(route.pattern),
21
+ JSON.stringify(mw.method),
22
+ JSON.stringify(h),
23
+ ])));
24
+ return [headers.join(","), ...rows.map((r) => r.join(","))].join("\n");
25
+ };
26
+ const csvString = toCSV(allRoutes).replace(/"/g, "&quot;");
27
+ return `
28
+ <style>
29
+ .download {
30
+ margin-top: 1rem;
31
+ display: flex;
32
+ gap: 1rem;
33
+ }
34
+ .download a {
35
+ padding: 0.5rem 1rem;
36
+ border: 1px solid var(--accent);
37
+ color: var(--accent);
38
+ text-decoration: none;
39
+ border-radius: 6px;
40
+ }
41
+ .download a:hover {
42
+ background: var(--accent);
43
+ color: white;
44
+ }
45
+ #searchBar {
46
+ margin: 1rem 0;
47
+ display: flex;
48
+ }
49
+ #searchBar input {
50
+ flex: 1;
51
+ }
52
+ table td button {
53
+ background: #e2e8f0;
54
+ border: none;
55
+ padding: 0.2rem 0.5rem;
56
+ border-radius: 0.375rem;
57
+ cursor: pointer;
58
+ margin-left: 0.5rem;
59
+ }
60
+ table td button:hover {
61
+ background: #cbd5e1;
62
+ }
63
+
64
+ .router-title {
65
+ font-size: 1.25rem;
66
+ margin-bottom: 0.5rem;
67
+ font-weight: 600;
68
+ color: #334155;
69
+ }
70
+ </style>
71
+
72
+ <div class="tabs toolbar">
73
+ <a class="tab-btn active" onclick="showTab('routes')">📋 Routes (${allRoutes.flat(2)?.length})</a>
74
+ <a class="tab-btn" onclick="showTab('json')">🧾 Raw JSON</a>
75
+ <a class="tab-btn" onclick="showTab('export')">📤 Export</a>
76
+ </div>
77
+ <h2 class="router-title">📌 Router: <span class="router-name">${app.router.name}</span></h2>
78
+ <div id="searchBar">
79
+ <input type="text" id="search" placeholder="Filter by path or middleware..." />
80
+ </div>
81
+
82
+ <div id="routesTab" class="table-container">
83
+ <table id="routesTable">
84
+ <thead>
85
+ <tr>
86
+ <th>#</th>
87
+ <th>Path</th>
88
+ <th>Middleware</th>
89
+ <th>Handlers</th>
90
+ </tr>
91
+ </thead>
92
+ <tbody>
93
+ ${allRoutes.flatMap((r, i) => r.handlers.map((hn) => `
94
+ <tr>
95
+ <td>${i + 1}</td>
96
+ <td>${r.pattern}</td>
97
+ <td>
98
+ ${hn.method === "ALL"
99
+ ? `<span style="
100
+ display: inline-block;
101
+ background-color: var(--accent);
102
+ color: #fff;
103
+ padding: 4px 10px;
104
+ border-radius: 6px;
105
+ font-weight: 500;
106
+ font-size: 13px;
107
+ box-shadow: 0 1px 4px rgba(0,0,0,0.15);
108
+ ">${hn.method} <small style="opacity: 0.8;">(middleware)</small></span>`
109
+ : `<span style="
110
+ font-size: 14px;
111
+ color: #333;
112
+ ">${hn.method}</span>`}
113
+ </td>
114
+ <td>${hn.handlerNames.join(", ")}</td>
115
+ </tr>
116
+ `))
117
+ .join("")}
118
+ </tbody>
119
+ </table>
120
+ </div>
121
+
122
+ <div id="jsonTab" style="display:none">
123
+ <div class="json-view"><pre>${rawJSON}</pre></div>
124
+ </div>
125
+
126
+ <div id="exportTab" style="display:none">
127
+ <div class="download">
128
+ <a href="data:text/json;charset=utf-8,${encodeURIComponent(rawJSON)}" download="routes.json">📥 JSON</a>
129
+ <a href="data:text/csv;charset=utf-8,${csvString}" download="routes.csv">📥 CSV</a>
130
+ </div>
131
+ </div>
132
+
133
+ <script>
134
+ const tabs = ['routes', 'json', 'export'];
135
+ const tabBtns = document.querySelectorAll('.tab-btn');
136
+
137
+ function showTab(tab) {
138
+ tabs.forEach(t => {
139
+ document.getElementById(t + 'Tab').style.display = t === tab ? 'block' : 'none';
140
+ });
141
+ tabBtns.forEach(btn => {
142
+ const active = btn.textContent.toLowerCase().includes(tab);
143
+ btn.classList.toggle('active', active);
144
+ });
145
+ document.getElementById('searchBar').style.display = (tab === 'routes') ? 'flex' : 'none';
146
+ }
147
+
148
+ document.getElementById('search').addEventListener('input', (e) => {
149
+ const keyword = e.target.value.toLowerCase();
150
+ const rows = document.querySelectorAll('#routesTable tbody tr');
151
+ rows.forEach(row => {
152
+ row.style.display = row.textContent.toLowerCase().includes(keyword) ? '' : 'none';
153
+ });
154
+ });
155
+ </script>
156
+ `;
157
+ }
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Callback, Context, TezX } from "tezx";
2
+ import { Tab, TabType } from "./html/index.js";
3
+ export type Options = {
4
+ extraTabs?: (ctx: Context) => Promise<TabType> | TabType;
5
+ disableTabs?: Tab[];
6
+ };
7
+ export declare function DevTools(app: TezX<any>, options?: Options): Callback;
8
+ export default DevTools;