1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00
unleash.unleash/src/lib/middleware/conditional-middleware.ts
Fredrik Strand Oseberg 929f824a3a
fix: refactor conditional middleware (#2261)
* fix: refactor conditional middleware

* fix: update tests

* test: update snapshot to hide things behing flag from openapi

Co-authored-by: kwasniew <kwasniewski.mateusz@gmail.com>
2022-10-26 13:00:49 +02:00

19 lines
387 B
TypeScript

import { RequestHandler, Router } from 'express';
export const conditionalMiddleware = (
condition: () => boolean,
middleware: RequestHandler,
): RequestHandler => {
const router = Router();
router.use((req, res, next) => {
if (condition()) {
middleware(req, res, next);
} else {
next();
}
});
return router;
};