http.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import axios from "axios";
  2. import type {
  3. CreateAxiosDefaults,
  4. AxiosInstance,
  5. AxiosInterceptorOptions,
  6. AxiosRequestConfig,
  7. InternalAxiosRequestConfig,
  8. AxiosResponse,
  9. } from "axios";
  10. class WrappedAxios {
  11. private instance?: AxiosInstance;
  12. constructor(options: CreateAxiosDefaults = {}) {
  13. this.instance = axios.create({
  14. timeout: 10000,
  15. ...options,
  16. });
  17. }
  18. beforeRequest<V = any>(
  19. onFulfilled?:
  20. | ((
  21. value: InternalAxiosRequestConfig<V>
  22. ) =>
  23. | InternalAxiosRequestConfig<V>
  24. | Promise<InternalAxiosRequestConfig<V>>)
  25. | null,
  26. onRejected?: ((error: any) => any) | null,
  27. options?: AxiosInterceptorOptions
  28. ) {
  29. this.instance.interceptors.request.use(onFulfilled, onRejected, options);
  30. return this;
  31. }
  32. afterResponse<V = any, D = any>(
  33. onFulfilled?:
  34. | ((
  35. value: AxiosResponse<V, D>
  36. ) => AxiosResponse<V, D> | Promise<AxiosResponse<V, D>>)
  37. | null,
  38. onRejected?: ((error: any) => any) | null,
  39. options?: AxiosInterceptorOptions
  40. ) {
  41. this.instance.interceptors.response.use(onFulfilled, onRejected, options);
  42. return this;
  43. }
  44. get axiosInstance() {
  45. return this.instance;
  46. }
  47. get<R = any>(
  48. url: string,
  49. data: Record<string, any>,
  50. config: AxiosRequestConfig = {}
  51. ) {
  52. return this.instance.get(url, {
  53. params: data,
  54. ...config,
  55. }) as Promise<R>;
  56. }
  57. post<R = any>(
  58. url: string,
  59. data: Record<string, any>,
  60. config: AxiosRequestConfig = {}
  61. ) {
  62. return this.instance.post(url, data, config) as Promise<R>;
  63. }
  64. put<R = any>(
  65. url: string,
  66. data: Record<string, any>,
  67. config: AxiosRequestConfig = {}
  68. ) {
  69. return this.instance.put(url, data, config) as Promise<R>;
  70. }
  71. delete<R = any>(
  72. url: string,
  73. data: Record<string, any>,
  74. config: AxiosRequestConfig = {}
  75. ) {
  76. return this.instance.delete(url, {
  77. params: data,
  78. ...config,
  79. }) as Promise<R>;
  80. }
  81. private createCancelObj<R>(
  82. config: AxiosRequestConfig = {},
  83. send: () => Promise<R>
  84. ) {
  85. const controller = new AbortController();
  86. config.signal = controller.signal;
  87. return {
  88. abort() {
  89. controller.abort();
  90. },
  91. send,
  92. };
  93. }
  94. createGetWithCancel<R = any>(
  95. url: string,
  96. data: Record<string, any>,
  97. config: AxiosRequestConfig = {}
  98. ) {
  99. return this.createCancelObj<R>(config, () => {
  100. return this.get<R>(url, data, config);
  101. });
  102. }
  103. createPostWithCancel<R = any>(
  104. url: string,
  105. data: Record<string, any>,
  106. config: AxiosRequestConfig = {}
  107. ) {
  108. return this.createCancelObj<R>(config, () => {
  109. return this.post<R>(url, data, config);
  110. });
  111. }
  112. createPutWithCancel<R = any>(
  113. url: string,
  114. data: Record<string, any>,
  115. config: AxiosRequestConfig = {}
  116. ) {
  117. return this.createCancelObj<R>(config, () => {
  118. return this.put<R>(url, data, config);
  119. });
  120. }
  121. createDeleteWithCancel<R = any>(
  122. url: string,
  123. data: Record<string, any>,
  124. config: AxiosRequestConfig = {}
  125. ) {
  126. return this.createCancelObj<R>(config, () => {
  127. return this.delete<R>(url, data, config);
  128. });
  129. }
  130. }
  131. export { WrappedAxios };