From 5216b519e0c2feb9e13297348c439e16cc7acb68 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 21 Jun 2023 10:21:05 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(API):=20add=20uploadFile=20?= =?UTF-8?q?function=20to=20upload=20files=20to=20the=20server=20The=20uplo?= =?UTF-8?q?adFile=20function=20allows=20the=20user=20to=20upload=20a=20fil?= =?UTF-8?q?e=20to=20the=20server.=20It=20takes=20in=20a=20file=20and=20an?= =?UTF-8?q?=20ID=20of=20the=20flow=20to=20upload=20the=20file=20to.=20The?= =?UTF-8?q?=20function=20creates=20a=20FormData=20object=20and=20appends?= =?UTF-8?q?=20the=20file=20to=20it.=20It=20then=20sends=20a=20POST=20reque?= =?UTF-8?q?st=20to=20the=20server=20with=20the=20FormData=20object=20as=20?= =?UTF-8?q?the=20body.=20The=20server=20responds=20with=20a=20Promise=20co?= =?UTF-8?q?ntaining=20the=20response=20data.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/controllers/API/index.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/frontend/src/controllers/API/index.ts b/src/frontend/src/controllers/API/index.ts index 28a41d578..2dca61a00 100644 --- a/src/frontend/src/controllers/API/index.ts +++ b/src/frontend/src/controllers/API/index.ts @@ -3,6 +3,7 @@ import { PromptTypeAPI, errorsTypeAPI, InitTypeAPI, + UploadFileTypeAPI, } from "./../../types/api/index"; import { APIObjectType, sendAllProps } from "../../types/api/index"; import axios, { AxiosResponse } from "axios"; @@ -22,7 +23,9 @@ const GITHUB_API_URL = "https://api.github.com"; export async function getRepoStars(owner, repo) { try { - const response = await axios.get(`${GITHUB_API_URL}/repos/${owner}/${repo}`); + const response = await axios.get( + `${GITHUB_API_URL}/repos/${owner}/${repo}` + ); return response.data.stargazers_count; } catch (error) { console.error("Error fetching repository data:", error); @@ -317,3 +320,21 @@ export async function postBuildInit( ): Promise> { return await axios.post(`/api/v1/build/init`, 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 axios.post(`/api/v1/upload/${id}`, formData); +}