TypeScript Types
Complete TypeScript type definitions for Fire Shield RBAC library.
Core Types
RBACUser
Represents a user in the RBAC system.
interface RBACUser {
/**
* Unique identifier for the user
*/
id: string;
/**
* Array of role names assigned to the user
*/
roles: string[];
/**
* Optional direct permissions (additive to role permissions)
*/
permissions?: string[];
/**
* Optional permission bitmask (for bit-based system)
*/
permissionMask?: number;
}Example:
const user: RBACUser = {
id: 'user-123',
roles: ['editor', 'moderator'],
permissions: ['beta:feature'],
permissionMask: 127
};AuthorizationResult
Result of an authorization check with detailed information.
interface AuthorizationResult {
/**
* Whether the permission is allowed
*/
allowed: boolean;
/**
* Reason for denial (only present when allowed = false)
*/
reason?: string;
/**
* The user object that was checked
*/
user?: RBACUser;
}Example:
const result: AuthorizationResult = rbac.authorize(user, 'admin:delete');
if (!result.allowed) {
console.log(result.reason); // "User lacks permission: admin:delete"
}AuthorizationContext
Context for authorization with additional metadata.
interface AuthorizationContext {
/**
* User to check authorization for
*/
user: RBACUser;
/**
* Resource being accessed
*/
resource: string;
/**
* Action being performed
*/
action: string;
/**
* Optional additional context
*/
metadata?: Record<string, any>;
}Example:
const context: AuthorizationContext = {
user: currentUser,
resource: 'document',
action: 'edit',
metadata: {
documentId: 'doc-123',
ownerId: 'user-456'
}
};
const result = rbac.authorizeWithContext(context);Configuration Types
RBACConfig
Main configuration object for RBAC instance.
interface RBACConfig {
/**
* Use bit-based permission system
* @default true
*/
useBitSystem?: boolean;
/**
* Enable strict mode (throws errors on invalid operations)
* @default false
*/
strictMode?: boolean;
/**
* Enable wildcard permission matching
* @default true
*/
enableWildcards?: boolean;
/**
* Preset configuration to load
*/
preset?: PresetConfig;
/**
* Configuration schema
*/
config?: RBACConfigSchema;
/**
* Optional audit logger
*/
auditLogger?: AuditLogger;
}Example:
const config: RBACConfig = {
useBitSystem: true,
strictMode: true,
enableWildcards: true,
auditLogger: new ConsoleAuditLogger()
};
const rbac = new RBAC(config);RBACConfigSchema
Schema for defining permissions and roles.
interface RBACConfigSchema {
/**
* Permission definitions
*/
permissions: Array<{
name: string;
bit?: number;
resource?: string;
action?: string;
description?: string;
metadata?: Record<string, any>;
}>;
/**
* Role definitions
*/
roles: Array<{
name: string;
permissions: string[];
level?: number;
description?: string;
metadata?: Record<string, any>;
}>;
/**
* Optional configuration options
*/
options?: {
autoBitAssignment?: boolean;
validatePermissions?: boolean;
};
}Example:
const schema: RBACConfigSchema = {
permissions: [
{ name: 'user:read', bit: 1, resource: 'user', action: 'read' },
{ name: 'user:write', bit: 2, resource: 'user', action: 'write' }
],
roles: [
{
name: 'admin',
permissions: ['user:read', 'user:write'],
level: 10,
description: 'Administrator role'
}
]
};PresetConfig
Preset configuration with permissions and roles.
interface PresetConfig {
/**
* Preset name
*/
name: string;
/**
* Preset description
*/
description?: string;
/**
* Permission definitions
*/
permissions: Array<{
name: string;
bit?: number;
resource?: string;
action?: string;
}>;
/**
* Role definitions
*/
roles: Array<{
name: string;
permissions: string[];
level?: number;
}>;
/**
* Configuration options
*/
options?: {
autoBitAssignment?: boolean;
};
}Audit Types
AuditEvent
Event logged during permission checks.
interface AuditEvent {
/**
* Type of event
*/
type: 'permission_check' | 'authorization' | 'role_check';
/**
* User ID that triggered the event
*/
userId: string;
/**
* Permission being checked
*/
permission: string;
/**
* Whether permission was allowed
*/
allowed: boolean;
/**
* Reason for denial (if denied)
*/
reason?: string;
/**
* Additional context
*/
context?: AuditEventContext;
/**
* Timestamp (milliseconds since epoch)
*/
timestamp: number;
}AuditEventContext
Additional context for audit events.
interface AuditEventContext {
/**
* User roles at time of check
*/
roles?: string[];
/**
* IP address of request
*/
ip?: string;
/**
* User agent string
*/
userAgent?: string;
/**
* Resource being accessed
*/
resource?: string;
/**
* Action being performed
*/
action?: string;
/**
* Additional metadata
*/
metadata?: Record<string, any>;
}AuditLogger
Interface for audit logging implementations.
interface AuditLogger {
/**
* Log an audit event
* Can be sync or async
*/
log(event: AuditEvent): void | Promise<void>;
/**
* Optional flush method for buffered loggers
*/
flush?(): void | Promise<void>;
}Example:
class CustomAuditLogger implements AuditLogger {
log(event: AuditEvent): void {
console.log(`[AUDIT] ${event.userId} - ${event.permission}: ${event.allowed}`);
}
flush(): void {
// Flush buffered events
}
}Role Types
UserRole
Represents a role with permissions and metadata.
interface UserRole {
/**
* Role name
*/
name: string;
/**
* Permission names assigned to this role
*/
permissions: string[];
/**
* Permission bitmask (bit-based system only)
*/
permissionMask?: number;
/**
* Optional role description
*/
description?: string;
/**
* Optional metadata
*/
metadata?: Record<string, any>;
}PermissionMask
Type alias for permission bitmask.
type PermissionMask = number;Usage:
const readMask: PermissionMask = 1; // 2^0
const writeMask: PermissionMask = 2; // 2^1
const combinedMask: PermissionMask = readMask | writeMask; // 3Permission Types
PermissionDefinition
Definition for a permission.
interface PermissionDefinition {
/**
* Permission name
*/
name: string;
/**
* Optional manual bit value (must be power of 2)
*/
bit?: number;
/**
* Optional resource name
*/
resource?: string;
/**
* Optional action name
*/
action?: string;
/**
* Optional description
*/
description?: string;
/**
* Optional metadata
*/
metadata?: Record<string, any>;
}RoleDefinition
Definition for a role.
interface RoleDefinition {
/**
* Role name
*/
name: string;
/**
* Permission names
*/
permissions: string[];
/**
* Optional hierarchy level
*/
level?: number;
/**
* Optional description
*/
description?: string;
/**
* Optional metadata
*/
metadata?: Record<string, any>;
}State Types
RBACSystemState
Serialized state of RBAC system.
interface RBACSystemState {
/**
* Bit-based system enabled
*/
useBitSystem: boolean;
/**
* Wildcards enabled
*/
enableWildcards: boolean;
/**
* All registered permissions
*/
permissions: Array<{
name: string;
bit?: number;
}>;
/**
* All registered roles
*/
roles: Array<{
name: string;
permissions: string[];
permissionMask?: number;
}>;
/**
* Role hierarchy levels
*/
hierarchy: Record<string, number>;
/**
* Denied permissions by user ID
*/
denyList: Record<string, string[]>;
}Example:
// Serialize
const state: RBACSystemState = rbac.serialize();
// Save to storage
localStorage.setItem('rbac-state', JSON.stringify(state));
// Restore
const savedState = JSON.parse(localStorage.getItem('rbac-state')!);
rbac.deserialize(savedState);Type Guards
Utility functions for type checking.
isRBACUser
function isRBACUser(obj: any): obj is RBACUser {
return (
typeof obj === 'object' &&
typeof obj.id === 'string' &&
Array.isArray(obj.roles)
);
}Usage:
if (isRBACUser(maybeUser)) {
// TypeScript knows this is an RBACUser
rbac.hasPermission(maybeUser, 'post:read');
}isAuditEvent
function isAuditEvent(obj: any): obj is AuditEvent {
return (
typeof obj === 'object' &&
typeof obj.type === 'string' &&
typeof obj.userId === 'string' &&
typeof obj.permission === 'string' &&
typeof obj.allowed === 'boolean' &&
typeof obj.timestamp === 'number'
);
}Generic Types
WithMetadata<T>
Add metadata to any type.
type WithMetadata<T> = T & {
metadata?: Record<string, any>;
};Example:
type UserWithMetadata = WithMetadata<RBACUser>;
const user: UserWithMetadata = {
id: 'user-1',
roles: ['admin'],
metadata: {
department: 'Engineering',
hireDate: '2025-01-01'
}
};Const Enums
PermissionCheckType
const enum PermissionCheckType {
PERMISSION_CHECK = 'permission_check',
AUTHORIZATION = 'authorization',
ROLE_CHECK = 'role_check'
}Usage:
const event: AuditEvent = {
type: PermissionCheckType.PERMISSION_CHECK,
userId: 'user-1',
permission: 'post:read',
allowed: true,
timestamp: Date.now()
};Import Paths
// Core types
import type {
RBACUser,
AuthorizationResult,
AuthorizationContext
} from '@fire-shield/core';
// Configuration types
import type {
RBACConfig,
RBACConfigSchema,
PresetConfig
} from '@fire-shield/core';
// Audit types
import type {
AuditEvent,
AuditEventContext,
AuditLogger
} from '@fire-shield/core';
// Role types
import type {
UserRole,
PermissionMask,
PermissionDefinition,
RoleDefinition
} from '@fire-shield/core';
// State types
import type {
RBACSystemState
} from '@fire-shield/core';Type Utilities
Extending Types
// Extend RBACUser with custom fields
interface AppUser extends RBACUser {
email: string;
name: string;
department: string;
}
const user: AppUser = {
id: 'user-1',
roles: ['editor'],
email: 'user@example.com',
name: 'John Doe',
department: 'Engineering'
};Type-Safe Permissions
// Define allowed permissions as const
const PERMISSIONS = {
USER_READ: 'user:read',
USER_WRITE: 'user:write',
POST_READ: 'post:read',
POST_WRITE: 'post:write',
} as const;
type Permission = typeof PERMISSIONS[keyof typeof PERMISSIONS];
// Type-safe permission checks
function checkPermission(user: RBACUser, permission: Permission): boolean {
return rbac.hasPermission(user, permission);
}
// ✅ Valid
checkPermission(user, PERMISSIONS.USER_READ);
// ❌ Type error
checkPermission(user, 'invalid:permission');Type-Safe Roles
// Define allowed roles
type Role = 'admin' | 'editor' | 'viewer';
function hasRole(user: RBACUser, role: Role): boolean {
return user.roles.includes(role);
}
// ✅ Valid
hasRole(user, 'admin');
// ❌ Type error
hasRole(user, 'invalid-role');Best Practices
1. Always Use TypeScript
// ✅ Good: Full type safety
import type { RBACUser } from '@fire-shield/core';
const user: RBACUser = {
id: 'user-1',
roles: ['editor']
};2. Define Custom Types
// ✅ Good: Define app-specific types
interface AppUser extends RBACUser {
email: string;
name: string;
}3. Use Type Guards
// ✅ Good: Validate at runtime
if (isRBACUser(userData)) {
rbac.hasPermission(userData, 'post:read');
}4. Const Assertions
// ✅ Good: Type-safe constants
const ROLES = ['admin', 'editor', 'viewer'] as const;
type Role = typeof ROLES[number];Next Steps
- Learn about Core API
- Explore RBAC Builder
- Check out TypeScript Guide
