@tezx/devtools 1.0.4 โ†’ 1.0.5

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/cookies.js CHANGED
@@ -1,142 +1,161 @@
1
1
  export function CookiesInspector(ctx) {
2
2
  const cookies = ctx.cookies.all();
3
- const rawJSON = JSON.stringify(cookies, null, 2);
4
- const tableRows = Object.entries(cookies)
5
- .map(([key, value], i) => `
6
- <tr>
7
- <td>${i + 1}</td>
8
- <td contenteditable="true" class="cookie-key">${key}</td>
9
- <td contenteditable="true" class="cookie-value">${value}</td>
10
- <td><button class="delete-btn">๐Ÿ—‘๏ธ</button></td>
11
- </tr>
12
- `).join("");
13
3
  const html = `
14
- <style>
15
- td[contenteditable="true"] {
16
- outline: 0px;
17
- }
18
- td[contenteditable="true"]:focus {
19
- outline: 1px solid var(--accent);
20
- }
4
+ <style>
5
+ td[contenteditable="true"] {
6
+ outline: 0px;
7
+ }
8
+ td[contenteditable="true"]:focus {
9
+ outline: 1px solid var(--accent);
10
+ }
21
11
 
22
- .delete-btn {
23
- background: #dc2626;
24
- color: white;
25
- border: none;
26
- border-radius: 0.375rem;
27
- padding: 0.3rem 0.6rem;
28
- font-size: 0.8rem;
29
- cursor: pointer;
30
- }
31
-
32
- .delete-btn:hover {
33
- background: #b91c1c;
34
- }
35
-
36
- pre#json-output {
37
- margin-top: 1rem;
38
- padding: 1rem;
39
- border-radius: 0.5rem;
40
- font-size: 0.875rem;
41
- overflow-x: auto;
42
- display: none;
43
- }
44
-
45
- </style>
46
-
47
- <div class="tabs">
48
- <a onclick="addCookieRow()">โž• Add Cookie</a>
49
- <a onclick="saveCookies()">๐Ÿ’พ Save All</a>
50
- <a onclick="exportCookies()">๐Ÿ“ค Export JSON</a>
51
- </div>
52
-
53
- <div class="table-container">
54
- <table>
55
- <thead>
56
- <tr>
57
- <th>#</th>
58
- <th>Key</th>
59
- <th>Value</th>
60
- <th>Action</th>
61
- </tr>
62
- </thead>
63
- <tbody id="cookie-body"></tbody>
64
- </table>
65
- </div>
66
-
67
- <pre class="json-view" id="json-output"></pre>
68
-
69
- <script>
70
- function getCookies() {
71
- const cookies = {};
72
- document.cookie.split(';').forEach(c => {
73
- const [k, ...v] = c.trim().split('=');
74
- cookies[k] = decodeURIComponent(v.join('='));
75
- });
76
- return cookies;
77
- }
78
-
79
- function renderCookies() {
80
- const tbody = document.getElementById('cookie-body');
81
- tbody.innerHTML = '';
82
- const cookies = getCookies();
83
- let index = 1;
84
- for (const [key, val] of Object.entries(cookies)) {
85
- const row = document.createElement('tr');
86
- row.innerHTML = \`
87
- <td>\${index++}</td>
88
- <td contenteditable="true" class="cookie-key">\${key}</td>
89
- <td contenteditable="true" class="cookie-value">\${val}</td>
90
- <td><button class="delete-btn" onclick="deleteRow(this)">Delete</button></td>
91
- \`;
92
- tbody.appendChild(row);
93
- }
94
- }
95
-
96
- function addCookieRow() {
97
- const tbody = document.getElementById('cookie-body');
12
+ .delete-btn,
13
+ .copy-btn {
14
+ background: #dc2626;
15
+ color: white;
16
+ border: none;
17
+ border-radius: 0.375rem;
18
+ padding: 0.3rem 0.6rem;
19
+ font-size: 0.8rem;
20
+ cursor: pointer;
21
+ margin-left: 4px;
22
+ }
23
+
24
+ .copy-btn {
25
+ background: #2563eb;
26
+ }
27
+
28
+ .delete-btn:hover {
29
+ background: #b91c1c;
30
+ }
31
+
32
+ .copy-btn:hover {
33
+ background: #1e40af;
34
+ }
35
+
36
+ pre#json-output {
37
+ margin-top: 1rem;
38
+ padding: 1rem;
39
+ border-radius: 0.5rem;
40
+ font-size: 0.875rem;
41
+ overflow-x: auto;
42
+ display: none;
43
+ background: #f3f4f6;
44
+ }
45
+ </style>
46
+
47
+ <div class="tabs">
48
+ <a onclick="addCookieRow()">โž• Add Cookie</a>
49
+ <a onclick="saveCookies()">๐Ÿ’พ Save All</a>
50
+ <a onclick="exportCookies()">๐Ÿ“ค Export JSON</a>
51
+ </div>
52
+
53
+ <div class="table-container">
54
+ <table>
55
+ <thead>
56
+ <tr>
57
+ <th>#</th>
58
+ <th>Key</th>
59
+ <th>Value</th>
60
+ <th>Actions</th>
61
+ </tr>
62
+ </thead>
63
+ <tbody id="cookie-body"></tbody>
64
+ </table>
65
+ </div>
66
+
67
+ <pre class="json-view" id="json-output"></pre>
68
+
69
+ <script>
70
+ function getCookies() {
71
+ const cookies = {};
72
+ document.cookie.split(';').forEach(c => {
73
+ const [k, ...v] = c.trim().split('=');
74
+ cookies[k] = decodeURIComponent(v.join('='));
75
+ });
76
+ return cookies;
77
+ }
78
+
79
+ function renderCookies() {
80
+ const tbody = document.getElementById('cookie-body');
81
+ tbody.innerHTML = '';
82
+ const cookies = getCookies();
83
+ let index = 1;
84
+ for (const [key, val] of Object.entries(cookies)) {
98
85
  const row = document.createElement('tr');
99
86
  row.innerHTML = \`
100
- <td>\${tbody.children.length + 1}</td>
101
- <td contenteditable="true" class="cookie-key"></td>
102
- <td contenteditable="true" class="cookie-value"></td>
103
- <td><button class="delete-btn" onclick="deleteRow(this)">Delete</button></td>
87
+ <td>\${index++}</td>
88
+ <td contenteditable="true" class="cookie-key" style="white-space: nowrap">\${key}</td>
89
+ <td contenteditable="true" class="cookie-value">\${val}</td>
90
+ <td class="action">
91
+ <button class="copy-btn" onclick="copyRow(this)">๐Ÿ“‹</button>
92
+ <button class="delete-btn" onclick="deleteRow(this)">Delete</button>
93
+ </td>
104
94
  \`;
105
95
  tbody.appendChild(row);
106
96
  }
107
-
108
- function deleteRow(button) {
109
- const row = button.closest('tr');
97
+ }
98
+
99
+ function addCookieRow() {
100
+ const tbody = document.getElementById('cookie-body');
101
+ const row = document.createElement('tr');
102
+ row.innerHTML = \`
103
+ <td>\${tbody.children.length + 1}</td>
104
+ <td contenteditable="true" class="cookie-key" style="white-space: nowrap"></td>
105
+ <td contenteditable="true" class="cookie-value"></td>
106
+ <td class="action">
107
+ <button class="copy-btn" onclick="copyRow(this)">๐Ÿ“‹</button>
108
+ <button class="delete-btn" onclick="deleteRow(this)">Delete</button>
109
+ </td>
110
+ \`;
111
+ tbody.appendChild(row);
112
+ }
113
+
114
+ function deleteRow(button) {
115
+ const row = button.closest('tr');
116
+ const key = row.querySelector('.cookie-key')?.innerText.trim();
117
+ if (key) {
118
+ document.cookie = key + '=; Max-Age=0; path=/';
119
+ }
120
+ row.remove();
121
+ renderCookies();
122
+ }
123
+
124
+ function saveCookies() {
125
+ const rows = document.querySelectorAll('#cookie-body tr');
126
+ rows.forEach(row => {
110
127
  const key = row.querySelector('.cookie-key')?.innerText.trim();
128
+ const value = row.querySelector('.cookie-value')?.innerText.trim();
111
129
  if (key) {
112
- document.cookie = key + '=; Max-Age=0; path=/';
130
+ document.cookie = key + '=' + encodeURIComponent(value) + '; path=/';
113
131
  }
114
- row.remove();
115
- renderCookies();
116
- }
117
-
118
- function saveCookies() {
119
- const rows = document.querySelectorAll('#cookie-body tr');
120
- rows.forEach(row => {
121
- const key = row.querySelector('.cookie-key')?.innerText.trim();
122
- const value = row.querySelector('.cookie-value')?.innerText.trim();
123
- if (key) {
124
- document.cookie = key + '=' + encodeURIComponent(value) + '; path=/';
125
- }
126
- });
127
- renderCookies();
128
- alert('โœ… Cookies saved!');
129
- }
130
-
131
- function exportCookies() {
132
- const cookies = getCookies();
133
- const output = document.getElementById('json-output');
134
- output.textContent = JSON.stringify(cookies, null, 2);
135
- output.style.display = 'block';
136
- }
137
-
132
+ });
138
133
  renderCookies();
139
- </script>
140
- `;
134
+ alert('โœ… Cookies saved!');
135
+ }
136
+
137
+ function exportCookies() {
138
+ const cookies = getCookies();
139
+ const output = document.getElementById('json-output');
140
+ output.textContent = JSON.stringify(cookies, null, 2);
141
+ output.style.display = 'block';
142
+ }
143
+
144
+ function copyRow(button) {
145
+ const row = button.closest('tr');
146
+ const key = row.querySelector('.cookie-key')?.innerText.trim();
147
+ const value = row.querySelector('.cookie-value')?.innerText.trim();
148
+ if (key) {
149
+ navigator.clipboard.writeText(\`\${key}: \${value}\`)
150
+ .then(() => {
151
+ button.innerText = 'โœ…';
152
+ setTimeout(() => button.innerText = '๐Ÿ“‹', 1000);
153
+ });
154
+ }
155
+ }
156
+
157
+ renderCookies();
158
+ </script>
159
+ `;
141
160
  return html;
142
161
  }
package/html/env.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { Context } from "tezx";
2
+ export declare function EnvInspector(ctx: Context): string;
package/html/env.js ADDED
@@ -0,0 +1,142 @@
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, "&quot;");
8
+ const safeValue = value.replace(/`/g, "\\`").replace(/"/g, "&quot;");
9
+ return `
10
+ <tr data-key="${safeKey.toLowerCase()}" data-value="${value.toLowerCase()}">
11
+ <td>${i + 1}</td>
12
+ <td>${safeKey}</td>
13
+ <td>${value}</td>
14
+ <td><button onclick="copyRowEnv('${safeKey}', \`${safeValue}\`)">๐Ÿ“‹</button></td>
15
+ </tr>
16
+ `;
17
+ })
18
+ .join("")
19
+ : `
20
+ <tr>
21
+ <td colspan="4">
22
+ <p>โš ๏ธ <strong>No environment variables found.</strong></p>
23
+ <pre style="margin-top: 1rem; background: #f1f5f9; padding: 1rem; border-radius: 0.5rem;">
24
+ const env = loadEnv();
25
+
26
+ export const app = new TezX({
27
+ env: env,
28
+ debugMode: true,
29
+ // basePath: 'v1',
30
+ allowDuplicateMw: true,
31
+ });
32
+ </pre>
33
+ </td>
34
+ </tr>`;
35
+ return `
36
+ <style>
37
+ table td button {
38
+ background: #e2e8f0;
39
+ border: none;
40
+ padding: 0.3rem 0.6rem;
41
+ border-radius: 0.375rem;
42
+ cursor: pointer;
43
+ }
44
+ table td button:hover {
45
+ background: #cbd5e1;
46
+ }
47
+ pre#json-output {
48
+ margin-top: 1rem;
49
+ padding: 1rem;
50
+ border-radius: 0.5rem;
51
+ background: #f9fafb;
52
+ border: 1px solid #e5e7eb;
53
+ font-size: 0.875rem;
54
+ overflow-x: auto;
55
+ display: none;
56
+ }
57
+ .search-container {
58
+ margin-bottom: 1rem;
59
+ }
60
+ .search-container input {
61
+ padding: 0.5rem;
62
+ width: 100%;
63
+ max-width: 400px;
64
+ border: 1px solid #e5e7eb;
65
+ border-radius: 0.375rem;
66
+ }
67
+ </style>
68
+
69
+ <div class="tabs">
70
+ <a onclick="exportEnv()">๐Ÿ“ค Export JSON</a>
71
+ <a onclick="copyAllEnv()">๐Ÿ“‹ Copy All</a>
72
+ </div>
73
+
74
+ <div class="search-container">
75
+ <input type="text" id="env-search" placeholder="๐Ÿ” Search environment variables..." oninput="filterEnvTable()" />
76
+ </div>
77
+
78
+ <div class="table-container">
79
+ <table>
80
+ <thead>
81
+ <tr>
82
+ <th>#</th>
83
+ <th>Key</th>
84
+ <th>Value</th>
85
+ <th>Copy</th>
86
+ </tr>
87
+ </thead>
88
+ <tbody id="env-body">
89
+ ${tableRows}
90
+ </tbody>
91
+ </table>
92
+ </div>
93
+
94
+ <pre id="json-output">${JSON.stringify(env, null, 2)}</pre>
95
+
96
+ <script>
97
+ function exportEnv() {
98
+ const output = document.getElementById('json-output');
99
+ const blob = new Blob([output.textContent], { type: 'application/json' });
100
+ const url = URL.createObjectURL(blob);
101
+ const a = document.createElement('a');
102
+ a.href = url;
103
+ a.download = 'env.json';
104
+ a.click();
105
+ URL.revokeObjectURL(url);
106
+ }
107
+
108
+ function copyAllEnv() {
109
+ const output = document.getElementById('json-output');
110
+ navigator.clipboard.writeText(output.textContent).then(() => {
111
+ alert('โœ… All environment variables copied!');
112
+ }).catch(err => {
113
+ alert('โŒ Failed to copy: ' + err);
114
+ });
115
+ }
116
+
117
+ function copyRowEnv(key, value) {
118
+ const text = \`\${key}=\${value}\`;
119
+ navigator.clipboard.writeText(text).then(() => {
120
+ alert(\`โœ… Copied: \${text}\`);
121
+ }).catch(err => {
122
+ alert('โŒ Failed to copy: ' + err);
123
+ });
124
+ }
125
+
126
+ function filterEnvTable() {
127
+ const input = document.getElementById('env-search').value.toLowerCase();
128
+ const rows = document.querySelectorAll('#env-body tr');
129
+
130
+ rows.forEach(row => {
131
+ const key = row.getAttribute('data-key') || '';
132
+ const value = row.getAttribute('data-value') || '';
133
+ if (key.includes(input) || value.includes(input)) {
134
+ row.style.display = '';
135
+ } else {
136
+ row.style.display = 'none';
137
+ }
138
+ });
139
+ }
140
+ </script>
141
+ `;
142
+ }
package/html/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Context, TezX } from "tezx";
2
- export type Tab = ("cookies" | "routes");
2
+ export type Tab = "cookies" | "routes" | ".env" | "middlewares";
3
3
  export type TabType = {
4
4
  doc_title: string;
5
5
  label: string;
6
- tab: Tab;
6
+ tab: Tab | string;
7
7
  content: string;
8
8
  }[];
