🚀 feat(API): add uploadFile function to upload files to the server

The uploadFile function allows the user to upload a file to the server. It takes in a file and an ID of the flow to upload the file to. The function creates a FormData object and appends the file to it. It then sends a POST request to the server with the FormData object as the body. The server responds with a Promise containing the response data.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-21 10:21:05 -03:00
commit 5216b519e0

View file

@ -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<AxiosResponse<InitTypeAPI>> {
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<AxiosResponse<UploadFileTypeAPI>> {
const formData = new FormData();
formData.append("file", file);
return await axios.post(`/api/v1/upload/${id}`, formData);
}