UNPKG

rolet

Version:

Powerful user permission management

99 lines (98 loc) 2.39 kB
import { T_action, T_actions, T_role, T_roles } from './type'; /** * Role node * * Each node stands for a role, and there is a set of potential * permissions behind it. */ export declare class Rnode<D = any, K extends string = string> implements T_role<D, K> { /** * Action type */ actions?: T_actions; /** * Child nodes map */ children?: T_rnodes<D, K>; /** * @see {T_role} */ custom?: D; /** * Parent node */ parent?: Rnode<D, K>; /** * Role name */ role?: K; constructor(); constructor(name: K, role: T_role<D, K>); /** * Get all actions (include inherited actions) */ collect_actions(): T_action[]; /** * Sum node roles */ collect_roles(direction?: 'up' | 'down'): string[]; /** * Sum node roles */ collect_values<T = any>(path: keyof Rnode | string, direction?: 'up' | 'down'): T[]; /** * Convert children to rnodes */ convert(role: T_role<D, K>): void; /** * Count all ascendants * @returns {number} */ count_ascendants(): number; /** * Count direct children * @returns {number} */ count_children(): number; /** * Count all descendants * @returns {number} */ count_descendants(): number; /** * Walk down 1 level * @param {{(parent: T_role<T_custom>)}} fn */ down(fn: (children: T_roles<D, K>) => any): void; /** * Walk up 1 level * @param {{(parent: T_role<T_custom>)}} fn */ up(fn: (parent: T_role<D, K>) => any): void; /** * Walk along children * @param {{(node: Rnode)}} fn */ walk_down(fn: (node: Rnode<D, K>) => any, opt?: T_walk_opt<D, K>): void; /** * Walk along parents * @param {{(node: Rnode)}} fn */ walk_up(fn: (node: Rnode<D, K>, name?: K) => any, opt?: T_walk_opt<D, K>): void; /** * Find node by role name */ static find_by_role<D = any, K extends string = string>(from: Rnode<D, K>, name: K): Rnode<D, K> | undefined; } export declare type T_rnodes<D = any, K extends string = string> = { [key in K]?: Rnode<D, K>; }; export interface T_walk_opt<D = any, K extends string = string> { detect_boolean?: boolean; /** * Stop condition */ stop?: { (node: Rnode<D, K>): boolean; }; }