From 42253c322051feed6d3d50fa6a36000ae7e5df8e Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 11 Mar 2024 23:46:24 +0700 Subject: [PATCH] upload file --- src/shared/lib/uploadFile.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/shared/lib/uploadFile.ts diff --git a/src/shared/lib/uploadFile.ts b/src/shared/lib/uploadFile.ts new file mode 100644 index 0000000..3713a29 --- /dev/null +++ b/src/shared/lib/uploadFile.ts @@ -0,0 +1,23 @@ +import { z } from "zod"; +import { getAxios } from "./getAxios"; + +const fileSchema = z.object({ + url: z.string(), +}); + +export async function uploadFile(file: File) { + const axios = getAxios(); + + try { + const data = new FormData(); + data.set("file", file); + + const response = await axios.post("/s3", data, { + headers: { "Content-Type": "multipart/form-data" }, + }); + + return fileSchema.parse(response.data).url; + } catch { + return null; + } +}