Skip to content

Commit

Permalink
fix: πŸ› middleware typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Evert De Spiegeleer committed Jan 24, 2024
1 parent 8352429 commit 0dc16a2
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/core/src/util/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export type MiddlewareHandler =
| AsyncRequestHandler
| AsyncErrorRequestHandler

interface MiddlewareProps<Handler extends MiddlewareHandler> {
interface MiddlewareProps {
name?: string
handler: Handler
handler: MiddlewareHandler
type: MiddlewareTypes
}

const log = loggerInstance.logger('zhttp:middlewareHandler')

function middlewareWrapper<Handler extends MiddlewareHandler> (
middlewareProps: MiddlewareProps<Handler>
): Handler {
function middlewareWrapper (
middlewareProps: MiddlewareProps
) {
const middlewareHandler = middlewareProps.handler
if (middlewareHandler.length === 3) {
return async function (req: Request, res: Response, next: NextFunction) {
Expand All @@ -58,7 +58,7 @@ function middlewareWrapper<Handler extends MiddlewareHandler> (
} catch (err) {
next(err)
}
} as Handler
} as RequestHandler
}

return async function (
Expand Down Expand Up @@ -91,11 +91,11 @@ function middlewareWrapper<Handler extends MiddlewareHandler> (
} catch (err) {
next(err)
}
} as Handler
} as ErrorRequestHandler
}

export class Middleware<Handler extends MiddlewareHandler = MiddlewareHandler> {
constructor (private readonly options: MiddlewareProps<Handler>) {}
export class Middleware {
constructor (private readonly options: MiddlewareProps) {}

get type () {
return this.options.type
Expand All @@ -106,8 +106,6 @@ export class Middleware<Handler extends MiddlewareHandler = MiddlewareHandler> {
}
}

export const middleware = <
Handler extends MiddlewareHandler = MiddlewareHandler,
>(
options: MiddlewareProps<Handler>
) => new Middleware<Handler>(options)
export const middleware = (
options: MiddlewareProps
) => new Middleware(options)

0 comments on commit 0dc16a2

Please sign in to comment.