API Reference
Complete API documentation for Bantai core functions
API Reference
Complete API documentation for all Bantai core functions and types.
Overview
Bantai provides a set of core functions and types for building policy evaluation systems:
- Functions - Core functions for creating contexts, rules, and policies
- Types - Type definitions for contexts, rules, policies, and results
- Utility Types - Helper types for extracting context information
- Function Signatures - Type signatures for rule and hook functions
Quick Reference
Core Functions
defineContext- Create a context definitiondefineRule- Define a rule within a contextdefinePolicy- Define a policy that combines rulesevaluatePolicy- Evaluate a policy against inputallow- Create an allow resultdeny- Create a deny result
Core Types
ContextDefinition- Context typeRuleDefinition- Rule typePolicyDefinition- Policy typeRuleResult- Rule evaluation resultPolicyResult- Policy evaluation resultViolatedRule- Violated rule informationEvaluatedRule- Evaluated rule information (all rules)PolicyStrategy- Evaluation strategy type
Complete Example
import { z } from 'zod';
import {
defineContext,
defineRule,
definePolicy,
evaluatePolicy,
allow,
deny,
} from '@bantai-dev/core';
// Define context
const context = defineContext(
z.object({
userId: z.string(),
age: z.number(),
})
);
// Define rules
const ageRule = defineRule(context, 'age-check', async (input) => {
if (input.age >= 18) {
return allow({ reason: 'User is of legal age' });
}
return deny({ reason: 'User must be 18 or older' });
});
const userRule = defineRule(context, 'user-check', async (input) => {
if (input.userId.startsWith('admin')) {
return allow({ reason: 'User is admin' });
}
return deny({ reason: 'User is not admin' });
});
// Define policy
const policy = definePolicy(context, 'access-policy', [ageRule, userRule], {
defaultStrategy: 'preemptive',
});
// Evaluate policy
const result = await evaluatePolicy(policy, {
userId: 'admin123',
age: 25,
});
console.log(result.decision); // 'allow' or 'deny'
console.log(result.isAllowed); // true or false
console.log(result.violatedRules); // Array of violations
console.log(result.evaluatedRules); // Array of all evaluated rulesRelated Documentation
- Concepts - Understand how these APIs work together
- Examples - See real-world usage examples
- Getting Started - Learn the basics