UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

120 lines (118 loc) 3.57 kB
class EmptyRequest extends Request { #headers = {}; constructor(input, init) { super(input, init); this.headers.forEach((value, key) => this.#headers[key] = value); return; } static async from(from) { if (from instanceof EmptyRequest) return from; const request = new Request(from.url, from); return new EmptyRequest(request); } get headersAsJson() { return this.#headers; } toJSON() { return { url: this.url, method: this.method, headers: this.headersAsJson, keepalive: this.keepalive, cache: this.cache, credentials: this.credentials, destination: this.destination, integrity: this.integrity, mode: this.mode, redirect: this.redirect, referrerPolicy: this.referrerPolicy }; } } class TextRequest extends Request { #headers = {}; #bodyAsText; constructor(input, init) { super(input, init); this.headers.forEach((value, key) => this.#headers[key] = value); this.#bodyAsText = init.body; } static async from(from) { if (from instanceof TextRequest) return from; const request = new Request(from.url, from); const body = await request.text(); return new TextRequest(request, { body }); } get headersAsJson() { return this.#headers; } get bodyAsText() { return this.#bodyAsText; } toJSON() { return { url: this.url, method: this.method, headers: this.headersAsJson, body: this.bodyAsText, keepalive: this.keepalive, cache: this.cache, credentials: this.credentials, destination: this.destination, integrity: this.integrity, mode: this.mode, redirect: this.redirect, referrerPolicy: this.referrerPolicy, }; } } class JsonRequest extends Request { #headers = {}; #bodyAsJson; #bodyAsText; constructor(input, init) { const body = JSON.stringify(init.body); super(input, { ...init, body }); if (!this.headers.get("Content-Type")?.includes("application/json")) this.headers.set("Content-Type", "application/json"); this.headers.forEach((value, key) => this.#headers[key] = value); this.#bodyAsJson = init.body; this.#bodyAsText = body; } static async from(from) { if (from instanceof JsonRequest) return from; const request = new Request(from.url, from); const body = await request.json(); return new JsonRequest(request, { body }); } get headersAsJson() { return this.#headers; } get bodyAsJson() { return this.#bodyAsJson; } get bodyAsText() { return this.#bodyAsText; } toJSON() { return { url: this.url, method: this.method, headers: this.headersAsJson, body: this.bodyAsText, keepalive: this.keepalive, cache: this.cache, credentials: this.credentials, destination: this.destination, integrity: this.integrity, mode: this.mode, redirect: this.redirect, referrerPolicy: this.referrerPolicy }; } } export { EmptyRequest, JsonRequest, TextRequest }; //# sourceMappingURL=index.mjs.map