unify and shorten sendToChat() errors (#2578)

* unify and shorten sendToChat() errors

* style: java script uses camelCase, not snake_case (these kind of issues tend to pile up and manifest then in the api somewhere, cmp max_serial - therefore better be strict :)
This commit is contained in:
bjoern
2023-06-03 10:16:32 +02:00
committed by GitHub
parent 01e3c46de3
commit c99ba34a4d
+9 -15
View File
@@ -43,13 +43,11 @@ window.webxdc = (() => {
sendToChat: async (message) => {
const data = {};
if (!message.file && !message.text) {
return Promise.reject(
"Error from sendToChat: Invalid empty message, at least one of text or file should be provided"
);
return Promise.reject("sendToChat() error: file or text missing");
}
/** @type {(file: Blob) => Promise<string>} */
const blob_to_base64 = (file) => {
const data_start = ";base64,";
const blobToBase64 = (file) => {
const dataStart = ";base64,";
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
@@ -57,7 +55,7 @@ window.webxdc = (() => {
/** @type {string} */
//@ts-ignore
let data = reader.result;
resolve(data.slice(data.indexOf(data_start) + data_start.length));
resolve(data.slice(data.indexOf(dataStart) + dataStart.length));
};
reader.onerror = () => reject(reader.error);
});
@@ -70,36 +68,32 @@ window.webxdc = (() => {
if (message.file) {
let base64content;
if (!message.file.name) {
return Promise.reject("file name is missing");
return Promise.reject("sendToChat() error: file name missing");
}
if (
Object.keys(message.file).filter((key) =>
["blob", "base64", "plainText"].includes(key)
).length > 1
) {
return Promise.reject(
"you can only set one of `blob`, `base64` or `plainText`, not multiple ones"
);
return Promise.reject("sendToChat() error: only one of blob, base64 or plainText allowed");
}
// @ts-ignore - needed because typescript imagines that blob would not exist
if (message.file.blob instanceof Blob) {
// @ts-ignore - needed because typescript imagines that blob would not exist
base64content = await blob_to_base64(message.file.blob);
base64content = await blobToBase64(message.file.blob);
// @ts-ignore - needed because typescript imagines that base64 would not exist
} else if (typeof message.file.base64 === "string") {
// @ts-ignore - needed because typescript imagines that base64 would not exist
base64content = message.file.base64;
// @ts-ignore - needed because typescript imagines that plainText would not exist
} else if (typeof message.file.plainText === "string") {
base64content = await blob_to_base64(
base64content = await blobToBase64(
// @ts-ignore - needed because typescript imagines that plainText would not exist
new Blob([message.file.plainText])
);
} else {
return Promise.reject(
"data is not set or wrong format, set one of `blob`, `base64` or `plainText`, see webxdc documentation for sendToChat"
);
return Promise.reject("sendToChat() error: none of blob, base64 or plainText set correctly");
}
data.base64 = base64content;
data.name = message.file.name;