@nova-ts/core
Version:
A serverside framework used to build scalable application
54 lines (52 loc) • 2.09 kB
JavaScript
import {
matchUriPatterns
} from "./chunk-BFVCNYWH.js";
import {
NovaConstant
} from "./chunk-INEUQFI4.js";
// src/Resolver/NovaFilterExecutor.ts
import { ApplicationContext } from "@nova-ts/context";
var NovaFilterExecutor = class {
/**
* Executes the filter chain and then invokes the provided controller function.
*
* Filters are executed sequentially. If any filter does not call `chain()` or terminates
* the response (e.g., `res.send()`), the controller will not be invoked.
*
* This method ensures that the controller logic runs **only after** all filters
* have passed control through `chain()`.
*
* @param {any} req - The Nova request object.
* @param {any} res - The Nova response object.
* @param {() => Promise<any>} controllerInvoker - A function that executes the controller method.
* @returns {Promise<any>} The result of the controller method if invoked, or `undefined` if short-circuited by a filter.
*/
async execute(req, res, controllerInvoker) {
const filters = ApplicationContext.getAll(NovaConstant.NovaFilter);
const matchingFilters = filters.filter((filter) => {
const meta = Reflect.getMetadata(NovaConstant.NovaFilterMeta, filter.constructor);
return matchUriPatterns(req.path, meta.URIS);
}).sort((a, b) => {
const aMeta = Reflect.getMetadata(NovaConstant.NovaFilterMeta, a.constructor);
const bMeta = Reflect.getMetadata(NovaConstant.NovaFilterMeta, b.constructor);
return aMeta.order - bMeta.order;
});
let currentchain = -1;
const chain = async (index) => {
if (index <= currentchain) throw new Error("chain() called multiple times in filter chain");
currentchain = index;
if (index >= matchingFilters.length) {
const result = await controllerInvoker();
res.body = result;
return;
}
const filter = matchingFilters[index];
return await filter.applyFilter(req, res, () => chain(index + 1));
};
return await chain(0);
}
};
export {
NovaFilterExecutor
};
//# sourceMappingURL=chunk-46BS5H2W.js.map