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

/**
 * Wrap an async route handler and forward errors to Express error middleware.
 * Removes the need for repetitive try/catch in every route.
 */
export function asyncHandler(fn: (req: Request, res: Response, next: NextFunction) => Promise<any> | any) {
  return (req: Request, res: Response, next: NextFunction) => {
    Promise.resolve(fn(req, res, next)).catch(next);
  };
}
