UNPKG

@sapphire/plugin-api

Version:

Plugin for @sapphire/framework to expose a REST API

177 lines (175 loc) 5.56 kB
import { __name, __privateAdd, __publicField, __privateGet, __privateSet } from '../../../chunk-S573YWRP.mjs'; import { isNullishOrEmpty } from '@sapphire/utilities'; import { IncomingMessage } from 'http'; import { RequestProxy } from '../../utils/_body/RequestProxy.mjs'; var _cachedRequest, _ApiRequest_instances, isFormContentType_get; var _ApiRequest = class _ApiRequest extends IncomingMessage { constructor() { super(...arguments); __privateAdd(this, _ApiRequest_instances); /** * The query parameters. */ __publicField(this, "query", {}); /** * The URI parameters. */ __publicField(this, "params", {}); /** * The authorization information. This field indicates three possible values: * * - `undefined`: The authorization middleware has not been executed yet. * - `null`: The user is not authorized. * - `AuthData`: The user is authorized. */ __publicField(this, "auth"); /** * The router node that matched the request. The field indicates three * possible values: * * - `undefined`: The router handler has not been executed yet. * - `null`: The router handler has been executed, but no node matched the * request. * - `RouterNode`: The router handler has been executed and a node matched * the request. * * @since 7.0.0 */ __publicField(this, "routerNode"); /** * The route that matched the request. The field indicates three possible * values: * * - `undefined`: The router handler has not been executed yet. * - `null`: The router handler has been executed, but no route matched the * request. * - `Route`: The router handler has been executed and a route matched the * request. * * @since 7.0.0 */ __publicField(this, "route"); /** * The response object. This field is cached to prevent multiple response * objects from being created. */ __privateAdd(this, _cachedRequest, null); } /** * The response object, used to validate the request's headers and body. */ asWeb() { __privateGet(this, _cachedRequest) ?? __privateSet(this, _cachedRequest, new RequestProxy(this)); return __privateGet(this, _cachedRequest); } /** * Reads the request body and tries to parse using JSON or form-urlencoded. * * @example * ```typescript * const body = await request.readBody(); * ``` * * @returns The result of the body parsing */ readBody() { return __privateGet(this, _ApiRequest_instances, isFormContentType_get) ? this.readBodyFormData() : this.readBodyJson(); } /** * Reads the request body as an {@link ArrayBuffer}. * * @returns The result of the body parsing */ readBodyArrayBuffer() { return this.asWeb().arrayBuffer(); } /** * Reads the request body as a {@link Blob}. * * @returns The result of the body parsing */ readBodyBlob() { return this.asWeb().blob(); } /** * Reads the request body as a {@link FormData}. * * @remarks * * This will throw an error if the content type is not one of the following: * * - `application/x-www-form-urlencoded` * - `multipart/form-data` * * @returns The result of the body parsing */ readBodyFormData() { return this.asWeb().formData(); } /** * Reads the request body as text, using {@link TextDecoder}. Afterward, it * parses the body as JSON with {@link JSON.parse}. * * @returns The result of the body parsing */ readBodyJson() { return this.asWeb().json(); } /** * Reads the request body as text, using {@link TextDecoder}. * * @returns The result of the body parsing */ readBodyText() { return this.asWeb().text(); } /** * Identical to {@link ApiRequest.readBody}, but it validates the result. * * @param validator The validator function to use on the body parsing result * @returns The validated body */ readValidatedBody(validator) { return this.readBody().then(validator); } /** * Identical to {@link ApiRequest.readBodyFormData}, but it validates the * result. * * @param validator The validator function to use on the body parsing result * @returns The validated body */ readValidatedBodyFormData(validator) { return this.readBodyFormData().then(validator); } /** * Identical to {@link ApiRequest.readBodyJson}, but it validates the result. * * @param validator The validator function to use on the body parsing result * @returns The validated body */ readValidatedBodyJson(validator) { return this.readBodyJson().then(validator); } /** * Identical to {@link ApiRequest.readBodyText}, but it validates the result. * * @param validator The validator function to use on the body parsing result * @returns The validated body */ readValidatedBodyText(validator) { return this.readBodyText().then(validator); } }; _cachedRequest = new WeakMap(); _ApiRequest_instances = new WeakSet(); isFormContentType_get = /* @__PURE__ */ __name(function() { const contentType = this.asWeb().headers.get("content-type"); if (isNullishOrEmpty(contentType)) return false; return contentType.startsWith("application/x-www-form-urlencoded") || contentType.startsWith("multipart/form-data"); }, "#isFormContentType"); __name(_ApiRequest, "ApiRequest"); var ApiRequest = _ApiRequest; export { ApiRequest }; //# sourceMappingURL=ApiRequest.mjs.map //# sourceMappingURL=ApiRequest.mjs.map