UNPKG

edge-mock

Version:

types for testing an developer edge applications

98 lines 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.check_method = exports.EdgeRequest = void 0; // stubs https://developer.mozilla.org/en-US/docs/Web/API/Request const Headers_1 = require("./Headers"); const Body_1 = require("./Body"); const RequestCf_1 = require("./RequestCf"); const DEFAULT_HEADERS = { accept: '*/*', }; const MethodStrings = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; class EdgeRequest extends Body_1.EdgeBody { url; method; mode; credentials; cache; redirect; referrer; integrity; headers; cf; destination = ''; isHistoryNavigation = false; isReloadNavigation = false; keepalive = false; referrerPolicy = ''; constructor(input, init) { let url; if (typeof input == 'string') { url = input || '/'; } else { url = input.url; init = { body: input.body, credentials: input.credentials, headers: input.headers, method: input.method, mode: input.mode, referrer: input.referrer, cf: input.cf, ...init, }; } const method = check_method(init?.method); if (init?.body && (method == 'GET' || method == 'HEAD')) { throw new TypeError('Request with GET/HEAD method cannot have body.'); } const headers = Headers_1.asHeaders(init?.headers, DEFAULT_HEADERS); const boundary = Body_1.findBoundary(headers, init?.body); super(init?.body, boundary); this.headers = headers; this.url = 'https://example.com' + url; this.method = method; this.mode = init?.mode || 'same-origin'; this.cache = init?.cache || 'default'; this.referrer = init?.referrer && init?.referrer !== 'no-referrer' ? init?.referrer : ''; // See https://fetch.spec.whatwg.org/#concept-request-credentials-mode this.credentials = init?.credentials || (this.mode === 'navigate' ? 'include' : 'omit'); this.redirect = init?.redirect || 'follow'; this.integrity = init?.integrity || '-'; this.cf = RequestCf_1.example_cf(init?.cf); } get signal() { throw new Error('signal not yet implemented'); } clone() { this._check_used('clone'); const constructor = this.constructor; return new constructor(this.url, { method: this.method, headers: this.headers, body: this.body, mode: this.mode, credentials: this.credentials, cache: this.cache, redirect: this.redirect, referrer: this.referrer, integrity: this.integrity, cf: this.cf, }); } } exports.EdgeRequest = EdgeRequest; const MethodsSet = new Set(MethodStrings); function check_method(m) { if (m == undefined) { return 'GET'; } const method = m.toUpperCase(); if (!MethodsSet.has(method)) { throw new TypeError(`"${m}" is not a valid request method`); } return method; } exports.check_method = check_method; //# sourceMappingURL=Request.js.map