diff --git a/src/frontend/src/controllers/API/index.ts b/src/frontend/src/controllers/API/index.ts index 5d4f112d0..ae7385b5f 100644 --- a/src/frontend/src/controllers/API/index.ts +++ b/src/frontend/src/controllers/API/index.ts @@ -3,36 +3,13 @@ import { AxiosRequestConfig, AxiosResponse } from "axios"; import { BASE_URL_API } from "../../constants/constants"; import { api } from "../../controllers/API/api"; import { - APIObjectType, Component, - CustomComponentRequest, - PromptTypeAPI, - Users, VertexBuildTypeAPI, VerticesOrderTypeAPI, - sendAllProps, } from "../../types/api/index"; import { FlowStyleType, FlowType } from "../../types/flow"; import { StoreComponentResponse } from "../../types/store"; import { FlowPoolType } from "../../types/zustand/flow"; -import { - APIClassType, - BuildStatusTypeAPI, - InitTypeAPI, - UploadFileTypeAPI, -} from "./../../types/api/index"; - -/** - * Fetches all objects from the API endpoint. - * - * @param {boolean} force_refresh - Whether to force a refresh of the data. - * @returns {Promise>} A promise that resolves to an AxiosResponse containing all the objects. - */ -export async function getAll( - force_refresh: boolean = true, -): Promise> { - return await api.get(`${BASE_URL_API}all?force_refresh=${force_refresh}`); -} const GITHUB_API_URL = "https://api.github.com"; @@ -46,299 +23,6 @@ export async function getRepoStars(owner: string, repo: string) { } } -/** - * Sends data to the API for prediction. - * - * @param {sendAllProps} data - The data to be sent to the API. - * @returns {AxiosResponse} The API response. - */ -export async function sendAll(data: sendAllProps) { - return await api.post(`${BASE_URL_API}predict`, data); -} - -/** - * Checks the prompt for the code block by sending it to an API endpoint. - * @param {string} name - The name of the field to check. - * @param {string} template - The template string of the prompt to check. - * @param {APIClassType} frontend_node - The frontend node to check. - * @returns {Promise>} A promise that resolves to an AxiosResponse containing the validation results. - */ -export async function postValidatePrompt( - name: string, - template: string, - frontend_node: APIClassType, -): Promise> { - return api.post(`${BASE_URL_API}validate/prompt`, { - name, - template, - frontend_node, - }); -} - -/** - * Fetches a list of JSON files from a GitHub repository and returns their contents as an array of FlowType objects. - * - * @returns {Promise} A promise that resolves to an array of FlowType objects. - */ -export async function getExamples(): Promise { - const url = - "https://api.github.com/repos/langflow-ai/langflow_examples/contents/examples?ref=main"; - const response = await api.get(url); - - const jsonFiles = response?.data.filter((file: any) => { - return file.name.endsWith(".json"); - }); - - const contentsPromises = jsonFiles.map(async (file: any) => { - const contentResponse = await api.get(file.download_url); - return contentResponse.data; - }); - - return await Promise.all(contentsPromises); -} - -/** - * Saves a new flow to the database. - * - * @param {FlowType} newFlow - The flow data to save. - * @returns {Promise} The saved flow data. - * @throws Will throw an error if saving fails. - */ -export async function saveFlowToDatabase(newFlow: { - name: string; - id: string; - data: ReactFlowJsonObject | null; - description: string; - style?: FlowStyleType; - is_component?: boolean; - folder_id?: string; - endpoint_name?: string; -}): Promise { - try { - const response = await api.post(`${BASE_URL_API}flows/`, { - name: newFlow.name, - data: newFlow.data, - description: newFlow.description, - is_component: newFlow.is_component, - folder_id: newFlow.folder_id === "" ? null : newFlow.folder_id, - endpoint_name: newFlow.endpoint_name, - }); - - if (response?.status !== 201) { - throw new Error(`HTTP error! status: ${response?.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -/** - * Reads all flows from the database. - * - * @returns {Promise} The flows data. - * @throws Will throw an error if reading fails. - */ -export async function readFlowsFromDatabase() { - try { - const response = await api.get(`${BASE_URL_API}flows/`); - if (response && response?.status !== 200) { - throw new Error(`HTTP error! status: ${response?.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -export async function uploadFlowsToDatabase(flows: FormData) { - try { - const response = await api.post(`${BASE_URL_API}flows/upload/`, flows); - - if (response?.status !== 201) { - throw new Error(`HTTP error! status: ${response?.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -/** - * Deletes a flow from the database. - * - * @param {string} flowId - The ID of the flow to delete. - * @returns {Promise} The deleted flow data. - * @throws Will throw an error if deletion fails. - */ -export async function deleteFlowFromDatabase(flowId: string) { - try { - const response = await api.delete(`${BASE_URL_API}flows/${flowId}`); - if (response.status !== 200) { - throw new Error(`HTTP error! status: ${response.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -/** - * Fetches a flow from the database by ID. - * - * @param {number} flowId - The ID of the flow to fetch. - * @returns {Promise} The flow data. - * @throws Will throw an error if fetching fails. - */ -export async function getFlowFromDatabase(flowId: number) { - try { - const response = await api.get(`${BASE_URL_API}flows/${flowId}`); - if (response && response?.status !== 200) { - throw new Error(`HTTP error! status: ${response?.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -/** - * Fetches flow styles from the database. - * - * @returns {Promise} The flow styles data. - * @throws Will throw an error if fetching fails. - */ -export async function getFlowStylesFromDatabase() { - try { - const response = await api.get(`${BASE_URL_API}flow_styles/`); - if (response.status !== 200) { - throw new Error(`HTTP error! status: ${response.status}`); - } - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - -/** - * Fetches the version of the API. - * - * @returns {Promise>} A promise that resolves to an AxiosResponse containing the version information. - */ -export async function getVersion() { - const response = await api.get(`${BASE_URL_API}version`); - return response?.data; -} - -/** - * Fetches the build status of a flow. - * @param {string} flowId - The ID of the flow to fetch the build status for. - * @returns {Promise} A promise that resolves to an AxiosResponse containing the build status. - * - */ -export async function getBuildStatus( - flowId: string, -): Promise> { - return await api.get(`${BASE_URL_API}build/${flowId}/status`); -} - -//docs for postbuildinit -/** - * Posts the build init of a flow. - * @param {string} flowId - The ID of the flow to fetch the build status for. - * @returns {Promise} A promise that resolves to an AxiosResponse containing the build status. - * - */ -export async function postBuildInit( - flow: FlowType, -): Promise> { - return await api.post(`${BASE_URL_API}build/init/${flow.id}`, flow); -} - -// fetch(`/upload/${id}`, { -// method: "POST", -// body: formData, -// }); -/** - * Uploads a file to the server. - * @param {File} file - The file to upload. - * @param {string} id - The ID of the flow to upload the file to. - */ -export async function uploadFile( - file: File, - id: string, -): Promise> { - const formData = new FormData(); - formData.append("file", file); - return await api.post(`${BASE_URL_API}files/upload/${id}`, formData); -} - -export async function postCustomComponent( - code: string, - apiClass: APIClassType, -): Promise> { - // let template = apiClass.template; - return await api.post(`${BASE_URL_API}custom_component`, { - code, - frontend_node: apiClass, - }); -} - -export async function renewAccessToken() { - try { - return await api.post(`${BASE_URL_API}refresh`); - } catch (error) { - throw error; - } -} - -export async function getLoggedUser(): Promise { - try { - const res = await api.get(`${BASE_URL_API}users/whoami`); - - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } - return null; -} - -export async function getUsersPage( - skip: number, - limit: number, -): Promise> { - try { - const res = await api.get( - `${BASE_URL_API}users/?skip=${skip}&limit=${limit}`, - ); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } - return []; -} - -export async function getApiKey() { - try { - const res = await api.get(`${BASE_URL_API}api_key/`); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - export async function createApiKey(name: string) { try { const res = await api.post(`${BASE_URL_API}api_key/`, { name }); @@ -350,30 +34,6 @@ export async function createApiKey(name: string) { } } -export async function deleteApiKey(api_key: string) { - try { - const res = await api.delete(`${BASE_URL_API}api_key/${api_key}`); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - -export async function addApiKeyStore(key: string) { - try { - const res = await api.post(`${BASE_URL_API}api_key/store`, { - api_key: key, - }); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - /** * Saves a new flow to the database. * @@ -417,15 +77,6 @@ export async function saveFlowStore( } } -/** - * Fetches the flows from the store. - * @returns {Promise<>} A promise that resolves to an AxiosResponse containing the build status. - * - */ -export async function getFlowsStore(): Promise> { - return await api.get(`${BASE_URL_API}store/`); -} - export async function getStoreComponents({ component_id = null, page = 1, @@ -507,17 +158,6 @@ export async function getStoreComponents({ } } -export async function postStoreComponents(component: Component) { - try { - const res = await api.post(`${BASE_URL_API}store/components/`, component); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - export async function getComponent(component_id: string) { try { const res = await api.get( @@ -531,45 +171,6 @@ export async function getComponent(component_id: string) { } } -export async function searchComponent( - query: string | null, - page?: number | null, - limit?: number | null, - status?: string | null, - tags?: string[], -): Promise { - try { - let url = `${BASE_URL_API}store/components/`; - const queryParams: any = []; - if (query !== undefined && query !== null) { - queryParams.push(`search=${query}`); - } - if (page !== undefined && page !== null) { - queryParams.push(`page=${page}`); - } - if (limit !== undefined && limit !== null) { - queryParams.push(`limit=${limit}`); - } - if (status !== undefined && status !== null) { - queryParams.push(`status=${status}`); - } - if (tags !== undefined && tags !== null) { - queryParams.push(`tags=${tags}`); - } - if (queryParams.length > 0) { - url += `?${queryParams.join("&")}`; - } - - const res = await api.get(url); - - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - export async function checkHasApiKey() { try { const res = await api.get(`${BASE_URL_API}store/check/api_key`); @@ -592,43 +193,6 @@ export async function checkHasStore() { } } -export async function getCountComponents(is_component?: boolean | null) { - try { - let url = `${BASE_URL_API}store/components/count`; - const queryParams: any = []; - if (is_component !== undefined) { - queryParams.push(`is_component=${is_component}`); - } - - if (queryParams.length > 0) { - url += `?${queryParams.join("&")}`; - } - - const res = await api.get(url); - - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - -export async function getStoreTags() { - try { - const res = await api.get(`${BASE_URL_API}store/tags`); - if (res.status === 200) { - return res.data; - } - } catch (error) { - throw error; - } -} - -export const postLikeComponent = (componentId: string) => { - return api.post(`${BASE_URL_API}store/users/likes/${componentId}`); -}; - /** * Updates an existing flow in the Store. * @@ -672,16 +236,6 @@ export async function updateFlowStore( } } -export async function requestLogout() { - try { - const response = await api.post(`${BASE_URL_API}logout`); - return response?.data; - } catch (error) { - console.error(error); - throw error; - } -} - export async function getVerticesOrder( flowId: string, startNodeId?: string | null, @@ -730,25 +284,3 @@ export async function postBuildVertex( data, ); } - -export async function downloadImage({ flowId, fileName }): Promise { - return await api.get(`${BASE_URL_API}files/images/${flowId}/${fileName}`); -} - -export async function getFlowPool({ - flowId, -}: { - flowId: string; -}): Promise> { - const config = {}; - config["params"] = { flow_id: flowId }; - return await api.get(`${BASE_URL_API}monitor/builds`, config); -} - -export async function deleteFlowPool( - flowId: string, -): Promise> { - const config = {}; - config["params"] = { flow_id: flowId }; - return await api.delete(`${BASE_URL_API}monitor/builds`, config); -} diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index 8f1415ec5..c816a35a1 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -104,16 +104,6 @@ export type OutputFieldType = { hidden?: boolean; proxy?: OutputFieldProxyType; }; -export type sendAllProps = { - nodes: Node[]; - edges: Edge[]; - name: string; - description: string; - viewport: Viewport; - inputs: { text?: string }; - chatKey: string; - chatHistory: { message: string | object; isSend: boolean }[]; -}; export type errorsTypeAPI = { function: { errors: Array }; imports: { errors: Array };