trpc-shield
Version:
tRPC permissions as another layer of abstraction!
99 lines (79 loc) • 2.67 kB
text/typescript
// Rule
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>>;
}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
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>>;
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface IRuleConstructorOptions {}
// Rules Definition Tree
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>;
// Generator Options
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;