Typescript v5 + eslint 8.57 WIP

This commit also improves some type handling in the project.
This commit is contained in:
John Livingston
2024-09-07 14:49:27 +02:00
parent 64a9c7be21
commit 7b3d93b290
41 changed files with 2652 additions and 3054 deletions

View File

@ -6,22 +6,57 @@ import { eachSeries } from 'async'
import type { 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
export type RequestPromiseHandler = ((req: Request, res: Response, next: NextFunction) => Promise<any>)
function asyncMiddleware (fun: RequestPromiseHandler | RequestPromiseHandler[]): RequestHandler {
// 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) => {
Promise.resolve(f(req, res, (err: any) => cb(err)))
.catch(err => next(err))
// 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))
.catch(err => { next(err) })
}
}