UNPKG

@sapphire/plugin-api

Version:

Plugin for @sapphire/framework to expose a REST API

116 lines (114 loc) 4.44 kB
import { __name, __publicField, __privateAdd, __privateSet, __privateGet } from '../../../chunk-S573YWRP.mjs'; import { isNullish, isNullishOrEmpty } from '@sapphire/utilities'; import { Blob } from 'buffer'; import { arrayBuffer } from 'stream/consumers'; import { ReadableStream } from 'stream/web'; import { Response } from 'undici'; import { RequestHeadersProxy } from './RequestHeadersProxy.mjs'; import { RequestURLProxy } from './RequestURLProxy.mjs'; var _request, _url, _cachedMethod, _cachedHasBody, _abortController, _bodyStream; var _RequestProxy = class _RequestProxy { constructor(request) { __publicField(this, "cache", "default"); __publicField(this, "credentials", "same-origin"); __publicField(this, "destination", ""); __publicField(this, "integrity", ""); __publicField(this, "keepalive", false); __publicField(this, "mode", "cors"); __publicField(this, "redirect", "follow"); __publicField(this, "referrer", "about:client"); __publicField(this, "referrerPolicy", ""); __publicField(this, "headers"); __publicField(this, "bodyUsed", false); __publicField(this, "duplex", "half"); __privateAdd(this, _request); __privateAdd(this, _url); __privateAdd(this, _cachedMethod, null); __privateAdd(this, _cachedHasBody, null); __privateAdd(this, _abortController, null); __privateAdd(this, _bodyStream, null); __privateSet(this, _request, request); __privateSet(this, _url, new RequestURLProxy(request)); this.headers = new RequestHeadersProxy(request); } async bytes() { const { body } = this; return isNullish(body) ? new Uint8Array(0) : new Uint8Array(await arrayBuffer(body)); } get url() { return __privateGet(this, _url).href; } get method() { __privateGet(this, _cachedMethod) ?? __privateSet(this, _cachedMethod, __privateGet(this, _request).method?.toUpperCase() ?? "GET"); return __privateGet(this, _cachedMethod); } get signal() { __privateGet(this, _abortController) ?? __privateSet(this, _abortController, new AbortController()); return __privateGet(this, _abortController).signal; } get body() { if (!this.hasBody) return null; __privateGet(this, _bodyStream) ?? __privateSet(this, _bodyStream, new ReadableStream({ start: /* @__PURE__ */ __name((controller) => { __privateGet(this, _request).on("data", (chunk) => controller.enqueue(chunk)).once("error", (error) => { controller.error(error); __privateGet(this, _abortController)?.abort(); }).once("close", () => { __privateGet(this, _abortController)?.abort(); }).once("end", () => { controller.close(); }); }, "start") })); return __privateGet(this, _bodyStream); } async arrayBuffer() { const { body } = this; return isNullish(body) ? new ArrayBuffer(0) : arrayBuffer(body); } async blob() { const arrayBuffer2 = await this.arrayBuffer(); return new Blob([arrayBuffer2], { type: this.headers.get("content-type") ?? "" }); } async formData() { return new Response(this.body, { headers: this.headers }).formData(); } async json() { return JSON.parse(await this.text()); } async text() { return new TextDecoder().decode(await this.arrayBuffer()); } clone() { return new _RequestProxy(__privateGet(this, _request)); } get hasBody() { if (__privateGet(this, _cachedHasBody) !== null) return __privateGet(this, _cachedHasBody); const contentLengthString = this.headers.get("content-length"); const contentLength = isNullishOrEmpty(contentLengthString) ? 0 : Number(contentLengthString); if (Number.isSafeInteger(contentLength) && contentLength > 0) { __privateSet(this, _cachedHasBody, true); return true; } const transferEncoding = this.headers.get("transfer-encoding"); if (transferEncoding?.includes("chunked")) { __privateSet(this, _cachedHasBody, true); return true; } __privateSet(this, _cachedHasBody, false); return false; } }; _request = new WeakMap(); _url = new WeakMap(); _cachedMethod = new WeakMap(); _cachedHasBody = new WeakMap(); _abortController = new WeakMap(); _bodyStream = new WeakMap(); __name(_RequestProxy, "RequestProxy"); var RequestProxy = _RequestProxy; export { RequestProxy }; //# sourceMappingURL=RequestProxy.mjs.map //# sourceMappingURL=RequestProxy.mjs.map