@sapphire/plugin-api
Version:
Plugin for @sapphire/framework to expose a REST API
189 lines (186 loc) • 6.52 kB
JavaScript
;
var utilities = require('@sapphire/utilities');
var http = require('http');
var RequestProxy_cjs = require('../../utils/_body/RequestProxy.cjs');
var __defProp = Object.defineProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
var _cachedRequest, _ApiRequest_instances, isFormContentType_get;
var _ApiRequest = class _ApiRequest extends http.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_cjs.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 (utilities.isNullishOrEmpty(contentType)) return false;
return contentType.startsWith("application/x-www-form-urlencoded") || contentType.startsWith("multipart/form-data");
}, "#isFormContentType");
__name(_ApiRequest, "ApiRequest");
var ApiRequest = _ApiRequest;
exports.ApiRequest = ApiRequest;
//# sourceMappingURL=ApiRequest.cjs.map
//# sourceMappingURL=ApiRequest.cjs.map