@umijs/plugins 4.0.0-rc.10 → 4.0.0-rc.13

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 CHANGED
@@ -45,9 +45,9 @@ export function accessProvider(container) {
45
45
  }
46
46
  `,
47
47
  });
48
- // index.ts
48
+ // index.tsx
49
49
  api.writeTmpFile({
50
- path: 'index.ts',
50
+ path: 'index.tsx',
51
51
  content: `
52
52
  import React from 'react';
53
53
  import { AccessContext } from './context';
@@ -66,7 +66,7 @@ export const Access: React.FC<AccessProps> = (props) => {
66
66
  throw new Error('[access] the \`accessible\` property on <Access /> should be a boolean');
67
67
  }
68
68
 
69
- return props.accessible ? props.children : props.fallback;
69
+ return <>{ props.accessible ? props.children : props.fallback }</>;
70
70
  };
71
71
 
72
72
  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) {
@@ -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>loading</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") || {};
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: result => result?.data,
88
+ formatResult: {{{formatResult}}},
85
89
  requestMethod: (requestOptions: any) => {
86
90
  if (typeof requestOptions === 'string') {
87
91
  return request(requestOptions);
@@ -96,114 +100,37 @@ function useRequest(service: any, options: any = {}) {
96
100
  });
97
101
  }
98
102
 
99
- export interface RequestConfig extends AxiosRequestConfig {
100
- errorConfig?: {
101
- errorPage?: string;
102
- adaptor?: IAdaptor; // adaptor 用以用户将不满足接口的后端数据修改成 errorInfo
103
- errorHandler?: IErrorHandler;
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,
103
+ // request 方法 opts 参数的接口
104
+ interface IRequestOptions extends AxiosRequestConfig {
105
+ skipErrorHandler?: boolean
116
106
  }
117
107
 
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;
126
- [key: string]: any;
127
- }
128
- // resData 其实就是 response.data, response 则是 axios 的响应对象
129
- interface IAdaptor {
130
- (resData: any, response: AxiosResponse): IErrorInfo;
108
+ interface IRequestOptionsWithResponse extends IRequestOptions {
109
+ getResponse: true;
131
110
  }
132
111
 
133
- export interface RequestError extends Error {
134
- data?: any;
135
- info?: IErrorInfo;
112
+ interface IRequestOptionsWithoutResponse extends IRequestOptions{
113
+ getResponse: false;
136
114
  }
137
115
 
138
- interface IRequest {
139
- (
140
- url: string,
141
- opts: AxiosRequestConfig & { skipErrorHandler?: boolean },
142
- ): Promise<AxiosResponse<any, any>>;
116
+ interface IRequest{
117
+ <T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<T>>;
118
+ <T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<T>;
119
+ <T = any>(url: string, opts: IRequestOptions): Promise<T>; // getResponse 默认是 false, 因此不提供该参数时,只返回 data
120
+ <T = any>(url: string): Promise<T>; // 不提供 opts 时,默认使用 'GET' method,并且默认返回 data
143
121
  }
144
122
 
145
123
  interface IErrorHandler {
146
- (error: RequestError, opts: AxiosRequestConfig & { skipErrorHandler?: boolean }, config: RequestConfig): void;
124
+ (error: RequestError, opts: IRequestOptions): void;
147
125
  }
148
126
 
149
- interface IFormatResultAdaptor {
150
- (res: AxiosResponse): any;
127
+ export interface RequestConfig extends AxiosRequestConfig {
128
+ errorConfig?: {
129
+ errorHandler?: IErrorHandler;
130
+ errorThrower?: <T = any>( res: T ) => void
131
+ };
151
132
  }
152
133
 
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
134
  let requestInstance: AxiosInstance;
208
135
  let config: RequestConfig;
209
136
  const getConfig = (): RequestConfig => {
@@ -222,44 +149,38 @@ const getRequestInstance = (): AxiosInstance => {
222
149
 
223
150
  // 当响应的数据 success 是 false 的时候,抛出 error 以供 errorHandler 处理。
224
151
  requestInstance.interceptors.response.use((response)=>{
225
- const {data} = response;
226
- const adaptor = config?.errorConfig?.adaptor || ((resData) => resData);
227
- const errorInfo = adaptor(data,response);
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;
152
+ const { data } = response;
153
+ if(config?.errorConfig?.errorThrower){
154
+ config.errorConfig.errorThrower(data);
235
155
  }
236
156
  return response;
237
157
  })
238
158
  return requestInstance;
239
159
  };
240
160
 
241
- const request: IRequest = (url, opts) => {
161
+ const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
242
162
  const requestInstance = getRequestInstance();
243
163
  const config = getConfig();
244
- return new Promise((resolve, reject) => {
164
+ const { getResponse = false } = opts;
165
+ return new Promise((resolve, reject)=>{
245
166
  requestInstance
246
- .request({ ...opts, url })
247
- .then((res) => {
248
- const formatResultAdaptor =
249
- config?.formatResultAdaptor || ((res) => res.data);
250
- resolve(formatResultAdaptor(res));
167
+ .request({...opts, url})
168
+ .then((res)=>{
169
+ resolve(getResponse ? res : res.data);
251
170
  })
252
- .catch((error) => {
171
+ .catch((error)=>{
253
172
  try {
254
173
  const handler =
255
- config.errorConfig?.errorHandler || defaultErrorHandler;
256
- handler(error, opts, config);
174
+ config.errorConfig?.errorHandler;
175
+ if(handler)
176
+ handler(error, opts, config);
257
177
  } catch (e) {
258
178
  reject(e);
259
179
  }
260
- });
261
- });
262
- };
180
+ reject(error);
181
+ })
182
+ })
183
+ }
263
184
 
264
185
  export {
265
186
  useRequest,
@@ -279,16 +200,16 @@ export type {
279
200
  var _a;
280
201
  const umiRequestPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('@ahooksjs/use-request/package.json')));
281
202
  const axiosPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('axios/package.json')));
282
- const antdPkg = (0, plugin_utils_1.winPath)(
283
- // use path from antd plugin first
284
- ((_a = api.appData.antd) === null || _a === void 0 ? void 0 : _a.pkgPath) ||
285
- (0, path_1.dirname)(require.resolve('antd/package.json')));
203
+ let dataField = (_a = api.config.request) === null || _a === void 0 ? void 0 : _a.dataField;
204
+ if (dataField === undefined)
205
+ dataField = 'data';
206
+ const formatResult = dataField === '' ? `result => result` : `result => result?.${dataField}`;
286
207
  api.writeTmpFile({
287
208
  path: 'request.ts',
288
209
  content: plugin_utils_1.Mustache.render(requestTpl, {
289
210
  umiRequestPath,
290
211
  axiosPath,
291
- antdPkg,
212
+ formatResult,
292
213
  }),
293
214
  });
294
215
  api.writeTmpFile({
@@ -299,6 +220,10 @@ export {
299
220
  UseRequestProvider,
300
221
  request,
301
222
  } from './request';
223
+
224
+ export type {
225
+ RequestConfig
226
+ } from './request';
302
227
  `,
303
228
  });
304
229
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-rc.10",
3
+ "version": "4.0.0-rc.13",
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",
@@ -24,8 +24,8 @@
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.34.6",
28
- "@umijs/bundler-utils": "4.0.0-rc.10",
27
+ "@ant-design/pro-layout": "^6.35.1",
28
+ "@umijs/bundler-utils": "4.0.0-rc.13",
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.1",
40
- "qiankun": "^2.6.3",
39
+ "moment": "^2.29.3",
40
+ "qiankun": "^2.7.0",
41
41
  "react-intl": "3.12.1",
42
- "react-redux": "^7.2.6",
43
- "redux": "^4.1.2",
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.10"
47
+ "umi": "4.0.0-rc.13"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"