@gateweb/react-utils 2.4.0 → 2.5.0
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.
- package/dist/cjs/client.d.ts +31 -2
- package/dist/cjs/client.js +2 -1
- package/dist/cjs/download-12s-C5yIKqRX.js +65 -0
- package/dist/es/client.d.mts +31 -2
- package/dist/es/client.mjs +1 -1
- package/dist/es/download-12s-DtxLfLpr.mjs +64 -0
- package/package.json +1 -1
- package/dist/cjs/download-12s-DKxkL92w.js +0 -27
- package/dist/es/download-12s-CnaJ0p_f.mjs +0 -27
package/dist/cjs/client.d.ts
CHANGED
|
@@ -286,6 +286,35 @@ declare function mergeRefs<T = any>(refs: Array<React__default.MutableRefObject<
|
|
|
286
286
|
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
287
287
|
*/
|
|
288
288
|
declare const downloadFile: (source: string | Blob, filename?: string, fileExtension?: string) => void;
|
|
289
|
+
type DownloadFileOptions = {
|
|
290
|
+
/**
|
|
291
|
+
* The url string to download.
|
|
292
|
+
*/
|
|
293
|
+
url: string;
|
|
294
|
+
/**
|
|
295
|
+
* The filename to use for the downloaded file (without extension)
|
|
296
|
+
*
|
|
297
|
+
* @default The current timestamp as a string (e.g. "1686789123456")
|
|
298
|
+
*/
|
|
299
|
+
filename?: string;
|
|
300
|
+
/**
|
|
301
|
+
* The file extension to append to the filename (e.g. "txt", "pdf").
|
|
302
|
+
*/
|
|
303
|
+
fileExtension?: string;
|
|
304
|
+
/**
|
|
305
|
+
* `fetch` init options (e.g. `credentials`, `headers`).
|
|
306
|
+
*
|
|
307
|
+
* Note: the URL must allow CORS (or be same-origin) for the browser to read the response.
|
|
308
|
+
*/
|
|
309
|
+
fetchInit?: RequestInit;
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* Downloads a file by fetching the URL into a Blob first, then triggering a blob download.
|
|
313
|
+
*
|
|
314
|
+
* This is useful when a URL returns a previewable Content-Type (e.g. `application/pdf`) and
|
|
315
|
+
* the browser would otherwise open a new tab to preview instead of downloading.
|
|
316
|
+
*/
|
|
317
|
+
declare const downloadFileWithFetch: ({ url, filename, fileExtension, fetchInit, }: DownloadFileOptions) => Promise<void>;
|
|
289
318
|
|
|
290
319
|
/**
|
|
291
320
|
* 從 localStorage 取得資料,支援槽狀取值(Json 物件)
|
|
@@ -307,5 +336,5 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
307
336
|
*/
|
|
308
337
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
309
338
|
|
|
310
|
-
export { QueryProvider, createDataContext, createStoreContext, downloadFile, getLocalStorage, mergeRefs, setLocalStorage, useCountdown, useDisclosure, useHorizontalWheel, useQueryContext, useValue };
|
|
311
|
-
export type { TCountdownActions, UseDisclosureReturn };
|
|
339
|
+
export { QueryProvider, createDataContext, createStoreContext, downloadFile, downloadFileWithFetch, getLocalStorage, mergeRefs, setLocalStorage, useCountdown, useDisclosure, useHorizontalWheel, useQueryContext, useValue };
|
|
340
|
+
export type { DownloadFileOptions, TCountdownActions, UseDisclosureReturn };
|
package/dist/cjs/client.js
CHANGED
|
@@ -6,7 +6,7 @@ var React = require('react');
|
|
|
6
6
|
var useCountdown12s = require('./useCountdown-12s-uiqhgllY.js');
|
|
7
7
|
var useDisclosure12s = require('./useDisclosure-12s-SZtbSE4A.js');
|
|
8
8
|
var useHorizontalWheel12s = require('./useHorizontalWheel-12s-bwbWVJeM.js');
|
|
9
|
-
var download12s = require('./download-12s-
|
|
9
|
+
var download12s = require('./download-12s-C5yIKqRX.js');
|
|
10
10
|
var webStorage12s = require('./webStorage-12s-0RtNO_uc.js');
|
|
11
11
|
|
|
12
12
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -114,6 +114,7 @@ exports.useCountdown = useCountdown12s.useCountdown;
|
|
|
114
114
|
exports.useDisclosure = useDisclosure12s.useDisclosure;
|
|
115
115
|
exports.useHorizontalWheel = useHorizontalWheel12s.useHorizontalWheel;
|
|
116
116
|
exports.downloadFile = download12s.downloadFile;
|
|
117
|
+
exports.downloadFileWithFetch = download12s.downloadFileWithFetch;
|
|
117
118
|
exports.getLocalStorage = webStorage12s.getLocalStorage;
|
|
118
119
|
exports.setLocalStorage = webStorage12s.setLocalStorage;
|
|
119
120
|
exports.createDataContext = createDataContext;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
const sanitizeUrlForError = (input)=>{
|
|
3
|
+
try {
|
|
4
|
+
const parsed = new URL(input);
|
|
5
|
+
return `${parsed.origin}${parsed.pathname}`;
|
|
6
|
+
} catch {
|
|
7
|
+
return input.split('#')[0].split('?')[0];
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Downloads a file from a given source.
|
|
12
|
+
*
|
|
13
|
+
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
14
|
+
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
15
|
+
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
19
|
+
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
20
|
+
*/ const downloadFile = (source, filename = new Date().getTime().toString(), fileExtension)=>{
|
|
21
|
+
const shouldRevokeObjectUrl = typeof source !== 'string';
|
|
22
|
+
const url = shouldRevokeObjectUrl ? URL.createObjectURL(source) : source;
|
|
23
|
+
const link = document.createElement('a');
|
|
24
|
+
link.id = `download-${new Date().getTime()}`;
|
|
25
|
+
document.body.appendChild(link);
|
|
26
|
+
if (!fileExtension) {
|
|
27
|
+
link.download = filename;
|
|
28
|
+
} else {
|
|
29
|
+
link.download = `${filename}.${fileExtension}`;
|
|
30
|
+
}
|
|
31
|
+
link.href = url;
|
|
32
|
+
link.target = '_blank';
|
|
33
|
+
link.click();
|
|
34
|
+
link.remove();
|
|
35
|
+
if (shouldRevokeObjectUrl) {
|
|
36
|
+
// Defer revocation to avoid interfering with the download in some browsers.
|
|
37
|
+
setTimeout(()=>{
|
|
38
|
+
URL.revokeObjectURL(url);
|
|
39
|
+
}, 0);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Downloads a file by fetching the URL into a Blob first, then triggering a blob download.
|
|
44
|
+
*
|
|
45
|
+
* This is useful when a URL returns a previewable Content-Type (e.g. `application/pdf`) and
|
|
46
|
+
* the browser would otherwise open a new tab to preview instead of downloading.
|
|
47
|
+
*/ const downloadFileWithFetch = async ({ url, filename = new Date().getTime().toString(), fileExtension, fetchInit })=>{
|
|
48
|
+
const safeUrl = sanitizeUrlForError(url);
|
|
49
|
+
let response;
|
|
50
|
+
try {
|
|
51
|
+
response = await fetch(url, fetchInit);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new Error(`downloadFileWithFetch: fetch failed for ${safeUrl}`, {
|
|
54
|
+
cause: error
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new Error(`downloadFileWithFetch: failed to fetch ${safeUrl} (status ${response.status} ${response.statusText})`);
|
|
59
|
+
}
|
|
60
|
+
const blob = await response.blob();
|
|
61
|
+
downloadFile(blob, filename, fileExtension);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
exports.downloadFile = downloadFile;
|
|
65
|
+
exports.downloadFileWithFetch = downloadFileWithFetch;
|
package/dist/es/client.d.mts
CHANGED
|
@@ -286,6 +286,35 @@ declare function mergeRefs<T = any>(refs: Array<React__default.MutableRefObject<
|
|
|
286
286
|
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
287
287
|
*/
|
|
288
288
|
declare const downloadFile: (source: string | Blob, filename?: string, fileExtension?: string) => void;
|
|
289
|
+
type DownloadFileOptions = {
|
|
290
|
+
/**
|
|
291
|
+
* The url string to download.
|
|
292
|
+
*/
|
|
293
|
+
url: string;
|
|
294
|
+
/**
|
|
295
|
+
* The filename to use for the downloaded file (without extension)
|
|
296
|
+
*
|
|
297
|
+
* @default The current timestamp as a string (e.g. "1686789123456")
|
|
298
|
+
*/
|
|
299
|
+
filename?: string;
|
|
300
|
+
/**
|
|
301
|
+
* The file extension to append to the filename (e.g. "txt", "pdf").
|
|
302
|
+
*/
|
|
303
|
+
fileExtension?: string;
|
|
304
|
+
/**
|
|
305
|
+
* `fetch` init options (e.g. `credentials`, `headers`).
|
|
306
|
+
*
|
|
307
|
+
* Note: the URL must allow CORS (or be same-origin) for the browser to read the response.
|
|
308
|
+
*/
|
|
309
|
+
fetchInit?: RequestInit;
|
|
310
|
+
};
|
|
311
|
+
/**
|
|
312
|
+
* Downloads a file by fetching the URL into a Blob first, then triggering a blob download.
|
|
313
|
+
*
|
|
314
|
+
* This is useful when a URL returns a previewable Content-Type (e.g. `application/pdf`) and
|
|
315
|
+
* the browser would otherwise open a new tab to preview instead of downloading.
|
|
316
|
+
*/
|
|
317
|
+
declare const downloadFileWithFetch: ({ url, filename, fileExtension, fetchInit, }: DownloadFileOptions) => Promise<void>;
|
|
289
318
|
|
|
290
319
|
/**
|
|
291
320
|
* 從 localStorage 取得資料,支援槽狀取值(Json 物件)
|
|
@@ -307,5 +336,5 @@ declare const getLocalStorage: <T>(key: string, deCode?: boolean) => T | undefin
|
|
|
307
336
|
*/
|
|
308
337
|
declare const setLocalStorage: (key: string, value: Record<string, any>, enCode?: boolean) => void;
|
|
309
338
|
|
|
310
|
-
export { QueryProvider, createDataContext, createStoreContext, downloadFile, getLocalStorage, mergeRefs, setLocalStorage, useCountdown, useDisclosure, useHorizontalWheel, useQueryContext, useValue };
|
|
311
|
-
export type { TCountdownActions, UseDisclosureReturn };
|
|
339
|
+
export { QueryProvider, createDataContext, createStoreContext, downloadFile, downloadFileWithFetch, getLocalStorage, mergeRefs, setLocalStorage, useCountdown, useDisclosure, useHorizontalWheel, useQueryContext, useValue };
|
|
340
|
+
export type { DownloadFileOptions, TCountdownActions, UseDisclosureReturn };
|
package/dist/es/client.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import React, { useMemo, createContext, useContext, useState, useCallback } from
|
|
|
4
4
|
export { u as useCountdown } from './useCountdown-12s-t52WIHfq.mjs';
|
|
5
5
|
export { u as useDisclosure } from './useDisclosure-12s-BQAHpAXK.mjs';
|
|
6
6
|
export { u as useHorizontalWheel } from './useHorizontalWheel-12s-D3IfutV9.mjs';
|
|
7
|
-
export { d as downloadFile } from './download-12s-
|
|
7
|
+
export { d as downloadFile, a as downloadFileWithFetch } from './download-12s-DtxLfLpr.mjs';
|
|
8
8
|
export { g as getLocalStorage, s as setLocalStorage } from './webStorage-12s-Bo7x8q5t.mjs';
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
const sanitizeUrlForError = (input)=>{
|
|
3
|
+
try {
|
|
4
|
+
const parsed = new URL(input);
|
|
5
|
+
return `${parsed.origin}${parsed.pathname}`;
|
|
6
|
+
} catch {
|
|
7
|
+
return input.split('#')[0].split('?')[0];
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Downloads a file from a given source.
|
|
12
|
+
*
|
|
13
|
+
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
14
|
+
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
15
|
+
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
19
|
+
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
20
|
+
*/ const downloadFile = (source, filename = new Date().getTime().toString(), fileExtension)=>{
|
|
21
|
+
const shouldRevokeObjectUrl = typeof source !== 'string';
|
|
22
|
+
const url = shouldRevokeObjectUrl ? URL.createObjectURL(source) : source;
|
|
23
|
+
const link = document.createElement('a');
|
|
24
|
+
link.id = `download-${new Date().getTime()}`;
|
|
25
|
+
document.body.appendChild(link);
|
|
26
|
+
if (!fileExtension) {
|
|
27
|
+
link.download = filename;
|
|
28
|
+
} else {
|
|
29
|
+
link.download = `${filename}.${fileExtension}`;
|
|
30
|
+
}
|
|
31
|
+
link.href = url;
|
|
32
|
+
link.target = '_blank';
|
|
33
|
+
link.click();
|
|
34
|
+
link.remove();
|
|
35
|
+
if (shouldRevokeObjectUrl) {
|
|
36
|
+
// Defer revocation to avoid interfering with the download in some browsers.
|
|
37
|
+
setTimeout(()=>{
|
|
38
|
+
URL.revokeObjectURL(url);
|
|
39
|
+
}, 0);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Downloads a file by fetching the URL into a Blob first, then triggering a blob download.
|
|
44
|
+
*
|
|
45
|
+
* This is useful when a URL returns a previewable Content-Type (e.g. `application/pdf`) and
|
|
46
|
+
* the browser would otherwise open a new tab to preview instead of downloading.
|
|
47
|
+
*/ const downloadFileWithFetch = async ({ url, filename = new Date().getTime().toString(), fileExtension, fetchInit })=>{
|
|
48
|
+
const safeUrl = sanitizeUrlForError(url);
|
|
49
|
+
let response;
|
|
50
|
+
try {
|
|
51
|
+
response = await fetch(url, fetchInit);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new Error(`downloadFileWithFetch: fetch failed for ${safeUrl}`, {
|
|
54
|
+
cause: error
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new Error(`downloadFileWithFetch: failed to fetch ${safeUrl} (status ${response.status} ${response.statusText})`);
|
|
59
|
+
}
|
|
60
|
+
const blob = await response.blob();
|
|
61
|
+
downloadFile(blob, filename, fileExtension);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { downloadFileWithFetch as a, downloadFile as d };
|
package/package.json
CHANGED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
/**
|
|
3
|
-
* Downloads a file from a given source.
|
|
4
|
-
*
|
|
5
|
-
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
6
|
-
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
7
|
-
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
11
|
-
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
12
|
-
*/ const downloadFile = (source, filename = new Date().getTime().toString(), fileExtension)=>{
|
|
13
|
-
const url = typeof source === 'string' ? source : URL.createObjectURL(source);
|
|
14
|
-
const link = document.createElement('a');
|
|
15
|
-
link.id = `download-${new Date().getTime()}`;
|
|
16
|
-
document.body.appendChild(link);
|
|
17
|
-
if (!fileExtension) {
|
|
18
|
-
link.download = filename;
|
|
19
|
-
} else {
|
|
20
|
-
link.download = `${filename}.${fileExtension}`;
|
|
21
|
-
}
|
|
22
|
-
link.href = url;
|
|
23
|
-
link.target = '_blank';
|
|
24
|
-
link.click();
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
exports.downloadFile = downloadFile;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
/**
|
|
3
|
-
* Downloads a file from a given source.
|
|
4
|
-
*
|
|
5
|
-
* @param source - The source of the file to be downloaded. It can be a URL string or a Blob object.
|
|
6
|
-
* @param filename - The name of the file to be downloaded. Defaults to the current timestamp if not provided.
|
|
7
|
-
* @param fileExtension - The file extension to be appended to the filename. Optional.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* downloadFile('http://example.com/file.txt', 'testfile', 'txt');
|
|
11
|
-
* downloadFile(new Blob(['test content'], { type: 'text/plain' }), 'testfile', 'txt');
|
|
12
|
-
*/ const downloadFile = (source, filename = new Date().getTime().toString(), fileExtension)=>{
|
|
13
|
-
const url = typeof source === 'string' ? source : URL.createObjectURL(source);
|
|
14
|
-
const link = document.createElement('a');
|
|
15
|
-
link.id = `download-${new Date().getTime()}`;
|
|
16
|
-
document.body.appendChild(link);
|
|
17
|
-
if (!fileExtension) {
|
|
18
|
-
link.download = filename;
|
|
19
|
-
} else {
|
|
20
|
-
link.download = `${filename}.${fileExtension}`;
|
|
21
|
-
}
|
|
22
|
-
link.href = url;
|
|
23
|
-
link.target = '_blank';
|
|
24
|
-
link.click();
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export { downloadFile as d };
|