You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
803 B
31 lines
803 B
import { Context } from '@/middleware/Context';
|
|
|
|
type RunMiddleware = (context: Context) => void;
|
|
function nextFactory(
|
|
context: Context,
|
|
middlewares: Array<RunMiddleware>,
|
|
index: number
|
|
) {
|
|
const subsequentMiddleware = middlewares[index];
|
|
if (!subsequentMiddleware) return context.next;
|
|
|
|
return (...args: any[]) => {
|
|
// @ts-ignore
|
|
context.next(...args);
|
|
subsequentMiddleware({
|
|
...context,
|
|
next: nextFactory(context, middlewares, index++),
|
|
});
|
|
};
|
|
}
|
|
export function runMiddleware(context: Context) {
|
|
const { to } = context;
|
|
const middlewares = [
|
|
...((Array.isArray(to.meta.middleware)
|
|
? to.meta.middleware
|
|
: [to.meta.middleware]) as Array<RunMiddleware>),
|
|
];
|
|
|
|
middlewares[0]({ ...context, next: nextFactory(context, middlewares, 1) });
|
|
}
|