UNPKG

edge-mock

Version:

types for testing an developer edge applications

52 lines 2.05 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fetch_1 = __importDefault(require("node-fetch")); const utils_1 = require("./utils"); const models_1 = require("./models"); const Request_1 = require("./models/Request"); const forms_1 = require("./forms"); const Headers_1 = require("./models/Headers"); async function default_1(resource, init = {}) { const method = Request_1.check_method(init.method); let headers = {}; if (init.headers) { const h = Headers_1.asHeaders(init.headers); h.delete('host'); headers = Object.fromEntries(h); } let body = undefined; const init_body = init.body; if (init_body) { if (typeof init_body == 'string') { body = init_body; } else if ('arrayBuffer' in init_body) { // Blob body = await init_body.arrayBuffer(); } else if ('getReader' in init_body) { body = await utils_1.rsToArrayBufferView(init_body); } else if (init_body instanceof models_1.EdgeFormData) { const [boundary, form_body] = await forms_1.formDataAsString(init_body); const ct = headers['content-type']; if (!ct || ct == 'multipart/form-data') { headers['content-type'] = `multipart/form-data; boundary=${boundary}`; } body = form_body; } else { // TODO this is a bodge until all cases can be checked body = init_body; } } const r = await node_fetch_1.default(resource, { method, headers, body }); const response_headers = Object.fromEntries(r.headers); const response_body = await r.arrayBuffer(); return new models_1.EdgeResponse(response_body, { status: r.status, headers: response_headers }, r.url); } exports.default = default_1; //# sourceMappingURL=live_fetch.js.map