Skip to content

Commit 75f9ae6

Browse files
Fix the storing feature. How the heck it was working previously?!
1 parent 0d91bda commit 75f9ae6

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

mavo-gsheets.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,23 @@
458458
"majorDimension": this.dataInColumns ? "columns" : "rows",
459459
"values": data
460460
};
461+
const headers = {
462+
"Content-Type": "application/json",
463+
Authorization: `Bearer ${this.accessToken}`
464+
};
461465

462-
let res;
463-
try {
464-
res = await this.request(url, body, "PUT");
465-
} catch (e) {
466-
switch (e.status) {
466+
let response = await fetch(url.href, {
467+
method: "PUT",
468+
headers,
469+
body: JSON.stringify(body)
470+
});
471+
472+
if (!response.ok) {
473+
switch (response.status) {
467474
case 401:
468475
// Unauthorized
469476
this.mavo.error(this.mavo._("mv-gsheets-login-to-proceed"));
477+
await this.logout();
470478
return true;
471479
case 403:
472480
// No write permissions
@@ -476,12 +484,10 @@
476484
// No spreadsheet
477485
this.mavo.error(this.mavo._("mv-gsheets-spreadsheet-not-found"));
478486
return true;
479-
default:
480-
res = e;
481487
}
482488
}
483489

484-
if (res.response && res.status !== 200) { // res.status == 400
490+
if (response.status === 400) {
485491
if (this.sheet) {
486492
// It might be there is no sheet with the specified name.
487493
// Let's check it.
@@ -503,52 +509,68 @@
503509
};
504510

505511
try {
506-
await this.request(_.buildURL(`${this.spreadsheet}:batchUpdate`), req, "POST");
512+
response = await fetch(_.buildURL(`${this.spreadsheet}:batchUpdate`).href, {
513+
method: "POST",
514+
headers,
515+
body: JSON.stringify(req)
516+
});
517+
518+
if (!response.ok) {
519+
throw response;
520+
}
507521

508522
// Warn a user about the newly created sheet.
509523
Mavo.warn(this.mavo._("mv-gsheets-no-sheet-to-store-data", { name: this.sheet }));
510524

511525
// Try to store data one more time.
512-
res = await this.request(url, body, "PUT");
513-
} catch (e) {
514-
res = e;
515-
}
526+
response = await fetch(url.href, {
527+
method: "PUT",
528+
headers,
529+
body: JSON.stringify(body)
530+
});
531+
532+
if (!response.ok) {
533+
throw response;
534+
}
535+
} catch {}
516536
}
517537
}
518538

519539
// Something went wrong?
520-
if (res.response && res.status !== 200) {
521-
if (res.response.error.message.startsWith("Unable to parse range")) {
540+
if (response.status !== 200) {
541+
const error = (await response.json()).error.message;
542+
543+
if (error.startsWith("Unable to parse range")) {
522544
// Invalid range
523545
this.mavo.error(this.mavo._("mv-gsheets-invalid-range"));
524546
}
525-
else if (res.response.error.message.startsWith("Requested writing within range")) {
547+
else if (error.startsWith("Requested writing within range")) {
526548
// The range is too small
527549
this.mavo.error(this.mavo._("mv-gsheets-small-range"));
528550
}
529-
else if (res.response.error.message.includes("protected cell or object")) {
551+
else if (error.includes("protected cell or object")) {
530552
// The sheet and/or range is protected
531-
this.mavo.error(res.response.error.message);
553+
this.mavo.error(error);
532554
}
533-
else if (res.response.error.message.startsWith("Invalid values")) {
555+
else if (error.startsWith("Invalid values")) {
534556
// An app's data structure is not supported
535557
this.mavo.error(this.mavo._("mv-gsheets-unsupported-data-structure"));
536-
Mavo.warn(res.response.error.message);
558+
Mavo.warn(error);
537559
}
538560
else {
539561
// Unknown error
540-
Mavo.warn(res.response.error.message);
562+
Mavo.warn(error);
541563
}
542564

543565
return true;
544566
}
545567
};
546568

547569
// Saved successfully, update the fields.
548-
this.loadedData = res.updatedData?.values;
570+
this.loadedData = response.updatedData?.values;
549571
this.recordCount = recordCount;
550572

551-
return res;
573+
return response;
552574
}
553575

554576
async login (passive) {

0 commit comments

Comments
 (0)