UNPKG

4.57 kBTypeScriptView Raw
1import type { ActionCallback, ActionSetInterface, ActionSetOptions, AnyAction, Dispatch, ErrorSubscriber, MetaAction, Unsubscribe } from '../actions/types';
2import { Group } from '../actions/types';
3import type { Handler, MessageTransport } from '../MessageTransport';
4import { Context } from '../MessageTransport';
5/**
6 * @public
7 */
8export interface AppConfig {
9 apiKey: string;
10 host: string;
11 forceRedirect?: boolean;
12}
13/**
14 * @internal
15 */
16export declare enum PermissionType {
17 Dispatch = "Dispatch",
18 Subscribe = "Subscribe"
19}
20/**
21 * @internal
22 */
23export declare type FeaturePermission = {
24 [key in PermissionType]: boolean;
25};
26/**
27 * @internal
28 */
29export interface FeaturesAction {
30 [key: string]: FeaturePermission;
31}
32/**
33 * @internal
34 */
35export declare type FeaturesAvailable<T extends Group = Group> = Record<T, FeaturesAction>;
36/**
37 * @internal
38 */
39export declare type FeaturesState<T = FeaturesAvailable> = {
40 [key in Context]?: T;
41};
42/**
43 * Application instance, required for use with actions.
44 * @public
45 */
46export interface ClientApplication<S> {
47 dispatch: Dispatch<AnyAction>;
48 localOrigin: string;
49 error: ErrorSubscriber;
50 hooks?: HooksInterface;
51 getState(query?: string): Promise<S>;
52 featuresAvailable(): Promise<FeaturesAvailable<Group>>;
53 featuresAvailable<T extends Group[]>(...features: T): Promise<FeaturesAvailable<typeof features[number]>>;
54 subscribe(callback: ActionCallback): Unsubscribe;
55 subscribe(eventNameSpace: string, callback: ActionCallback, id?: string): Unsubscribe;
56}
57/**
58 * @public
59 */
60export interface ClientApplicationCreator {
61 <S>(config: AppConfig): ClientApplication<S>;
62}
63/**
64 * @internalremarks
65 * TODO: Generalize—pramaterize return type
66 * @internal
67 */
68export interface ClientApplicationTransportInjector {
69 (transport: MessageTransport, middleware?: AppMiddleware[]): ClientApplicationCreator;
70}
71/**
72 * @internal
73 */
74export interface Dispatcher {
75 (type: MessageType, payload?: AnyAction | undefined): void;
76}
77/**
78 * @internal
79 */
80export declare enum MessageType {
81 GetState = "getState",
82 Dispatch = "dispatch",
83 Subscribe = "subscribe",
84 Unsubscribe = "unsubscribe"
85}
86/**
87 * @internal
88 */
89export interface TransportDispatch {
90 type: MessageType;
91 source: AppConfig;
92 payload?: AnyAction;
93}
94/**
95 * @internal
96 */
97export interface TransportSubscription {
98 id?: string;
99 type: string;
100}
101/**
102 * @deprecated Not to be used, there is no replacement.
103 * @internal
104 */
105export interface Params {
106 [key: string]: string;
107}
108/**
109 *
110 * There are two types of hooks: `LifecycleHook.DispatchAction` and `LifecycleHook.UpdateAction`.
111 *
112 * @remarks
113 * `DispatchAction` hooks are run when an action is dispatched with the `.dispatch()` function:
114 *
115 * ```js
116 * const toastNotice = Toast.create(app, {message: 'Product saved'});
117 * toastNotice.dispatch(Toast.Action.SHOW);
118 * ```
119 *
120 * `UpdateAction` hooks are run when an action is updated, using the `.set()` function:
121 *
122 * ```js
123 * toastNotice.set({message: 'Product could not be saved', isError: true});
124 * ```
125 *
126 * @public
127 */
128export declare enum LifecycleHook {
129 UpdateAction = "UpdateAction",
130 DispatchAction = "DispatchAction"
131}
132/**
133 * @internal
134 */
135export interface Hook {
136 handler: LifecycleHandler;
137 remove: Unsubscribe;
138}
139/**
140 * @internal
141 */
142export interface HookMap {
143 [key: string]: Hook[];
144}
145/**
146 * @internal
147 */
148export interface HooksInterface {
149 set(hook: LifecycleHook.UpdateAction, handler: UpdateActionHook): any;
150 set(hook: LifecycleHook.DispatchAction, handler: DispatchActionHook): any;
151 set(hook: LifecycleHook, handler: LifecycleHandler): any;
152 get(hook: LifecycleHook): LifecycleHandler[] | undefined;
153 run<C>(hook: LifecycleHook, final: Function, context: C, ...arg: any[]): any;
154}
155/**
156 * @internal
157 */
158export interface AppMiddleware {
159 (hooks: HooksInterface, app: ClientApplication<any>): void;
160}
161/**
162 * @internal
163 */
164export interface LifecycleHandler {
165 (next: Function): (...args: any[]) => any;
166}
167/**
168 * @internal
169 */
170export interface UpdateActionHandler {
171 <O>(this: ActionSetInterface & ActionSetOptions<O>, options: O): any;
172}
173/**
174 * @internal
175 */
176export interface UpdateActionHook {
177 (next: Function): UpdateActionHandler;
178}
179/**
180 * @internal
181 */
182export interface DispatchActionHandler {
183 <A extends MetaAction>(this: ClientApplication<any>, action: A): any;
184}
185/**
186 * @internal
187 */
188export interface DispatchActionHook {
189 (next: Function): DispatchActionHandler;
190}
191export { Dispatch, Handler, Unsubscribe };