request.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 as unknown as T
  21. },
  22. post: async <T = any>(option: any) => {
  23. const res = await request({ method: 'POST', ...option })
  24. return res as unknown as T
  25. },
  26. delete: async <T = any>(option: any) => {
  27. const res = await request({ method: 'DELETE', ...option })
  28. return res as unknown as T
  29. },
  30. put: async <T = any>(option: any) => {
  31. const res = await request({ method: 'PUT', ...option })
  32. return res as unknown as T
  33. },
  34. patch: async <T = any>(option: any) => {
  35. const res = await request({ method: 'PATCH', ...option })
  36. return res as unknown as T
  37. },
  38. download: async <T = any>(option: any) => {
  39. const res = await request({ method: 'GET', responseType: 'blob', ...option })
  40. return res as unknown as Promise<T>
  41. },
  42. upload: async <T = any>(option: any) => {
  43. option.headersType = 'multipart/form-data'
  44. const res = await request({ method: 'POST', ...option })
  45. return res as unknown as Promise<T>
  46. }
  47. }