chore: Move to Typescript (#2783)

This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
This commit is contained in:
Tom Moor
2021-11-29 06:40:55 -08:00
committed by GitHub
parent 25ccfb5d04
commit 15b1069bcc
1017 changed files with 17410 additions and 54942 deletions

61
app/utils/uploadFile.ts Normal file
View File

@@ -0,0 +1,61 @@
import invariant from "invariant";
import { client } from "./ApiClient";
type Options = {
name?: string;
documentId?: string;
public?: boolean;
};
export const uploadFile = async (
file: File | Blob,
options: Options = {
name: "",
}
) => {
const name = file instanceof File ? file.name : options.name;
const response = await client.post("/attachments.create", {
public: options.public,
documentId: options.documentId,
contentType: file.type,
size: file.size,
name,
});
invariant(response, "Response should be available");
const data = response.data;
const attachment = data.attachment;
const formData = new FormData();
for (const key in data.form) {
formData.append(key, data.form[key]);
}
// @ts-expect-error ts-migrate(2339) FIXME: Property 'blob' does not exist on type 'File | Blo... Remove this comment to see the full error message
if (file.blob) {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'file' does not exist on type 'File | Blo... Remove this comment to see the full error message
formData.append("file", file.file);
} else {
formData.append("file", file);
}
const uploadResponse = await fetch(data.uploadUrl, {
method: "post",
body: formData,
});
invariant(uploadResponse.ok, "Upload failed, try again?");
return attachment;
};
export const dataUrlToBlob = (dataURL: string) => {
const blobBin = atob(dataURL.split(",")[1]);
const array = [];
for (let i = 0; i < blobBin.length; i++) {
array.push(blobBin.charCodeAt(i));
}
const file = new Blob([new Uint8Array(array)], {
type: "image/png",
});
return file;
};