UNPKG

4.47 kBTypeScriptView Raw
1declare module "single-spa" {
2 interface CustomProps {
3 [str: string]: any;
4 [num: number]: any;
5 }
6
7 type CustomPropsFn<T extends CustomProps = CustomProps> = (
8 name: string,
9 location: Location
10 ) => T;
11
12 export type AppProps = {
13 name: string;
14 singleSpa: any;
15 mountParcel(
16 parcelConfig: ParcelConfig,
17 customProps: ParcelProps & CustomProps
18 ): Parcel;
19 };
20
21 export type ParcelConfig =
22 | ParcelConfigObject
23 | (() => Promise<ParcelConfigObject>);
24
25 type ParcelProps = { domElement: HTMLElement };
26 type ParcelConfigObject = { name?: string } & LifeCycles;
27
28 type Parcel = {
29 mount(): Promise<null>;
30 unmount(): Promise<null>;
31 update?(customProps: CustomProps): Promise<any>;
32 getStatus():
33 | "NOT_LOADED"
34 | "LOADING_SOURCE_CODE"
35 | "NOT_BOOTSTRAPPED"
36 | "BOOTSTRAPPING"
37 | "NOT_MOUNTED"
38 | "MOUNTING"
39 | "MOUNTED"
40 | "UPDATING"
41 | "UNMOUNTING"
42 | "UNLOADING"
43 | "SKIP_BECAUSE_BROKEN"
44 | "LOAD_ERROR";
45 loadPromise: Promise<null>;
46 bootstrapPromise: Promise<null>;
47 mountPromise: Promise<null>;
48 unmountPromise: Promise<null>;
49 };
50
51 type LifeCycleFn<T> = (config: T & AppProps) => Promise<any>;
52 export type LifeCycles<T = {}> = {
53 bootstrap: LifeCycleFn<T> | Array<LifeCycleFn<T>>;
54 mount: LifeCycleFn<T> | Array<LifeCycleFn<T>>;
55 unmount: LifeCycleFn<T> | Array<LifeCycleFn<T>>;
56 update?: LifeCycleFn<T> | Array<LifeCycleFn<T>>;
57 };
58
59 export type StartOpts = {
60 urlRerouteOnly?: boolean;
61 };
62
63 // ./start.js
64 export function start(opts?: StartOpts): void;
65 export function isStarted(): boolean;
66
67 // ./jquery-support.js
68 export function ensureJQuerySupport(jQuery?: any): void;
69
70 // ./applications/timeouts.js
71 export function setBootstrapMaxTime(
72 time: number,
73 dieOnTimeout?: boolean
74 ): void;
75 export function setMountMaxTime(time: number, dieOnTimeout?: boolean): void;
76 export function setUnmountMaxTime(time: number, dieOnTimeout?: boolean): void;
77 export function setUnloadMaxTime(time: number, dieOnTimeout?: boolean): void;
78
79 type Application<T = {}> =
80 | LifeCycles<T>
81 | ((config: T & AppProps) => Promise<LifeCycles<T>>);
82
83 type ActivityFn = (location: Location) => boolean;
84
85 type Activity = ActivityFn | string | (ActivityFn | string)[];
86
87 export type RegisterApplicationConfig<T extends CustomProps = {}> = {
88 name: string;
89 app: Application<T>;
90 activeWhen: Activity;
91 customProps?: T | CustomPropsFn<T>;
92 };
93
94 // ./applications/apps.js
95 export function registerApplication<T extends object = {}>(
96 appName: string,
97 applicationOrLoadingFn: Application<T>,
98 activityFn: ActivityFn,
99 customProps?: T | CustomPropsFn<T>
100 ): void;
101
102 export function registerApplication<T extends object = {}>(
103 config: RegisterApplicationConfig<T>
104 ): void;
105
106 export function getMountedApps(): string[];
107
108 export const {
109 NOT_LOADED = "NOT_LOADED",
110 LOADING_SOURCE_CODE = "LOADING_SOURCE_CODE",
111 NOT_BOOTSTRAPPED = "NOT_BOOTSTRAPPED",
112 BOOTSTRAPPING = "BOOTSTRAPPING",
113 NOT_MOUNTED = "NOT_MOUNTED",
114 MOUNTING = "MOUNTING",
115 MOUNTED = "MOUNTED",
116 UPDATING = "UPDATING",
117 UNMOUNTING = "UNMOUNTING",
118 UNLOADING = "UNLOADING",
119 SKIP_BECAUSE_BROKEN = "SKIP_BECAUSE_BROKEN",
120 LOAD_ERROR = "LOAD_ERROR",
121 };
122
123 export function getAppStatus(appName: string): string | null;
124
125 export function unloadApplication(
126 appName: string,
127 opts?: { waitForUnmount: boolean }
128 ): Promise<any>;
129
130 export function checkActivityFunctions(location: Location): string[];
131 export function getAppNames(): string[];
132
133 // ./navigation/navigation-events.js'
134 export function navigateToUrl(
135 obj:
136 | string
137 | {
138 currentTarget: {
139 href: string;
140 };
141 preventDefault: any;
142 },
143 opts?: Object
144 ): void;
145
146 // './navigation/reroute.js'
147 export function triggerAppChange(): Promise<any>;
148
149 // './applications/app-errors.js'
150 type AppError = Error & {
151 appOrParcelName: string;
152 };
153 export function addErrorHandler(handler: (error: AppError) => void): void;
154 export function removeErrorHandler(handler: (error: AppError) => void): void;
155
156 // './parcels/mount-parcel.js'
157 export function mountRootParcel(
158 parcelConfig: ParcelConfig,
159 parcelProps: ParcelProps & CustomProps
160 ): Parcel;
161
162 export function pathToActiveWhen(path: string): ActivityFn;
163}