peertube-plugin-livechat/server/lib/middlewares/async.ts
John Livingston 7b3d93b290
Typescript v5 + eslint 8.57 WIP
This commit also improves some type handling in the project.
2024-09-09 15:57:04 +02:00

66 lines
2.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import { eachSeries } from 'async'
import type { NextFunction, Request, RequestHandler, Response } from 'express'
// Syntactic sugar to avoid try/catch in express controllers
export type RequestPromiseHandler = ((req: Request, res: Response, next: NextFunction) => Promise<any>)
// type asyncMiddleWareFunction = (req: Request, res: Response, next: NextFunction) => Promise<void>
// function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]): asyncMiddleWareFunction {
// return async (req: Request, res: Response, next: NextFunction): Promise<void> => {
// if (!Array.isArray(fun)) {
// // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
// Promise.resolve((fun as RequestHandler)(req, res, next))
// .catch(err => { next(err) })
// return
// }
// try {
// for (const f of fun) {
// await new Promise<void>((resolve, reject) => {
// // eslint-disable-next-line @typescript-eslint/no-floating-promises
// asyncMiddleware(f)(req, res, err => {
// if (err) {
// reject(err)
// return
// }
// resolve()
// })
// })
// }
// next()
// } catch (err) {
// next(err)
// }
// }
// }
function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]) {
return (req: Request, res: Response, next: NextFunction) => {
if (Array.isArray(fun)) {
eachSeries(fun as RequestHandler[], (f, cb) => {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
Promise.resolve(f(req, res, (err: any) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
cb(err)
}))
.catch(err => { next(err) })
}, next)
return
}
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
Promise.resolve((fun as RequestHandler)(req, res, next))
.catch(err => { next(err) })
}
}
export {
asyncMiddleware
}