feathers-trigger
Version:
Add triggers and actions to your feathers app.
110 lines (105 loc) • 5.29 kB
TypeScript
import { HookContext, Params, Id, NextFunction, ServiceInterface, Paginated } from '@feathersjs/feathers';
type Promisable<T> = T | PromiseLike<T>;
type MaybeArray<T> = T | T[];
type Change<T = any> = {
before: T;
item: T;
};
type ChangesById<T = any> = {
[key: string]: Change<T>;
[key: number]: Change<T>;
};
type ManipulateParams<H extends HookContext = HookContext> = (params: Params, context: H) => Promisable<Params | null>;
interface HookChangesByIdOptions<H extends HookContext = HookContext> {
/**
* @default false
*/
skipHooks: boolean;
params?: ManipulateParams<H>;
/**
* @default []
*/
deleteParams?: string[];
/**
* The name of the property to store the changesById in context.params
*
* @default "changesById"
*/
name?: string | string[];
/**
* @default false
*/
fetchBefore?: boolean;
}
interface ChangesByIdParams extends Params {
changesById: any;
}
declare module '@feathersjs/feathers' {
interface Params {
paginate?: any;
changesById?: any;
}
}
declare const changesById: <H extends HookContext, T = any>(cb: (changesById: Record<Id, Change<T>>, context: H) => void | Promise<void>, _options?: Partial<HookChangesByIdOptions<H>>) => (context: H, next?: NextFunction) => Promise<H>;
declare const changesByIdBefore: <H extends HookContext>(context: H, _options: HookChangesByIdOptions<H>) => Promise<Record<string, unknown> | unknown[]>;
declare const changesByIdAfter: <H extends HookContext, T = any>(context: H, itemsBefore: any, cb?: ((changesById: Record<Id, Change<T>>, context: H) => void | Promise<void>) | null, _options?: HookChangesByIdOptions<H>) => Promise<Record<Id, Change> | undefined>;
type GetOrFindByIdParamsOptions<H extends HookContext = HookContext> = Pick<HookChangesByIdOptions<H>, 'params' | 'skipHooks' | 'deleteParams'> & {
type: 'before' | 'after';
};
declare const getOrFindByIdParams: <H extends HookContext = HookContext>(context: H, options: GetOrFindByIdParamsOptions<H>) => Promise<Params | undefined>;
type GetOrFindByIdOptions<H extends HookContext = HookContext> = GetOrFindByIdParamsOptions<H> & {
byId?: boolean;
};
type ActionOptions<H extends HookContext = HookContext, T = any> = {
subscription: Subscription<H, T>;
items: Change<T>[];
context: H;
};
type Action<H extends HookContext = HookContext, T = any> = (change: Change<T>, options: ActionOptions<H, T>) => Promisable<void>;
type BatchAction<H extends HookContext = HookContext, T = any> = (changes: [change: Change<T>, options: ActionOptions<H, T>][], context: H) => Promisable<void>;
type HookTriggerOptions<H extends HookContext = HookContext, T = any> = MaybeArray<Subscription<H, T>> | ((context: H) => Promisable<MaybeArray<Subscription<H, T>>>);
type Condition<H extends HookContext = HookContext, T = Record<string, any>> = Record<string, any> | ((item: T, context: H) => Promisable<boolean | Record<string, any>>);
type ConditionChange<H extends HookContext = HookContext, T = Record<string, any>> = Record<string, any> | ((change: {
item: T;
before: T | undefined;
}, context: H) => Promisable<boolean | Record<string, any>>);
interface SubscriptionBase<H extends HookContext = HookContext, T = Record<string, any>> {
/**
* The name of the subscription
*
* Can be used to filter subscriptions
*/
name?: string;
service?: string | string[];
method?: string | string[];
data?: Condition<H, T>;
result?: ConditionChange<H, T>;
before?: Condition<H, T>;
params?: Condition<H, T>;
manipulateParams?: ManipulateParams;
/**
* @default true
*/
isBlocking?: boolean;
/**
* @default false
*/
fetchBefore?: boolean;
/**
* @default false
*/
debug?: boolean;
}
type SubscriptionSingleAction<H extends HookContext = HookContext, T = Record<string, any>> = {
action: Action<H, T>;
} & SubscriptionBase<H, T>;
type SubscriptionBatchAction<H extends HookContext = HookContext, T = Record<string, any>> = {
batchAction: BatchAction<H, T>;
} & SubscriptionBase<H, T>;
type Subscription<H extends HookContext = HookContext, T = Record<string, any>> = SubscriptionSingleAction<H, T> | SubscriptionBatchAction<H, T>;
type SubscriptionResolved<H extends HookContext = HookContext, T = Record<string, any>> = Subscription<H, T> & {
identifier?: string;
paramsResolved?: Record<string, any>;
};
declare const trigger: <H extends HookContext, T = H extends HookContext<any, infer S> ? S extends ServiceInterface<infer TT> ? TT extends Paginated<infer TTT> ? TTT : TT extends Array<infer TTT> ? TTT : TT : any : any>(options: HookTriggerOptions<H, T>) => (context: H, next?: NextFunction) => Promise<H>;
export { type Action, type ActionOptions, type BatchAction, type Change, type ChangesById, type ChangesByIdParams, type Condition, type ConditionChange, type GetOrFindByIdOptions, type GetOrFindByIdParamsOptions, type HookChangesByIdOptions, type HookTriggerOptions, type ManipulateParams, type Subscription, type SubscriptionBase, type SubscriptionBatchAction, type SubscriptionResolved, type SubscriptionSingleAction, changesById, changesByIdAfter, changesByIdBefore, getOrFindByIdParams, trigger };