@umijs/plugins 4.0.0-rc.9 → 4.0.0
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/dist/access.js +26 -21
- package/dist/analytics.js +1 -1
- package/dist/antd.js +25 -8
- package/dist/dva.js +14 -3
- package/dist/initial-state.js +12 -9
- package/dist/layout.js +56 -20
- package/dist/locale.js +32 -34
- package/dist/model.js +16 -35
- package/dist/qiankun/master.js +31 -11
- package/dist/qiankun/slave.js +66 -44
- package/dist/qiankun.js +1 -0
- package/dist/request.js +135 -127
- package/dist/utils/localeUtils.js +5 -14
- package/dist/utils/modelUtils.d.ts +6 -1
- package/dist/utils/modelUtils.js +110 -8
- package/libs/model.tsx +43 -5
- package/libs/qiankun/master/MicroApp.tsx +15 -6
- package/libs/qiankun/master/common.ts +15 -10
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +5 -16
- package/libs/qiankun/master/masterRuntimePlugin.tsx +6 -5
- package/libs/qiankun/master/types.ts +2 -0
- package/libs/qiankun/slave/connectMaster.tsx +0 -1
- package/libs/qiankun/slave/lifecycles.ts +14 -8
- package/libs/qiankun/slave/qiankunModel.ts +0 -1
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +9 -15
- package/package.json +13 -13
package/dist/qiankun/master.js
CHANGED
|
@@ -23,22 +23,30 @@ exports.default = (api) => {
|
|
|
23
23
|
api.addRuntimePlugin(() => {
|
|
24
24
|
return [(0, withTmpPath_1.withTmpPath)({ api, path: 'masterRuntimePlugin.tsx' })];
|
|
25
25
|
});
|
|
26
|
-
api.modifyDefaultConfig((config) => (
|
|
27
|
-
|
|
26
|
+
api.modifyDefaultConfig((config) => ({
|
|
27
|
+
...config,
|
|
28
|
+
mountElementId: constants_1.defaultMasterRootId,
|
|
29
|
+
qiankun: {
|
|
30
|
+
...config.qiankun,
|
|
31
|
+
master: {
|
|
32
|
+
...JSON.parse(process.env.INITIAL_QIANKUN_MASTER_OPTIONS || '{}'),
|
|
33
|
+
...(config.qiankun || {}).master,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
}));
|
|
28
37
|
api.modifyRoutes((memo) => {
|
|
29
38
|
Object.keys(memo).forEach((id) => {
|
|
39
|
+
var _a;
|
|
30
40
|
const route = memo[id];
|
|
31
41
|
if (route.microApp) {
|
|
32
42
|
const appName = route.microApp;
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
// TODO: config masterHistoryType
|
|
36
|
-
const masterHistoryType = 'browser';
|
|
43
|
+
const base = api.config.base || '/';
|
|
44
|
+
const masterHistoryType = ((_a = api.config.history) === null || _a === void 0 ? void 0 : _a.type) || 'browser';
|
|
37
45
|
const routeProps = route.microAppProps || {};
|
|
38
46
|
const normalizedRouteProps = JSON.stringify(routeProps).replace(/"/g, "'");
|
|
39
47
|
route.file = `(async () => {
|
|
40
48
|
const { getMicroAppRouteComponent } = await import('@@/plugin-qiankun-master/getMicroAppRouteComponent');
|
|
41
|
-
return getMicroAppRouteComponent({ appName: '${appName}', base: '${base}', masterHistoryType: '${masterHistoryType}', routeProps: ${normalizedRouteProps} })
|
|
49
|
+
return getMicroAppRouteComponent({ appName: '${appName}', base: '${base}', routePath: '${route.path}', masterHistoryType: '${masterHistoryType}', routeProps: ${normalizedRouteProps} })
|
|
42
50
|
})()`;
|
|
43
51
|
}
|
|
44
52
|
});
|
|
@@ -65,7 +73,11 @@ exports.default = (api) => {
|
|
|
65
73
|
api.writeTmpFile({
|
|
66
74
|
path: 'masterOptions.ts',
|
|
67
75
|
content: `
|
|
68
|
-
let options = ${JSON.stringify(
|
|
76
|
+
let options = ${JSON.stringify({
|
|
77
|
+
masterHistoryType: ((_a = api.config.history) === null || _a === void 0 ? void 0 : _a.type) || constants_1.defaultHistoryType,
|
|
78
|
+
base: api.config.base || '/',
|
|
79
|
+
...api.config.qiankun.master,
|
|
80
|
+
})};
|
|
69
81
|
export const getMasterOptions = () => options;
|
|
70
82
|
export const setMasterOptions = (newOpts) => options = ({ ...options, ...newOpts });
|
|
71
83
|
`,
|
|
@@ -91,7 +103,6 @@ export const setMasterOptions = (newOpts) => options = ({ ...options, ...newOpts
|
|
|
91
103
|
path: file.replace(/\.tpl$/, ''),
|
|
92
104
|
tpl: getFileContent(file),
|
|
93
105
|
context: {
|
|
94
|
-
runtimeHistory: api.config.runtimeHistory,
|
|
95
106
|
dynamicRoot: false,
|
|
96
107
|
hasModelPlugin: api.isPluginEnable('model'),
|
|
97
108
|
// dynamicRoot:
|
|
@@ -100,16 +111,25 @@ export const setMasterOptions = (newOpts) => options = ({ ...options, ...newOpts
|
|
|
100
111
|
});
|
|
101
112
|
}
|
|
102
113
|
else {
|
|
114
|
+
let content = getFileContent(file);
|
|
115
|
+
if (!api.config.qiankun.externalQiankun) {
|
|
116
|
+
content = content.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`);
|
|
117
|
+
}
|
|
103
118
|
api.writeTmpFile({
|
|
104
119
|
path: file.replace(/\.tpl$/, ''),
|
|
105
|
-
content:
|
|
120
|
+
content: content
|
|
106
121
|
.replace('__USE_MODEL__', api.isPluginEnable('model')
|
|
107
122
|
? `import { useModel } from '@@/plugin-model'`
|
|
108
123
|
: `const useModel = null;`)
|
|
109
|
-
.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
|
|
110
124
|
.replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
|
|
111
125
|
});
|
|
112
126
|
}
|
|
113
127
|
});
|
|
128
|
+
api.writeTmpFile({
|
|
129
|
+
path: 'index.ts',
|
|
130
|
+
content: `
|
|
131
|
+
export { MicroApp } from './MicroApp';
|
|
132
|
+
`,
|
|
133
|
+
});
|
|
114
134
|
});
|
|
115
135
|
};
|
package/dist/qiankun/slave.js
CHANGED
|
@@ -30,20 +30,32 @@ exports.default = (api) => {
|
|
|
30
30
|
key: 'addExtraModels',
|
|
31
31
|
fn() {
|
|
32
32
|
return [
|
|
33
|
-
|
|
33
|
+
(0, withTmpPath_1.withTmpPath)({
|
|
34
|
+
api,
|
|
35
|
+
path: `qiankunModel.ts#{"namespace":"${constants_1.qiankunStateFromMasterModelNamespace}"}`,
|
|
36
|
+
}),
|
|
34
37
|
];
|
|
35
38
|
},
|
|
36
39
|
});
|
|
37
40
|
api.modifyDefaultConfig((memo) => {
|
|
38
|
-
var _a, _b, _c;
|
|
39
|
-
const initialSlaveOptions =
|
|
40
|
-
|
|
41
|
+
var _a, _b, _c, _d;
|
|
42
|
+
const initialSlaveOptions = {
|
|
43
|
+
devSourceMap: true,
|
|
44
|
+
...JSON.parse(process.env.INITIAL_QIANKUN_SLAVE_OPTIONS || '{}'),
|
|
45
|
+
...(memo.qiankun || {}).slave,
|
|
46
|
+
};
|
|
47
|
+
const modifiedDefaultConfig = {
|
|
48
|
+
...memo,
|
|
41
49
|
// 默认开启 runtimePublicPath,避免出现 dynamic import 场景子应用资源地址出问题
|
|
42
|
-
runtimePublicPath: true,
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
runtimePublicPath: true,
|
|
51
|
+
qiankun: {
|
|
52
|
+
...memo.qiankun,
|
|
53
|
+
slave: initialSlaveOptions,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
45
56
|
const shouldNotModifyDefaultBase = (_c = (_b = (_a = api.userConfig.qiankun) === null || _a === void 0 ? void 0 : _a.slave) === null || _b === void 0 ? void 0 : _b.shouldNotModifyDefaultBase) !== null && _c !== void 0 ? _c : initialSlaveOptions.shouldNotModifyDefaultBase;
|
|
46
|
-
|
|
57
|
+
const historyType = ((_d = api.userConfig.history) === null || _d === void 0 ? void 0 : _d.type) || 'browser';
|
|
58
|
+
if (!shouldNotModifyDefaultBase && historyType !== 'hash') {
|
|
47
59
|
// @ts-ignore
|
|
48
60
|
modifiedDefaultConfig.base = `/${api.pkg.name}`;
|
|
49
61
|
}
|
|
@@ -53,22 +65,23 @@ exports.default = (api) => {
|
|
|
53
65
|
var _a, _b;
|
|
54
66
|
// mfsu 场景默认给子应用增加 mfName 配置,从而避免冲突
|
|
55
67
|
if (config.mfsu !== false) {
|
|
56
|
-
config.mfsu =
|
|
57
|
-
|
|
68
|
+
config.mfsu = {
|
|
69
|
+
...config.mfsu,
|
|
70
|
+
mfName: ((_a = config.mfsu) === null || _a === void 0 ? void 0 : _a.mfName) ||
|
|
71
|
+
`mf_${(_b = api.pkg.name) === null || _b === void 0 ? void 0 : _b.replace(/^@/, '').replace(/\W/g, '_')}`,
|
|
72
|
+
};
|
|
58
73
|
}
|
|
59
74
|
return config;
|
|
60
75
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
// return publicPathStr;
|
|
71
|
-
// });
|
|
76
|
+
api.addHTMLHeadScripts(() => {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
const dontModify = (_b = (_a = api.config.qiankun) === null || _a === void 0 ? void 0 : _a.slave) === null || _b === void 0 ? void 0 : _b.shouldNotModifyRuntimePublicPath;
|
|
79
|
+
return dontModify
|
|
80
|
+
? []
|
|
81
|
+
: [
|
|
82
|
+
`window.publicPath = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || "${api.config.publicPath || '/'}";`,
|
|
83
|
+
];
|
|
84
|
+
});
|
|
72
85
|
api.chainWebpack((config) => {
|
|
73
86
|
(0, assert_1.default)(api.pkg.name, 'You should have name in package.json.');
|
|
74
87
|
const { shouldNotAddLibraryChunkName } = (api.config.qiankun || {}).slave;
|
|
@@ -113,30 +126,39 @@ if (!window.__POWERED_BY_QIANKUN__) {
|
|
|
113
126
|
function getFileContent(file) {
|
|
114
127
|
return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../libs/qiankun/slave', file), 'utf-8');
|
|
115
128
|
}
|
|
116
|
-
api.onGenerateFiles(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
api.onGenerateFiles({
|
|
130
|
+
fn() {
|
|
131
|
+
// api.writeTmpFile({
|
|
132
|
+
// path: 'slaveOptions.ts',
|
|
133
|
+
// content: `
|
|
134
|
+
// let options = ${JSON.stringify((api.config.qiankun || {}).slave || {})};
|
|
135
|
+
// export const getSlaveOptions = () => options;
|
|
136
|
+
// export const setSlaveOptions = (newOpts) => options = ({ ...options, ...newOpts });
|
|
137
|
+
// `,
|
|
138
|
+
// });
|
|
139
|
+
[
|
|
140
|
+
'qiankunModel.ts',
|
|
141
|
+
'connectMaster.tsx',
|
|
142
|
+
'slaveRuntimePlugin.ts',
|
|
143
|
+
'lifecycles.ts',
|
|
144
|
+
].forEach((file) => {
|
|
145
|
+
api.writeTmpFile({
|
|
146
|
+
path: file.replace(/\.tpl$/, ''),
|
|
147
|
+
content: getFileContent(file)
|
|
148
|
+
.replace('__USE_MODEL__', api.isPluginEnable('model')
|
|
149
|
+
? `import { useModel } from '@@/plugin-model'`
|
|
150
|
+
: `const useModel = null;`)
|
|
151
|
+
.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
|
|
152
|
+
.replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
|
|
153
|
+
});
|
|
154
|
+
});
|
|
131
155
|
api.writeTmpFile({
|
|
132
|
-
path:
|
|
133
|
-
content:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
: `const useModel = null;`)
|
|
137
|
-
.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
|
|
138
|
-
.replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
|
|
156
|
+
path: 'index.ts',
|
|
157
|
+
content: `
|
|
158
|
+
export { connectMaster } from './connectMaster';
|
|
159
|
+
`,
|
|
139
160
|
});
|
|
140
|
-
}
|
|
161
|
+
},
|
|
162
|
+
before: 'model',
|
|
141
163
|
});
|
|
142
164
|
};
|
package/dist/qiankun.js
CHANGED
package/dist/request.js
CHANGED
|
@@ -7,7 +7,12 @@ exports.default = (api) => {
|
|
|
7
7
|
key: 'request',
|
|
8
8
|
config: {
|
|
9
9
|
schema: (joi) => {
|
|
10
|
-
return joi.object(
|
|
10
|
+
return joi.object({
|
|
11
|
+
dataField: joi
|
|
12
|
+
.string()
|
|
13
|
+
.pattern(/^[a-zA-Z]*$/)
|
|
14
|
+
.allow(''),
|
|
15
|
+
});
|
|
11
16
|
},
|
|
12
17
|
},
|
|
13
18
|
enableBy: api.EnableBy.config,
|
|
@@ -20,7 +25,6 @@ import axios, {
|
|
|
20
25
|
type AxiosResponse,
|
|
21
26
|
} from '{{{axiosPath}}}';
|
|
22
27
|
import useUmiRequest, { UseRequestProvider } from '{{{umiRequestPath}}}';
|
|
23
|
-
import { message, notification } from '{{{antdPkg}}}';
|
|
24
28
|
import { ApplyPluginsType } from 'umi';
|
|
25
29
|
import { getPluginManager } from '../core/plugin';
|
|
26
30
|
|
|
@@ -81,7 +85,7 @@ function useRequest<Item = any, U extends Item = any>(
|
|
|
81
85
|
): PaginatedResult<Item>;
|
|
82
86
|
function useRequest(service: any, options: any = {}) {
|
|
83
87
|
return useUmiRequest(service, {
|
|
84
|
-
formatResult:
|
|
88
|
+
formatResult: {{{formatResult}}},
|
|
85
89
|
requestMethod: (requestOptions: any) => {
|
|
86
90
|
if (typeof requestOptions === 'string') {
|
|
87
91
|
return request(requestOptions);
|
|
@@ -96,114 +100,49 @@ function useRequest(service: any, options: any = {}) {
|
|
|
96
100
|
});
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
defaultNoneResponseErrorMessage?: string;
|
|
105
|
-
defaultRequestErrorMessage?: string;
|
|
106
|
-
};
|
|
107
|
-
formatResultAdaptor?: IFormatResultAdaptor;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export enum ErrorShowType {
|
|
111
|
-
SILENT = 0,
|
|
112
|
-
WARN_MESSAGE = 1,
|
|
113
|
-
ERROR_MESSAGE = 2,
|
|
114
|
-
NOTIFICATION = 3,
|
|
115
|
-
REDIRECT = 9,
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export interface IErrorInfo {
|
|
119
|
-
success: boolean;
|
|
120
|
-
data?: any;
|
|
121
|
-
errorCode?: string;
|
|
122
|
-
errorMessage?: string;
|
|
123
|
-
showType?: ErrorShowType;
|
|
124
|
-
traceId?: string;
|
|
125
|
-
host?: string;
|
|
103
|
+
// request 方法 opts 参数的接口
|
|
104
|
+
interface IRequestOptions extends AxiosRequestConfig {
|
|
105
|
+
skipErrorHandler?: boolean;
|
|
106
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
107
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
126
108
|
[key: string]: any;
|
|
127
109
|
}
|
|
128
|
-
|
|
129
|
-
interface
|
|
130
|
-
|
|
110
|
+
|
|
111
|
+
interface IRequestOptionsWithResponse extends IRequestOptions {
|
|
112
|
+
getResponse: true;
|
|
131
113
|
}
|
|
132
114
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
info?: IErrorInfo;
|
|
115
|
+
interface IRequestOptionsWithoutResponse extends IRequestOptions{
|
|
116
|
+
getResponse: false;
|
|
136
117
|
}
|
|
137
118
|
|
|
138
|
-
interface IRequest
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
119
|
+
interface IRequest{
|
|
120
|
+
<T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<T>>;
|
|
121
|
+
<T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<T>;
|
|
122
|
+
<T = any>(url: string, opts: IRequestOptions): Promise<T>; // getResponse 默认是 false, 因此不提供该参数时,只返回 data
|
|
123
|
+
<T = any>(url: string): Promise<T>; // 不提供 opts 时,默认使用 'GET' method,并且默认返回 data
|
|
143
124
|
}
|
|
144
125
|
|
|
145
126
|
interface IErrorHandler {
|
|
146
|
-
(error: RequestError, opts:
|
|
127
|
+
(error: RequestError, opts: IRequestOptions): void;
|
|
147
128
|
}
|
|
129
|
+
type IRequestInterceptorAxios = (config: RequestOptions) => RequestOptions;
|
|
130
|
+
type IRequestInterceptorUmiRequest = (url: string, config : RequestOptions) => { url: string, options: RequestOptions };
|
|
131
|
+
type IRequestInterceptor = IRequestInterceptorAxios;
|
|
132
|
+
type IErrorInterceptor = (error: Error) => Promise<Error>;
|
|
133
|
+
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
|
|
134
|
+
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor
|
|
135
|
+
type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor
|
|
148
136
|
|
|
149
|
-
interface
|
|
150
|
-
|
|
137
|
+
export interface RequestConfig extends AxiosRequestConfig {
|
|
138
|
+
errorConfig?: {
|
|
139
|
+
errorHandler?: IErrorHandler;
|
|
140
|
+
errorThrower?: <T = any>( res: T ) => void
|
|
141
|
+
};
|
|
142
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
143
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
151
144
|
}
|
|
152
145
|
|
|
153
|
-
const defaultErrorHandler: IErrorHandler = (error, opts, config) => {
|
|
154
|
-
if (opts?.skipErrorHandler) throw error;
|
|
155
|
-
const { errorConfig } = config;
|
|
156
|
-
if (error.response) {
|
|
157
|
-
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围 或者 成功响应,success字段为false 由我们抛出的错误
|
|
158
|
-
let errorInfo: IErrorInfo | undefined;
|
|
159
|
-
// 不是我们的错误
|
|
160
|
-
if(error.name === 'ResponseError'){
|
|
161
|
-
const adaptor: IAdaptor =
|
|
162
|
-
errorConfig?.adaptor || ((errorData) => errorData);
|
|
163
|
-
errorInfo = adaptor(error.response.data, error.response);
|
|
164
|
-
error.info = errorInfo;
|
|
165
|
-
error.data = error.response.data;
|
|
166
|
-
}
|
|
167
|
-
errorInfo = error.info;
|
|
168
|
-
if (errorInfo) {
|
|
169
|
-
const { errorMessage, errorCode } = errorInfo;
|
|
170
|
-
switch (errorInfo.showType) {
|
|
171
|
-
case ErrorShowType.SILENT:
|
|
172
|
-
// do nothong
|
|
173
|
-
break;
|
|
174
|
-
case ErrorShowType.WARN_MESSAGE:
|
|
175
|
-
message.warn(errorMessage);
|
|
176
|
-
break;
|
|
177
|
-
case ErrorShowType.ERROR_MESSAGE:
|
|
178
|
-
message.error(errorMessage);
|
|
179
|
-
break;
|
|
180
|
-
case ErrorShowType.NOTIFICATION:
|
|
181
|
-
notification.open({ description: errorMessage, message: errorCode });
|
|
182
|
-
break;
|
|
183
|
-
case ErrorShowType.REDIRECT:
|
|
184
|
-
// TODO: redirect
|
|
185
|
-
break;
|
|
186
|
-
default:
|
|
187
|
-
message.error(errorMessage);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
} else if (error.request) {
|
|
191
|
-
// 请求已经成功发起,但没有收到响应
|
|
192
|
-
// \`error.request\` 在浏览器中是 XMLHttpRequest 的实例,
|
|
193
|
-
// 而在node.js中是 http.ClientRequest 的实例
|
|
194
|
-
message.error(
|
|
195
|
-
errorConfig?.defaultNoneResponseErrorMessage ||
|
|
196
|
-
'None response! Please retry.',
|
|
197
|
-
);
|
|
198
|
-
} else {
|
|
199
|
-
// 发送请求时出了点问题
|
|
200
|
-
message.error(
|
|
201
|
-
errorConfig?.defaultRequestErrorMessage || 'Request error, please retry.',
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
throw error;
|
|
205
|
-
};
|
|
206
|
-
|
|
207
146
|
let requestInstance: AxiosInstance;
|
|
208
147
|
let config: RequestConfig;
|
|
209
148
|
const getConfig = (): RequestConfig => {
|
|
@@ -215,51 +154,112 @@ const getConfig = (): RequestConfig => {
|
|
|
215
154
|
});
|
|
216
155
|
return config;
|
|
217
156
|
};
|
|
157
|
+
|
|
218
158
|
const getRequestInstance = (): AxiosInstance => {
|
|
219
159
|
if (requestInstance) return requestInstance;
|
|
220
160
|
const config = getConfig();
|
|
221
161
|
requestInstance = axios.create(config);
|
|
222
162
|
|
|
163
|
+
config?.requestInterceptors?.forEach((interceptor) => {
|
|
164
|
+
if(interceptor instanceof Array){
|
|
165
|
+
requestInstance.interceptors.request.use((config) => {
|
|
166
|
+
const { url } = config;
|
|
167
|
+
if(interceptor[0].length === 2){
|
|
168
|
+
const { url: newUrl, options } = interceptor[0](url, config);
|
|
169
|
+
return { ...options, url: newUrl };
|
|
170
|
+
}
|
|
171
|
+
return interceptor[0](config);
|
|
172
|
+
}, interceptor[1]);
|
|
173
|
+
} else {
|
|
174
|
+
requestInstance.interceptors.request.use((config) => {
|
|
175
|
+
const { url } = config;
|
|
176
|
+
if(interceptor.length === 2){
|
|
177
|
+
const { url: newUrl, options } = interceptor(url, config);
|
|
178
|
+
return { ...options, url: newUrl };
|
|
179
|
+
}
|
|
180
|
+
return interceptor(config);
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
config?.responseInterceptors?.forEach((interceptor) => {
|
|
186
|
+
interceptor instanceof Array ?
|
|
187
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
188
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
189
|
+
});
|
|
190
|
+
|
|
223
191
|
// 当响应的数据 success 是 false 的时候,抛出 error 以供 errorHandler 处理。
|
|
224
|
-
requestInstance.interceptors.response.use((response)=>{
|
|
225
|
-
const {data} = response;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if(errorInfo.success === false){
|
|
229
|
-
const error: RequestError = new Error(errorInfo.errorMessage);
|
|
230
|
-
error.name = 'BizError';
|
|
231
|
-
error.data = data;
|
|
232
|
-
error.info = errorInfo;
|
|
233
|
-
error.response = response;
|
|
234
|
-
throw error;
|
|
192
|
+
requestInstance.interceptors.response.use((response) => {
|
|
193
|
+
const { data } = response;
|
|
194
|
+
if(data?.success === false && config?.errorConfig?.errorThrower){
|
|
195
|
+
config.errorConfig.errorThrower(data);
|
|
235
196
|
}
|
|
236
197
|
return response;
|
|
237
198
|
})
|
|
238
199
|
return requestInstance;
|
|
239
200
|
};
|
|
240
201
|
|
|
241
|
-
const request: IRequest = (url, opts) => {
|
|
202
|
+
const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
|
|
242
203
|
const requestInstance = getRequestInstance();
|
|
243
204
|
const config = getConfig();
|
|
244
|
-
|
|
205
|
+
const { getResponse = false, requestInterceptors, responseInterceptors } = opts;
|
|
206
|
+
const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
|
|
207
|
+
if(interceptor instanceof Array){
|
|
208
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
209
|
+
const { url } = config;
|
|
210
|
+
if(interceptor[0].length === 2){
|
|
211
|
+
const { url: newUrl, options } = interceptor[0](url, config);
|
|
212
|
+
return { ...options, url: newUrl };
|
|
213
|
+
}
|
|
214
|
+
return interceptor[0](config);
|
|
215
|
+
}, interceptor[1]);
|
|
216
|
+
} else {
|
|
217
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
218
|
+
const { url } = config;
|
|
219
|
+
if(interceptor.length === 2){
|
|
220
|
+
const { url: newUrl, options } = interceptor(url, config);
|
|
221
|
+
return { ...options, url: newUrl };
|
|
222
|
+
}
|
|
223
|
+
return interceptor(config);
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
const responseInterceptorsToEject = responseInterceptors?.map((interceptor) => {
|
|
228
|
+
return interceptor instanceof Array ?
|
|
229
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
230
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
231
|
+
});
|
|
232
|
+
return new Promise((resolve, reject)=>{
|
|
245
233
|
requestInstance
|
|
246
|
-
.request({
|
|
247
|
-
.then((res)
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
234
|
+
.request({...opts, url})
|
|
235
|
+
.then((res)=>{
|
|
236
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
237
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
238
|
+
});
|
|
239
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
240
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
241
|
+
});
|
|
242
|
+
resolve(getResponse ? res : res.data);
|
|
251
243
|
})
|
|
252
|
-
.catch((error)
|
|
244
|
+
.catch((error)=>{
|
|
245
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
246
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
247
|
+
});
|
|
248
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
249
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
250
|
+
});
|
|
253
251
|
try {
|
|
254
252
|
const handler =
|
|
255
|
-
config
|
|
256
|
-
handler
|
|
253
|
+
config?.errorConfig?.errorHandler;
|
|
254
|
+
if(handler)
|
|
255
|
+
handler(error, opts, config);
|
|
257
256
|
} catch (e) {
|
|
258
257
|
reject(e);
|
|
259
258
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
259
|
+
reject(error);
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
263
|
|
|
264
264
|
export {
|
|
265
265
|
useRequest,
|
|
@@ -272,6 +272,8 @@ export type {
|
|
|
272
272
|
AxiosInstance,
|
|
273
273
|
AxiosRequestConfig,
|
|
274
274
|
AxiosResponse,
|
|
275
|
+
IRequestOptions as RequestOptions,
|
|
276
|
+
IRequest as Request,
|
|
275
277
|
};
|
|
276
278
|
|
|
277
279
|
`;
|
|
@@ -279,18 +281,24 @@ export type {
|
|
|
279
281
|
var _a;
|
|
280
282
|
const umiRequestPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('@ahooksjs/use-request/package.json')));
|
|
281
283
|
const axiosPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('axios/package.json')));
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
284
|
+
let dataField = (_a = api.config.request) === null || _a === void 0 ? void 0 : _a.dataField;
|
|
285
|
+
if (dataField === undefined)
|
|
286
|
+
dataField = 'data';
|
|
287
|
+
const formatResult = dataField === '' ? `result => result` : `result => result?.${dataField}`;
|
|
286
288
|
api.writeTmpFile({
|
|
287
289
|
path: 'request.ts',
|
|
288
290
|
content: plugin_utils_1.Mustache.render(requestTpl, {
|
|
289
291
|
umiRequestPath,
|
|
290
292
|
axiosPath,
|
|
291
|
-
|
|
293
|
+
formatResult,
|
|
292
294
|
}),
|
|
293
295
|
});
|
|
296
|
+
api.writeTmpFile({
|
|
297
|
+
path: 'types.d.ts',
|
|
298
|
+
content: `
|
|
299
|
+
export type { RequestConfig } from './request';
|
|
300
|
+
`,
|
|
301
|
+
});
|
|
294
302
|
api.writeTmpFile({
|
|
295
303
|
path: 'index.ts',
|
|
296
304
|
content: `
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.isNeedPolyfill = exports.exactLocalePaths = exports.getLocaleList = exports.getAntdLocale = exports.getMomentLocale = void 0;
|
|
13
4
|
const fs_1 = require("fs");
|
|
@@ -55,7 +46,7 @@ const modulesHasLocale = (localePath) => {
|
|
|
55
46
|
return false;
|
|
56
47
|
}
|
|
57
48
|
};
|
|
58
|
-
const getLocaleList = (opts) =>
|
|
49
|
+
const getLocaleList = async (opts) => {
|
|
59
50
|
const { localeFolder, separator = '-', absSrcPath = '', absPagesPath = '', addAntdLocales, resolveKey = 'moment', } = opts;
|
|
60
51
|
const localeFileMath = new RegExp(`^([a-z]{2})${separator}?([A-Z]{2})?\.(js|json|ts)$`);
|
|
61
52
|
const localeFiles = plugin_utils_1.glob
|
|
@@ -80,11 +71,11 @@ const getLocaleList = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
80
71
|
};
|
|
81
72
|
});
|
|
82
73
|
const groups = plugin_utils_1.lodash.groupBy(localeFiles, 'name');
|
|
83
|
-
const promises = Object.keys(groups).map((name) =>
|
|
74
|
+
const promises = Object.keys(groups).map(async (name) => {
|
|
84
75
|
const [lang, country = ''] = name.split(separator);
|
|
85
76
|
const { momentLocale } = (0, exports.getMomentLocale)(lang, country, resolveKey);
|
|
86
77
|
const antdLocale = plugin_utils_1.lodash
|
|
87
|
-
.uniq(
|
|
78
|
+
.uniq(await addAntdLocales({ lang, country }))
|
|
88
79
|
.filter((localePath) => modulesHasLocale(localePath));
|
|
89
80
|
return {
|
|
90
81
|
lang,
|
|
@@ -97,9 +88,9 @@ const getLocaleList = (opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
97
88
|
paths: groups[name].map((item) => (0, plugin_utils_1.winPath)(item.path)),
|
|
98
89
|
momentLocale,
|
|
99
90
|
};
|
|
100
|
-
})
|
|
91
|
+
});
|
|
101
92
|
return Promise.all(promises);
|
|
102
|
-
}
|
|
93
|
+
};
|
|
103
94
|
exports.getLocaleList = getLocaleList;
|
|
104
95
|
const exactLocalePaths = (data) => {
|
|
105
96
|
return plugin_utils_1.lodash.flatten(data.map((item) => item.paths));
|
|
@@ -7,12 +7,15 @@ interface IOpts {
|
|
|
7
7
|
content: string;
|
|
8
8
|
}) => Boolean;
|
|
9
9
|
}
|
|
10
|
+
export declare function getNamespace(absFilePath: string, absSrcPath: string): string;
|
|
10
11
|
export declare class Model {
|
|
11
12
|
file: string;
|
|
12
13
|
namespace: string;
|
|
13
14
|
id: string;
|
|
14
15
|
exportName: string;
|
|
15
|
-
|
|
16
|
+
deps: string[];
|
|
17
|
+
constructor(file: string, absSrcPath: string, sort: {} | undefined, id: number);
|
|
18
|
+
findDeps(sort: object): string[];
|
|
16
19
|
}
|
|
17
20
|
export declare class ModelUtils {
|
|
18
21
|
api: IApi;
|
|
@@ -20,8 +23,10 @@ export declare class ModelUtils {
|
|
|
20
23
|
count: number;
|
|
21
24
|
constructor(api: IApi | null, opts: IOpts);
|
|
22
25
|
getAllModels(opts: {
|
|
26
|
+
sort?: object;
|
|
23
27
|
extraModels: string[];
|
|
24
28
|
}): Model[];
|
|
29
|
+
getSortedNamespaces(models: Model[]): string[];
|
|
25
30
|
getModels(opts: {
|
|
26
31
|
base: string;
|
|
27
32
|
pattern?: string;
|