anything-llm/frontend/src/models/dataConnector.js
Sean Hatfield 612a7e1662
[FEAT] Website depth scraping data connector (#1191)
* WIP website depth scraping, (sort of works)

* website depth data connector stable + add maxLinks option

* linting + loading small ui tweak

* refactor website depth data connector for stability, speed, & readability

* patch: remove console log
Guard clause on URL validitiy check
reasonable overrides

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2024-05-14 12:49:14 -07:00

107 lines
3.1 KiB
JavaScript

import { API_BASE } from "@/utils/constants";
import { baseHeaders } from "@/utils/request";
import showToast from "@/utils/toast";
const DataConnector = {
github: {
branches: async ({ repo, accessToken }) => {
return await fetch(`${API_BASE}/ext/github/branches`, {
method: "POST",
headers: baseHeaders(),
cache: "force-cache",
body: JSON.stringify({ repo, accessToken }),
})
.then((res) => res.json())
.then((res) => {
if (!res.success) throw new Error(res.reason);
return res.data;
})
.then((data) => {
return { branches: data?.branches || [], error: null };
})
.catch((e) => {
console.error(e);
showToast(e.message, "error");
return { branches: [], error: e.message };
});
},
collect: async function ({ repo, accessToken, branch, ignorePaths = [] }) {
return await fetch(`${API_BASE}/ext/github/repo`, {
method: "POST",
headers: baseHeaders(),
body: JSON.stringify({ repo, accessToken, branch, ignorePaths }),
})
.then((res) => res.json())
.then((res) => {
if (!res.success) throw new Error(res.reason);
return { data: res.data, error: null };
})
.catch((e) => {
console.error(e);
return { data: null, error: e.message };
});
},
},
youtube: {
transcribe: async ({ url }) => {
return await fetch(`${API_BASE}/ext/youtube/transcript`, {
method: "POST",
headers: baseHeaders(),
body: JSON.stringify({ url }),
})
.then((res) => res.json())
.then((res) => {
if (!res.success) throw new Error(res.reason);
return { data: res.data, error: null };
})
.catch((e) => {
console.error(e);
return { data: null, error: e.message };
});
},
},
websiteDepth: {
scrape: async ({ url, depth, maxLinks }) => {
return await fetch(`${API_BASE}/ext/website-depth`, {
method: "POST",
headers: baseHeaders(),
body: JSON.stringify({ url, depth, maxLinks }),
})
.then((res) => res.json())
.then((res) => {
if (!res.success) throw new Error(res.reason);
return { data: res.data, error: null };
})
.catch((e) => {
console.error(e);
return { data: null, error: e.message };
});
},
},
confluence: {
collect: async function ({ pageUrl, username, accessToken }) {
return await fetch(`${API_BASE}/ext/confluence`, {
method: "POST",
headers: baseHeaders(),
body: JSON.stringify({
pageUrl,
username,
accessToken,
}),
})
.then((res) => res.json())
.then((res) => {
if (!res.success) throw new Error(res.reason);
return { data: res.data, error: null };
})
.catch((e) => {
console.error(e);
return { data: null, error: e.message };
});
},
},
};
export default DataConnector;