UNPKG

@auth70/bodyguard

Version:

Fetch API compatible streaming JSON and form data body parser and guard

112 lines (111 loc) 4.45 kB
export declare const MAX_KEYS = 100; export declare const MAX_DEPTH = 10; export declare const MAX_SIZE: number; export declare const MAX_KEY_LENGTH = 100; export declare const CONTENT_TYPES: string[]; export declare const ERRORS: { BODY_NOT_AVAILABLE: string; INVALID_TYPE: string; INVALID_CONTENT_TYPE: string; NO_CONTENT_TYPE: string; MAX_SIZE_EXCEEDED: string; TOO_MANY_KEYS: string; INVALID_INPUT: string; TOO_DEEP: string; KEY_TOO_LONG: string; TOO_MANY_FILES: string; FILENAME_TOO_LONG: string; }; /** * Types */ export type State = 'START' | 'KEY' | 'VALUE'; export type BodyguardValidator<T extends JSONLike = JSONLike> = (data: unknown) => T; export interface BodyguardConfig { /** The maximum number of keys */ maxKeys: number; /** The maximum depth of the object */ maxDepth: number; /** The maximum size of the input in bytes */ maxSize: number; /** The maximum length of a key */ maxKeyLength: number; /** Automatically cast numbers from strings */ castNumbers: boolean; /** Automatically cast booleans from strings */ castBooleans: boolean; } export interface BodyguardFormConfig extends BodyguardConfig { /** Convert plus signs to spaces in urlencoded form data */ convertPluses: boolean; /** The maximum number of files in a multipart form */ maxFiles: number; /** The maximum length of a filename in a multipart form */ maxFilenameLength: number; /** Allow list for content types in a multipart form */ allowedContentTypes: string[] | undefined; } export type JSONLike = { [property: string]: JSONLike; } | readonly JSONLike[] | string | number | boolean | File | null; /** * A standard generic issue. This is based on the Zod issue type, but may be thrown by other libraries through a possible rethrowing adapter. */ export type GenericIssue = { code: string; path: (string | number)[]; message: string; minimum?: number | bigint; maximum?: number | bigint; exact?: boolean; inclusive?: boolean; validation?: any; }; export type GenericError = { issues?: GenericIssue[]; message?: string; }; export type BodyguardError<ErrorType = GenericError, ValueType extends JSONLike = Record<string, any>> = { success: false; /** The error message */ error: ErrorType; /** The value that was being processed. May be undefined if the error occurred before the value was processed. */ value?: ValueType; }; export type BodyguardSuccess<ValueType extends JSONLike = Record<string, any>> = { success: true; value: ValueType; }; export type BodyguardResult<ValueType extends JSONLike = Record<string, any>, ErrorType = GenericError> = BodyguardSuccess<ValueType> | BodyguardError<ErrorType, ValueType>; /** * Utility functions */ /** * Create a byte stream counter. This is a transform stream that counts the number of bytes. If the number of bytes exceeds the maxSize, it will throw an error. * @param {ReadableStream<Uint8Array>} stream - The input stream * @param {number} maxSize - The maximum number of bytes * @param {(reason?: any) => void} reject - A reject function to call when the max size is exceeded * @returns {TransformStream<Uint8Array>} - The transform stream */ export declare function createByteStreamCounter(stream: ReadableStream<Uint8Array>, maxSize: number, reject?: (reason?: any) => void): TransformStream<Uint8Array<ArrayBufferLike>, any>; /** * Possible cast a value to a number or boolean if it matches the criteria. * Also converts plus signs to spaces if convertPluses is true. * @param {string} value - The value to cast * @param {BodyguardConfig | BodyguardFormConfig} config - The configuration * @returns {string | number | boolean} - The casted value */ export declare function possibleCast(value: string, config: BodyguardConfig | BodyguardFormConfig): string | number | boolean; /** * Assign a nested value to an object * @param {Record<string, any>} obj - The object to assign to * @param {string[]} path - The path to assign to * @param {any} value - The value to assign */ export declare function assignNestedValue(obj: Record<string, any>, path: string[], value: any): void; /** * Extract a nested key into an array of segments * @param {string} keyName - The key name * @returns {string[]} - The segments */ export declare function extractNestedKey(keyName: string): string[];