UNPKG

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