UNPKG

edge-mock

Version:

types for testing an developer edge applications

67 lines 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EdgeResponse = void 0; // stubs https://developer.mozilla.org/en-US/docs/Web/API/Response const Body_1 = require("./Body"); const Headers_1 = require("./Headers"); const RedirectStatuses = new Set([301, 302, 303, 307, 308]); class EdgeResponse extends Body_1.EdgeBody { status; ok; statusText; headers; redirected = false; type = 'default'; url; _extra; constructor(body, init = {}, url = 'https://example.com', extra) { const headers = Headers_1.asHeaders(init.headers); const boundary = Body_1.findBoundary(headers, body); super(body, boundary); if (typeof body == 'string' && !headers.has('content-type')) { headers.set('content-type', 'text/plain'); } this.headers = headers; this.status = init.status === undefined ? 200 : init.status; this.ok = this.status >= 200 && this.status < 300; this.statusText = init.statusText || ''; this.url = url; if (extra) { this._extra = extra; } } get trailer() { throw new Error('trailer not yet implemented'); } clone() { const init = { status: this.status, statusText: this.statusText, headers: this.headers }; if (!this.body) { return new EdgeResponse(null, init); } else if (this._stream?.locked) { throw new TypeError('Response body is already used'); } else { const [s1, s2] = this.body.tee(); this._stream = s1; return new EdgeResponse(s2, init); } } static redirect(url, status = 302) { // see https://fetch.spec.whatwg.org/#dom-response-redirect if (!RedirectStatuses.has(status)) { throw new RangeError('Invalid status code'); } return new EdgeResponse(null, { status: status, headers: { location: new URL(url).href, }, }); } static error() { return new EdgeResponse(null, { status: 0 }); } } exports.EdgeResponse = EdgeResponse; //# sourceMappingURL=Response.js.map