29 lines
711 B
Plaintext
29 lines
711 B
Plaintext
|
import { defineMiddleware } from "./index.js";
|
||
|
function sequence(...handlers) {
|
||
|
const filtered = handlers.filter((h) => !!h);
|
||
|
const length = filtered.length;
|
||
|
if (!length) {
|
||
|
const handler = defineMiddleware((context, next) => {
|
||
|
return next();
|
||
|
});
|
||
|
return handler;
|
||
|
}
|
||
|
return defineMiddleware((context, next) => {
|
||
|
return applyHandle(0, context);
|
||
|
function applyHandle(i, handleContext) {
|
||
|
const handle = filtered[i];
|
||
|
const result = handle(handleContext, async () => {
|
||
|
if (i < length - 1) {
|
||
|
return applyHandle(i + 1, handleContext);
|
||
|
} else {
|
||
|
return next();
|
||
|
}
|
||
|
});
|
||
|
return result;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
export {
|
||
|
sequence
|
||
|
};
|