UNPKG

7.17 kBTypeScriptView Raw
1declare module "single-spa" {
2 interface CustomProps {
3 [str: string]: any;
4 [num: number]: any;
5 }
6
7 type CustomPropsFn<ExtraProps extends CustomProps = CustomProps> = (
8 name: string,
9 location: Location
10 ) => ExtraProps;
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<ExtraProps = CustomProps> =
22 | ParcelConfigObject<ExtraProps>
23 | (() => Promise<ParcelConfigObject<ExtraProps>>);
24
25 type ParcelProps = { domElement: HTMLElement };
26 type ParcelConfigObject<ExtraProps = CustomProps> = {
27 name?: string;
28 } & LifeCycles<ExtraProps>;
29
30 type Parcel<ExtraProps = CustomProps> = {
31 mount(): Promise<null>;
32 unmount(): Promise<null>;
33 update?(customProps: ExtraProps): Promise<any>;
34 getStatus():
35 | "NOT_LOADED"
36 | "LOADING_SOURCE_CODE"
37 | "NOT_BOOTSTRAPPED"
38 | "BOOTSTRAPPING"
39 | "NOT_MOUNTED"
40 | "MOUNTING"
41 | "MOUNTED"
42 | "UPDATING"
43 | "UNMOUNTING"
44 | "UNLOADING"
45 | "SKIP_BECAUSE_BROKEN"
46 | "LOAD_ERROR";
47 loadPromise: Promise<null>;
48 bootstrapPromise: Promise<null>;
49 mountPromise: Promise<null>;
50 unmountPromise: Promise<null>;
51 };
52
53 type LifeCycleFn<ExtraProps> = (
54 config: ExtraProps & AppProps
55 ) => Promise<any>;
56 export type LifeCycles<ExtraProps = {}> = {
57 bootstrap: LifeCycleFn<ExtraProps> | Array<LifeCycleFn<ExtraProps>>;
58 mount: LifeCycleFn<ExtraProps> | Array<LifeCycleFn<ExtraProps>>;
59 unmount: LifeCycleFn<ExtraProps> | Array<LifeCycleFn<ExtraProps>>;
60 update?: LifeCycleFn<ExtraProps> | Array<LifeCycleFn<ExtraProps>>;
61 };
62
63 export type StartOpts = {
64 urlRerouteOnly?: boolean;
65 };
66
67 // ./start.js
68 export function start(opts?: StartOpts): void;
69
70 // ./jquery-support.js
71 export function ensureJQuerySupport(jQuery?: any): void;
72
73 // ./applications/timeouts.js
74 /**
75 * Sets the timeout for the given lifecycle.
76 * @param {number} time - The timeout in milliseconds.
77 * @param {boolean=} dieOnTimeout - Whether to throw an error if the timeout is reached. Default false.
78 * @param {number=} warningMillis - The number of milliseconds before the timeout warning is triggered. Default 1000.
79 * @returns {void}
80 **/
81 export function setBootstrapMaxTime(
82 time: number,
83 dieOnTimeout?: boolean,
84 warningMillis?: number
85 ): void;
86 /**
87 * Sets the timeout for the given lifecycle.
88 * @param {number} time - The timeout in milliseconds.
89 * @param {boolean=} dieOnTimeout - Whether to throw an error if the timeout is reached. Default false.
90 * @param {number=} warningMillis - The number of milliseconds before the timeout warning is triggered. Default 1000.
91 * @returns {void}
92 **/
93 export function setMountMaxTime(
94 time: number,
95 dieOnTimeout?: boolean,
96 warningMillis?: number
97 ): void;
98 /**
99 * Sets the timeout for the given lifecycle.
100 * @param {number} time - The timeout in milliseconds.
101 * @param {boolean=} dieOnTimeout - Whether to throw an error if the timeout is reached. Default false.
102 * @param {number=} warningMillis - The number of milliseconds before the timeout warning is triggered. Default 1000.
103 * @returns {void}
104 **/
105 export function setUnmountMaxTime(
106 time: number,
107 dieOnTimeout?: boolean,
108 warningMillis?: number
109 ): void;
110 /**
111 * Sets the timeout for the given lifecycle.
112 * @param {number} time - The timeout in milliseconds.
113 * @param {boolean=} dieOnTimeout - Whether to throw an error if the timeout is reached. Default false.
114 * @param {number=} warningMillis - The number of milliseconds before the timeout warning is triggered. Default 1000.
115 * @returns {void}
116 **/
117 export function setUnloadMaxTime(
118 time: number,
119 dieOnTimeout?: boolean,
120 warningMillis?: number
121 ): void;
122
123 type Application<ExtraProps = {}> =
124 | LifeCycles<ExtraProps>
125 | ((config: ExtraProps & AppProps) => Promise<LifeCycles<ExtraProps>>);
126
127 type ActivityFn = (location: Location) => boolean;
128
129 type Activity = ActivityFn | string | (ActivityFn | string)[];
130
131 export type RegisterApplicationConfig<ExtraProps extends CustomProps = {}> = {
132 name: string;
133 app: Application<ExtraProps>;
134 activeWhen: Activity;
135 customProps?: ExtraProps | CustomPropsFn<ExtraProps>;
136 };
137
138 interface SingleSpaNewAppStatus {
139 [key: string]:
140 | "MOUNTED"
141 | "NOT_MOUNTED"
142 | "NOT_LOADED"
143 | "SKIP_BECAUSE_BROKEN";
144 }
145 interface SingleSpaAppsByNewStatus {
146 [MOUNTED]: [];
147 [NOT_MOUNTED]: [];
148 [NOT_LOADED]: [];
149 [SKIP_BECAUSE_BROKEN]: [];
150 }
151 export type SingleSpaCustomEventDetail = {
152 newAppStatuses: SingleSpaNewAppStatus;
153 appsByNewStatus: SingleSpaAppsByNewStatus;
154 totalAppChanges: number;
155 originalEvent?: Event;
156 oldUrl: string;
157 newUrl: string;
158 navigationIsCanceled: boolean;
159 cancelNavigation?: () => void;
160 };
161
162 // ./applications/apps.js
163 export function registerApplication<ExtraProps extends CustomProps = {}>(
164 appName: string,
165 applicationOrLoadingFn: Application<ExtraProps>,
166 activityFn: ActivityFn,
167 customProps?: ExtraProps | CustomPropsFn<ExtraProps>
168 ): void;
169
170 export function registerApplication<ExtraProps extends CustomProps = {}>(
171 config: RegisterApplicationConfig<ExtraProps>
172 ): void;
173
174 export function unregisterApplication(appName: string): Promise<any>;
175
176 export function getMountedApps(): string[];
177
178 export const {
179 NOT_LOADED = "NOT_LOADED",
180 LOADING_SOURCE_CODE = "LOADING_SOURCE_CODE",
181 NOT_BOOTSTRAPPED = "NOT_BOOTSTRAPPED",
182 BOOTSTRAPPING = "BOOTSTRAPPING",
183 NOT_MOUNTED = "NOT_MOUNTED",
184 MOUNTING = "MOUNTING",
185 MOUNTED = "MOUNTED",
186 UPDATING = "UPDATING",
187 UNMOUNTING = "UNMOUNTING",
188 UNLOADING = "UNLOADING",
189 SKIP_BECAUSE_BROKEN = "SKIP_BECAUSE_BROKEN",
190 LOAD_ERROR = "LOAD_ERROR",
191 };
192
193 export function getAppStatus(appName: string): string | null;
194
195 export function unloadApplication(
196 appName: string,
197 opts?: { waitForUnmount: boolean }
198 ): Promise<any>;
199
200 export function checkActivityFunctions(location: Location): string[];
201 export function getAppNames(): string[];
202
203 // ./navigation/navigation-events.js'
204 export function navigateToUrl(
205 obj:
206 | string
207 | {
208 currentTarget: {
209 href: string;
210 };
211 preventDefault: any;
212 },
213 opts?: Object
214 ): void;
215
216 // './navigation/reroute.js'
217 export function triggerAppChange(): Promise<any>;
218
219 // './applications/app-errors.js'
220 type AppError = Error & {
221 appOrParcelName: string;
222 };
223 export function addErrorHandler(handler: (error: AppError) => void): void;
224 export function removeErrorHandler(handler: (error: AppError) => void): void;
225
226 // './parcels/mount-parcel.js'
227 export function mountRootParcel<ExtraProps = CustomProps>(
228 parcelConfig: ParcelConfig<ExtraProps>,
229 parcelProps: ParcelProps & ExtraProps
230 ): Parcel<ExtraProps>;
231
232 export function pathToActiveWhen(
233 path: string,
234 exactMatch?: boolean
235 ): ActivityFn;
236}