/** * Express Route Helpers * * Utility functions for working with Express routes. * These helpers reduce boilerplate for common operations like * extracting query parameters and parsing request bodies. * * @example Query params with Zod validation * ```typescript * import { extractQueryParams } from '@plyaz/core/frameworks/express'; * import { z } from 'zod'; * * const querySchema = z.object({ * page: z.coerce.number().default(1), * limit: z.coerce.number().default(10), * search: z.string().optional(), * }); * * app.get('/users', (req, res) => { * const { page, limit, search } = extractQueryParams(req, querySchema); * // Use typed params... * }); * ``` * * @example Body parsing with validation * ```typescript * import { extractJsonBody } from '@plyaz/core/frameworks/express'; * import { z } from 'zod'; * * const createUserSchema = z.object({ * name: z.string(), * email: z.string().email(), * }); * * app.post('/users', (req, res) => { * const body = extractJsonBody(req, createUserSchema); * // body is typed as { name: string; email: string } * }); * ``` */ import type { CoreExpressRequest } from '@plyaz/types/core'; interface ZodLikeSchema { parse: (data: unknown) => T; safeParse: (data: unknown) => { success: true; data: T; } | { success: false; error: { message: string; }; }; } /** * Extract and parse query parameters from an Express request. * * @param req - Express request object * @param schema - Zod schema for validation (optional) * @returns Parsed and validated query parameters * @throws If validation fails * * @example Without validation * ```typescript * const params = extractQueryParams(req); * // params: Record * ``` * * @example With Zod validation * ```typescript * const schema = z.object({ * page: z.coerce.number().default(1), * limit: z.coerce.number().max(100).default(20), * }); * * const { page, limit } = extractQueryParams(req, schema); * // page: number, limit: number * ``` */ export declare function extractQueryParams(req: CoreExpressRequest, schema: ZodLikeSchema): T; export declare function extractQueryParams(req: CoreExpressRequest): Record; /** * Extract and parse JSON body from an Express request. * NOTE: Requires body-parser middleware to be configured. * * @param req - Express request object * @param schema - Zod schema for validation (optional) * @returns Parsed and validated body * @throws If validation fails * * @example Without validation * ```typescript * const body = extractJsonBody(req); * // body: unknown * ``` * * @example With Zod validation * ```typescript * const schema = z.object({ * name: z.string(), * email: z.string().email(), * }); * * const body = extractJsonBody(req, schema); * // body: { name: string; email: string } * ``` */ export declare function extractJsonBody(req: CoreExpressRequest, schema: ZodLikeSchema): T; export declare function extractJsonBody(req: CoreExpressRequest): unknown; /** * Safely extract and parse JSON body from an Express request. * Returns null if parsing fails instead of throwing. * * @param req - Express request object * @param schema - Zod schema for validation (optional) * @returns Parsed body or null if parsing failed * * @example * ```typescript * const body = safeExtractJsonBody(req, schema); * if (!body) { * res.status(400).json({ error: 'Invalid request body' }); * return; * } * ``` */ export declare function safeExtractJsonBody(req: CoreExpressRequest, schema: ZodLikeSchema): T | null; export declare function safeExtractJsonBody(req: CoreExpressRequest): unknown | null; /** * Extract route parameters from Express request. * * @param req - Express request object * @param schema - Zod schema for validation (optional) * @returns Params object * * @example * ```typescript * // app/api/users/:id * app.get('/users/:id', (req, res) => { * const { id } = extractRouteParams(req); * // id: string * }); * ``` */ export declare function extractRouteParams = Record>(req: CoreExpressRequest, schema?: ZodLikeSchema): T; /** * Extract headers from an Express request as a plain object. * * @param req - Express request object * @returns Headers as a plain object * * @example * ```typescript * const headers = extractHeaders(req); * const auth = headers['authorization']; * const contentType = headers['content-type']; * ``` */ export declare function extractHeaders(req: CoreExpressRequest): Record; /** * Extract a specific header value from an Express request. * * @param req - Express request object * @param name - Header name (case-insensitive) * @returns Header value or undefined * * @example * ```typescript * const auth = extractHeader(req, 'authorization'); * if (!auth?.startsWith('Bearer ')) { * res.status(401).json({ error: 'Unauthorized' }); * return; * } * ``` */ export declare function extractHeader(req: CoreExpressRequest, name: string): string | undefined; export declare function extractBearerToken(req: CoreExpressRequest): string | null; export {}; //# sourceMappingURL=helpers.d.ts.map