import { Request, Response, NextFunction, RequestHandler } from 'express';

/**
 * Wraps an async Express handler to forward rejected promises to next().
 * Required because Express 4.x does not natively handle async route errors.
 *
 * @param fn - Async Express handler function.
 * @returns Synchronous Express RequestHandler.
 */
export function asyncHandler(
  fn: (req: Request, res: Response, next: NextFunction) => Promise<void>,
): RequestHandler {
  return (req, res, next) => {
    fn(req, res, next).catch(next);
  };
}
