index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { service } from './service'
  2. import { config } from './config'
  3. const { default_headers } = config
  4. const request = (option: any) => {
  5. const { url, method, params, data, headersType, responseType } = option
  6. return service({
  7. url: url,
  8. method,
  9. params,
  10. data,
  11. responseType: responseType,
  12. headers: {
  13. 'Content-Type': headersType || default_headers
  14. }
  15. })
  16. }
  17. export default {
  18. get: async <T = any>(option: any) => {
  19. const res = await request({ method: 'GET', ...option })
  20. return res.data as unknown as T
  21. },
  22. post: async <T = any>(option: any) => {
  23. const res = await request({ method: 'POST', ...option })
  24. return res.data as unknown as T
  25. },
  26. delete: async <T = any>(option: any) => {
  27. const res = await request({ method: 'DELETE', ...option })
  28. return res.data as unknown as T
  29. },
  30. put: async <T = any>(option: any) => {
  31. const res = await request({ method: 'PUT', ...option })
  32. return res.data as unknown as T
  33. },
  34. download: async <T = any>(option: any) => {
  35. const res = await request({ method: 'GET', responseType: 'blob', ...option })
  36. return res as unknown as Promise<T>
  37. },
  38. upload: async <T = any>(option: any) => {
  39. option.headersType = 'multipart/form-data'
  40. const res = await request({ method: 'PUT', ...option })
  41. return res as unknown as Promise<T>
  42. }
  43. }