From 7c5c6a95f746d2f39c96fbd7ef3126dd235e725e Mon Sep 17 00:00:00 2001 From: matthiaaas <33065597+matthiaaas@users.noreply.github.com> Date: Tue, 7 Feb 2023 14:31:05 +0100 Subject: [PATCH] feat: restrict router accepted generic type --- src/router.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/router.ts b/src/router.ts index 06e88b7..8263682 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,4 +1,4 @@ -import type { Express } from "express" +import type { Express, Router } from "express" import path from "path" import type { Options } from "./types" @@ -10,6 +10,8 @@ import { getHandlers, getMethodKey } from "./utils" const REQUIRE_MAIN_FILE = path.dirname(require.main?.filename || process.cwd()) +type ExpressLike = Express | Router + /** * Attach routes to an Express app or router instance * @@ -20,7 +22,10 @@ const REQUIRE_MAIN_FILE = path.dirname(require.main?.filename || process.cwd()) * @param app An express app or router instance * @param options An options object (optional) */ -const createRouter = (app: T, options: Options = {}): T => { +const createRouter = ( + app: T, + options: Options = {} +): T => { const files = walkTree( options.directory || path.join(REQUIRE_MAIN_FILE, "routes") ) @@ -45,7 +50,7 @@ const createRouter = (app: T, options: Options = {}): T => { // wildcard default export route matching if (typeof exports.default !== "undefined") { - ;(app as unknown as any).all(url, ...getHandlers(exports.default)) + app.all.apply(null, [url, ...getHandlers(exports.default)]) } }