MCPcopy Create free account
hub / github.com/EvoMap/evolver / request

Function request

test/proxyServer.test.js:18–46  ·  view source on GitHub ↗
(url, method, body, token)

Source from the content-addressed store, hash-verified

16}
17
18function request(url, method, body, token) {
19 return new Promise((resolve, reject) => {
20 const u = new URL(url);
21 const payload = body ? JSON.stringify(body) : '';
22 const headers = {
23 'Content-Type': 'application/json',
24 'Content-Length': Buffer.byteLength(payload),
25 };
26 if (token) headers['Authorization'] = 'Bearer ' + token;
27 const req = http.request({
28 hostname: u.hostname,
29 port: u.port,
30 path: u.pathname + u.search,
31 method,
32 headers,
33 }, (res) => {
34 const chunks = [];
35 res.on('data', c => chunks.push(c));
36 res.on('end', () => {
37 const raw = Buffer.concat(chunks).toString();
38 try { resolve({ status: res.statusCode, body: JSON.parse(raw) }); }
39 catch { resolve({ status: res.statusCode, body: raw }); }
40 });
41 });
42 req.on('error', reject);
43 if (payload) req.write(payload);
44 req.end();
45 });
46}
47
48describe('ProxyHttpServer', () => {
49 let store, server, baseUrl, dataDir, serverToken;

Callers 1

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected