index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { getHosts } from "@/envHosts";
  2. import {fetchUrl} from "@/config"
  3. class request {
  4. request(config: any) {
  5. return new Promise(async (resolve, reject) => {
  6. if (config.method == 'post') {
  7. config.body = JSON.stringify(config.data);
  8. }
  9. config.headers = {
  10. 'Content-Type': 'application/json',
  11. // 'Access-Token':
  12. // '22qTpDZa9xKK9B+4NW8UqCH/BCGfO8wJmNB4TDeY7AkP2WtHK7Fw3Fma82SSEs3b',
  13. ...config.headers,
  14. };
  15. try {
  16. // const { Host00 } = getHosts();
  17. const Host00 = fetchUrl
  18. const res = await fetch(`${Host00}${config.url}`, {
  19. ...config
  20. // cache: 'no-store'
  21. });
  22. let response
  23. if(config.responseType != 'blob'){
  24. response = await res.json();
  25. }else {
  26. response = await res.blob();
  27. }
  28. resolve(response);
  29. } catch (error) {
  30. reject(error);
  31. }
  32. });
  33. }
  34. get(config: any) {
  35. return this.request({
  36. ...config,
  37. method: 'get'
  38. });
  39. }
  40. post(config: any) {
  41. return this.request({
  42. ...config,
  43. method: 'post'
  44. });
  45. }
  46. }
  47. const requestInstance = new request();
  48. export default requestInstance;