@auth70/bodyguard
Version:
Fetch API compatible streaming JSON and form data body parser and guard
111 lines (110 loc) • 9.03 kB
TypeScript
import type { BodyguardValidator, JSONLike, BodyguardConfig, BodyguardError, BodyguardResult, BodyguardSuccess, BodyguardFormConfig, GenericIssue, GenericError } from "./lib.js";
import { ERRORS, MAX_DEPTH, MAX_KEYS, MAX_KEY_LENGTH, MAX_SIZE } from "./lib.js";
import { FormDataParser, JSONParser, TextParser, URLParamsParser } from "./parser.js";
export type { GenericIssue, GenericError, BodyguardError, BodyguardResult, BodyguardSuccess, BodyguardConfig, BodyguardFormConfig, BodyguardValidator, JSONLike };
export { ERRORS, MAX_DEPTH, MAX_KEYS, MAX_KEY_LENGTH, MAX_SIZE, FormDataParser, JSONParser, TextParser, URLParamsParser };
export declare class Bodyguard {
config: BodyguardConfig | BodyguardFormConfig;
/**
* Constructs a Bodyguard instance with the provided configuration or defaults to preset values.
* @param {BodyguardConfig} config - Configuration settings to initialize the Bodyguard instance.
* @param {number} config.maxKeys - Maximum number of keys.
* @param {number} config.maxDepth - Maximum depth of an object or array.
* @param {number} config.maxSize - Maximum size of a Request or Response body in bytes.
* @param {number} config.maxKeyLength - Maximum length of a key in characters.
* @param {boolean} config.castBooleans - Whether to cast boolean values to boolean type.
* @param {boolean} config.castNumbers - Whether to cast numeric values to number type.
* @param {boolean} config.convertPluses - Whether to convert plus signs to spaces in urlencoded form data.
* @param {number} config.maxFiles - Maximum number of files; used only for multipart form data.
* @param {number} config.maxFilenameLength - Maximum length of a filename; used only for multipart form data.
* @param {string[]} config.allowedContentTypes - Allow list for content types; used only for multipart form data.
* @example
* const bodyguard = new Bodyguard({
* maxKeys: 100, // Maximum number of keys.
* maxDepth: 10, // Maximum depth of an object or array.
* maxSize: 1024 * 1024, // Maximum size of a Request or Response body in bytes.
* maxKeyLength: 100, // Maximum length of a key in characters.
* castBooleans: false, // Whether to cast boolean values to boolean type.
* castNumbers: false, // Whether to cast numeric values to number type.
* });
*/
constructor(config?: Partial<BodyguardConfig | BodyguardFormConfig>);
/**
* Attempts to parse a Request or Response body. Returns the parsed object in case of success and
* an error object in case of failure.
* @param {Request | Response} input - Request or Response to parse the body from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed body against.
* @param {Partial<BodyguardConfig | BodyguardFormConfig>} config - Optional configuration to override the default configuration.
* @returns {Promise<BodyguardResult<E, K>>} - Result of the parsing operation.
*/
softPat<T extends BodyguardValidator, E = GenericError, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig | BodyguardFormConfig>): Promise<BodyguardResult<K, E>>;
/**
* Attempts to parse a Request or Response body. Returns the parsed object in case of success and
* an error object in case of failure.
* @param {Request | Response} input - Request or Response to parse the body from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed body against.
* @param {Partial<BodyguardConfig | BodyguardFormConfig>} config - Optional configuration to override the default configuration.
* @returns {Promise<K>} - Result of the parsing operation.
* @throws {Error} - If content-type is not present or is invalid, or the body is invalid, it throws an error.
*/
pat<T extends BodyguardValidator, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig | BodyguardFormConfig>): Promise<K>;
private formInternal;
/**
* Attempts to parse a form from a Request or Response. Returns the parsed object in case of success and
* an error object in case of failure.
* @param {Request | Response} input - Request or Response to parse the form from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed form against.
* @param {Partial<BodyguardFormConfig>} config - Optional configuration to override the default configuration.
* @return {Promise<BodyguardResult<E, K>>} - Result of the parsing operation.
*/
softForm<T extends BodyguardValidator, E = GenericError, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardFormConfig>): Promise<BodyguardResult<K, E>>;
/**
* Parses a form from a Request or Response. Form could be urlencoded or multipart.
* @param {Request | Response} input - Request or Response to parse the form from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed form against.
* @param {Partial<BodyguardFormConfig>} config - Optional configuration to override the default configuration.
* @param {boolean} soft - Whether to throw an error or return an error object in case of failure.
* @return {Promise<K>} - Parsed form from the Request or Response.
* @throws {Error} - If content-type is not present or is invalid, or the form data is invalid, it throws an error.
*/
form<T extends BodyguardValidator, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardFormConfig>, soft?: boolean): Promise<K>;
private jsonInternal;
/**
* Attempts to parse JSON from a Request or Response. Returns the parsed JSON in case of success and
* an error object in case of failure.
* @param {Request | Response} input - Request or Response to parse the JSON from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed JSON against.
* @param {BodyguardConfig} config - Optional configuration to override the default configuration.
* @return {Promise<BodyguardResult<E, K>>} - Result of the parsing operation.
*/
softJson<T extends BodyguardValidator, E = GenericError, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig>): Promise<BodyguardResult<K, E>>;
/**
* Parses JSON from a Request or Response.
* @param {Request | Response} input - Request or Response to parse the JSON from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed JSON against.
* @param {BodyguardConfig} config - Optional configuration to override the default configuration.
* @return {Promise<K>} - Parsed JSON from the Request or Response.
* @throws {Error} - If JSON parsing fails, it throws an error.
*/
json<T extends BodyguardValidator, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : JSONLike>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig>): Promise<K>;
private textInternal;
/**
* Attempts to parse text from a Request or Response. Returns the parsed text in case of success and
* an error object in case of failure.
* @param {Request | Response} input - Request or Response to parse the text from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed text against.
* @param {BodyguardConfig} config - Optional configuration to override the default configuration.
* @returns {Promise<BodyguardResult<K>>} - Result of the parsing operation.
*/
softText<T extends BodyguardValidator, E = GenericError, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : string>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig>): Promise<BodyguardResult<K, E>>;
/**
* Parses text from a Request or Response.
* @param {Request | Response} input - Request or Response to parse the text from.
* @param {BodyguardValidator} validator - Optional validator to validate the parsed text against.
* @param {BodyguardConfig} config - Optional configuration to override the default configuration.
* @returns {Promise<K>} - Parsed text from the Request or Response.
* @throws {Error} - If text parsing fails, it throws an error.
*/
text<T extends BodyguardValidator, K extends JSONLike = T extends BodyguardValidator ? ReturnType<T> : string>(input: Request | Response, validator?: T, config?: Partial<BodyguardConfig>): Promise<K>;
private constructConfig;
}