当前位置:   article > 正文

用TS封装了一个axios,支持全局拦截、实例拦截、单个请求拦截、loading等待功能_interceptors.response.use ts

interceptors.response.use ts

先给拍一张结构图

 最外层 index.ts统一出口

request文件夹中的 config.ts进行了baseurl和响应时间等基础配置

request文件夹中的index.ts就是我们主要的axios封装问加啦

 最外层 index.ts

  1. // service统一出口
  2. import HYRequest from './request'
  3. import {BASE_URL,TIME_OUT} from './request/config'
  4. const hyRequest = new HYRequest({
  5. baseURL:BASE_URL,
  6. timeout: TIME_OUT
  7. })
  8. export default hyRequest

request文件夹中的 config.ts

  1. // 环境
  2. let BASE_URL = ''
  3. let TIME_OUT = 10000
  4. if (process.env.NODE_ENV === 'development') {
  5. BASE_URL = 'http://123.207.32.32:8000/'
  6. }else if (process.env.NODE_ENV === 'production'){
  7. BASE_URL = ''
  8. } else {
  9. BASE_URL = ''
  10. }
  11. export {BASE_URL, TIME_OUT}

request文件夹中的index.ts

  1. import axios from 'axios'
  2. import type {AxiosInstance,AxiosRequestConfig,AxiosResponse} from 'axios'
  3. import { ElLoading } from 'element-plus';
  4. import { ILoadingInstance } from 'element-plus/lib/el-loading/src/loading.type'
  5. const DEAFULT_LOADING = true
  6. // 定义接口,四个函数类型
  7. interface HYRequestInterceptors<T = AxiosResponse> {
  8. requestInterceptor?:(config:AxiosRequestConfig) => AxiosRequestConfig
  9. requestInterceptorCatcher?:(error:any) => any
  10. responseInterceptor?:(res:T) => T
  11. responseInterceptorCatcher?:(error:any) => any
  12. }
  13. // 继承AxiosRequestConfig接口,增加HYRequestInterceptors接口类型
  14. interface HYRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
  15. interceptors?:HYRequestInterceptors<T>
  16. showLoading?: boolean
  17. }
  18. // 使用class进行封装
  19. class HYRequest{
  20. instance:AxiosInstance
  21. interceptors?:HYRequestInterceptors
  22. showLoading: boolean
  23. loading?:ILoadingInstance
  24. // 构造器
  25. constructor(config:HYRequestConfig){
  26. // 创建axios实例
  27. this.instance = axios.create(config)
  28. // 保存基本信息
  29. this.interceptors = config.interceptors
  30. this.showLoading = config.showLoading ?? DEAFULT_LOADING
  31. /**1.从config中取出的拦截器是对应实例的拦截器 */
  32. // 使用请求拦截器
  33. this.instance.interceptors.request.use(
  34. this.interceptors?.requestInterceptor,
  35. this.interceptors?.requestInterceptorCatcher
  36. )
  37. // 使用响应拦截器
  38. this.instance.interceptors.response.use(
  39. this.interceptors?.responseInterceptor,
  40. this.interceptors?.responseInterceptorCatcher
  41. )
  42. /**2.添加所有实例都有的拦截器 */
  43. this.instance.interceptors.request.use(
  44. (config)=>{
  45. if (this.showLoading) {
  46. this.loading = ElLoading.service({
  47. lock:true,
  48. text:'正在加载中...',
  49. background:'rgba(0,0,0,0.5)'
  50. });
  51. }
  52. return config
  53. },
  54. (err)=>{
  55. return err
  56. }
  57. )
  58. this.instance.interceptors.response.use(
  59. (res)=>{
  60. // 讲loading移除
  61. this.loading?.close();
  62. return res.data
  63. },
  64. (err)=>{
  65. // 讲loading移除
  66. this.loading?.close();
  67. return err
  68. }
  69. )
  70. }
  71. request<T>(config:HYRequestConfig<T>):Promise<T> {
  72. return new Promise((resolve,reject) => {
  73. // 单个请求对请求config的处理
  74. if (config.interceptors?.requestInterceptor) {
  75. config = config.interceptors.requestInterceptor(config)
  76. }
  77. // 判断是否需要显示loading
  78. if (config.showLoading === false) {
  79. this.showLoading = config.showLoading
  80. }
  81. this.instance
  82. .request<any,T>(config)
  83. .then((res) => {
  84. // 1.单个请求对返回数据的处理
  85. if (config.interceptors?.responseInterceptor) {
  86. // res = config.interceptors.responseInterceptor(res)
  87. }
  88. // 2.将showLoading设置成true,这样不会影响到下一个请求
  89. this.showLoading = DEAFULT_LOADING;
  90. // 3.将结果resolve返回出去
  91. resolve(res)
  92. }).catch(err => {
  93. // 将showLoading设置成true,这样不会影响到下一个请求
  94. this.showLoading = DEAFULT_LOADING;
  95. reject(err)
  96. return err
  97. })
  98. })
  99. }
  100. post<T>(config:HYRequestConfig<T>):Promise<T> {
  101. return this.request<T>({...config, method:'post'})
  102. }
  103. delete<T>(config:HYRequestConfig<T>):Promise<T> {
  104. return this.request<T>({...config, method:'delete'})
  105. }
  106. patch<T>(config:HYRequestConfig<T>):Promise<T> {
  107. return this.request<T>({...config, method:'patch'})
  108. }
  109. get<T>(config:HYRequestConfig<T>):Promise<T> {
  110. return this.request<T>({...config, method:'get'})
  111. }
  112. }
  113. export default HYRequest;

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/57140
推荐阅读
相关标签
  

闽ICP备14008679号