feat: add frontend API helper with env-aware base URL
This commit is contained in:
45
frontend/src/lib/api.ts
Normal file
45
frontend/src/lib/api.ts
Normal file
@@ -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<Blob> {
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user