9
9
  export declare function html(ctx: Context, app: TezX): TabType;
package/html/index.js CHANGED
@@ -1,19 +1,33 @@
1
- import { Routes } from "./routes.js";
2
1
  import { CookiesInspector } from "./cookies.js";
2
+ import { EnvInspector } from "./env.js";
3
+ import { Middlewares } from "./middlewares.js";
4
+ import { Routes } from "./routes.js";
3
5
  export function html(ctx, app) {
4
6
  let tabDb = [
5
7
  {
6
8
  doc_title: "DevTools - Route Inspector",
7
9
  label: "Routes",
8
- tab: 'routes',
9
- content: Routes(ctx, app)
10
+ tab: "routes",
11
+ content: Routes(ctx, app),
12
+ },
13
+ {
14
+ doc_title: "DevTools - Middleware Inspector",
15
+ label: "Middlewares",
16
+ tab: "middleware",
17
+ content: Middlewares(ctx, app),
10
18
  },
11
19
  {
12
- tab: 'cookies',
13
- label: 'Cookies',
14
- doc_title: 'DevTools - Cookie Inspector',
15
- content: CookiesInspector(ctx)
16
- }
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
+ },
17
31
  ];
18
32
  return tabDb;
19
33
  }
@@ -0,0 +1,7 @@
1
+ import { Context, TezX } from "tezx";
2
+ export type MiddlewareEntry = {
3
+ pattern: string;
4
+ type: "static" | "wildcard" | "optional params" | "dynamic params";
5
+ appliedMiddlewares: string[];
6
+ };
7
+ export declare function Middlewares(ctx: Context, app: TezX): string;
@@ -0,0 +1,181 @@
1
+ import { dumpMiddlewares } from "../devtools";
2
+ export function Middlewares(ctx, app) {
3
+ const allRoutes = dumpMiddlewares(app);
4
+ const totalRoutes = allRoutes.length;
5
+ const middlewareRoutes = {};
6
+ for (const route of allRoutes) {
7
+ for (const mw of route.appliedMiddlewares || []) {
8
+ (middlewareRoutes[mw] ||= []).push(route);
9
+ }
10
+ }
11
+ const rawJSON = JSON.stringify(allRoutes, null, 2);
12
+ const toCSV = (data) => {
13
+ const headers = ["pattern", "type", "appliedMiddlewares"];
14
+ const csvRows = [
15
+ headers.join(","),
16
+ ...data.map((r) => headers.map((h) => JSON.stringify(r[h] ?? "")).join(",")),
17
+ ];
18
+ return csvRows.join("\n");
19
+ };
20
+ const csvString = toCSV(allRoutes).replace(/"/g, "&quot;");
21
+ return `
22
+ <style>
23
+ .download {
24
+ margin-top: 1rem;
25
+ display: flex;
26
+ gap: 1rem;
27
+ }
28
+ .download a {
29
+ padding: 0.5rem 1rem;
30
+ border: 1px solid var(--accent);
31
+ color: var(--accent);
32
+ text-decoration: none;
33
+ border-radius: 6px;
34
+ }
35
+
36
+ .download a:hover {
37
+ background: var(--accent);
38
+ color: white;
39
+ }
40
+
41
+ #searchBar {
42
+ margin: 1rem 0;
43
+ display: flex;
44
+ }
45
+
46
+ #searchBar input {
47
+ flex: 1;
48
+ }
49
+
50
+ table td button {
51
+ background: #e2e8f0;
52
+ border: none;
53
+ padding: 0.2rem 0.5rem;
54
+ border-radius: 0.375rem;
55
+ cursor: pointer;
56
+ margin-left: 0.5rem;
57
+ }
58
+
59
+ table td button:hover {
60
+ background: #cbd5e1;
61
+ }
62
+ </style>
63
+
64
+ <div class="tabs toolbar">
65
+ <a class="tab-btn active counting" data-count="${totalRoutes}" onclick="showTab('routes')">๐Ÿ“‹ Routes</a>
66
+ <a class="tab-btn counting" data-count="${Object.keys(middlewareRoutes).length}" onclick="showTab('middlewares')">๐Ÿงฉ Middlewares</a>
67
+ <a class="tab-btn" onclick="showTab('json')">๐Ÿงพ Raw JSON</a>
68
+ <a class="tab-btn" onclick="showTab('export')">๐Ÿ“ค Export</a>
69
+ </div>
70
+
71
+ <div id="searchBar">
72
+ <input type="text" id="search" placeholder="Filter by pattern/type/middleware..." />
73
+ </div>
74
+
75
+ <!-- ROUTES -->
76
+ <div id="routesTab" class="table-container">
77
+ <table id="routesTable">
78
+ <thead>
79
+ <tr>
80
+ <th>#</th>
81
+ <th>Pattern</th>
82
+ <th>Type</th>
83
+ <th>Middlewares</th>
84
+ </tr>
85
+ </thead>
86
+ <tbody>
87
+ ${allRoutes
88
+ .map((r, i) => `
89
+ <tr>
90
+ <td>${i + 1}</td>
91
+ <td>
92
+ ${r.pattern}
93
+ <button onclick="copyText(\`${r.pattern}\`)">๐Ÿ“‹</button>
94
+ </td>
95
+ <td>
96
+ <span style="
97
+ display: inline-block;
98
+ padding: 0.2rem 0.5rem;
99
+ border-radius: 0.375rem;
100
+ color: white;
101
+ font-size: 0.75rem;
102
+ background-color: ${r.type === "static"
103
+ ? "#16a34a"
104
+ : r.type === "wildcard"
105
+ ? "#f97316"
106
+ : r.type === "optional params"
107
+ ? "#3b82f6"
108
+ : r.type === "dynamic params"
109
+ ? "#9333ea"
110
+ : "#6b7280"};
111
+ ">
112
+ ${r.type}
113
+ </span>
114
+ </td>
115
+ <td>${(r.appliedMiddlewares || []).join(", ")}</td>
116
+ </tr>
117
+ `)
118
+ .join("")}
119
+ </tbody>
120
+ </table>
121
+ </div>
122
+
123
+ <!-- MIDDLEWARES -->
124
+ <div id="middlewaresTab" style="display:none">
125
+ ${Object.entries(middlewareRoutes)
126
+ .map(([mw, routes]) => `
127
+ <h3>๐Ÿ”น ${mw} (${routes.length})</h3>
128
+ <ul>
129
+ ${routes.map((r) => `<li><code>${r.pattern}</code> <span>(${r.type})</span></li>`).join("")}
130
+ </ul>
131
+ `)
132
+ .join("")}
133
+ </div>
134
+
135
+ <!-- RAW JSON -->
136
+ <div id="jsonTab" style="display:none">
137
+ <div class="json-view"><pre>${rawJSON}</pre></div>
138
+ </div>
139
+
140
+ <!-- EXPORT -->
141
+ <div id="exportTab" style="display:none">
142
+ <div class="download">
143
+ <a href="data:text/json;charset=utf-8,${encodeURIComponent(rawJSON)}" download="middlewares.json">๐Ÿ“ฅ JSON</a>
144
+ <a href="data:text/csv;charset=utf-8,${csvString}" download="middlewares.csv">๐Ÿ“ฅ CSV</a>
145
+ </div>
146
+ </div>
147
+
148
+ <script>
149
+ const tabs = ['routes', 'middlewares', 'json', 'export'];
150
+ const tabBtns = document.querySelectorAll('.tab-btn');
151
+
152
+ function showTab(tab) {
153
+ tabs.forEach(t => {
154
+ document.getElementById(t + 'Tab').style.display = t === tab ? 'block' : 'none';
155
+ document.getElementById(t + 'Tab').classList.toggle('active', t === tab);
156
+ });
157
+ tabBtns.forEach(btn => {
158
+ const active = btn.textContent.toLowerCase().includes(tab);
159
+ btn.classList.toggle('active', active);
160
+ });
161
+ document.getElementById('searchBar').style.display = (tab === 'routes') ? 'flex' : 'none';
162
+ }
163
+
164
+ document.getElementById('search').addEventListener('input', (e) => {
165
+ const keyword = e.target.value.toLowerCase();
166
+ const rows = document.querySelectorAll('#routesTable tbody tr');
167
+ rows.forEach(row => {
168
+ row.style.display = row.textContent.toLowerCase().includes(keyword) ? '' : 'none';
169
+ });
170
+ });
171
+
172
+ function copyText(text) {
173
+ navigator.clipboard.writeText(text).then(() => {
174
+ alert('โœ… Copied: ' + text);
175
+ }).catch(err => {
176
+ alert('โŒ Failed to copy: ' + err);
177
+ });
178
+ }
179
+ </script>
180
+ `;
181
+ }