-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture_api.mjs
More file actions
141 lines (130 loc) · 4.21 KB
/
capture_api.mjs
File metadata and controls
141 lines (130 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { chromium } from 'playwright';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const demoDir = path.join(__dirname, 'demo');
const API_KEY = 'dev-key-change-me';
function syntaxHighlightScript() {
return `
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(
/("(\\\\u[a-zA-Z0-9]{4}|\\\\[^u]|[^\\\\\\\\"])*"(\\s*:)?|\\b(true|false|null)\\b|-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?)/g,
function (match) {
let cls = 'number';
if (/^"/.test(match)) {
cls = /:$/.test(match) ? 'key' : 'string';
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
}
);
}
`;
}
function buildHtml(endpoint, prettyJson) {
const escapedJson = JSON.stringify(prettyJson);
return `<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0; padding: 24px;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 13px;
background: #1e1e1e;
color: #d4d4d4;
}
.header {
background: #2d2d2d;
border: 1px solid #444;
border-radius: 6px;
padding: 12px 18px;
margin-bottom: 16px;
display: flex;
align-items: center;
gap: 12px;
}
.method {
background: #49cc90;
color: #fff;
font-weight: bold;
padding: 4px 12px;
border-radius: 4px;
font-size: 12px;
}
.url {
color: #9cdcfe;
font-size: 14px;
}
.status {
margin-left: auto;
color: #49cc90;
font-weight: bold;
}
pre {
background: #1e1e1e;
border: 1px solid #333;
border-radius: 6px;
padding: 20px;
overflow-x: auto;
line-height: 1.5;
}
.key { color: #9cdcfe; }
.string { color: #ce9178; }
.number { color: #b5cea8; }
.boolean { color: #569cd6; }
.null { color: #569cd6; }
</style>
</head>
<body>
<div class="header">
<span class="method">GET</span>
<span class="url">${endpoint}</span>
<span class="status">200 OK</span>
</div>
<pre id="json"></pre>
<script>
${syntaxHighlightScript()}
document.getElementById('json').innerHTML = syntaxHighlight(${escapedJson});
</script>
</body>
</html>`;
}
(async () => {
const browser = await chromium.launch();
const endpoints = [
{ url: 'http://localhost:8000/', name: 'api_root', path: '/' },
{ url: 'http://localhost:8000/api/kpi/current', name: 'api_kpi_current', path: '/api/kpi/current' },
{ url: 'http://localhost:8000/api/agents/status', name: 'api_agents_status', path: '/api/agents/status' },
{ url: 'http://localhost:8000/api/inventory/status?product_id=PRD-0001', name: 'api_inventory_status', path: '/api/inventory/status?product_id=PRD-0001' },
{ url: 'http://localhost:8000/api/events/recent?limit=5', name: 'api_events_recent', path: '/api/events/recent?limit=5' },
{ url: 'http://localhost:8000/api/simulation/status', name: 'api_simulation_status', path: '/api/simulation/status' },
{ url: 'http://localhost:8000/api/forecast/PRD-0001?horizon=7', name: 'api_forecast', path: '/api/forecast/PRD-0001?horizon=7' },
];
for (const ep of endpoints) {
const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });
// Inject API key header for all /api/ requests
await page.route('**/api/**', async (route) => {
await route.continue({ headers: { ...route.request().headers(), 'X-API-Key': API_KEY } });
});
// Fetch raw JSON
const resp = await page.goto(ep.url, { waitUntil: 'networkidle' });
const rawText = await resp.text();
let parsed;
try { parsed = JSON.parse(rawText); } catch { parsed = rawText; }
const pretty = JSON.stringify(parsed, null, 2);
// Render styled page
const html = buildHtml(ep.path, pretty);
await page.setContent(html);
await page.waitForTimeout(500);
await page.screenshot({ path: path.join(demoDir, `${ep.name}.png`), fullPage: true });
console.log(`Captured: ${ep.name}.png`);
await page.close();
}
await browser.close();
console.log('Done');
})();