Bantai
BANTAI

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 definition
  • defineRule - Define a rule within a context
  • definePolicy - Define a policy that combines rules
  • evaluatePolicy - Evaluate a policy against input
  • allow - Create an allow result
  • deny - Create a deny result

Core Types

  • ContextDefinition - Context type
  • RuleDefinition - Rule type
  • PolicyDefinition - Policy type
  • RuleResult - Rule evaluation result
  • PolicyResult - Policy evaluation result
  • ViolatedRule - Violated rule information
  • EvaluatedRule - 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 rules

On this page