|
458 | 458 | "majorDimension": this.dataInColumns ? "columns" : "rows", |
459 | 459 | "values": data |
460 | 460 | }; |
| 461 | + const headers = { |
| 462 | + "Content-Type": "application/json", |
| 463 | + Authorization: `Bearer ${this.accessToken}` |
| 464 | + }; |
461 | 465 |
|
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) { |
467 | 474 | case 401: |
468 | 475 | // Unauthorized |
469 | 476 | this.mavo.error(this.mavo._("mv-gsheets-login-to-proceed")); |
| 477 | + await this.logout(); |
470 | 478 | return true; |
471 | 479 | case 403: |
472 | 480 | // No write permissions |
|
476 | 484 | // No spreadsheet |
477 | 485 | this.mavo.error(this.mavo._("mv-gsheets-spreadsheet-not-found")); |
478 | 486 | return true; |
479 | | - default: |
480 | | - res = e; |
481 | 487 | } |
482 | 488 | } |
483 | 489 |
|
484 | | - if (res.response && res.status !== 200) { // res.status == 400 |
| 490 | + if (response.status === 400) { |
485 | 491 | if (this.sheet) { |
486 | 492 | // It might be there is no sheet with the specified name. |
487 | 493 | // Let's check it. |
|
503 | 509 | }; |
504 | 510 |
|
505 | 511 | 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 | + } |
507 | 521 |
|
508 | 522 | // Warn a user about the newly created sheet. |
509 | 523 | Mavo.warn(this.mavo._("mv-gsheets-no-sheet-to-store-data", { name: this.sheet })); |
510 | 524 |
|
511 | 525 | // 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 {} |
516 | 536 | } |
517 | 537 | } |
518 | 538 |
|
519 | 539 | // 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")) { |
522 | 544 | // Invalid range |
523 | 545 | this.mavo.error(this.mavo._("mv-gsheets-invalid-range")); |
524 | 546 | } |
525 | | - else if (res.response.error.message.startsWith("Requested writing within range")) { |
| 547 | + else if (error.startsWith("Requested writing within range")) { |
526 | 548 | // The range is too small |
527 | 549 | this.mavo.error(this.mavo._("mv-gsheets-small-range")); |
528 | 550 | } |
529 | | - else if (res.response.error.message.includes("protected cell or object")) { |
| 551 | + else if (error.includes("protected cell or object")) { |
530 | 552 | // The sheet and/or range is protected |
531 | | - this.mavo.error(res.response.error.message); |
| 553 | + this.mavo.error(error); |
532 | 554 | } |
533 | | - else if (res.response.error.message.startsWith("Invalid values")) { |
| 555 | + else if (error.startsWith("Invalid values")) { |
534 | 556 | // An app's data structure is not supported |
535 | 557 | this.mavo.error(this.mavo._("mv-gsheets-unsupported-data-structure")); |
536 | | - Mavo.warn(res.response.error.message); |
| 558 | + Mavo.warn(error); |
537 | 559 | } |
538 | 560 | else { |
539 | 561 | // Unknown error |
540 | | - Mavo.warn(res.response.error.message); |
| 562 | + Mavo.warn(error); |
541 | 563 | } |
542 | 564 |
|
543 | 565 | return true; |
544 | 566 | } |
545 | 567 | }; |
546 | 568 |
|
547 | 569 | // Saved successfully, update the fields. |
548 | | - this.loadedData = res.updatedData?.values; |
| 570 | + this.loadedData = response.updatedData?.values; |
549 | 571 | this.recordCount = recordCount; |
550 | 572 |
|
551 | | - return res; |
| 573 | + return response; |
552 | 574 | } |
553 | 575 |
|
554 | 576 | async login (passive) { |
|
0 commit comments