Files
growthbook/packages/front-end/services/files.ts
Jeremy Dorn 301e13a10d Changes to enable running Growth Book in a docker container (#13)
-  Load front-end environment variables at runtime instead of buildtime
-  Add root Dockerfile and docker-compose.yml
-  Update CI to push docker images to Docker Hub
-  Update quick start in README to use `docker-compose up -d` instead of `yarn dev`
2021-06-02 16:15:31 -05:00

32 lines
701 B
TypeScript

import { ApiCallType } from "./auth";
import { getApiHost } from "./env";
export async function uploadFile(
apiCall: ApiCallType<{ uploadURL: string; fileURL: string }>,
file: File
) {
const ext = file.name.split(".").reverse()[0];
const { uploadURL, fileURL } = await apiCall(`/upload/${ext}`, {
method: "POST",
});
const res = await fetch(
uploadURL.match(/^\//) ? getApiHost() + uploadURL : uploadURL,
{
method: "PUT",
headers: {
"Content-Type": file.type,
},
body: file,
}
);
if (!res.ok) {
throw new Error("Failed to upload file");
}
return {
fileURL: fileURL.match(/^\//) ? getApiHost() + fileURL : fileURL,
};
}