MCPcopy Index your code
hub / github.com/nodeSolidServer/node-solid-server / fetchRequest

Function fetchRequest

test/integration/acl-oidc-test.mjs:14–40  ·  view source on GitHub ↗
(method, options, callback)

Source from the content-addressed store, hash-verified

12
13// Helper to mimic request's callback API for get, put, post, head, patch
14function fetchRequest (method, options, callback) {
15 // options: { url, headers, body, ... }
16 const fetchOptions = {
17 method: method.toUpperCase(),
18 headers: options.headers || {},
19 body: options.body
20 }
21 // For GET/HEAD, don't send body
22 if (['GET', 'HEAD'].includes(fetchOptions.method)) {
23 delete fetchOptions.body
24 }
25 fetch(options.url, fetchOptions)
26 .then(async res => {
27 let body = await res.text()
28 // Try to parse as JSON if content-type is json
29 if (res.headers.get('content-type') && res.headers.get('content-type').includes('json')) {
30 try { body = JSON.parse(body) } catch (e) {}
31 }
32 callback(null, {
33 statusCode: res.status,
34 headers: Object.fromEntries(res.headers.entries()),
35 body: body,
36 statusMessage: res.statusText
37 }, body)
38 })
39 .catch(err => callback(err))
40}
41
42function request (options, cb) {
43 // Allow string URL

Callers 2

requestFunction · 0.85
acl-oidc-test.mjsFile · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected