@umijs/plugins 4.0.0-rc.11 → 4.0.0-rc.14
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 +20 -6
- package/dist/antd.js +2 -0
- package/dist/dva.js +10 -2
- package/dist/initial-state.js +2 -2
- package/dist/layout.js +1 -1
- package/dist/locale.js +2 -2
- package/dist/qiankun/master.js +6 -0
- package/dist/qiankun/slave.js +7 -1
- package/dist/request.js +130 -126
- package/package.json +10 -10
package/dist/access.js
CHANGED
|
@@ -8,7 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const fs_1 = __importDefault(require("fs"));
|
|
12
16
|
const path_1 = require("path");
|
|
13
17
|
const withTmpPath_1 = require("./utils/withTmpPath");
|
|
14
18
|
exports.default = (api) => {
|
|
@@ -21,18 +25,28 @@ exports.default = (api) => {
|
|
|
21
25
|
enableBy: api.EnableBy.config,
|
|
22
26
|
});
|
|
23
27
|
api.onGenerateFiles(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
// allow enable access without access file
|
|
29
|
+
const hasAccessFile = ['js', 'jsx', 'ts', 'tsx'].some((ext) => fs_1.default.existsSync((0, path_1.join)(api.paths.absSrcPath, `access.${ext}`)));
|
|
24
30
|
// runtime.tsx
|
|
25
31
|
api.writeTmpFile({
|
|
26
32
|
path: 'runtime.tsx',
|
|
27
33
|
content: `
|
|
28
|
-
import React from 'react'
|
|
29
|
-
|
|
34
|
+
import React from 'react';${hasAccessFile
|
|
35
|
+
? `
|
|
36
|
+
import accessFactory from '@/access'
|
|
30
37
|
import { useModel } from '@@/plugin-model';
|
|
38
|
+
`
|
|
39
|
+
: ''}
|
|
31
40
|
import { AccessContext } from './context';
|
|
32
41
|
|
|
33
|
-
function Provider(props) {
|
|
42
|
+
function Provider(props) {${hasAccessFile
|
|
43
|
+
? `
|
|
34
44
|
const { initialState } = useModel('@@initialState');
|
|
35
45
|
const access = React.useMemo(() => accessFactory(initialState), [initialState]);
|
|
46
|
+
`
|
|
47
|
+
: `
|
|
48
|
+
const access = {};
|
|
49
|
+
`}
|
|
36
50
|
return (
|
|
37
51
|
<AccessContext.Provider value={access}>
|
|
38
52
|
{ props.children }
|
|
@@ -45,9 +59,9 @@ export function accessProvider(container) {
|
|
|
45
59
|
}
|
|
46
60
|
`,
|
|
47
61
|
});
|
|
48
|
-
// index.
|
|
62
|
+
// index.tsx
|
|
49
63
|
api.writeTmpFile({
|
|
50
|
-
path: 'index.
|
|
64
|
+
path: 'index.tsx',
|
|
51
65
|
content: `
|
|
52
66
|
import React from 'react';
|
|
53
67
|
import { AccessContext } from './context';
|
|
@@ -66,7 +80,7 @@ export const Access: React.FC<AccessProps> = (props) => {
|
|
|
66
80
|
throw new Error('[access] the \`accessible\` property on <Access /> should be a boolean');
|
|
67
81
|
}
|
|
68
82
|
|
|
69
|
-
return props.accessible ? props.children : props.fallback
|
|
83
|
+
return <>{ props.accessible ? props.children : props.fallback }</>;
|
|
70
84
|
};
|
|
71
85
|
|
|
72
86
|
export const useAccessMarkedRoutes = (routes: IRoute[]) => {
|
package/dist/antd.js
CHANGED
|
@@ -77,6 +77,7 @@ exports.default = (api) => {
|
|
|
77
77
|
libraryDirectory: 'es',
|
|
78
78
|
style: style === 'less' ? true : 'css',
|
|
79
79
|
},
|
|
80
|
+
'antd',
|
|
80
81
|
],
|
|
81
82
|
]
|
|
82
83
|
: [];
|
|
@@ -88,6 +89,7 @@ exports.default = (api) => {
|
|
|
88
89
|
api.writeTmpFile({
|
|
89
90
|
path: `runtime.tsx`,
|
|
90
91
|
content: plugin_utils_1.Mustache.render(`
|
|
92
|
+
import React from 'react';
|
|
91
93
|
import { ConfigProvider, Modal, message, notification } from 'antd';
|
|
92
94
|
|
|
93
95
|
export function rootContainer(container) {
|
package/dist/dva.js
CHANGED
|
@@ -25,11 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.getAllModels = exports.getModelUtil = void 0;
|
|
27
27
|
const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
28
|
+
const utils_1 = require("@umijs/utils");
|
|
28
29
|
const path_1 = require("path");
|
|
29
30
|
const plugin_utils_1 = require("umi/plugin-utils");
|
|
30
31
|
const modelUtils_1 = require("./utils/modelUtils");
|
|
31
32
|
const withTmpPath_1 = require("./utils/withTmpPath");
|
|
32
|
-
const utils_1 = require("@umijs/utils");
|
|
33
33
|
exports.default = (api) => {
|
|
34
34
|
const pkgPath = (0, path_1.join)(__dirname, '../libs/dva.ts');
|
|
35
35
|
api.describe({
|
|
@@ -80,15 +80,22 @@ import dvaImmer, { enableES5, enableAllPlugins } from '${(0, utils_1.winPath)(re
|
|
|
80
80
|
`
|
|
81
81
|
: ''}
|
|
82
82
|
import React, { useRef } from 'react';
|
|
83
|
-
import { history } from 'umi';
|
|
83
|
+
import { history, ApplyPluginsType, useAppData } from 'umi';
|
|
84
84
|
import { models } from './models';
|
|
85
85
|
|
|
86
86
|
export function RootContainer(props: any) {
|
|
87
|
+
const { pluginManager } = useAppData();
|
|
87
88
|
const app = useRef<any>();
|
|
89
|
+
const runtimeDva = pluginManager.applyPlugins({
|
|
90
|
+
key: 'dva',
|
|
91
|
+
type: ApplyPluginsType.modify,
|
|
92
|
+
initialValue: {},
|
|
93
|
+
});
|
|
88
94
|
if (!app.current) {
|
|
89
95
|
app.current = create(
|
|
90
96
|
{
|
|
91
97
|
history,
|
|
98
|
+
...(runtimeDva.config || {}),
|
|
92
99
|
},
|
|
93
100
|
{
|
|
94
101
|
initialReducer: {},
|
|
@@ -139,6 +146,7 @@ export { connect, useDispatch, useStore, useSelector } from 'dva';`,
|
|
|
139
146
|
api.addRuntimePlugin(() => {
|
|
140
147
|
return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
|
|
141
148
|
});
|
|
149
|
+
api.addRuntimePluginKey(() => ['dva']);
|
|
142
150
|
// dva list model
|
|
143
151
|
api.registerCommand({
|
|
144
152
|
name: 'dva',
|
package/dist/initial-state.js
CHANGED
|
@@ -31,7 +31,7 @@ import React from 'react';
|
|
|
31
31
|
import { useModel } from '@@/plugin-model';
|
|
32
32
|
${loading
|
|
33
33
|
? `import Loading from '${loading}'`
|
|
34
|
-
: `function Loading() { return <div
|
|
34
|
+
: `function Loading() { return <div />; }`}
|
|
35
35
|
export default function InitialStateProvider(props: any) {
|
|
36
36
|
const appLoaded = React.useRef(false);
|
|
37
37
|
const { loading = false } = useModel("@@initialState") || {};
|
|
@@ -107,7 +107,7 @@ export default () => ({ loading: false, refresh: () => {} })
|
|
|
107
107
|
content: `
|
|
108
108
|
import React from 'react';
|
|
109
109
|
import Provider from './Provider';
|
|
110
|
-
export function
|
|
110
|
+
export function dataflowProvider(container) {
|
|
111
111
|
return <Provider>{ container }</Provider>;
|
|
112
112
|
}
|
|
113
113
|
`,
|
package/dist/layout.js
CHANGED
package/dist/locale.js
CHANGED
|
@@ -157,7 +157,7 @@ exports.default = (api) => {
|
|
|
157
157
|
})) }))),
|
|
158
158
|
Antd: !!antd,
|
|
159
159
|
DefaultLocale: JSON.stringify(defaultLocale),
|
|
160
|
-
warningPkgPath: (0, plugin_utils_1.winPath)(require.resolve('warning/package')),
|
|
160
|
+
warningPkgPath: (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('warning/package'))),
|
|
161
161
|
reactIntlPkgPath,
|
|
162
162
|
}),
|
|
163
163
|
});
|
|
@@ -184,7 +184,7 @@ exports.default = (api) => {
|
|
|
184
184
|
api.writeTmpFile({
|
|
185
185
|
path: 'index.ts',
|
|
186
186
|
content: `
|
|
187
|
-
export { setLocale, getLocale, useIntl, formatMessage, FormattedMessage } from './localeExports.ts';
|
|
187
|
+
export { setLocale, getLocale, useIntl, injectIntl, formatMessage, FormattedMessage } from './localeExports.ts';
|
|
188
188
|
export { SelectLang } from './SelectLang.tsx';
|
|
189
189
|
`,
|
|
190
190
|
});
|
package/dist/qiankun/master.js
CHANGED
package/dist/qiankun/slave.js
CHANGED
|
@@ -129,11 +129,17 @@ if (!window.__POWERED_BY_QIANKUN__) {
|
|
|
129
129
|
path: file.replace(/\.tpl$/, ''),
|
|
130
130
|
content: getFileContent(file)
|
|
131
131
|
.replace('__USE_MODEL__', api.isPluginEnable('model')
|
|
132
|
-
? `import { useModel } from '@@/plugin
|
|
132
|
+
? `import { useModel } from '@@/plugin-model'`
|
|
133
133
|
: `const useModel = null;`)
|
|
134
134
|
.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
|
|
135
135
|
.replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
|
|
136
136
|
});
|
|
137
137
|
});
|
|
138
|
+
api.writeTmpFile({
|
|
139
|
+
path: 'index.ts',
|
|
140
|
+
content: `
|
|
141
|
+
export { connectMaster } from './connectMaster';
|
|
142
|
+
`,
|
|
143
|
+
});
|
|
138
144
|
});
|
|
139
145
|
};
|
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
192
|
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;
|
|
193
|
+
const { data } = response;
|
|
194
|
+
if(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.errorConfig?.errorHandler
|
|
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,
|
|
@@ -279,16 +279,16 @@ export type {
|
|
|
279
279
|
var _a;
|
|
280
280
|
const umiRequestPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('@ahooksjs/use-request/package.json')));
|
|
281
281
|
const axiosPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('axios/package.json')));
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
282
|
+
let dataField = (_a = api.config.request) === null || _a === void 0 ? void 0 : _a.dataField;
|
|
283
|
+
if (dataField === undefined)
|
|
284
|
+
dataField = 'data';
|
|
285
|
+
const formatResult = dataField === '' ? `result => result` : `result => result?.${dataField}`;
|
|
286
286
|
api.writeTmpFile({
|
|
287
287
|
path: 'request.ts',
|
|
288
288
|
content: plugin_utils_1.Mustache.render(requestTpl, {
|
|
289
289
|
umiRequestPath,
|
|
290
290
|
axiosPath,
|
|
291
|
-
|
|
291
|
+
formatResult,
|
|
292
292
|
}),
|
|
293
293
|
});
|
|
294
294
|
api.writeTmpFile({
|
|
@@ -299,6 +299,10 @@ export {
|
|
|
299
299
|
UseRequestProvider,
|
|
300
300
|
request,
|
|
301
301
|
} from './request';
|
|
302
|
+
|
|
303
|
+
export type {
|
|
304
|
+
RequestConfig
|
|
305
|
+
} from './request';
|
|
302
306
|
`,
|
|
303
307
|
});
|
|
304
308
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/plugins",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.14",
|
|
4
4
|
"description": "@umijs/plugins",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugins#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
@@ -17,15 +17,15 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "pnpm tsc",
|
|
20
|
-
"build:deps": "
|
|
20
|
+
"build:deps": "umi-scripts bundleDeps",
|
|
21
21
|
"dev": "pnpm build -- --watch",
|
|
22
|
-
"test": "
|
|
22
|
+
"test": "umi-scripts jest-turbo"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@ahooksjs/use-request": "^2.0.0",
|
|
26
26
|
"@ant-design/icons": "^4.7.0",
|
|
27
|
-
"@ant-design/pro-layout": "^6.
|
|
28
|
-
"@umijs/bundler-utils": "4.0.0-rc.
|
|
27
|
+
"@ant-design/pro-layout": "^6.35.1",
|
|
28
|
+
"@umijs/bundler-utils": "4.0.0-rc.14",
|
|
29
29
|
"antd-dayjs-webpack-plugin": "^1.0.6",
|
|
30
30
|
"axios": "^0.26.1",
|
|
31
31
|
"babel-plugin-import": "^1.13.3",
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
"event-emitter": "~0.3.5",
|
|
37
37
|
"fast-deep-equal": "3.1.3",
|
|
38
38
|
"lodash": "^4.17.21",
|
|
39
|
-
"moment": "^2.29.
|
|
40
|
-
"qiankun": "^2.
|
|
39
|
+
"moment": "^2.29.3",
|
|
40
|
+
"qiankun": "^2.7.0",
|
|
41
41
|
"react-intl": "3.12.1",
|
|
42
|
-
"react-redux": "^
|
|
43
|
-
"redux": "^4.
|
|
42
|
+
"react-redux": "^8.0.0",
|
|
43
|
+
"redux": "^4.2.0",
|
|
44
44
|
"warning": "^4.0.3"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"umi": "4.0.0-rc.
|
|
47
|
+
"umi": "4.0.0-rc.14"
|
|
48
48
|
},
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|