UNPKG

@toolpad/utils

Version:

Shared utilities used by Toolpad packages.

42 lines (40 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adaptRequestFromExpressToFetch = adaptRequestFromExpressToFetch; exports.encodeRequestBody = encodeRequestBody; function encodeRequestBody(req) { const contentType = req.headers['content-type']; if (typeof req.body === 'object' && contentType?.includes('application/x-www-form-urlencoded')) { return Object.entries(req.body).reduce((acc, [key, value]) => { const encKey = encodeURIComponent(key); const encValue = encodeURIComponent(value); return `${acc ? `${acc}&` : ''}${encKey}=${encValue}`; }, ''); } if (contentType?.includes('application/json')) { return JSON.stringify(req.body); } return req.body; } function adaptRequestFromExpressToFetch(req) { // Converting Express req headers to Fetch API's Headers const headers = new Headers(); for (const headerName of Object.keys(req.headers)) { const headerValue = req.headers[headerName]?.toString() ?? ''; if (Array.isArray(headerValue)) { for (const value of headerValue) { headers.append(headerName, value); } } else { headers.append(headerName, headerValue); } } // Creating Fetch API's Request object from Express' req return new Request(`${req.protocol}://${req.get('host')}${req.originalUrl}`, { method: req.method, headers, body: /GET|HEAD/.test(req.method) ? undefined : encodeRequestBody(req) }); }