@shuvi/platform-shared 1.0.0-rc.2 → 1.0.0-rc.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/shared/application.d.ts +18 -69
- package/esm/shared/application.js +58 -14
- package/esm/shared/applicationTypes.d.ts +19 -20
- package/esm/shared/helper/getAppData.d.ts +1 -4
- package/esm/shared/helper/getAppData.js +0 -1
- package/esm/shared/helper/getPublicPath.js +1 -2
- package/esm/shared/index.d.ts +3 -2
- package/esm/shared/index.js +2 -3
- package/esm/shared/loader/index.d.ts +0 -1
- package/esm/shared/loader/index.js +0 -1
- package/esm/shared/loader/loader.d.ts +1 -1
- package/esm/shared/loader/loader.js +11 -7
- package/esm/shared/loader/types.d.ts +6 -10
- package/esm/shared/models/error.d.ts +4 -4
- package/esm/shared/models/error.js +2 -2
- package/esm/shared/models/loader.d.ts +13 -0
- package/esm/shared/models/loader.js +24 -0
- package/esm/shared/routerTypes.d.ts +4 -13
- package/esm/shared/{lifecycle.d.ts → runtimPlugin.d.ts} +3 -5
- package/esm/shared/{lifecycle.js → runtimPlugin.js} +4 -3
- package/esm/shared/shuvi-singleton-runtimeConfig.d.ts +5 -4
- package/esm/shared/shuvi-singleton-runtimeConfig.js +38 -8
- package/esm/shuvi-app/application.d.ts +4 -4
- package/esm/shuvi-app/application.js +3 -3
- package/esm/shuvi-app/shuvi-runtime-app.d.ts +1 -1
- package/esm/shuvi-app/shuvi-runtime-index.d.ts +1 -1
- package/esm/shuvi-app/shuvi-runtime-index.js +1 -1
- package/lib/node/platform/index.d.ts +1 -14
- package/lib/node/platform/plugins/main/hooks.d.ts +2 -3
- package/lib/node/platform/plugins/main/hooks.js +0 -2
- package/lib/node/platform/plugins/main/index.d.ts +1 -14
- package/lib/node/platform/plugins/main/index.js +1 -5
- package/lib/node/platform/runtimeFiles.d.ts +1 -1
- package/lib/node/platform/runtimeFiles.js +11 -13
- package/lib/node/project/file-presets/files/app/core/entry.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/error.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/platform.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/runtimeConfig.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/core/setRuntimeConfig.js.js +1 -1
- package/lib/node/project/file-presets/files/app/user/app.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/user/error.js.d.ts +1 -1
- package/lib/node/project/file-presets/files/app/user/server.js.d.ts +1 -1
- package/lib/node/project/file-presets/index.d.ts +3 -3
- package/lib/node/project/projectContext.d.ts +0 -1
- package/lib/node/project/projectContext.js +0 -1
- package/lib/node/route/helpers.d.ts +0 -1
- package/lib/node/route/helpers.js +7 -30
- package/lib/node/route/index.d.ts +1 -1
- package/lib/node/route/index.js +2 -1
- package/lib/node/route/route.d.ts +3 -3
- package/lib/node/route/route.js +34 -20
- package/lib/shared/application.d.ts +18 -69
- package/lib/shared/application.js +62 -18
- package/lib/shared/applicationTypes.d.ts +19 -20
- package/lib/shared/helper/getAppData.d.ts +1 -4
- package/lib/shared/helper/getAppData.js +0 -1
- package/lib/shared/helper/getPublicPath.js +1 -2
- package/lib/shared/index.d.ts +3 -2
- package/lib/shared/index.js +7 -4
- package/lib/shared/loader/index.d.ts +0 -1
- package/lib/shared/loader/index.js +0 -1
- package/lib/shared/loader/loader.d.ts +1 -1
- package/lib/shared/loader/loader.js +11 -7
- package/lib/shared/loader/types.d.ts +6 -10
- package/lib/shared/models/error.d.ts +4 -4
- package/lib/shared/models/error.js +2 -2
- package/lib/shared/models/loader.d.ts +13 -0
- package/lib/shared/models/loader.js +27 -0
- package/lib/shared/routerTypes.d.ts +4 -13
- package/lib/shared/{lifecycle.d.ts → runtimPlugin.d.ts} +3 -5
- package/lib/shared/{lifecycle.js → runtimPlugin.js} +5 -3
- package/lib/shared/shuvi-singleton-runtimeConfig.d.ts +5 -4
- package/lib/shared/shuvi-singleton-runtimeConfig.js +41 -10
- package/package.json +14 -12
- package/shuvi-type-extensions-node.js +1 -0
- package/shuvi-type-extensions-runtime.d.ts +2 -11
- package/esm/shared/loader/loaderManager.d.ts +0 -16
- package/esm/shared/loader/loaderManager.js +0 -49
- package/lib/node/project/file-presets/files/app/core/polyfill.js.d.ts +0 -3
- package/lib/node/project/file-presets/files/app/core/polyfill.js.js +0 -6
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.d.ts +0 -2
- package/lib/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.js +0 -7
- package/lib/shared/loader/loaderManager.d.ts +0 -16
- package/lib/shared/loader/loaderManager.js +0 -53
|
@@ -1,82 +1,31 @@
|
|
|
1
|
-
import { IRouter } from './routerTypes';
|
|
2
|
-
import {
|
|
3
|
-
export declare class
|
|
4
|
-
private _error;
|
|
1
|
+
import { IRouter, IPageRouteRecord } from './routerTypes';
|
|
2
|
+
import { RedoxStore, Application, IAppContext, ApplicationOptions, IRerenderConfig, IError } from './applicationTypes';
|
|
3
|
+
export declare class ApplicationImpl<Config extends {} = {}> {
|
|
5
4
|
private _router;
|
|
6
5
|
private _appComponent;
|
|
7
6
|
private _pluginManager;
|
|
8
7
|
private _context;
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
private _config;
|
|
9
|
+
private _store;
|
|
10
|
+
private _error;
|
|
11
|
+
private _loader;
|
|
12
|
+
constructor(options: ApplicationOptions<Config>);
|
|
13
|
+
get router(): IRouter<IPageRouteRecord>;
|
|
14
|
+
get config(): Config;
|
|
12
15
|
get context(): IAppContext;
|
|
13
|
-
get pluginManager(): import("@shuvi/hook").HookManager<import("./
|
|
16
|
+
get pluginManager(): import("@shuvi/hook").HookManager<import("./runtimPlugin").RuntimePluginHooks, void>;
|
|
14
17
|
get appComponent(): any;
|
|
15
|
-
get error():
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
$actions: {
|
|
21
|
-
setError: (payload?: import("./applicationTypes").IError | undefined) => import("@shuvi/redox").Action<import("./applicationTypes").IError | undefined>;
|
|
22
|
-
} & {
|
|
23
|
-
error: (payload?: import("./applicationTypes").IError | undefined) => void;
|
|
24
|
-
clear: () => void;
|
|
25
|
-
} & {
|
|
26
|
-
[X: string]: never;
|
|
27
|
-
[X: number]: never;
|
|
28
|
-
[X: symbol]: never;
|
|
29
|
-
};
|
|
30
|
-
$views: import("@shuvi/redox").RedoxViews<({
|
|
31
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
32
|
-
hasError(): boolean;
|
|
33
|
-
} & ThisType<import("./applicationTypes").IErrorState & {
|
|
34
|
-
$state: import("./applicationTypes").IErrorState;
|
|
35
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
36
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
37
|
-
hasError(): boolean;
|
|
38
|
-
}> & {
|
|
39
|
-
$dep: {} & {} & {};
|
|
40
|
-
}>) | undefined>;
|
|
41
|
-
$createSelector: <TReturn>(selector: import("@shuvi/redox").ISelector<import("@shuvi/redox").Model<"error", import("./applicationTypes").IErrorState, {}, {
|
|
42
|
-
setError(_state: import("./applicationTypes").IErrorState, error?: import("./applicationTypes").IError | undefined): {
|
|
43
|
-
error: import("./applicationTypes").IError | undefined;
|
|
44
|
-
};
|
|
45
|
-
}, {
|
|
46
|
-
error(payload?: import("./applicationTypes").IError | undefined): void;
|
|
47
|
-
clear(): void;
|
|
48
|
-
}, {
|
|
49
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
50
|
-
hasError(): boolean;
|
|
51
|
-
}>, TReturn>) => (() => TReturn) & {
|
|
52
|
-
clearCache: () => void;
|
|
53
|
-
};
|
|
54
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
55
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
56
|
-
hasError(): boolean;
|
|
57
|
-
} & ThisType<import("./applicationTypes").IErrorState & {
|
|
58
|
-
$state: import("./applicationTypes").IErrorState;
|
|
59
|
-
} & import("@shuvi/redox").RedoxViews<{
|
|
60
|
-
getError(): import("./applicationTypes").IError | undefined;
|
|
61
|
-
hasError(): boolean;
|
|
62
|
-
}> & {
|
|
63
|
-
$dep: {} & {} & {};
|
|
64
|
-
}>> & {
|
|
65
|
-
setError: (payload?: import("./applicationTypes").IError | undefined) => import("@shuvi/redox").Action<import("./applicationTypes").IError | undefined>;
|
|
66
|
-
} & {
|
|
67
|
-
error: (payload?: import("./applicationTypes").IError | undefined) => void;
|
|
68
|
-
clear: () => void;
|
|
69
|
-
} & {
|
|
70
|
-
[X: string]: never;
|
|
71
|
-
[X: number]: never;
|
|
72
|
-
[X: symbol]: never;
|
|
73
|
-
};
|
|
18
|
+
get error(): IError | undefined;
|
|
19
|
+
setError(err: IError): void;
|
|
20
|
+
clearError(): void;
|
|
21
|
+
getLoadersData(): Record<string, any>;
|
|
22
|
+
setLoadersData(datas: Record<string, any>): void;
|
|
74
23
|
init(): Promise<void>;
|
|
75
|
-
get
|
|
24
|
+
get store(): RedoxStore;
|
|
76
25
|
updateComponents({ AppComponent }?: IRerenderConfig): Promise<void>;
|
|
77
26
|
dispose(): Promise<void>;
|
|
78
27
|
private _initPlugin;
|
|
79
28
|
private _initAppContext;
|
|
80
29
|
private _initAppComponent;
|
|
81
|
-
getPublicAPI():
|
|
30
|
+
getPublicAPI(): Application<Config>;
|
|
82
31
|
}
|
|
@@ -7,16 +7,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { getManager } from './
|
|
11
|
-
import { initPlugins } from './
|
|
10
|
+
import { getManager } from './runtimPlugin';
|
|
11
|
+
import { initPlugins } from './runtimPlugin';
|
|
12
12
|
import { redox } from '@shuvi/redox';
|
|
13
13
|
import { errorModel } from './models/error';
|
|
14
|
-
|
|
14
|
+
import { loaderModel } from './models/loader';
|
|
15
|
+
export class ApplicationImpl {
|
|
15
16
|
constructor(options) {
|
|
17
|
+
this._config = options.config;
|
|
16
18
|
this._router = options.router;
|
|
17
19
|
this._context = {};
|
|
18
|
-
this.
|
|
19
|
-
this._error = this.
|
|
20
|
+
this._store = redox({ initialState: options.initialState });
|
|
21
|
+
this._error = this._store.getModel(errorModel);
|
|
22
|
+
this._loader = this._store.getModel(loaderModel);
|
|
20
23
|
this._appComponent = options.AppComponent;
|
|
21
24
|
this._pluginManager = getManager();
|
|
22
25
|
initPlugins(this._pluginManager, options.plugins || []);
|
|
@@ -24,6 +27,9 @@ export class Application {
|
|
|
24
27
|
get router() {
|
|
25
28
|
return this._router;
|
|
26
29
|
}
|
|
30
|
+
get config() {
|
|
31
|
+
return this._config;
|
|
32
|
+
}
|
|
27
33
|
get context() {
|
|
28
34
|
return this._context;
|
|
29
35
|
}
|
|
@@ -34,7 +40,19 @@ export class Application {
|
|
|
34
40
|
return this._appComponent;
|
|
35
41
|
}
|
|
36
42
|
get error() {
|
|
37
|
-
return this._error;
|
|
43
|
+
return this._error.errorObject;
|
|
44
|
+
}
|
|
45
|
+
setError(err) {
|
|
46
|
+
this._error.set(err);
|
|
47
|
+
}
|
|
48
|
+
clearError() {
|
|
49
|
+
this._error.clear();
|
|
50
|
+
}
|
|
51
|
+
getLoadersData() {
|
|
52
|
+
return this._loader.getAllData;
|
|
53
|
+
}
|
|
54
|
+
setLoadersData(datas) {
|
|
55
|
+
this._loader.setDatas(datas);
|
|
38
56
|
}
|
|
39
57
|
init() {
|
|
40
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -44,8 +62,8 @@ export class Application {
|
|
|
44
62
|
this._router.init();
|
|
45
63
|
});
|
|
46
64
|
}
|
|
47
|
-
get
|
|
48
|
-
return this.
|
|
65
|
+
get store() {
|
|
66
|
+
return this._store;
|
|
49
67
|
}
|
|
50
68
|
updateComponents({ AppComponent } = {}) {
|
|
51
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -67,7 +85,7 @@ export class Application {
|
|
|
67
85
|
}
|
|
68
86
|
_initAppContext() {
|
|
69
87
|
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
|
|
88
|
+
yield this._pluginManager.runner.appContext(this._context);
|
|
71
89
|
});
|
|
72
90
|
}
|
|
73
91
|
_initAppComponent() {
|
|
@@ -76,12 +94,38 @@ export class Application {
|
|
|
76
94
|
});
|
|
77
95
|
}
|
|
78
96
|
getPublicAPI() {
|
|
97
|
+
const self = this;
|
|
79
98
|
return {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
99
|
+
get config() {
|
|
100
|
+
return self._config;
|
|
101
|
+
},
|
|
102
|
+
get context() {
|
|
103
|
+
return self._context;
|
|
104
|
+
},
|
|
105
|
+
get router() {
|
|
106
|
+
return self._router;
|
|
107
|
+
},
|
|
108
|
+
get appComponent() {
|
|
109
|
+
return self._appComponent;
|
|
110
|
+
},
|
|
111
|
+
get store() {
|
|
112
|
+
return self._store;
|
|
113
|
+
},
|
|
114
|
+
get error() {
|
|
115
|
+
return self.error;
|
|
116
|
+
},
|
|
117
|
+
setError(err) {
|
|
118
|
+
self.setError(err);
|
|
119
|
+
},
|
|
120
|
+
clearError() {
|
|
121
|
+
self.clearError();
|
|
122
|
+
},
|
|
123
|
+
getLoadersData() {
|
|
124
|
+
return self.getLoadersData();
|
|
125
|
+
},
|
|
126
|
+
setLoadersData(datas) {
|
|
127
|
+
self.setLoadersData(datas);
|
|
128
|
+
}
|
|
85
129
|
};
|
|
86
130
|
}
|
|
87
131
|
}
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
import type { IncomingMessage } from 'http';
|
|
3
|
-
import { IStoreManager } from '@shuvi/redox';
|
|
1
|
+
import { RedoxStore } from '@shuvi/redox';
|
|
4
2
|
import { CustomAppContext } from '@shuvi/runtime';
|
|
5
|
-
import { IRouter } from './routerTypes';
|
|
6
|
-
import { IPluginList } from './
|
|
7
|
-
export declare type IRequest = IncomingMessage & {
|
|
8
|
-
[x: string]: any;
|
|
9
|
-
};
|
|
3
|
+
import { IRouter, IPageRouteRecord } from './routerTypes';
|
|
4
|
+
import { IPluginList } from './runtimPlugin';
|
|
10
5
|
export interface IAppContext extends CustomAppContext {
|
|
11
6
|
[x: string]: unknown;
|
|
12
7
|
}
|
|
13
8
|
export declare type IRerenderConfig = {
|
|
14
9
|
AppComponent?: any;
|
|
15
10
|
};
|
|
16
|
-
export type {
|
|
11
|
+
export type { RedoxStore };
|
|
12
|
+
export declare type ErrorSource = 'server';
|
|
17
13
|
export interface IError {
|
|
18
14
|
code?: number;
|
|
19
15
|
message?: string;
|
|
16
|
+
stack?: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
source?: ErrorSource;
|
|
20
19
|
}
|
|
21
20
|
export interface IErrorState {
|
|
22
21
|
error?: IError;
|
|
@@ -24,20 +23,20 @@ export interface IErrorState {
|
|
|
24
23
|
export declare type IAppState = {
|
|
25
24
|
error?: IErrorState;
|
|
26
25
|
};
|
|
27
|
-
export interface
|
|
28
|
-
|
|
29
|
-
error: (err: IError) => void;
|
|
30
|
-
clear: () => void;
|
|
31
|
-
hasError: boolean;
|
|
32
|
-
}
|
|
33
|
-
export interface IApplication {
|
|
26
|
+
export interface Application<Config extends {} = {}> {
|
|
27
|
+
readonly config: Config;
|
|
34
28
|
readonly context: IAppContext;
|
|
35
|
-
readonly router: IRouter
|
|
29
|
+
readonly router: IRouter<IPageRouteRecord>;
|
|
36
30
|
readonly appComponent: any;
|
|
37
|
-
readonly
|
|
38
|
-
readonly
|
|
31
|
+
readonly store: RedoxStore;
|
|
32
|
+
readonly error: IError | undefined;
|
|
33
|
+
setError(err: IError): void;
|
|
34
|
+
clearError(): void;
|
|
35
|
+
getLoadersData(): Record<string, any>;
|
|
36
|
+
setLoadersData(datas: Record<string, any>): void;
|
|
39
37
|
}
|
|
40
|
-
export interface
|
|
38
|
+
export interface ApplicationOptions<C extends {}> {
|
|
39
|
+
config: C;
|
|
41
40
|
router: IRouter;
|
|
42
41
|
AppComponent: any;
|
|
43
42
|
initialState?: IAppState;
|
|
@@ -5,13 +5,10 @@ export declare type IData = {
|
|
|
5
5
|
export declare type IAppData<Data = {}, appState = any> = {
|
|
6
6
|
ssr: boolean;
|
|
7
7
|
runtimeConfig?: Record<string, string>;
|
|
8
|
+
appState?: appState;
|
|
8
9
|
pageData?: {
|
|
9
10
|
[key: string]: any;
|
|
10
11
|
};
|
|
11
|
-
loadersData?: {
|
|
12
|
-
[x: string]: any;
|
|
13
|
-
};
|
|
14
|
-
appState?: appState;
|
|
15
12
|
filesByRoutId: Record<string, string[]>;
|
|
16
13
|
publicPath: string;
|
|
17
14
|
} & {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { IDENTITY_RUNTIME_PUBLICPATH } from '@shuvi/shared/lib/constants';
|
|
2
1
|
import { getAppData } from './getAppData';
|
|
3
2
|
export function getPublicPath() {
|
|
4
3
|
const { publicPath } = getAppData();
|
|
5
|
-
return
|
|
4
|
+
return publicPath;
|
|
6
5
|
}
|
package/esm/shared/index.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ export * from './router';
|
|
|
4
4
|
export * from './response';
|
|
5
5
|
export * from './loader';
|
|
6
6
|
export { errorModel } from './models/error';
|
|
7
|
+
export { loaderModel } from './models/loader';
|
|
7
8
|
export * from './applicationTypes';
|
|
8
|
-
export type {
|
|
9
|
+
export type { ApplicationImpl } from './application';
|
|
9
10
|
export type { IRuntimeConfig } from './runtimeConfigTypes';
|
|
10
|
-
export { IAppModule, IPluginInstance, BuiltInRuntimePluginHooks, CustomRuntimePluginHooks, RuntimePluginHooks, createRuntimePlugin, RuntimePluginInstance } from './
|
|
11
|
+
export { IAppModule, IPluginInstance, BuiltInRuntimePluginHooks, CustomRuntimePluginHooks, RuntimePluginHooks, createRuntimePlugin, createRuntimePluginBefore, createRuntimePluginAfter, RuntimePluginInstance } from './runtimPlugin';
|
package/esm/shared/index.js
CHANGED
|
@@ -4,7 +4,6 @@ export * from './router';
|
|
|
4
4
|
export * from './response';
|
|
5
5
|
export * from './loader';
|
|
6
6
|
export { errorModel } from './models/error';
|
|
7
|
+
export { loaderModel } from './models/loader';
|
|
7
8
|
export * from './applicationTypes';
|
|
8
|
-
export {
|
|
9
|
-
// fix createRuntimePlugin is not portable end
|
|
10
|
-
createRuntimePlugin } from './lifecycle';
|
|
9
|
+
export { createRuntimePlugin, createRuntimePluginBefore, createRuntimePluginAfter } from './runtimPlugin';
|
|
@@ -5,5 +5,5 @@ interface Result<T> {
|
|
|
5
5
|
result?: T;
|
|
6
6
|
}
|
|
7
7
|
export declare function runInParallerAndBail<T>(fns: Array<() => Promise<T> | T>): Promise<Result<T>[]>;
|
|
8
|
-
export declare function runLoaders(matches: IRouteMatch<IPageRouteRecord>[], loadersByRouteId: Record<string, Loader>, {
|
|
8
|
+
export declare function runLoaders(matches: IRouteMatch<IPageRouteRecord>[], loadersByRouteId: Record<string, Loader>, { query, req, getAppContext }: LoaderContextOptions): Promise<LoaderDataRecord>;
|
|
9
9
|
export {};
|
|
@@ -68,25 +68,30 @@ function errorHelper(msg, statusCode = 500) {
|
|
|
68
68
|
invariant(statusCode >= 400 && statusCode < 600, 'status code should be 4xx and 5xx');
|
|
69
69
|
throw response(msg, { status: statusCode });
|
|
70
70
|
}
|
|
71
|
-
export function runLoaders(matches, loadersByRouteId, {
|
|
71
|
+
export function runLoaders(matches, loadersByRouteId, { query, req, getAppContext }) {
|
|
72
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const loaderDatas = {};
|
|
73
74
|
if (!matches.length) {
|
|
74
|
-
return
|
|
75
|
+
return loaderDatas;
|
|
75
76
|
}
|
|
76
77
|
const appContext = getAppContext();
|
|
77
78
|
const createLoader = (match) => () => __awaiter(this, void 0, void 0, function* () {
|
|
78
79
|
const loaderFn = loadersByRouteId[match.route.id];
|
|
80
|
+
if (typeof loaderFn !== 'function') {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
79
83
|
let res;
|
|
80
84
|
try {
|
|
81
|
-
const value = yield loaderFn(Object.assign({
|
|
82
|
-
if (value) {
|
|
83
|
-
|
|
85
|
+
const value = yield loaderFn(Object.assign({ pathname: match.pathname, params: match.params, query: query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
|
|
86
|
+
if (value === undefined) {
|
|
87
|
+
throw new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
|
|
88
|
+
`anything from your \`loader\` function. Please return a value or \`null\`.`);
|
|
84
89
|
}
|
|
90
|
+
res = createJson(value);
|
|
85
91
|
}
|
|
86
92
|
catch (error) {
|
|
87
93
|
if (process.env.NODE_ENV === 'development' && !isResponse(error)) {
|
|
88
94
|
console.error(`loader function error of route "${match.route.path}"`);
|
|
89
|
-
console.error(error);
|
|
90
95
|
}
|
|
91
96
|
throw error;
|
|
92
97
|
}
|
|
@@ -94,7 +99,6 @@ export function runLoaders(matches, loadersByRouteId, { isServer, query, req, ge
|
|
|
94
99
|
});
|
|
95
100
|
// call loaders in parallel
|
|
96
101
|
const resultList = yield runInParallerAndBail(matches.map(createLoader));
|
|
97
|
-
const loaderDatas = {};
|
|
98
102
|
for (let index = 0; index < resultList.length; index++) {
|
|
99
103
|
const item = resultList[index];
|
|
100
104
|
if (item.error) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { ShuviRequest } from '@shuvi/service';
|
|
1
2
|
import { IURLQuery, IURLParams } from '../routerTypes';
|
|
2
|
-
import { IAppContext
|
|
3
|
+
import { IAppContext } from '../applicationTypes';
|
|
3
4
|
import { Response } from '../response';
|
|
4
5
|
export interface LoaderContextOptions {
|
|
5
|
-
|
|
6
|
-
req?: IRequest;
|
|
6
|
+
req?: ShuviRequest;
|
|
7
7
|
query: IURLQuery;
|
|
8
8
|
getAppContext: () => IAppContext;
|
|
9
9
|
}
|
|
@@ -17,10 +17,6 @@ export interface ErrorFunction {
|
|
|
17
17
|
* route component getInitialProps params `context`
|
|
18
18
|
*/
|
|
19
19
|
export interface IRouteLoaderContext {
|
|
20
|
-
/**
|
|
21
|
-
* is running on server, if server is true, client will be false
|
|
22
|
-
*/
|
|
23
|
-
isServer: boolean;
|
|
24
20
|
/**
|
|
25
21
|
* current url path
|
|
26
22
|
*/
|
|
@@ -62,12 +58,12 @@ export interface IRouteLoaderContext {
|
|
|
62
58
|
/**
|
|
63
59
|
* server only
|
|
64
60
|
*/
|
|
65
|
-
req?:
|
|
61
|
+
req?: ShuviRequest;
|
|
66
62
|
/**
|
|
67
63
|
* Application context object, which is accessiable during the entire lifecycle of application
|
|
68
64
|
*/
|
|
69
65
|
appContext: IAppContext;
|
|
70
66
|
}
|
|
71
|
-
export declare type Loader<T
|
|
72
|
-
export declare type NormalizedLoader = (
|
|
67
|
+
export declare type Loader<T = any> = (loaderContext: IRouteLoaderContext) => Promise<T> | T;
|
|
68
|
+
export declare type NormalizedLoader = (loaderContext: IRouteLoaderContext) => Promise<Response | undefined>;
|
|
73
69
|
export declare type LoaderDataRecord = Record<string, any>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { IErrorState, IError } from '../applicationTypes';
|
|
2
|
-
export declare const errorModel: import("@shuvi/redox").
|
|
2
|
+
export declare const errorModel: import("@shuvi/redox/esm/core/defineModel").DefineModel<"error", IErrorState, {
|
|
3
3
|
setError(_state: IErrorState, error?: IError): {
|
|
4
4
|
error: IError | undefined;
|
|
5
5
|
};
|
|
6
6
|
}, {
|
|
7
|
-
|
|
7
|
+
set(payload?: IError): void;
|
|
8
8
|
clear(): void;
|
|
9
9
|
}, {
|
|
10
|
-
|
|
10
|
+
errorObject(): IError | undefined;
|
|
11
11
|
hasError(): boolean;
|
|
12
|
-
}>;
|
|
12
|
+
}, {}>;
|
|
13
13
|
export declare type ErrorModel = typeof errorModel;
|
|
@@ -12,7 +12,7 @@ export const errorModel = defineModel({
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
actions: {
|
|
15
|
-
|
|
15
|
+
set(payload) {
|
|
16
16
|
this.setError(payload || SHUVI_ERROR.APP_ERROR);
|
|
17
17
|
},
|
|
18
18
|
clear() {
|
|
@@ -22,7 +22,7 @@ export const errorModel = defineModel({
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
views: {
|
|
25
|
-
|
|
25
|
+
errorObject() {
|
|
26
26
|
return this.error;
|
|
27
27
|
},
|
|
28
28
|
hasError() {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface LoaderState {
|
|
2
|
+
dataByRouteId: Record<string, any>;
|
|
3
|
+
}
|
|
4
|
+
export declare const loaderModel: import("@shuvi/redox/esm/core/defineModel").DefineModel<"loader", LoaderState, {
|
|
5
|
+
setDatas(state: LoaderState, newData: Record<string, any>): {
|
|
6
|
+
dataByRouteId: {
|
|
7
|
+
[x: string]: any;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
}, import("@shuvi/redox/esm/core/defineModel").ActionOptions, {
|
|
11
|
+
getAllData(): Record<string, any>;
|
|
12
|
+
}, {}>;
|
|
13
|
+
export declare type LoaderModel = typeof loaderModel;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineModel } from '@shuvi/redox';
|
|
2
|
+
const initState = {
|
|
3
|
+
dataByRouteId: {}
|
|
4
|
+
};
|
|
5
|
+
export const loaderModel = defineModel({
|
|
6
|
+
name: 'loader',
|
|
7
|
+
state: initState,
|
|
8
|
+
reducers: {
|
|
9
|
+
setDatas(state, newData) {
|
|
10
|
+
return Object.assign(Object.assign({}, state), { dataByRouteId: Object.assign(Object.assign({}, state.dataByRouteId), newData) });
|
|
11
|
+
}
|
|
12
|
+
// clearDatas(state) {
|
|
13
|
+
// return {
|
|
14
|
+
// ...state,
|
|
15
|
+
// dataByRouteId: {}
|
|
16
|
+
// };
|
|
17
|
+
// }
|
|
18
|
+
},
|
|
19
|
+
views: {
|
|
20
|
+
getAllData() {
|
|
21
|
+
return this.dataByRouteId;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
@@ -7,18 +7,10 @@ export interface IPageRouteRecord extends IRouteRecord {
|
|
|
7
7
|
path: string;
|
|
8
8
|
component?: any;
|
|
9
9
|
children?: IPageRouteRecord[];
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
path: string;
|
|
15
|
-
component?: any;
|
|
16
|
-
children?: IRawPageRouteRecord[];
|
|
17
|
-
fullPath: string;
|
|
18
|
-
__componentSource__: string;
|
|
19
|
-
__componentSourceWithAffix__: string;
|
|
20
|
-
__import__: () => Promise<any>;
|
|
21
|
-
__resolveWeak__: () => any;
|
|
10
|
+
__import__?: () => Promise<any>;
|
|
11
|
+
__resolveWeak__?: () => string[];
|
|
12
|
+
__componentSource__?: string;
|
|
13
|
+
__componentRawRequest__?: string;
|
|
22
14
|
[x: string]: any;
|
|
23
15
|
}
|
|
24
16
|
export interface IPlatformConfig {
|
|
@@ -34,7 +26,6 @@ export interface IPageRouteConfig {
|
|
|
34
26
|
component?: string;
|
|
35
27
|
redirect?: string;
|
|
36
28
|
path: string;
|
|
37
|
-
fullPath?: string;
|
|
38
29
|
}
|
|
39
30
|
export interface IPageRouteConfigWithId extends IPageRouteConfig {
|
|
40
31
|
id: string;
|
|
@@ -3,7 +3,7 @@ import { CustomRuntimePluginHooks } from '@shuvi/runtime';
|
|
|
3
3
|
import { IAppContext } from './applicationTypes';
|
|
4
4
|
export declare type AppComponent = unknown;
|
|
5
5
|
declare const init: import("@shuvi/hook").AsyncParallelHook<void, void, void>;
|
|
6
|
-
declare const appContext: import("@shuvi/hook").
|
|
6
|
+
declare const appContext: import("@shuvi/hook").AsyncSeriesHook<IAppContext, void, void>;
|
|
7
7
|
declare const appComponent: import("@shuvi/hook").AsyncSeriesWaterfallHook<unknown, IAppContext>;
|
|
8
8
|
declare const dispose: import("@shuvi/hook").AsyncParallelHook<void, void, void>;
|
|
9
9
|
export interface BuiltInRuntimePluginHooks extends HookMap {
|
|
@@ -14,10 +14,8 @@ export interface BuiltInRuntimePluginHooks extends HookMap {
|
|
|
14
14
|
}
|
|
15
15
|
export interface RuntimePluginHooks extends BuiltInRuntimePluginHooks, CustomRuntimePluginHooks {
|
|
16
16
|
}
|
|
17
|
-
export declare const getManager: () => import("@shuvi/hook").HookManager<
|
|
18
|
-
export declare const
|
|
19
|
-
setup?: import("@shuvi/hook").Setup<CustomRuntimePluginHooks> | undefined;
|
|
20
|
-
}, options?: import("@shuvi/hook").PluginOptions | undefined) => IPluginInstance<BuiltInRuntimePluginHooks & CustomRuntimePluginHooks, void>;
|
|
17
|
+
export declare const getManager: () => import("@shuvi/hook").HookManager<RuntimePluginHooks, void>;
|
|
18
|
+
export declare const createRuntimePluginBefore: import("@shuvi/shared/lib/plugins").PluginFunc<RuntimePluginHooks, void>, createRuntimePlugin: import("@shuvi/shared/lib/plugins").PluginFunc<RuntimePluginHooks, void>, createRuntimePluginAfter: import("@shuvi/shared/lib/plugins").PluginFunc<RuntimePluginHooks, void>;
|
|
21
19
|
export type { IPluginInstance, CustomRuntimePluginHooks };
|
|
22
20
|
export declare type PluginManager = ReturnType<typeof getManager>;
|
|
23
21
|
export declare type RuntimePluginInstance = IPluginInstance<RuntimePluginHooks, void>;
|
|
@@ -7,9 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { createAsyncParallelHook, createAsyncSeriesWaterfallHook, createHookManager, isPluginInstance } from '@shuvi/hook';
|
|
10
|
+
import { createAsyncParallelHook, createAsyncSeriesHook, createAsyncSeriesWaterfallHook, createHookManager, isPluginInstance } from '@shuvi/hook';
|
|
11
|
+
import { createPluginCreator } from '@shuvi/shared/lib/plugins';
|
|
11
12
|
const init = createAsyncParallelHook();
|
|
12
|
-
const appContext =
|
|
13
|
+
const appContext = createAsyncSeriesHook();
|
|
13
14
|
const appComponent = createAsyncSeriesWaterfallHook();
|
|
14
15
|
const dispose = createAsyncParallelHook();
|
|
15
16
|
const builtinRuntimePluginHooks = {
|
|
@@ -19,7 +20,7 @@ const builtinRuntimePluginHooks = {
|
|
|
19
20
|
dispose
|
|
20
21
|
};
|
|
21
22
|
export const getManager = () => createHookManager(builtinRuntimePluginHooks, false);
|
|
22
|
-
export const { createPlugin: createRuntimePlugin } =
|
|
23
|
+
export const { createPluginBefore: createRuntimePluginBefore, createPlugin: createRuntimePlugin, createPluginAfter: createRuntimePluginAfter } = createPluginCreator();
|
|
23
24
|
export const initPlugins = (pluginManager, plugins) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
25
|
// clear plugin at development mode every time
|
|
25
26
|
if (process.env.NODE_ENV === 'development') {
|
|
@@ -2,11 +2,12 @@ import { IRuntimeConfig } from './runtimeConfigTypes';
|
|
|
2
2
|
/**
|
|
3
3
|
* getRuntimeConfig function
|
|
4
4
|
*
|
|
5
|
-
* @returns
|
|
5
|
+
* @returns serverRuntimeConfig
|
|
6
6
|
*/
|
|
7
7
|
export declare function getRuntimeConfig(): {
|
|
8
8
|
[x: string]: string;
|
|
9
9
|
};
|
|
10
|
-
export declare function getPublicRuntimeConfig(): IRuntimeConfig | null;
|
|
11
|
-
export declare function
|
|
12
|
-
export declare function
|
|
10
|
+
export declare function getPublicRuntimeConfig(): IRuntimeConfig | undefined | null;
|
|
11
|
+
export declare function setPublicRuntimeConfig(config: IRuntimeConfig | null): void;
|
|
12
|
+
export declare function getServerRuntimeConfig(): IRuntimeConfig | undefined | null;
|
|
13
|
+
export declare function setServerRuntimeConfig(config: IRuntimeConfig | null): void;
|