requests.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import "whatwg-fetch";
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { addToast } from "@heroui/react";
  4. const SERVER_PROTOCOL = process.env.NEXT_PUBLIC_SERVER_PROTOCOL;
  5. const SERVER_PORT = process.env.NEXT_PUBLIC_SERVER_PORT;
  6. export function getHost(): string {
  7. const SERVER_IP = process.env.NEXT_PUBLIC_SERVER_IP || globalThis.location?.hostname;
  8. let host = SERVER_PROTOCOL + "://" + SERVER_IP;
  9. // 非默认值端口显式添加
  10. if (SERVER_PORT != "80" && SERVER_PORT != "443") {
  11. host = host + ":" + SERVER_PORT;
  12. }
  13. return host;
  14. }
  15. function getUrl(path: string): string {
  16. // 如果包含http则直接返回(完整路径)
  17. if (path.includes("http")) return path;
  18. return getHost() + path;
  19. }
  20. export function getWsUrl(path: string): string {
  21. // 如果包含http则直接返回(完整路径)
  22. if (path.includes("ws")) return path;
  23. return getHost().replace("https", "wss").replace("http", "ws") + path;
  24. }
  25. export function errorHandler(error: Error, signal: AbortSignal | null = null ) {
  26. // 主动取消请求
  27. if (signal && signal.aborted ) {
  28. return;
  29. }
  30. // 错误提示
  31. addToast({
  32. title: error.message,
  33. variant: "flat",
  34. color: "danger",
  35. });
  36. }
  37. export async function responseParse(response: Response): Promise<any> {
  38. // if (response.status != 200) {
  39. // response.text().then((text) => {
  40. // errorHandler(new Error(text));
  41. // });
  42. // throw new Error("Internal Server Error");
  43. // }
  44. return response.json().then((data) => {
  45. if (data.code && data.code != 0) {
  46. throw new Error(data.message);
  47. } else {
  48. return data;
  49. }
  50. });
  51. }
  52. export async function get(
  53. path: string,
  54. signal?: AbortSignal,
  55. headers: { [key: string]: string } = {"Content-Type": "application/json"}
  56. ): Promise<any> {
  57. const url = getUrl(path);
  58. headers["Request-Id"] = uuidv4();
  59. headers["User-Id"] = "";
  60. return fetch(url, {
  61. method: "GET",
  62. headers: headers,
  63. signal: signal,
  64. })
  65. .then((response) => {
  66. return responseParse(response);
  67. })
  68. .catch((error) => {
  69. errorHandler(error, signal);
  70. return Promise.reject(error.message);
  71. });
  72. }
  73. export async function post(
  74. path: string,
  75. data?: string | Record<string, any>,
  76. signal?: AbortSignal,
  77. headers: { [key: string]: string } = {"Content-Type": "application/json"}
  78. ): Promise<any> {
  79. const body = typeof data === "string" ? data : JSON.stringify(data);
  80. const url = getUrl(path);
  81. headers["Request-Id"] = uuidv4();
  82. headers["User-Id"] = "";
  83. return fetch(url, {
  84. method: "POST",
  85. body,
  86. headers: headers,
  87. signal: signal,
  88. })
  89. .then((response) => {
  90. return responseParse(response);
  91. })
  92. .catch((error) => {
  93. errorHandler(error, signal);
  94. return Promise.reject(error.message);
  95. });
  96. }
  97. export async function filePost(
  98. path: string,
  99. body: FormData,
  100. signal: AbortSignal,
  101. headers: { [key: string]: string } = {}
  102. ): Promise<any> {
  103. const url = getUrl(path);
  104. headers["Request-Id"] = uuidv4();
  105. headers["User-Id"] = "";
  106. return fetch(url, {
  107. method: "POST",
  108. body: body,
  109. headers: headers,
  110. signal: signal,
  111. })
  112. .then((response) => {
  113. return responseParse(response);
  114. })
  115. .catch((error) => {
  116. errorHandler(error, signal);
  117. return Promise.reject(error.message);
  118. });
  119. }
  120. export async function put(
  121. path: string,
  122. body: string | null,
  123. signal: AbortSignal,
  124. headers: { [key: string]: string } = {"Content-Type": "application/json"}
  125. ): Promise<any> {
  126. const url = getUrl(path);
  127. headers["Request-Id"] = uuidv4();
  128. headers["User-Id"] = "";
  129. return fetch(url, {
  130. method: "PUT",
  131. body: body,
  132. headers: headers,
  133. signal: signal,
  134. })
  135. .then((response) => {
  136. return responseParse(response);
  137. })
  138. .catch((error) => {
  139. errorHandler(error, signal);
  140. return Promise.reject(error.message);
  141. });
  142. }
  143. export async function del(
  144. path: string,
  145. signal: AbortSignal,
  146. headers: { [key: string]: string } = {"Content-Type": "application/json"}
  147. ): Promise<any> {
  148. const url = getUrl(path);
  149. headers["Request-Id"] = uuidv4();
  150. headers["User-Id"] = "";
  151. return fetch(url, {
  152. method: "DELETE",
  153. headers: headers,
  154. signal: signal,
  155. })
  156. .then((response) => {
  157. return responseParse(response);
  158. })
  159. .catch((error) => {
  160. errorHandler(error, signal);
  161. return Promise.reject(error.message);
  162. });
  163. }