From cefa32dc93cee2566ce1a8152c5466359c99f218 Mon Sep 17 00:00:00 2001 From: jason Date: Sat, 7 Mar 2026 23:02:34 -0600 Subject: [PATCH] feat: add frontend API helper with env-aware base URL --- frontend/src/lib/api.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 frontend/src/lib/api.ts diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts new file mode 100644 index 0000000..dc3ac40 --- /dev/null +++ b/frontend/src/lib/api.ts @@ -0,0 +1,45 @@ +export interface TransformOptions { + width?: number; + height?: number; + quality?: number; + format?: "png" | "webp" | "jpeg"; + fit?: "inside" | "outside" | "cover" | "contain"; + position?: + | "center" + | "top" + | "right" + | "bottom" + | "left" + | "top-left" + | "top-right" + | "bottom-left" + | "bottom-right"; +} + +const API_BASE = + import.meta.env.DEV ? "http://localhost:3000/api" : "/api"; + +export async function transformImage( + file: File, + options: TransformOptions +): Promise { + const formData = new FormData(); + formData.append("file", file); + + Object.entries(options).forEach(([key, value]) => { + if (value !== undefined && value !== null) { + formData.append(key, String(value)); + } + }); + + const res = await fetch(`${API_BASE}/transform`, { + method: "POST", + body: formData + }); + + if (!res.ok) { + throw new Error("Transform failed"); + } + + return await res.blob(); +} \ No newline at end of file