Skip to content

Commit c737fa1

Browse files
Or-MiOr-Mi
authored andcommitted
compress request before sending to BE
1 parent 9c77c24 commit c737fa1

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/taskpane/components/App.tsx

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "@fluentui/react-components";
1919
import { getCellAddress } from "../taskpane";
2020
import { useState } from "react";
21+
import pako from 'pako';
2122

2223

2324
// ProJets brand color
@@ -175,7 +176,7 @@ const App = () => {
175176

176177
const readWorkbook = async () => {
177178
resetProgress();
178-
179+
179180
const readRange = async (worksheetData, range) => {
180181
range.load([
181182
"columnCount",
@@ -317,17 +318,42 @@ const App = () => {
317318

318319
try {
319320
const workbook = await readWorkbook();
320-
321-
await fetch(targetUrl as string, {
322-
method: "POST",
323-
body: JSON.stringify({
321+
let jsonData = JSON.stringify({
324322
worksheets: workbook,
325323
params: { param1, param2 },
326-
}),
324+
});
325+
326+
const compressedData = pako.gzip(jsonData);
327+
const response = await fetch(targetUrl as string, {
328+
method: 'POST',
329+
headers: {
330+
'Content-Type': 'application/json',
331+
'Content-Encoding': 'gzip',
332+
'Accept-Encoding': 'gzip'
333+
},
334+
body: compressedData
327335
});
336+
337+
if (!response.ok) {
338+
throw new Error(`HTTP error! status: ${response.status}`);
339+
}
340+
341+
// // Handle compressed response
342+
// const responseBuffer = await response.arrayBuffer();
343+
// const responseUint8Array = new Uint8Array(responseBuffer);
344+
// const responseString = responseUint8Array.toString();
345+
// // const decompressedResponse = pako.ungzip(responseUint8Array, { to: 'string' });
346+
347+
// return JSON.parse(responseString);
348+
return await response.json();
328349
} catch (error) {
329-
setReadErrorMessage(error.message || "An error occurred during the read operation");
330-
} finally {
350+
// Compose a detailed error message with stack trace
351+
const detailedError =
352+
(error.message ? `Error: ${error.message}\n` : "") +
353+
(error.stack ? `Stack trace:\n${error.stack}` : "");
354+
console.error("Error reading workbook:", detailedError);
355+
setReadErrorMessage(detailedError || "An error occurred during the read operation");
356+
} finally {
331357
setIsReadLoading(false);
332358
}
333359
};
@@ -411,8 +437,13 @@ const App = () => {
411437
const jsonData = JSON.parse(textArea as string);
412438
await writeWorkbook(jsonData);
413439
} catch (error) {
414-
setWriteErrorMessage(error.message || "An error occurred during the write operation");
415-
} finally {
440+
// Compose a detailed error message with stack trace
441+
const detailedError =
442+
(error.message ? `Error: ${error.message}\n` : "") +
443+
(error.stack ? `Stack trace:\n${error.stack}` : "");
444+
console.error("Error reading workbook:", detailedError);
445+
setReadErrorMessage(detailedError || "An error occurred during the write operation");
446+
} finally {
416447
setIsWriteLoading(false);
417448
}
418449
};

0 commit comments

Comments
 (0)