X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fasync.ts;h=dd209b115d2d46cb4701896dbdf5900e7c9d13a8;hb=4195cd2bc5046d4cdf1c677c27cd41f427d7a13a;hp=29ebd169d4a3e911adc2f86a22226394fcc4068b;hpb=eb08047657e739bcd9e592d76307befa3998482b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/async.ts b/server/middlewares/async.ts index 29ebd169d..dd209b115 100644 --- a/server/middlewares/async.ts +++ b/server/middlewares/async.ts @@ -1,11 +1,22 @@ -import { Request, Response, NextFunction } from 'express' +import { eachSeries } from 'async' +import { NextFunction, Request, RequestHandler, Response } from 'express' // Syntactic sugar to avoid try/catch in express controllers // Thanks: https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 -function asyncMiddleware (fn: (req: Request, res: Response, next: NextFunction) => Promise) { + +export type RequestPromiseHandler = (req: Request, res: Response, next: NextFunction) => Promise + +function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]) { return (req: Request, res: Response, next: NextFunction) => { - return Promise.resolve(fn(req, res, next)) - .catch(next) + if (Array.isArray(fun) === true) { + return eachSeries(fun as RequestHandler[], (f, cb) => { + Promise.resolve(f(req, res, cb)) + .catch(err => next(err)) + }, next) + } + + return Promise.resolve((fun as RequestHandler)(req, res, next)) + .catch(err => next(err)) } }