import * as t from "io-ts"; import { MaybePromise, MaybeArray, Predicate, Dict } from "./utils/util-types"; /** * Covers all the primitive mutation types used in the MutationNotification objects * that are constructed by GRelDAL */ export declare enum PrimitiveMutationType { Insert = "INSERT", Update = "UPDATE", Delete = "DELETE" } /** * A notification to be published that conveys information about a mutation that * has happened in the application. * * These mutations may not be GraphQL mutations, and application backend * can choose to use NotificationDispatcher to dispatch events which will * become available through GraphQL subscriptions. * * Must be serializable */ export interface MutationNotification { /** * Type of Mutation. For notifications generated by GRelDAL this will * always be of PrimitiveMutationType */ type: string; /** * Mapping of mapped source name to List of entities affected by this mutation. * * For GRelDAL generated mutation notifications, this collection will * have only a single key-value pair */ entities: Dict; /** * Application specific metadata */ metadata?: any; } /** * Represents a notification for a single (usually atomic) operation that changes the state of * a singe datasource */ export interface PrimitiveMutationNotification extends MutationNotification { type: PrimitiveMutationType; } /** * A function that intercepts a list of notifications and can transform it. * * Only those notifications will be published which have been removed from the interceptor. */ interface NotificationDispatchInterceptor { (notification: Array>): MaybePromise>>; } declare const NotificationDispatchInterceptorConfigRT: t.IntersectionC<[t.PartialC<{ type: t.UnionC<[t.Type, t.ArrayC>]>; source: t.UnionC<[t.Type, t.ArrayC>]>; }>, t.TypeC<{ intercept: t.UnionC<[t.FunctionC, t.BooleanC]>; }>]>; /** * Specifies what Notifications are to be intercepted. */ interface NotificationDispatchInterceptorConfig extends t.TypeOf { /** * Specifies which notifications are to be intercepted based on type. * * This can be: * * - A string: In which case an exact match is performed against the type property of notification * - A regular expression to match the type property by a pattern * - A function that takes the type property value and returns true/false * - An array of the above, in which case any of the predicates matching will be considered a successful match. * * If both type and source are specified, then incoming notifications must match *both* of them * to be intercepted. */ type?: MaybeArray>; /** * Specifies which notifications are to be intercepted based on source. * * This can be: * * - A string: In which case an exact match is performed against the source property of notification * - A regular expression to match the source property by a pattern * - A function that takes the source property value and returns true/false * - An array of the above, in which case any of the predicates matching will be considered a successful match. * * If both type and source are specified, then incoming notifications must match *both* of them * to be intercepted. */ source?: MaybeArray>; /** * Whether the notifications not matching the aforementioned type/source should be retained * or discarded. * * True (retained) by default. * * In a chain of dispatch interceptors, if any of the interceptors choses not to retain * notifications that don't match, they will not be available to other interceptors * down the chain. */ retainRest?: boolean; /** * Interceptor function that receives list of notifications to be published and can choose * to augment, transform or remove them. * * Only the notifications actually returned from the interceptor will be published. * * This can be: * - A function, which will receives list of notifications to be published and can choose * to augment, transform or remove them. * - True, in which case all matched notifications are returned as is * - False, in which case all matched notifications are discarded */ intercept: NotificationDispatchInterceptor | boolean; } interface NormalizedNotificationDispatcherConfig { intercept: NotificationDispatchInterceptor; publish: (notification: MutationNotification) => void; } declare const NotificationDispatcherConfigRT: t.IntersectionC<[t.PartialC<{ intercept: t.UnionC<[t.Type, t.ArrayC>]>; }>, t.TypeC<{ publish: t.FunctionC; }>]>; /** * Global notification Dispatcher Configuration */ interface NotificationDispatcherConfig extends t.TypeOf { /** * Interceptors which can receive and transform, add or remove notifications before * they are published. * * The interceptor can be: * * 1. A configuration of type {@link NotificationDispatchInterceptorConfig} which will specify * which notifications are to be intercepted based on type or source. * * 2. A function that receives an array of notifications and returns transformed list of notifications * Only the notifications that are returned by this interceptor will be published. * * 3. An array of either of the above, in which case the interceptors will be composed * as a chain of middlewares, and only the notifications that are returned by the last interceptor * in the chain will be published. */ intercept?: MaybeArray; /** * Function used to publish the notifications to an observable channel. * * This can be used for graphql-subscriptions integration: */ publish: (notification: MutationNotification) => void; } export declare const defaultConfig: NormalizedNotificationDispatcherConfig; /** * The current configuration of NotificationDispatcher. * * Default configuration does nothing: it has no interceptors and it publishes nowhere. */ export declare let config: NormalizedNotificationDispatcherConfig; /** * Configure NotificationDispatcher. * * Note that the NotificationDispatcher is singleton and thus re-invocation of this function is * not recommended. * * If re-invoked the previous configuration will be overriden completely (and NOT merged). So * any previous interceptors will get lost. If merging is desired, it should be taken care of either * at the application level or by passing a function to configure which will receive previous/default * configuration. */ export declare function configure(cfg: NotificationDispatcherConfig): void; /** * Reset Notification dispatcher configuration to default. * * Primarily useful in tests. */ export declare function resetConfig(): void; /** * Publish notifications to an observable channel after passing them through a chain * of interceptors (Refer {@link NotificationDispatcherConfig}) if configured. */ export declare function publish(notifications: MaybeArray>): Promise; export {};