Added form file processing

This commit is contained in:
Dmitriy Shishkov 2021-07-15 18:38:57 +05:00
parent cfc89ec987
commit c25873158d
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
2 changed files with 60 additions and 7 deletions

50
src/api.ts Normal file
View File

@ -0,0 +1,50 @@
import { IBook, optionalBookProps, requiredBookProps } from "./types/book";
const API_URL = import.meta.env.SNOWPACK_PUBLIC_API_URL;
export const validState = (file: File | undefined): file is File => {
if (!file) throw new Error("Book file is required. Please, attach one");
if (!file.name.match(/\.(fb2|epub)/))
throw new Error(
"Wrong file type. Only FB2 and Epub files are supported. \
If you are trying to upload fb2.zip, please, uncopress it first."
);
if (file.size > 100 * 1024 * 1024)
throw new Error(
"File size is too big. Sorry, but parser is served on a rather cheap hosting, \
so application can't handle such big files."
);
return true;
};
export const submitFile = async (
file: File
): Promise<{ [key: string]: string }> => {
const body = new FormData();
body.append("file", file);
try {
const res = await fetch(API_URL + "/uploadfile/", {
method: "POST",
body,
});
return await res.json();
} catch (err) {
console.log("Network error:", err.message);
throw err;
}
};
export const validateResponse = (
content: Record<string, unknown>
): content is IBook => {
for (const key of requiredBookProps)
if (!(key in content))
throw new Error(`${key} is not specified in server response`);
return true;
};

View File

@ -1,7 +1,10 @@
export interface IBook {
title: string;
author: string;
cover?: string;
content: string;
hash?: string;
}
export const requiredBookProps = ["title", "author", "content"] as const;
export const optionalBookProps = ["cover", "hash"] as const;
export type IBook =
| {
[key in typeof requiredBookProps[number]]: string;
}
| {
[key in typeof optionalBookProps[number]]: string | undefined;
};