SurfSense/surfsense_web/lib/apis/search-source-connectors.api.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-11-15 02:07:20 +02:00
import type { Connector, CreateConnectorRequest } from "@/hooks/use-connectors";
2025-11-12 18:48:50 +02:00
export const createConnector = async (
2025-11-14 00:42:19 +02:00
data: CreateConnectorRequest,
authToken: string
2025-11-12 18:48:50 +02:00
): Promise<Connector> => {
2025-11-14 00:42:19 +02:00
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(data),
}
);
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to create connector");
}
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
return response.json();
2025-11-12 18:48:50 +02:00
};
export const getConnectors = async (
2025-11-14 00:42:19 +02:00
skip = 0,
limit = 100,
authToken: string
2025-11-12 18:48:50 +02:00
): Promise<Connector[]> => {
2025-11-14 00:42:19 +02:00
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors?skip=${skip}&limit=${limit}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connectors");
}
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
return response.json();
2025-11-12 18:48:50 +02:00
};
2025-11-14 00:42:19 +02:00
export const getConnector = async (connectorId: number, authToken: string): Promise<Connector> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connector");
}
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
return response.json();
2025-11-12 18:48:50 +02:00
};
export const updateConnector = async (
2025-11-14 00:42:19 +02:00
connectorId: number,
data: CreateConnectorRequest,
authToken: string
2025-11-12 18:48:50 +02:00
): Promise<Connector> => {
2025-11-14 00:42:19 +02:00
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(data),
}
);
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to update connector");
}
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
return response.json();
2025-11-12 18:48:50 +02:00
};
2025-11-14 00:42:19 +02:00
export const deleteConnector = async (connectorId: number, authToken: string): Promise<void> => {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
2025-11-12 18:48:50 +02:00
2025-11-14 00:42:19 +02:00
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to delete connector");
}
2025-11-12 18:48:50 +02:00
};