UNPKG

trpc-shield

Version:

tRPC permissions as another layer of abstraction!

95 lines 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isRule = isRule; exports.isLogicRule = isLogicRule; exports.isRuleFunction = isRuleFunction; exports.isRuleFieldMap = isRuleFieldMap; exports.flattenObjectOf = flattenObjectOf; exports.withDefault = withDefault; const rules_1 = require("./rules"); /** * * @param x * * Makes sure that a certain field is a rule. * */ function isRule(x) { return x instanceof rules_1.Rule || ((x === null || x === void 0 ? void 0 : x.constructor) && x.constructor.name === 'Rule'); } /** * * @param x * * Makes sure that a certain field is a logic rule. * */ function isLogicRule(x) { return (x instanceof rules_1.LogicRule || ((x === null || x === void 0 ? void 0 : x.constructor) && (x.constructor.name === 'RuleOr' || x.constructor.name === 'RuleAnd' || x.constructor.name === 'RuleChain' || x.constructor.name === 'RuleRace' || x.constructor.name === 'RuleNot' || x.constructor.name === 'RuleTrue' || x.constructor.name === 'RuleFalse'))); } /** * * @param x * * Makes sure that a certain field is a rule or a logic rule. * */ function isRuleFunction(x) { return isRule(x) || isLogicRule(x); } /** * * @param x * * Determines whether a certain field is rule field map or not. * */ function isRuleFieldMap(x) { return typeof x === 'object' && Object.values(x).every((rule) => isRuleFunction(rule)); } /** * * @param obj * @param func * * Flattens object of particular type by checking if the leaf * evaluates to true from particular function. * */ function flattenObjectOf(obj, f) { const values = Object.keys(obj).reduce((acc, key) => { const val = obj[key]; if (f(val)) { return [...acc, val]; } else if (typeof val === 'object' && !f(val)) { return [...acc, ...flattenObjectOf(val, f)]; } else { return acc; } }, []); return values; } /** * * Returns fallback is provided value is undefined * * @param fallback */ function withDefault(fallback) { return (value) => { if (value === undefined) return fallback; return value; }; } //# sourceMappingURL=utils.js.map