UNPKG

@noony-serverless/core

Version:

A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript

141 lines 4.2 kB
import { BaseMiddleware, Context, NoonyRequest } from '../core'; /** * Configuration for body parsing functionality */ export interface BodyParserConfig { maxSize?: number; supportPubSub?: boolean; allowEmptyBody?: boolean; customParser?: (body: unknown) => Promise<unknown> | unknown; enableAsyncParsing?: boolean; asyncThreshold?: number; } /** * Configuration for query parameter processing */ export interface QueryProcessingConfig { parseArrays?: boolean; parseNumbers?: boolean; parseBooleans?: boolean; maxKeys?: number; delimiter?: string; arrayDelimiter?: string; customParser?: (query: Record<string, unknown>) => Record<string, unknown>; } /** * Configuration for HTTP attributes extraction */ export interface AttributesConfig { extractIP?: boolean; extractUserAgent?: boolean; extractTimestamp?: boolean; extractContentLength?: boolean; extractAcceptLanguage?: boolean; extractReferer?: boolean; customExtractors?: Record<string, (req: NoonyRequest) => unknown>; trustProxy?: boolean; } /** * Complete configuration for ProcessingMiddleware */ export interface ProcessingMiddlewareConfig { parser?: BodyParserConfig; query?: QueryProcessingConfig; attributes?: AttributesConfig; skipProcessing?: <TBody, TUser>(context: Context<TBody, TUser>) => boolean; } /** * Consolidated ProcessingMiddleware that combines body parsing, query processing, and attribute extraction. * * This middleware replaces the need for separate: * - BodyParserMiddleware * - QueryParametersMiddleware * - HttpAttributesMiddleware * * @template TBody - The type of the request body payload (preserves type chain) * @template TUser - The type of the authenticated user (preserves type chain) * * @example * Complete processing setup: * ```typescript * const handler = new Handler() * .use(new ProcessingMiddleware({ * parser: { * maxSize: 1024 * 1024, // 1MB * supportPubSub: true, * enableAsyncParsing: true * }, * query: { * parseArrays: true, * parseNumbers: true, * parseBooleans: true, * maxKeys: 100 * }, * attributes: { * extractIP: true, * extractUserAgent: true, * extractTimestamp: true, * trustProxy: true * } * })) * .handle(async (context) => { * // context.req.parsedBody contains parsed JSON * // context.req.query contains processed query parameters * // context.req.ip, context.req.userAgent, etc. are extracted * return { message: 'Processing complete' }; * }); * ``` * * @example * Parser-only for API endpoints: * ```typescript * const handler = new Handler() * .use(new ProcessingMiddleware({ * parser: { * maxSize: 512 * 1024, // 512KB * supportPubSub: false * } * })); * ``` */ export declare class ProcessingMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> { private config; constructor(config?: ProcessingMiddlewareConfig); before(context: Context<TBody, TUser>): Promise<void>; private extractAttributes; private processQueryParameters; private parseBody; private parseStringBody; private parsePubSubMessage; private processQueryValue; private extractIPAddress; private extractUserAgent; private extractContentLength; private extractAcceptLanguage; private extractReferer; private isPubSubMessage; private validateBase64Format; private parseJsonAsync; } /** * Factory functions for creating ProcessingMiddleware with common configurations */ export declare const createProcessingMiddleware: { /** * API processing with JSON parsing and basic attributes */ api: () => ProcessingMiddleware; /** * PubSub processing with base64 decoding */ pubsub: () => ProcessingMiddleware; /** * Lightweight processing for simple endpoints */ lightweight: () => ProcessingMiddleware; /** * Full processing with all features enabled */ complete: () => ProcessingMiddleware; }; //# sourceMappingURL=ProcessingMiddleware.d.ts.map