tsv2-library 1.0.61-alpha.61 → 1.0.61-alpha.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,4 @@
1
1
  export declare const buildFileURL: (name?: string, width?: number, height?: number) => string;
2
- export declare const fetchBlobFile: (url: string, token: string) => Promise<Blob>;
3
2
  /**
4
3
  *
5
4
  * @param fileUrl The full URL
@@ -7,5 +6,5 @@ export declare const fetchBlobFile: (url: string, token: string) => Promise<Blob
7
6
  * @param immediateRevoke Immediately revoke the object URL after download - default to true
8
7
  * @returns
9
8
  */
10
- export declare const downloadFile: (fileUrl: string, fileName: string, immediateRevoke?: boolean) => Promise<string>;
9
+ export declare const downloadFile: (fileUrl: string, customFileName?: string, immediateRevoke?: boolean) => Promise<string>;
11
10
  export declare const getImageURL: (name?: string | null, width?: number, height?: number, returnURLOnly?: boolean) => Promise<string | undefined>;
@@ -19186,11 +19186,11 @@ function generateXlsxFile(data30, _ref2) {
19186
19186
  }
19187
19187
  const prepareRows = (headers, data30) => {
19188
19188
  const rows3 = [];
19189
+ if (headers) {
19190
+ rows3.push(headers.map((h2) => ({ value: h2 })));
19191
+ }
19189
19192
  const isObjectArray = Array.isArray(data30) && data30.length > 0 && !Array.isArray(data30[0]);
19190
19193
  if (isObjectArray) {
19191
- if (headers) {
19192
- rows3.push(headers.map((h2) => ({ value: h2 })));
19193
- }
19194
19194
  if (data30.length > 0) {
19195
19195
  const keys2 = Object.keys(data30[0]);
19196
19196
  data30.forEach((row2) => {
@@ -19204,9 +19204,6 @@ const prepareRows = (headers, data30) => {
19204
19204
  }
19205
19205
  } else if (Array.isArray(data30) && Array.isArray(data30[0])) {
19206
19206
  const arrayData = data30;
19207
- if (headers) {
19208
- rows3.push(headers.map((h2) => ({ value: h2 })));
19209
- }
19210
19207
  arrayData.forEach((row2) => {
19211
19208
  if (row2.length === 0) {
19212
19209
  rows3.push([{ value: "" }]);
@@ -19605,43 +19602,54 @@ const buildFileURL = (name, width2, height) => {
19605
19602
  }
19606
19603
  return url;
19607
19604
  };
19605
+ const getFilenameFromResponse = (response) => {
19606
+ const contentDisposition = response.headers["Content-Disposition"];
19607
+ if (!contentDisposition)
19608
+ return null;
19609
+ const quotedFilenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
19610
+ const quotedMatch = contentDisposition.match(quotedFilenameRegex);
19611
+ return (quotedMatch == null ? void 0 : quotedMatch[1]) ?? null;
19612
+ };
19608
19613
  const getAuthToken = () => {
19609
19614
  const user = JSON.parse(localStorage.getItem("user") ?? "{}");
19610
19615
  return user.jwt ?? user.token ?? "";
19611
19616
  };
19612
- const fetchBlobFile = async (url, token) => {
19613
- const res = await fetch(url, {
19617
+ const fetchFile = async (url, token) => {
19618
+ const res = await axios.get(url, {
19619
+ responseType: "blob",
19614
19620
  headers: {
19615
19621
  Authorization: `Bearer ${token}`
19616
19622
  }
19617
19623
  });
19618
- if (!res.ok) {
19619
- throw new Error(`Image fetch failed: ${res.status} ${res.statusText}`);
19620
- }
19621
- const arrayBuffer = await res.arrayBuffer();
19622
- return new Blob([arrayBuffer], {
19623
- type: res.headers.get("Content-Type") || "image/webp"
19624
- });
19624
+ return {
19625
+ blob: new Blob([res.data], {
19626
+ type: res.headers["Content-Type"] || "image/webp"
19627
+ }),
19628
+ fileName: getFilenameFromResponse(res)
19629
+ };
19625
19630
  };
19626
- const downloadFile = async (fileUrl, fileName, immediateRevoke = true) => {
19631
+ const downloadFile = async (fileUrl, customFileName, immediateRevoke = true) => {
19627
19632
  const token = getAuthToken();
19628
- const blob2 = await fetchBlobFile(fileUrl, token);
19633
+ const { blob: blob2, fileName: fetchFileName } = await fetchFile(fileUrl, token);
19629
19634
  let objectUrl = "";
19630
19635
  const isViewable = /^(image|application\/pdf)/i.test(blob2.type);
19631
- if (isViewable) {
19632
- const file = new File([blob2], fileName, {
19633
- type: blob2.type
19634
- });
19635
- objectUrl = URL.createObjectURL(file);
19636
- window.open(objectUrl, "_blank");
19637
- } else {
19638
- objectUrl = URL.createObjectURL(blob2);
19639
- const a = document.createElement("a");
19640
- a.href = objectUrl;
19641
- a.download = fileName;
19642
- document.body.appendChild(a);
19643
- a.click();
19644
- a.remove();
19636
+ const fileName = customFileName ?? fetchFileName;
19637
+ if (fileName) {
19638
+ if (isViewable) {
19639
+ const file = new File([blob2], fileName, {
19640
+ type: blob2.type
19641
+ });
19642
+ objectUrl = URL.createObjectURL(file);
19643
+ window.open(objectUrl, "_blank");
19644
+ } else {
19645
+ objectUrl = URL.createObjectURL(blob2);
19646
+ const a = document.createElement("a");
19647
+ a.href = objectUrl;
19648
+ a.download = fileName;
19649
+ document.body.appendChild(a);
19650
+ a.click();
19651
+ a.remove();
19652
+ }
19645
19653
  }
19646
19654
  if (immediateRevoke) {
19647
19655
  URL.revokeObjectURL(objectUrl);
@@ -19651,7 +19659,7 @@ const downloadFile = async (fileUrl, fileName, immediateRevoke = true) => {
19651
19659
  const createBlobURL = async (rawFileUrl) => {
19652
19660
  try {
19653
19661
  const token = getAuthToken();
19654
- const blob2 = await fetchBlobFile(rawFileUrl, token);
19662
+ const { blob: blob2 } = await fetchFile(rawFileUrl, token);
19655
19663
  return URL.createObjectURL(blob2);
19656
19664
  } catch (err) {
19657
19665
  return void 0;