@creekjs/umi-plugins 1.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/README.md +326 -0
- package/dist/creek-layout/index.d.ts +3 -0
- package/dist/creek-layout/index.js +193 -0
- package/dist/creek-layout/template/icons.tpl +6 -0
- package/dist/creek-layout/template/layout.tpl +123 -0
- package/dist/creek-layout/template/runtime-config.type.tpl +5 -0
- package/dist/creek-layout/template/runtime.tpl +41 -0
- package/dist/creek-layout/template/type.tpl +36 -0
- package/dist/open-api/index.d.ts +3 -0
- package/dist/open-api/index.js +76 -0
- package/dist/template/locale/i18N-package.tpl +16 -0
- package/dist/template/locale/i18N.tpl +15 -0
- package/dist/template/locale/index.tpl +2 -0
- package/dist/template/request/index.tpl +8 -0
- package/dist/template/request/request.tpl +580 -0
- package/dist/template/request/runtime-config.tpl +6 -0
- package/dist/template/request/type.tpl +6 -0
- package/dist/utils/file.d.ts +4 -0
- package/dist/utils/file.js +74 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +27 -0
- package/dist/utils/spawn.d.ts +5 -0
- package/dist/utils/spawn.js +71 -0
- package/dist/utils/withTmpPath.d.ts +6 -0
- package/dist/utils/withTmpPath.js +39 -0
- package/package.json +17 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { notification } from 'antd';
|
|
3
|
+
import { ApplyPluginsType } from 'umi';
|
|
4
|
+
import { ICacheLike, cacheAdapterEnhancer } from '{{{axiosExtensionsPath}}}';
|
|
5
|
+
import {CURRENT_LANGUAGE_KEY, LanguageEnum} from '{{{constantsPath}}}'
|
|
6
|
+
import axios, {
|
|
7
|
+
type AxiosError,
|
|
8
|
+
type AxiosInstance,
|
|
9
|
+
type AxiosRequestConfig,
|
|
10
|
+
type AxiosResponse,
|
|
11
|
+
} from '{{{axiosPath}}}';
|
|
12
|
+
import { useRequest as useUmiRequest } from '{{{umiRequestPath}}}';
|
|
13
|
+
import { getPluginManager } from '../core/plugin';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
Options,
|
|
17
|
+
Service,
|
|
18
|
+
Result
|
|
19
|
+
} from '{{{umiRequestPath}}}/es/useRequest/src/types';
|
|
20
|
+
|
|
21
|
+
type OptionsWithNoFullApiData<TData, TParams> = Options<TData, TParams> & {
|
|
22
|
+
formatResult?: (data: TData['result'], params: TParams) => TData['result'],
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type OptionsWithFullApiData<TData, TParams> = Options<TData, TParams> & {
|
|
26
|
+
formatResult?: (data: TData, params: TParams) => TData,
|
|
27
|
+
needFullApiData: true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function useRequest<
|
|
31
|
+
TData,
|
|
32
|
+
TParams extends any[]
|
|
33
|
+
>(
|
|
34
|
+
service:Service<TData, TParams>,
|
|
35
|
+
options?: OptionsWithFullApiData<TData, TParams>,
|
|
36
|
+
plugins?: Plugin<TData, TParams>[]
|
|
37
|
+
):Result<TData, TParams>;
|
|
38
|
+
|
|
39
|
+
function useRequest<
|
|
40
|
+
TData,
|
|
41
|
+
TParams extends any[]
|
|
42
|
+
>(
|
|
43
|
+
service:Service<TData, TParams>,
|
|
44
|
+
options?: OptionsWithNoFullApiData<TData, TParams>,
|
|
45
|
+
plugins?: Plugin<TData, TParams>[]
|
|
46
|
+
):Result<TData{{{resultDataField}}}, TParams>;
|
|
47
|
+
|
|
48
|
+
function useRequest<TData, TParams extends any[]>( service: Service<TData, TParams>,options?: Options<TData, TParams> = {}) {
|
|
49
|
+
const resquestResult = useUmiRequest(service, {
|
|
50
|
+
...options,
|
|
51
|
+
});
|
|
52
|
+
let needFullApiData = options['needFullApiData'] ;
|
|
53
|
+
if(options['formatResult']){
|
|
54
|
+
resquestResult.data = options['formatResult'](resquestResult.data, options);
|
|
55
|
+
}
|
|
56
|
+
if(needFullApiData){
|
|
57
|
+
resquestResult.data = resquestResult.data;
|
|
58
|
+
}else{
|
|
59
|
+
resquestResult.data = resquestResult.data? resquestResult.data['result'] : resquestResult.data ;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return resquestResult;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface IRequestOptions extends AxiosRequestConfig {
|
|
66
|
+
skipErrorHandler?: boolean;
|
|
67
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
68
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
69
|
+
closeMessage?: boolean;
|
|
70
|
+
setCustomMessage?: (error?: AxiosResponse<any, any> & {message: string}, opts?: IRequestOptions) => string;
|
|
71
|
+
cache?: boolean | {maxCount?: number, maxAge?: number};
|
|
72
|
+
[key: string]: any;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface IRequestOptionsWithResponse extends IRequestOptions {
|
|
76
|
+
getResponse: true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface IRequestOptionsWithoutResponse extends IRequestOptions{
|
|
80
|
+
getResponse: false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ApiResponse<T> {
|
|
84
|
+
code: string,
|
|
85
|
+
msg: string,
|
|
86
|
+
totalRows?: number;
|
|
87
|
+
totalPages?: number;
|
|
88
|
+
result?: T,
|
|
89
|
+
records?: T,
|
|
90
|
+
pageSize?: number,
|
|
91
|
+
currentPage?: number
|
|
92
|
+
success: boolean,
|
|
93
|
+
data: T{{{resultDataField}}} & T{{{resultDataField}}}['records'] & T['records'] & T,
|
|
94
|
+
total: number
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface AntdApiResponse<T> {
|
|
98
|
+
success: boolean,
|
|
99
|
+
data: T,
|
|
100
|
+
total: number
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
interface IRequest{
|
|
105
|
+
<T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<ApiResponse<T>>>;
|
|
106
|
+
<T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<ApiResponse<T>>;
|
|
107
|
+
<T = any>(url: string, opts: IRequestOptions): Promise<ApiResponse<T>>;
|
|
108
|
+
<T = any>(url: string): Promise<ApiResponse<T>>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type RequestError = AxiosError | Error
|
|
112
|
+
|
|
113
|
+
interface IErrorHandler {
|
|
114
|
+
(error: AxiosResponse<any, any> & {message: string}, opts: IRequestOptions): void;
|
|
115
|
+
}
|
|
116
|
+
type IRequestInterceptorAxios = (config: IRequestOptions) => IRequestOptions | Promise<IRequestOptions>;
|
|
117
|
+
type IRequestInterceptorUmiRequest = (url: string, config : IRequestOptions) => { url: string, options: RequestOptions } | Promise<{ url: string, options: RequestOptions }>;
|
|
118
|
+
type IRequestInterceptor = IRequestInterceptorAxios | IRequestInterceptorUmiRequest;
|
|
119
|
+
type IErrorInterceptor = (error: Error) => Promise<Error>;
|
|
120
|
+
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
|
|
121
|
+
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor;
|
|
122
|
+
type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor;
|
|
123
|
+
|
|
124
|
+
enum HttpStatusEnum {
|
|
125
|
+
CONTINUE = 100,
|
|
126
|
+
SWITCHING_PROTOCOLS = 101,
|
|
127
|
+
PROCESSING = 102,
|
|
128
|
+
SUCCESS = 200,
|
|
129
|
+
CREATED = 201,
|
|
130
|
+
ACCEPTED = 202,
|
|
131
|
+
NON_AUTHORITATIVE_INFORMATION = 203,
|
|
132
|
+
NO_CONTENT = 204,
|
|
133
|
+
RESET_CONTENT = 205,
|
|
134
|
+
PARTIAL_CONTENT = 206,
|
|
135
|
+
AMBIGUOUS = 300,
|
|
136
|
+
MOVED_PERMANENTLY = 301,
|
|
137
|
+
FOUND = 302,
|
|
138
|
+
SEE_OTHER = 303,
|
|
139
|
+
NOT_MODIFIED = 304,
|
|
140
|
+
TEMPORARY_REDIRECT = 307,
|
|
141
|
+
PERMANENT_REDIRECT = 308,
|
|
142
|
+
BAD_REQUEST = 400,
|
|
143
|
+
UNAUTHORIZED = 401,
|
|
144
|
+
PAYMENT_REQUIRED = 402,
|
|
145
|
+
FORBIDDEN = 403,
|
|
146
|
+
NOT_FOUND = 404,
|
|
147
|
+
METHOD_NOT_ALLOWED = 405,
|
|
148
|
+
NOT_ACCEPTABLE = 406,
|
|
149
|
+
PROXY_AUTHENTICATION_REQUIRED = 407,
|
|
150
|
+
REQUEST_TIMEOUT = 408,
|
|
151
|
+
CONFLICT = 409,
|
|
152
|
+
GONE = 410,
|
|
153
|
+
LENGTH_REQUIRED = 411,
|
|
154
|
+
PRECONDITION_FAILED = 412,
|
|
155
|
+
PAYLOAD_TOO_LARGE = 413,
|
|
156
|
+
URI_TOO_LONG = 414,
|
|
157
|
+
UNSUPPORTED_MEDIA_TYPE = 415,
|
|
158
|
+
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
|
|
159
|
+
EXPECTATION_FAILED = 417,
|
|
160
|
+
I_AM_A_TEAPOT = 418,
|
|
161
|
+
UNPROCESSABLE_ENTITY = 422,
|
|
162
|
+
TOO_MANY_REQUESTS = 429,
|
|
163
|
+
INTERNAL_SERVER_ERROR = 500,
|
|
164
|
+
NOT_IMPLEMENTED = 501,
|
|
165
|
+
BAD_GATEWAY = 502,
|
|
166
|
+
SERVICE_UNAVAILABLE = 503,
|
|
167
|
+
GATEWAY_TIMEOUT = 504,
|
|
168
|
+
HTTP_VERSION_NOT_SUPPORTED = 505,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
enum HttpMessageLocalEnum {
|
|
172
|
+
BAD_GATEWAY = {
|
|
173
|
+
[LanguageEnum.zhCn]: '网关错误',
|
|
174
|
+
[LanguageEnum.enUs]: 'gateway error',
|
|
175
|
+
},
|
|
176
|
+
SERVICE_UNAVAILABLE = {
|
|
177
|
+
[LanguageEnum.zhCn]: '服务不可用,服务器暂时过载或维护',
|
|
178
|
+
[LanguageEnum.enUs]: 'Service unavailable, server temporarily overloaded or maintenance gateway error',
|
|
179
|
+
},
|
|
180
|
+
GATEWAY_TIMEOUT = {
|
|
181
|
+
[LanguageEnum.zhCn]: '网关超时',
|
|
182
|
+
[LanguageEnum.enUs]: 'gateway timeout',
|
|
183
|
+
},
|
|
184
|
+
NOT_FOUND = {
|
|
185
|
+
[LanguageEnum.zhCn]: '路径错误',
|
|
186
|
+
[LanguageEnum.enUs]: 'wrong path',
|
|
187
|
+
},
|
|
188
|
+
UNAUTHORIZED = {
|
|
189
|
+
[LanguageEnum.zhCn]: '认证失败',
|
|
190
|
+
[LanguageEnum.enUs]: 'Authentication failed',
|
|
191
|
+
},
|
|
192
|
+
BAD_REQUEST = {
|
|
193
|
+
[LanguageEnum.zhCn]: '错误的请求',
|
|
194
|
+
[LanguageEnum.enUs]: 'bad Request',
|
|
195
|
+
},
|
|
196
|
+
INTERNAL_SERVER_ERROR = {
|
|
197
|
+
[LanguageEnum.zhCn]: '错误的请求',
|
|
198
|
+
[LanguageEnum.enUs]: 'bad Request',
|
|
199
|
+
},
|
|
200
|
+
ERROR_HTTP = {
|
|
201
|
+
[LanguageEnum.zhCn]: '服务器错误',
|
|
202
|
+
[LanguageEnum.enUs]: 'Server Error',
|
|
203
|
+
},
|
|
204
|
+
FORBIDDEN = {
|
|
205
|
+
[LanguageEnum.zhCn]: '无此权限',
|
|
206
|
+
[LanguageEnum.enUs]: 'no permission',
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
enum HttpMessageEnum {
|
|
211
|
+
BAD_GATEWAY = HttpMessageLocalEnum["BAD_GATEWAY"][CURRENT_LANGUAGE_KEY],
|
|
212
|
+
SERVICE_UNAVAILABLE = HttpMessageLocalEnum["SERVICE_UNAVAILABLE"][CURRENT_LANGUAGE_KEY],
|
|
213
|
+
GATEWAY_TIMEOUT = HttpMessageLocalEnum["GATEWAY_TIMEOUT"][CURRENT_LANGUAGE_KEY],
|
|
214
|
+
NOT_FOUND = HttpMessageLocalEnum["NOT_FOUND"][CURRENT_LANGUAGE_KEY],
|
|
215
|
+
UNAUTHORIZED = HttpMessageLocalEnum["UNAUTHORIZED"][CURRENT_LANGUAGE_KEY],
|
|
216
|
+
BAD_REQUEST = HttpMessageLocalEnum["BAD_REQUEST"][CURRENT_LANGUAGE_KEY],
|
|
217
|
+
INTERNAL_SERVER_ERROR=HttpMessageLocalEnum["INTERNAL_SERVER_ERROR"][CURRENT_LANGUAGE_KEY],
|
|
218
|
+
ERROR_HTTP =HttpMessageLocalEnum["ERROR_HTTP"][CURRENT_LANGUAGE_KEY],
|
|
219
|
+
FORBIDDEN = HttpMessageLocalEnum["FORBIDDEN"][CURRENT_LANGUAGE_KEY],
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface RequestConfig<T = any> extends AxiosRequestConfig {
|
|
223
|
+
errorConfig?: {
|
|
224
|
+
errorHandler?: IErrorHandler;
|
|
225
|
+
customErrorShow?: IErrorHandler;
|
|
226
|
+
};
|
|
227
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
228
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 判断如果是 Promise, 则从 Promise 中取出并执行剩余操作, 此时返回 Promise
|
|
233
|
+
* 如果不是 Promise, 则直接执行剩余操作, 此时直接返回结果
|
|
234
|
+
*/
|
|
235
|
+
const getPromiseValue = <T>(value: T | Promise<T>, callback: (val: T) => any): T | Promise<T> => {
|
|
236
|
+
if (value instanceof Promise) {
|
|
237
|
+
return value.then(callback);
|
|
238
|
+
}
|
|
239
|
+
return callback(value);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let requestInstance: AxiosInstance;
|
|
243
|
+
let config: RequestConfig;
|
|
244
|
+
const getConfig = (): RequestConfig => {
|
|
245
|
+
if (config) return config;
|
|
246
|
+
config = getPluginManager().applyPlugins({
|
|
247
|
+
key: 'request',
|
|
248
|
+
type: ApplyPluginsType.modify,
|
|
249
|
+
initialValue: {},
|
|
250
|
+
});
|
|
251
|
+
return config;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const dealRequestData = (response: AxiosResponse<any, any>, opts: IRequestOptions) => {
|
|
255
|
+
const { data, status, message } = response;
|
|
256
|
+
const regex = /^0+$/;
|
|
257
|
+
let httpMessage = null;
|
|
258
|
+
if (Number(status) === 200) {
|
|
259
|
+
if (data?.code && !regex.test(data?.code)) {
|
|
260
|
+
if (!opts?.closeMessage) {
|
|
261
|
+
config?.errorConfig?.customErrorShow ?
|
|
262
|
+
config?.errorConfig?.customErrorShow(response, opts) :
|
|
263
|
+
notification.error({
|
|
264
|
+
message: data?.code,
|
|
265
|
+
description: opts.setCustomMessage ? opts.setCustomMessage(response, opts): data?.msg
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
if (config?.errorConfig?.errorHandler) {
|
|
269
|
+
config?.errorConfig?.errorHandler(response, opts)
|
|
270
|
+
}
|
|
271
|
+
return Promise.reject(response);
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
switch (status) {
|
|
275
|
+
case HttpStatusEnum.BAD_GATEWAY:
|
|
276
|
+
httpMessage = HttpMessageEnum.BAD_GATEWAY;
|
|
277
|
+
break;
|
|
278
|
+
case HttpStatusEnum.SERVICE_UNAVAILABLE:
|
|
279
|
+
httpMessage = HttpMessageEnum.SERVICE_UNAVAILABLE;
|
|
280
|
+
break;
|
|
281
|
+
case HttpStatusEnum.GATEWAY_TIMEOUT:
|
|
282
|
+
httpMessage = HttpMessageEnum.GATEWAY_TIMEOUT;
|
|
283
|
+
break;
|
|
284
|
+
case HttpStatusEnum.NOT_FOUND:
|
|
285
|
+
httpMessage = HttpMessageEnum.NOT_FOUND;
|
|
286
|
+
break;
|
|
287
|
+
case HttpStatusEnum.UNAUTHORIZED:
|
|
288
|
+
httpMessage = HttpMessageEnum.UNAUTHORIZED;
|
|
289
|
+
break;
|
|
290
|
+
case HttpStatusEnum.FORBIDDEN:
|
|
291
|
+
httpMessage = HttpMessageEnum.FORBIDDEN;
|
|
292
|
+
break;
|
|
293
|
+
case HttpStatusEnum.BAD_REQUEST:
|
|
294
|
+
httpMessage = HttpMessageEnum.BAD_REQUEST;
|
|
295
|
+
break;
|
|
296
|
+
default:
|
|
297
|
+
httpMessage = HttpMessageEnum.ERROR_HTTP;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
if (!opts?.closeMessage && status !== HttpStatusEnum.UNAUTHORIZED) {
|
|
301
|
+
config?.errorConfig?.customErrorShow ?
|
|
302
|
+
config?.errorConfig?.customErrorShow(response, opts) :
|
|
303
|
+
notification.error({
|
|
304
|
+
message: status ? `${status}(${httpMessage})`: httpMessage,
|
|
305
|
+
description: opts.setCustomMessage ? opts.setCustomMessage(response, opts) : (message || "服务器错误")
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
if (config?.errorConfig?.errorHandler) {
|
|
309
|
+
config?.errorConfig?.errorHandler(response, opts)
|
|
310
|
+
}
|
|
311
|
+
return Promise.reject(response);
|
|
312
|
+
}
|
|
313
|
+
return response;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const getRequestInstance = (opts): AxiosInstance => {
|
|
317
|
+
if (requestInstance) return requestInstance;
|
|
318
|
+
const config = getConfig();
|
|
319
|
+
requestInstance = axios.create({
|
|
320
|
+
...config,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const configCache = opts.cache;
|
|
324
|
+
const finalCache = {
|
|
325
|
+
enabledByDefault: configCache === true || !!configCache?.maxAge || !!configCache?.maxCount,
|
|
326
|
+
maxCount: configCache?.maxCount,
|
|
327
|
+
maxAge: configCache?.maxAge
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
const cache = cacheAdapterEnhancer(finalCache);
|
|
332
|
+
const _request = requestInstance.request;
|
|
333
|
+
requestInstance.request = (config) => cache(_request, config);
|
|
334
|
+
|
|
335
|
+
config?.requestInterceptors?.forEach((interceptor) => {
|
|
336
|
+
if(interceptor instanceof Array){
|
|
337
|
+
requestInstance.interceptors.request.use((config) => {
|
|
338
|
+
const { url } = config;
|
|
339
|
+
if(interceptor[0].length === 2){
|
|
340
|
+
const res = interceptor[0](url, config);
|
|
341
|
+
return getPromiseValue(res, (val) => {
|
|
342
|
+
const { url: newUrl, options = {} } = val
|
|
343
|
+
return { ...options, url: newUrl }
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return interceptor[0](config);
|
|
347
|
+
}, interceptor[1]);
|
|
348
|
+
} else {
|
|
349
|
+
requestInstance.interceptors.request.use((config) => {
|
|
350
|
+
const { url } = config;
|
|
351
|
+
if(interceptor.length === 2){
|
|
352
|
+
const res = interceptor(url, config);
|
|
353
|
+
return getPromiseValue(res, (val) => {
|
|
354
|
+
const { url: newUrl, options = {} } = val
|
|
355
|
+
return { ...options, url: newUrl }
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return interceptor(config);
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
config?.responseInterceptors?.forEach((interceptor) => {
|
|
364
|
+
interceptor instanceof Array ?
|
|
365
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
366
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
requestInstance.interceptors.response.use((response) => {
|
|
370
|
+
return dealRequestData(response, opts);
|
|
371
|
+
})
|
|
372
|
+
return requestInstance;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const changeRequestData = (opts: IRequestOptions) => {
|
|
376
|
+
const optsData = opts?.data || {};
|
|
377
|
+
const optsParams = opts?.params || {};
|
|
378
|
+
const contentType = opts?.headers? opts?.headers['Content-Type']: null;
|
|
379
|
+
let finalOpts = {...opts};
|
|
380
|
+
if (optsData && optsData?.current) {
|
|
381
|
+
optsData.currentPage = optsData.current;
|
|
382
|
+
delete optsData.current;
|
|
383
|
+
finalOpts.data = {...optsData};
|
|
384
|
+
}
|
|
385
|
+
if(optsParams && optsParams?.current){
|
|
386
|
+
optsParams.currentPage = optsParams.current;
|
|
387
|
+
delete optsParams.current;
|
|
388
|
+
finalOpts.params = {...optsParams};
|
|
389
|
+
}
|
|
390
|
+
if( contentType && contentType.includes('multipart/form-data') ){
|
|
391
|
+
let formData = new FormData();
|
|
392
|
+
let userData = opts?.params || opts.data || {};
|
|
393
|
+
Object.keys(userData).forEach(key => {
|
|
394
|
+
formData.append(key, userData[key]);
|
|
395
|
+
})
|
|
396
|
+
finalOpts.data = formData;
|
|
397
|
+
if(finalOpts.params){
|
|
398
|
+
delete finalOpts.params;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
finalOpts.headers = {
|
|
402
|
+
...finalOpts.headers,
|
|
403
|
+
"Accept-Language": CURRENT_LANGUAGE_KEY
|
|
404
|
+
}
|
|
405
|
+
return finalOpts;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
const changeResponseData = (response: AxiosResponse<any, any>, getResponse: boolean) => {
|
|
409
|
+
const currentResponseData = response.data;
|
|
410
|
+
let finalResponse = currentResponseData;
|
|
411
|
+
if (currentResponseData?.code) {
|
|
412
|
+
const { code, result } = currentResponseData;
|
|
413
|
+
const regex = /^0+$/;
|
|
414
|
+
if (result) {
|
|
415
|
+
finalResponse = {
|
|
416
|
+
...currentResponseData,
|
|
417
|
+
success: regex.test(code),
|
|
418
|
+
data: Array.isArray(result) ? result : result?.records,
|
|
419
|
+
total: Array.isArray(result) ? result.length : result?.totalRows
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
response.data = { ...finalResponse };
|
|
423
|
+
}
|
|
424
|
+
return getResponse ? response : finalResponse;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const request: IRequest = (url: string, opts: IRequestOptions = { method: 'GET' }) => {
|
|
428
|
+
const requestInstance = getRequestInstance(opts);
|
|
429
|
+
const config = getConfig();
|
|
430
|
+
const finalOpts = changeRequestData(opts);
|
|
431
|
+
const { getResponse = false, requestInterceptors, responseInterceptors } = finalOpts;
|
|
432
|
+
const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
|
|
433
|
+
if(interceptor instanceof Array){
|
|
434
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
435
|
+
const { url } = config;
|
|
436
|
+
if(interceptor[0].length === 2){
|
|
437
|
+
const res = interceptor[0](url, config);
|
|
438
|
+
return getPromiseValue(res, (val) => {
|
|
439
|
+
const { url: newUrl, options = {} } = val
|
|
440
|
+
return { ...options, url: newUrl }
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
return interceptor[0](config);
|
|
444
|
+
}, interceptor[1]);
|
|
445
|
+
} else {
|
|
446
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
447
|
+
const { url } = config;
|
|
448
|
+
if(interceptor.length === 2){
|
|
449
|
+
const res = interceptor(url, config);
|
|
450
|
+
return getPromiseValue(res, (val) => {
|
|
451
|
+
const { url: newUrl, options = {} } = val
|
|
452
|
+
return { ...options, url: newUrl }
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return interceptor(config);
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
const responseInterceptorsToEject = responseInterceptors?.map((interceptor) => {
|
|
460
|
+
return interceptor instanceof Array ?
|
|
461
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
462
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
463
|
+
});
|
|
464
|
+
return new Promise((resolve, reject)=>{
|
|
465
|
+
requestInstance
|
|
466
|
+
.request({...finalOpts, url})
|
|
467
|
+
.then((res)=>{
|
|
468
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
469
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
470
|
+
});
|
|
471
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
472
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
473
|
+
});
|
|
474
|
+
resolve(changeResponseData(res,getResponse ))
|
|
475
|
+
})
|
|
476
|
+
.catch((error)=>{
|
|
477
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
478
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
479
|
+
});
|
|
480
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
481
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
482
|
+
});
|
|
483
|
+
const {response, message} = error;
|
|
484
|
+
if(response){
|
|
485
|
+
dealRequestData({
|
|
486
|
+
...response,
|
|
487
|
+
message
|
|
488
|
+
}, opts);
|
|
489
|
+
}
|
|
490
|
+
reject(error);
|
|
491
|
+
})
|
|
492
|
+
})
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// interface PageParams {
|
|
496
|
+
// pageSize?: number;
|
|
497
|
+
// current?: number;
|
|
498
|
+
// keyword?: string;
|
|
499
|
+
// }
|
|
500
|
+
|
|
501
|
+
// type WidthPageParams<T> = T & PageParams;
|
|
502
|
+
|
|
503
|
+
// type TableRequest<T, U> = ProTableProps<T, U>['request']
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
type ApiSort = {
|
|
507
|
+
field?: string;
|
|
508
|
+
direction?: string;
|
|
509
|
+
asc?: boolean;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
type SortOrder = Record<string, "descend" | "ascend" | null>
|
|
513
|
+
|
|
514
|
+
function sortFilterProTable<P, D>(
|
|
515
|
+
fn: (
|
|
516
|
+
params: P & {
|
|
517
|
+
sort?: ApiSort[];
|
|
518
|
+
}
|
|
519
|
+
) => Promise<ApiResponse<D>>
|
|
520
|
+
) {
|
|
521
|
+
return (
|
|
522
|
+
params: P & {
|
|
523
|
+
pageSize?: number;
|
|
524
|
+
current?: number;
|
|
525
|
+
keyword?: string;
|
|
526
|
+
},
|
|
527
|
+
sort: SortOrder,
|
|
528
|
+
filter: Record<string, (string | number)[] | null>
|
|
529
|
+
) => {
|
|
530
|
+
let finalSort: ApiSort = {};
|
|
531
|
+
let finalParams: P & {
|
|
532
|
+
pageSize?: number;
|
|
533
|
+
current?: number;
|
|
534
|
+
keyword?: string;
|
|
535
|
+
sort?: ApiSort[]
|
|
536
|
+
} = {...params};
|
|
537
|
+
|
|
538
|
+
if(sort){
|
|
539
|
+
Object.keys(sort).forEach((key) => {
|
|
540
|
+
if (sort[key]) {
|
|
541
|
+
finalSort.field = key;
|
|
542
|
+
finalSort.asc = sort[key] === "ascend" ? true : false;
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
if(filter){
|
|
548
|
+
Object.keys(filter).forEach((key) => {
|
|
549
|
+
if (filter[key]) {
|
|
550
|
+
finalParams[key] = filter[key];
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if(finalSort.field){
|
|
556
|
+
finalParams.sort = [finalSort];
|
|
557
|
+
}
|
|
558
|
+
return fn(finalParams);
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export {
|
|
563
|
+
useRequest,
|
|
564
|
+
request,
|
|
565
|
+
getRequestInstance,
|
|
566
|
+
sortFilterProTable,
|
|
567
|
+
HttpStatusEnum,
|
|
568
|
+
HttpMessageEnum
|
|
569
|
+
};
|
|
570
|
+
export type {
|
|
571
|
+
AxiosInstance,
|
|
572
|
+
AxiosRequestConfig,
|
|
573
|
+
AxiosResponse,
|
|
574
|
+
AxiosError,
|
|
575
|
+
RequestError,
|
|
576
|
+
IResponseInterceptor as ResponseInterceptor,
|
|
577
|
+
IRequestOptions as RequestOptions,
|
|
578
|
+
IRequest as Request,
|
|
579
|
+
};
|
|
580
|
+
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/utils/file.ts
|
|
30
|
+
var file_exports = {};
|
|
31
|
+
__export(file_exports, {
|
|
32
|
+
checkFileExists: () => checkFileExists,
|
|
33
|
+
createDirectory: () => createDirectory,
|
|
34
|
+
createFile: () => createFile
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(file_exports);
|
|
37
|
+
var import_plugin_utils = require("@umijs/max/plugin-utils");
|
|
38
|
+
var import_fs = __toESM(require("fs"));
|
|
39
|
+
var import_path = __toESM(require("path"));
|
|
40
|
+
function checkFileExists(fileName) {
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
import_fs.default.access(fileName, import_fs.default.constants.F_OK, (err) => {
|
|
43
|
+
if (err) {
|
|
44
|
+
resolve(false);
|
|
45
|
+
} else {
|
|
46
|
+
resolve(true);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function createDirectory(path2) {
|
|
52
|
+
if (!import_fs.default.existsSync(path2)) {
|
|
53
|
+
import_fs.default.mkdirSync(path2, { recursive: true });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function createFile(filePath, content = "") {
|
|
57
|
+
const finalFilePath = (0, import_plugin_utils.winPath)(filePath);
|
|
58
|
+
const absPath = import_path.default.resolve(finalFilePath);
|
|
59
|
+
if (import_fs.default.existsSync(absPath)) {
|
|
60
|
+
return;
|
|
61
|
+
} else {
|
|
62
|
+
const dir = import_path.default.dirname(absPath);
|
|
63
|
+
if (!import_fs.default.existsSync(dir)) {
|
|
64
|
+
import_fs.default.mkdirSync(dir, { recursive: true });
|
|
65
|
+
}
|
|
66
|
+
import_fs.default.writeFileSync(absPath, content);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
70
|
+
0 && (module.exports = {
|
|
71
|
+
checkFileExists,
|
|
72
|
+
createDirectory,
|
|
73
|
+
createFile
|
|
74
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/utils/index.ts
|
|
17
|
+
var utils_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(utils_exports);
|
|
19
|
+
__reExport(utils_exports, require("./file"), module.exports);
|
|
20
|
+
__reExport(utils_exports, require("./spawn"), module.exports);
|
|
21
|
+
__reExport(utils_exports, require("./withTmpPath"), module.exports);
|
|
22
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
23
|
+
0 && (module.exports = {
|
|
24
|
+
...require("./file"),
|
|
25
|
+
...require("./spawn"),
|
|
26
|
+
...require("./withTmpPath")
|
|
27
|
+
});
|