index.ts 1.5 KB

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