@umijs/plugins 4.0.0-rc.13 → 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 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
- import accessFactory from '@/access';
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 }
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',
@@ -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 innerProvider(container) {
110
+ export function dataflowProvider(container) {
111
111
  return <Provider>{ container }</Provider>;
112
112
  }
113
113
  `,
package/dist/layout.js CHANGED
@@ -39,7 +39,7 @@ exports.default = (api) => {
39
39
  schema(joi) {
40
40
  return joi.object();
41
41
  },
42
- onChange: api.ConfigChangeType.regenerateTmpFiles,
42
+ // onChange: api.ConfigChangeType.regenerateTmpFiles,
43
43
  },
44
44
  enableBy: api.EnableBy.config,
45
45
  });
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
  });
@@ -110,5 +110,11 @@ export const setMasterOptions = (newOpts) => options = ({ ...options, ...newOpts
110
110
  });
111
111
  }
112
112
  });
113
+ api.writeTmpFile({
114
+ path: 'index.ts',
115
+ content: `
116
+ export { MicroApp } from './MicroApp';
117
+ `,
118
+ });
113
119
  });
114
120
  };
@@ -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/model'`
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
@@ -102,7 +102,10 @@ function useRequest(service: any, options: any = {}) {
102
102
 
103
103
  // request 方法 opts 参数的接口
104
104
  interface IRequestOptions extends AxiosRequestConfig {
105
- skipErrorHandler?: boolean
105
+ skipErrorHandler?: boolean;
106
+ requestInterceptors?: IRequestInterceptorTuple[];
107
+ responseInterceptors?: IResponseInterceptorTuple[];
108
+ [key: string]: any;
106
109
  }
107
110
 
108
111
  interface IRequestOptionsWithResponse extends IRequestOptions {
@@ -123,12 +126,21 @@ interface IRequest{
123
126
  interface IErrorHandler {
124
127
  (error: RequestError, opts: IRequestOptions): void;
125
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
126
136
 
127
137
  export interface RequestConfig extends AxiosRequestConfig {
128
138
  errorConfig?: {
129
139
  errorHandler?: IErrorHandler;
130
140
  errorThrower?: <T = any>( res: T ) => void
131
141
  };
142
+ requestInterceptors?: IRequestInterceptorTuple[];
143
+ responseInterceptors?: IResponseInterceptorTuple[];
132
144
  }
133
145
 
134
146
  let requestInstance: AxiosInstance;
@@ -142,11 +154,40 @@ const getConfig = (): RequestConfig => {
142
154
  });
143
155
  return config;
144
156
  };
157
+
145
158
  const getRequestInstance = (): AxiosInstance => {
146
159
  if (requestInstance) return requestInstance;
147
160
  const config = getConfig();
148
161
  requestInstance = axios.create(config);
149
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
+
150
191
  // 当响应的数据 success 是 false 的时候,抛出 error 以供 errorHandler 处理。
151
192
  requestInstance.interceptors.response.use((response)=>{
152
193
  const { data } = response;
@@ -161,14 +202,52 @@ const getRequestInstance = (): AxiosInstance => {
161
202
  const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
162
203
  const requestInstance = getRequestInstance();
163
204
  const config = getConfig();
164
- const { getResponse = false } = opts;
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
+ });
165
232
  return new Promise((resolve, reject)=>{
166
233
  requestInstance
167
234
  .request({...opts, url})
168
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
+ });
169
242
  resolve(getResponse ? res : res.data);
170
243
  })
171
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
+ });
172
251
  try {
173
252
  const handler =
174
253
  config.errorConfig?.errorHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-rc.13",
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": "pnpm esno ../../scripts/bundleDeps.ts",
20
+ "build:deps": "umi-scripts bundleDeps",
21
21
  "dev": "pnpm build -- --watch",
22
- "test": "jest -c ../../jest.turbo.config.ts"
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
27
  "@ant-design/pro-layout": "^6.35.1",
28
- "@umijs/bundler-utils": "4.0.0-rc.13",
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",
@@ -44,7 +44,7 @@
44
44
  "warning": "^4.0.3"
45
45
  },
46
46
  "devDependencies": {
47
- "umi": "4.0.0-rc.13"
47
+ "umi": "4.0.0-rc.14"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"