You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.6 KiB

11 months ago
  1. import { API_BASE } from "@/utils/constants";
  2. import { baseHeaders } from "@/utils/request";
  3. import showToast from "@/utils/toast";
  4. const DataConnector = {
  5. github: {
  6. branches: async ({ repo, accessToken }) => {
  7. return await fetch(`${API_BASE}/ext/github/branches`, {
  8. method: "POST",
  9. headers: baseHeaders(),
  10. cache: "force-cache",
  11. body: JSON.stringify({ repo, accessToken }),
  12. })
  13. .then((res) => res.json())
  14. .then((res) => {
  15. if (!res.success) throw new Error(res.reason);
  16. return res.data;
  17. })
  18. .then((data) => {
  19. return { branches: data?.branches || [], error: null };
  20. })
  21. .catch((e) => {
  22. console.error(e);
  23. showToast(e.message, "error");
  24. return { branches: [], error: e.message };
  25. });
  26. },
  27. collect: async function ({ repo, accessToken, branch, ignorePaths = [] }) {
  28. return await fetch(`${API_BASE}/ext/github/repo`, {
  29. method: "POST",
  30. headers: baseHeaders(),
  31. body: JSON.stringify({ repo, accessToken, branch, ignorePaths }),
  32. })
  33. .then((res) => res.json())
  34. .then((res) => {
  35. if (!res.success) throw new Error(res.reason);
  36. return { data: res.data, error: null };
  37. })
  38. .catch((e) => {
  39. console.error(e);
  40. return { data: null, error: e.message };
  41. });
  42. },
  43. },
  44. gitlab: {
  45. branches: async ({ repo, accessToken }) => {
  46. return await fetch(`${API_BASE}/ext/gitlab/branches`, {
  47. method: "POST",
  48. headers: baseHeaders(),
  49. cache: "force-cache",
  50. body: JSON.stringify({ repo, accessToken }),
  51. })
  52. .then((res) => res.json())
  53. .then((res) => {
  54. if (!res.success) throw new Error(res.reason);
  55. return res.data;
  56. })
  57. .then((data) => {
  58. return { branches: data?.branches || [], error: null };
  59. })
  60. .catch((e) => {
  61. console.error(e);
  62. showToast(e.message, "error");
  63. return { branches: [], error: e.message };
  64. });
  65. },
  66. collect: async function ({
  67. repo,
  68. accessToken,
  69. branch,
  70. ignorePaths = [],
  71. fetchIssues = false,
  72. }) {
  73. return await fetch(`${API_BASE}/ext/gitlab/repo`, {
  74. method: "POST",
  75. headers: baseHeaders(),
  76. body: JSON.stringify({
  77. repo,
  78. accessToken,
  79. branch,
  80. ignorePaths,
  81. fetchIssues,
  82. }),
  83. })
  84. .then((res) => res.json())
  85. .then((res) => {
  86. if (!res.success) throw new Error(res.reason);
  87. return { data: res.data, error: null };
  88. })
  89. .catch((e) => {
  90. console.error(e);
  91. return { data: null, error: e.message };
  92. });
  93. },
  94. },
  95. youtube: {
  96. transcribe: async ({ url }) => {
  97. return await fetch(`${API_BASE}/ext/youtube/transcript`, {
  98. method: "POST",
  99. headers: baseHeaders(),
  100. body: JSON.stringify({ url }),
  101. })
  102. .then((res) => res.json())
  103. .then((res) => {
  104. if (!res.success) throw new Error(res.reason);
  105. return { data: res.data, error: null };
  106. })
  107. .catch((e) => {
  108. console.error(e);
  109. return { data: null, error: e.message };
  110. });
  111. },
  112. },
  113. websiteDepth: {
  114. scrape: async ({ url, depth, maxLinks }) => {
  115. return await fetch(`${API_BASE}/ext/website-depth`, {
  116. method: "POST",
  117. headers: baseHeaders(),
  118. body: JSON.stringify({ url, depth, maxLinks }),
  119. })
  120. .then((res) => res.json())
  121. .then((res) => {
  122. if (!res.success) throw new Error(res.reason);
  123. return { data: res.data, error: null };
  124. })
  125. .catch((e) => {
  126. console.error(e);
  127. return { data: null, error: e.message };
  128. });
  129. },
  130. },
  131. confluence: {
  132. collect: async function ({
  133. baseUrl,
  134. spaceKey,
  135. username,
  136. accessToken,
  137. cloud,
  138. personalAccessToken,
  139. }) {
  140. return await fetch(`${API_BASE}/ext/confluence`, {
  141. method: "POST",
  142. headers: baseHeaders(),
  143. body: JSON.stringify({
  144. baseUrl,
  145. spaceKey,
  146. username,
  147. accessToken,
  148. cloud,
  149. personalAccessToken,
  150. }),
  151. })
  152. .then((res) => res.json())
  153. .then((res) => {
  154. if (!res.success) throw new Error(res.reason);
  155. return { data: res.data, error: null };
  156. })
  157. .catch((e) => {
  158. console.error(e);
  159. return { data: null, error: e.message };
  160. });
  161. },
  162. },
  163. };
  164. export default DataConnector;