UNPKG

@platform/http

Version:

Tools for working with HTTP.

29 lines (28 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetch = void 0; var tslib_1 = require("tslib"); var cross_fetch_1 = tslib_1.__importDefault(require("cross-fetch")); var common_1 = require("../common"); var fetch = function (req) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var url, method, mode, data, onError, toBody, headers, body; return tslib_1.__generator(this, function (_a) { url = req.url, method = req.method, mode = req.mode, data = req.data; onError = function () { return "Failed to ".concat(method, " to '").concat(url, "'. The data could not be serialized to JSON."); }; toBody = function () { if (data instanceof Uint8Array) return data; if (common_1.util.isFormData(req.headers)) return data; if (typeof data === 'string') return data; return common_1.util.stringify(data, onError); }; headers = common_1.util.toRawHeaders(req.headers); body = ['GET', 'HEAD'].includes(method) ? undefined : toBody(); return [2, (0, cross_fetch_1.default)(url, { method: method, mode: mode, body: body, headers: headers })]; }); }); }; exports.fetch = fetch;