UNPKG

@sapphire/plugin-api

Version:

Plugin for @sapphire/framework to expose a REST API

1 lines 8.63 kB
{"version":3,"sources":["../../../../../src/lib/structures/api/ApiRequest.ts"],"names":["IncomingMessage","RequestProxy","isNullishOrEmpty"],"mappings":";;;;;;;;;;;;;;;;;AAAA,IAAA,cAAA,EAAA,qBAAA,EAAA,qBAAA;AAUO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmBA,oBAAgB,CAAA;AAAA,EAAzC,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA;AAAA,IAAA,YAAA,CAAA,IAAA,EAAA,qBAAA,CAAA;AAIN;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,SAA2C,EAAC,CAAA;AAKnD;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,UAAiC,EAAC,CAAA;AASzC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,YAAA,CAAA;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,OAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAiC,YAAA,CAAA,IAAA,EAAA,cAAA,EAAA,IAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAe1B,KAAiB,GAAA;AACvB,IAAA,YAAA,CAAA,IAAA,EAAK,cAAL,CAAA,IAAA,YAAA,CAAA,IAAA,EAAK,cAAmB,EAAA,IAAIC,8BAAa,IAAI,CAAA,CAAA;AAC7C,IAAA,OAAO,YAAK,CAAA,IAAA,EAAA,cAAA,CAAA;AAAA;AACb;AAAA;AAAA;AAAA;AAAA;AAAA;AA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A,YAAA,EAAe,CAAA,IAAA,CAAK,SAAS,CAAA;AAAA;AAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,sBAA4B,SAA4C,EAAA;AAC9E,IAAA,OAAO,IAAK,CAAA,YAAA,EAAe,CAAA,IAAA,CAAK,SAAS,CAAA;AAAA;AAE3C,CAAA;AA/HC,cAAA,GAAA,IAAA,OAAA,EAAA;AApDM,qBAAA,GAAA,IAAA,OAAA,EAAA;AAsDF,qBAAA,mBAAqB,MAAA,CAAA,WAAA;AACxB,EAAA,MAAM,cAAc,IAAK,CAAA,KAAA,EAAQ,CAAA,OAAA,CAAQ,IAAI,cAAc,CAAA;AAG3D,EAAI,IAAAC,0BAAA,CAAiB,WAAW,CAAA,EAAU,OAAA,KAAA;AAG1C,EAAA,OAAO,YAAY,UAAW,CAAA,mCAAmC,CAAK,IAAA,WAAA,CAAY,WAAW,qBAAqB,CAAA;AACnH,CARsB,EAAA,oBAAA,CAAA;AAtDyB,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA;AAAzC,IAAM,UAAN,GAAA","file":"ApiRequest.cjs","sourcesContent":["import { isNullishOrEmpty } from '@sapphire/utilities';\nimport type { Blob } from 'node:buffer';\nimport { IncomingMessage } from 'node:http';\nimport type { FormData, Request } from 'undici';\nimport type { MimeType } from '@sapphire/iana-mime-types';\nimport { RequestProxy } from '../../utils/_body/RequestProxy';\nimport type { Route } from '../Route';\nimport type { AuthData } from '../http/Auth';\nimport type { RouterNode } from '../router/RouterNode';\n\nexport class ApiRequest extends IncomingMessage {\n\t/**\n\t * The query parameters.\n\t */\n\tpublic query: Record<string, string | string[]> = {};\n\n\t/**\n\t * The URI parameters.\n\t */\n\tpublic params: Record<string, string> = {};\n\n\t/**\n\t * The authorization information. This field indicates three possible values:\n\t *\n\t * - `undefined`: The authorization middleware has not been executed yet.\n\t * - `null`: The user is not authorized.\n\t * - `AuthData`: The user is authorized.\n\t */\n\tpublic auth?: AuthData | null;\n\n\t/**\n\t * The router node that matched the request. The field indicates three\n\t * possible values:\n\t *\n\t * - `undefined`: The router handler has not been executed yet.\n\t * - `null`: The router handler has been executed, but no node matched the\n\t * request.\n\t * - `RouterNode`: The router handler has been executed and a node matched\n\t * the request.\n\t *\n\t * @since 7.0.0\n\t */\n\tpublic routerNode?: RouterNode | null;\n\n\t/**\n\t * The route that matched the request. The field indicates three possible\n\t * values:\n\t *\n\t * - `undefined`: The router handler has not been executed yet.\n\t * - `null`: The router handler has been executed, but no route matched the\n\t * request.\n\t * - `Route`: The router handler has been executed and a route matched the\n\t * request.\n\t *\n\t * @since 7.0.0\n\t */\n\tpublic route?: Route | null;\n\n\t/**\n\t * The response object. This field is cached to prevent multiple response\n\t * objects from being created.\n\t */\n\t#cachedRequest: Request | null = null;\n\n\tget #isFormContentType() {\n\t\tconst contentType = this.asWeb().headers.get('content-type') as MimeType | null;\n\n\t\t// If Content-Type isn't sent, we can't assume it's a form.\n\t\tif (isNullishOrEmpty(contentType)) return false;\n\n\t\t// If the Content-Type is application/x-www-form-urlencoded or multipart/form-data, it's a form.\n\t\treturn contentType.startsWith('application/x-www-form-urlencoded') || contentType.startsWith('multipart/form-data');\n\t}\n\n\t/**\n\t * The response object, used to validate the request's headers and body.\n\t */\n\tpublic asWeb(): Request {\n\t\tthis.#cachedRequest ??= new RequestProxy(this);\n\t\treturn this.#cachedRequest;\n\t}\n\n\t/**\n\t * Reads the request body and tries to parse using JSON or form-urlencoded.\n\t *\n\t * @example\n\t * ```typescript\n\t * const body = await request.readBody();\n\t * ```\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBody(): Promise<unknown> {\n\t\treturn this.#isFormContentType ? this.readBodyFormData() : this.readBodyJson();\n\t}\n\n\t/**\n\t * Reads the request body as an {@link ArrayBuffer}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyArrayBuffer(): Promise<ArrayBuffer> {\n\t\treturn this.asWeb().arrayBuffer();\n\t}\n\n\t/**\n\t * Reads the request body as a {@link Blob}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyBlob(): Promise<Blob> {\n\t\treturn this.asWeb().blob();\n\t}\n\n\t/**\n\t * Reads the request body as a {@link FormData}.\n\t *\n\t * @remarks\n\t *\n\t * This will throw an error if the content type is not one of the following:\n\t *\n\t * - `application/x-www-form-urlencoded`\n\t * - `multipart/form-data`\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyFormData(): Promise<FormData> {\n\t\treturn this.asWeb().formData(); // NOSONAR\n\t}\n\n\t/**\n\t * Reads the request body as text, using {@link TextDecoder}. Afterward, it\n\t * parses the body as JSON with {@link JSON.parse}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyJson(): Promise<unknown> {\n\t\treturn this.asWeb().json();\n\t}\n\n\t/**\n\t * Reads the request body as text, using {@link TextDecoder}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyText(): Promise<string> {\n\t\treturn this.asWeb().text();\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBody}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBody<Type>(validator: ValidatorFunction<unknown, Type>) {\n\t\treturn this.readBody().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyFormData}, but it validates the\n\t * result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyFormData<Type>(validator: ValidatorFunction<FormData, Type>) {\n\t\treturn this.readBodyFormData().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyJson}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyJson<Type>(validator: ValidatorFunction<unknown, Type>) {\n\t\treturn this.readBodyJson().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyText}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyText<Type>(validator: ValidatorFunction<string, Type>) {\n\t\treturn this.readBodyText().then(validator);\n\t}\n}\n\nexport type ValidatorFunction<Data, Type> = (data: Data) => Type;\n"]}