useAxios.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { service } from '@/config/axios'
  2. import { AxiosPromise } from 'axios'
  3. import { config } from '@/config/axios/config'
  4. const { default_headers } = config
  5. const request = (option: AxiosConfig) => {
  6. const { url, method, params, data, headersType, responseType } = option
  7. return service({
  8. url: url,
  9. method,
  10. params,
  11. data,
  12. responseType: responseType,
  13. headers: {
  14. 'Content-Type': headersType || default_headers
  15. }
  16. })
  17. }
  18. function getFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
  19. return request({ method: 'get', ...option })
  20. }
  21. function postFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
  22. return request({ method: 'post', ...option })
  23. }
  24. function deleteFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
  25. return request({ method: 'delete', ...option })
  26. }
  27. function putFn<T = any>(option: AxiosConfig): AxiosPromise<T> {
  28. return request({ method: 'put', ...option })
  29. }
  30. export const useAxios = () => {
  31. return {
  32. get: getFn,
  33. post: postFn,
  34. delete: deleteFn,
  35. put: putFn
  36. }
  37. }