UNPKG

trpc-shield

Version:

tRPC permissions as another layer of abstraction!

54 lines (53 loc) 2.44 kB
export type ShieldRule<TContext> = IRule<TContext> | ILogicRule<TContext>; export declare class IRule<TContext> { readonly name: string; constructor(options: IRuleOptions); equals(rule: IRule<TContext>): boolean; resolve(ctx: TContext, type: string, path: string, input: { [name: string]: any; }, rawInput: unknown, options: IOptions<TContext>): Promise<IRuleResult<TContext>>; } export interface IRuleOptions { } export declare class ILogicRule<TContext> { constructor(rules: ShieldRule<TContext>[]); getRules(): ShieldRule<TContext>[]; evaluate(ctx: TContext, type: string, path: string, input: { [name: string]: any; }, rawInput: unknown, options: IOptions<TContext>): Promise<IRuleResult<TContext>[]>; resolve(ctx: TContext, type: string, path: string, input: { [name: string]: any; }, rawInput: unknown, options: IOptions<TContext>): Promise<IRuleResult<TContext>>; } export type IRuleResult<TContext = any> = boolean | string | Error | { ctx: Partial<TContext>; }; export type IRuleFunction<TContext extends Record<string, any> = Record<string, any>> = (ctx: TContext, type: string, path: string, input: { [name: string]: any; }, rawInput: unknown, options: IOptions<TContext>) => IRuleResult<TContext> | Promise<IRuleResult<TContext>>; export interface IRuleConstructorOptions { } export interface IRuleTypeMap<TContext> { [key: string]: ShieldRule<TContext> | IRuleFieldMap<TContext> | IRuleTypeMap<TContext>; } export interface IRuleFieldMap<TContext> { [key: string]: ShieldRule<TContext>; } export type IRules<TContext> = ShieldRule<TContext> | IRuleTypeMap<TContext>; export type IFallbackErrorMapperType<TContext> = (err: unknown, ctx: TContext, type: string, path: string, input: { [name: string]: any; }, rawInput: unknown) => Promise<Error> | Error; export type IFallbackErrorType<TContext> = Error | IFallbackErrorMapperType<TContext>; export interface IOptions<TContext> { debug: boolean; allowExternalErrors: boolean; fallbackRule: ShieldRule<TContext>; fallbackError?: IFallbackErrorType<TContext>; } export interface IOptionsConstructor<TContext> { debug?: boolean; allowExternalErrors?: boolean; fallbackRule?: ShieldRule<TContext>; fallbackError?: string | IFallbackErrorType<TContext>; } export declare function shield<TContext>(ruleTree: IRules<TContext>, options: IOptions<TContext>): any;