Skip to content

Instantly share code, notes, and snippets.

@yawaramin
Last active September 13, 2019 00:31
Show Gist options
  • Save yawaramin/9747c3e4221a6688125320b59150959f to your computer and use it in GitHub Desktop.
Save yawaramin/9747c3e4221a6688125320b59150959f to your computer and use it in GitHub Desktop.
// @flow
import type { $Request, $Response, Middleware, NextFunction, Router } from 'express'
type Path = express$Path | express$Path[]
export type TypedMiddleware<A> =
| ((req: $Request & A, res: $Response, next: NextFunction) => mixed)
| ((error: Error, req: Request & A, res: $Response, next: NextFunction) => mixed)
export const TypedRouter = {
get2<R: Router, A1, A2>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>): R {
return router.get(path, coerce(middleware1), coerce(middleware2))
},
get3<R: Router, A1, A2, A3>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3))
},
get4<R: Router, A1, A2, A3, A4>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4))
},
get5<R: Router, A1, A2, A3, A4, A5>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5))
},
get6<R: Router, A1, A2, A3, A4, A5, A6>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6))
},
get7<R: Router, A1, A2, A3, A4, A5, A6, A7>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>, middleware7: TypedMiddleware<A7>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6), coerce(middleware7))
},
get8<R: Router, A1, A2, A3, A4, A5, A6, A7, A8>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>, middleware7: TypedMiddleware<A7>, middleware8: TypedMiddleware<A8>): R {
return router.get(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6), coerce(middleware7), coerce(middleware8))
},
use2<R: Router, A1, A2>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>): R {
return router.use(path, coerce(middleware1), coerce(middleware2))
},
use3<R: Router, A1, A2, A3>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3))
},
use4<R: Router, A1, A2, A3, A4>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4))
},
use5<R: Router, A1, A2, A3, A4, A5>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5))
},
use6<R: Router, A1, A2, A3, A4, A5, A6>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6))
},
use7<R: Router, A1, A2, A3, A4, A5, A6, A7>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>, middleware7: TypedMiddleware<A7>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6), coerce(middleware7))
},
use8<R: Router, A1, A2, A3, A4, A5, A6, A7, A8>(router: R, path: Path, middleware1: TypedMiddleware<A1>, middleware2: TypedMiddleware<A2>, middleware3: TypedMiddleware<A3>, middleware4: TypedMiddleware<A4>, middleware5: TypedMiddleware<A5>, middleware6: TypedMiddleware<A6>, middleware7: TypedMiddleware<A7>, middleware8: TypedMiddleware<A8>): R {
return router.use(path, coerce(middleware1), coerce(middleware2), coerce(middleware3), coerce(middleware4), coerce(middleware5), coerce(middleware6), coerce(middleware7), coerce(middleware8))
},
}
function coerce<A>(any: any): A {
return any
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment