astro-ghostcms/.pnpm-store/v3/files/d3/9be6d712126cc0e93037b3a23af...

29 lines
711 B
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
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
};