request.ts 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // @ts-nocheck
  2. // This file is generated by Umi automatically
  3. // DO NOT CHANGE IT MANUALLY!
  4. import axios, {
  5. type AxiosInstance,
  6. type AxiosRequestConfig,
  7. type AxiosResponse,
  8. type AxiosError,
  9. } from 'E:/work/jgz/admin2/node_modules/axios';
  10. import useUmiRequest, { UseRequestProvider } from 'E:/work/jgz/admin2/node_modules/@ahooksjs/use-request';
  11. import { ApplyPluginsType } from 'umi';
  12. import { getPluginManager } from '../core/plugin';
  13. import {
  14. BaseOptions,
  15. BasePaginatedOptions,
  16. BaseResult,
  17. CombineService,
  18. LoadMoreFormatReturn,
  19. LoadMoreOptions,
  20. LoadMoreOptionsWithFormat,
  21. LoadMoreParams,
  22. LoadMoreResult,
  23. OptionsWithFormat,
  24. PaginatedFormatReturn,
  25. PaginatedOptionsWithFormat,
  26. PaginatedParams,
  27. PaginatedResult,
  28. } from 'E:/work/jgz/admin2/node_modules/@ahooksjs/use-request/es/types';
  29. type ResultWithData< T = any > = { data?: T; [key: string]: any };
  30. function useRequest<
  31. R = any,
  32. P extends any[] = any,
  33. U = any,
  34. UU extends U = any,
  35. >(
  36. service: CombineService<R, P>,
  37. options: OptionsWithFormat<R, P, U, UU>,
  38. ): BaseResult<U, P>;
  39. function useRequest<R extends ResultWithData = any, P extends any[] = any>(
  40. service: CombineService<R, P>,
  41. options?: BaseOptions<R['data'], P>,
  42. ): BaseResult<R['data'], P>;
  43. function useRequest<R extends LoadMoreFormatReturn = any, RR = any>(
  44. service: CombineService<RR, LoadMoreParams<R>>,
  45. options: LoadMoreOptionsWithFormat<R, RR>,
  46. ): LoadMoreResult<R>;
  47. function useRequest<
  48. R extends ResultWithData<LoadMoreFormatReturn | any> = any,
  49. RR extends R = any,
  50. >(
  51. service: CombineService<R, LoadMoreParams<R['data']>>,
  52. options: LoadMoreOptions<RR['data']>,
  53. ): LoadMoreResult<R['data']>;
  54. function useRequest<R = any, Item = any, U extends Item = any>(
  55. service: CombineService<R, PaginatedParams>,
  56. options: PaginatedOptionsWithFormat<R, Item, U>,
  57. ): PaginatedResult<Item>;
  58. function useRequest<Item = any, U extends Item = any>(
  59. service: CombineService<
  60. ResultWithData<PaginatedFormatReturn<Item>>,
  61. PaginatedParams
  62. >,
  63. options: BasePaginatedOptions<U>,
  64. ): PaginatedResult<Item>;
  65. function useRequest(service: any, options: any = {}) {
  66. return useUmiRequest(service, {
  67. formatResult: result => result?.data,
  68. requestMethod: (requestOptions: any) => {
  69. if (typeof requestOptions === 'string') {
  70. return request(requestOptions);
  71. }
  72. if (typeof requestOptions === 'object') {
  73. const { url, ...rest } = requestOptions;
  74. return request(url, rest);
  75. }
  76. throw new Error('request options error');
  77. },
  78. ...options,
  79. });
  80. }
  81. // request 方法 opts 参数的接口
  82. interface IRequestOptions extends AxiosRequestConfig {
  83. skipErrorHandler?: boolean;
  84. requestInterceptors?: IRequestInterceptorTuple[];
  85. responseInterceptors?: IResponseInterceptorTuple[];
  86. [key: string]: any;
  87. }
  88. interface IRequestOptionsWithResponse extends IRequestOptions {
  89. getResponse: true;
  90. }
  91. interface IRequestOptionsWithoutResponse extends IRequestOptions{
  92. getResponse: false;
  93. }
  94. interface IRequest{
  95. <T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<T>>;
  96. <T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<T>;
  97. <T = any>(url: string, opts: IRequestOptions): Promise<T>; // getResponse 默认是 false, 因此不提供该参数时,只返回 data
  98. <T = any>(url: string): Promise<T>; // 不提供 opts 时,默认使用 'GET' method,并且默认返回 data
  99. }
  100. type RequestError = AxiosError | Error
  101. interface IErrorHandler {
  102. (error: RequestError, opts: IRequestOptions): void;
  103. }
  104. type IRequestInterceptorAxios = (config: RequestOptions) => RequestOptions;
  105. type IRequestInterceptorUmiRequest = (url: string, config : RequestOptions) => { url: string, options: RequestOptions };
  106. type IRequestInterceptor = IRequestInterceptorAxios | IRequestInterceptorUmiRequest;
  107. type IErrorInterceptor = (error: Error) => Promise<Error>;
  108. type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
  109. type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor
  110. type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor
  111. export interface RequestConfig<T = any> extends AxiosRequestConfig {
  112. errorConfig?: {
  113. errorHandler?: IErrorHandler;
  114. errorThrower?: ( res: T ) => void
  115. };
  116. requestInterceptors?: IRequestInterceptorTuple[];
  117. responseInterceptors?: IResponseInterceptorTuple[];
  118. }
  119. let requestInstance: AxiosInstance;
  120. let config: RequestConfig;
  121. const getConfig = (): RequestConfig => {
  122. if (config) return config;
  123. config = getPluginManager().applyPlugins({
  124. key: 'request',
  125. type: ApplyPluginsType.modify,
  126. initialValue: {},
  127. });
  128. return config;
  129. };
  130. const getRequestInstance = (): AxiosInstance => {
  131. if (requestInstance) return requestInstance;
  132. const config = getConfig();
  133. requestInstance = axios.create(config);
  134. config?.requestInterceptors?.forEach((interceptor) => {
  135. if(interceptor instanceof Array){
  136. requestInstance.interceptors.request.use((config) => {
  137. const { url } = config;
  138. if(interceptor[0].length === 2){
  139. const { url: newUrl, options } = interceptor[0](url, config);
  140. return { ...options, url: newUrl };
  141. }
  142. return interceptor[0](config);
  143. }, interceptor[1]);
  144. } else {
  145. requestInstance.interceptors.request.use((config) => {
  146. const { url } = config;
  147. if(interceptor.length === 2){
  148. const { url: newUrl, options } = interceptor(url, config);
  149. return { ...options, url: newUrl };
  150. }
  151. return interceptor(config);
  152. })
  153. }
  154. });
  155. config?.responseInterceptors?.forEach((interceptor) => {
  156. interceptor instanceof Array ?
  157. requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
  158. requestInstance.interceptors.response.use(interceptor);
  159. });
  160. // 当响应的数据 success 是 false 的时候,抛出 error 以供 errorHandler 处理。
  161. requestInstance.interceptors.response.use((response) => {
  162. const { data } = response;
  163. if(data?.success === false && config?.errorConfig?.errorThrower){
  164. config.errorConfig.errorThrower(data);
  165. }
  166. return response;
  167. })
  168. return requestInstance;
  169. };
  170. const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
  171. const requestInstance = getRequestInstance();
  172. const config = getConfig();
  173. const { getResponse = false, requestInterceptors, responseInterceptors } = opts;
  174. const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
  175. if(interceptor instanceof Array){
  176. return requestInstance.interceptors.request.use((config) => {
  177. const { url } = config;
  178. if(interceptor[0].length === 2){
  179. const { url: newUrl, options } = interceptor[0](url, config);
  180. return { ...options, url: newUrl };
  181. }
  182. return interceptor[0](config);
  183. }, interceptor[1]);
  184. } else {
  185. return requestInstance.interceptors.request.use((config) => {
  186. const { url } = config;
  187. if(interceptor.length === 2){
  188. const { url: newUrl, options } = interceptor(url, config);
  189. return { ...options, url: newUrl };
  190. }
  191. return interceptor(config);
  192. })
  193. }
  194. });
  195. const responseInterceptorsToEject = responseInterceptors?.map((interceptor) => {
  196. return interceptor instanceof Array ?
  197. requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
  198. requestInstance.interceptors.response.use(interceptor);
  199. });
  200. return new Promise((resolve, reject)=>{
  201. requestInstance
  202. .request({...opts, url})
  203. .then((res)=>{
  204. requestInterceptorsToEject?.forEach((interceptor) => {
  205. requestInstance.interceptors.request.eject(interceptor);
  206. });
  207. responseInterceptorsToEject?.forEach((interceptor) => {
  208. requestInstance.interceptors.response.eject(interceptor);
  209. });
  210. resolve(getResponse ? res : res.data);
  211. })
  212. .catch((error)=>{
  213. requestInterceptorsToEject?.forEach((interceptor) => {
  214. requestInstance.interceptors.request.eject(interceptor);
  215. });
  216. responseInterceptorsToEject?.forEach((interceptor) => {
  217. requestInstance.interceptors.response.eject(interceptor);
  218. });
  219. try {
  220. const handler =
  221. config?.errorConfig?.errorHandler;
  222. if(handler)
  223. handler(error, opts, config);
  224. } catch (e) {
  225. reject(e);
  226. }
  227. reject(error);
  228. })
  229. })
  230. }
  231. export {
  232. useRequest,
  233. UseRequestProvider,
  234. request,
  235. getRequestInstance,
  236. };
  237. export type {
  238. AxiosInstance,
  239. AxiosRequestConfig,
  240. AxiosResponse,
  241. AxiosError,
  242. RequestError,
  243. IResponseInterceptor as ResponseInterceptor,
  244. IRequestOptions as RequestOptions,
  245. IRequest as Request,
  246. };