import { compileExpression } from 'filtrex';
interface FiltrexError {
I18N_STRING: string;
message: string;
}
// filtrex returns a type of `(obj: any) => any`. eslint is not happy
// about the `any` types. But we want to use that here so we ignore the warning
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const conditionFunctionCache = new Map<string, (obj: any) => any>();
export function isConditionMet(
condition: string,
environment: Record<string, unknown>,
storeQuery: (key: string) => unknown
): boolean {
if (condition.trim().length === 0) {
return true;
}
let conditionFunction = conditionFunctionCache.get(condition);
if (!conditionFunction) {
conditionFunction = compileExpression(condition, {
extraFunctions: {
s: storeQuery
}
});
// filtrex bakes the store value into the compiled expression,
// so we can't cache conditions that use the store.
const hasStoreQuery = condition.includes('s(');
if (!hasStoreQuery) {
conditionFunctionCache.set(condition, conditionFunction);
}
}
const result: boolean | FiltrexError | undefined = conditionFunction(environment);
if (result && isFiltrexError(result)) {
console.warn(`ACL: Error evaluating the condition "${condition}": ${result.message}`);
}
return Boolean(result);
}
function isFiltrexError(result: boolean | FiltrexError): result is FiltrexError {
return (result as FiltrexError).I18N_STRING !== undefined;
}