@etsoo/shared 1.1.85 → 1.1.86

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/README.md CHANGED
@@ -191,6 +191,7 @@ DOM/window related utilities
191
191
  |detectedCountry|Current detected country|
192
192
  |detectedCulture|Current detected culture|
193
193
  |dimensionEqual|Check two rectangles equality|
194
+ |downloadFile|Download file from API fetch response body|
194
195
  |fileToDataURL|File to data URL|
195
196
  |formDataToObject|Form data to object|
196
197
  |getCulture|Get the available culture definition|
@@ -1,3 +1,4 @@
1
+ /// <reference lib="dom" />
1
2
  import { DataTypes } from './DataTypes';
2
3
  import { FormDataFieldValue, IFormData } from './types/FormData';
3
4
  /**
@@ -50,6 +51,13 @@ export declare namespace DomUtils {
50
51
  * @param d2 Dimension 2
51
52
  */
52
53
  function dimensionEqual(d1?: DOMRect, d2?: DOMRect): boolean;
54
+ /**
55
+ * Download file from API fetch response body
56
+ * @param data Data
57
+ * @param suggestedName Suggested file name
58
+ * @param autoDetect Auto detect, false will use link click way
59
+ */
60
+ function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<void>;
53
61
  /**
54
62
  * File to data URL
55
63
  * @param file File
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.DomUtils = void 0;
13
+ /// <reference lib="dom" />
13
14
  const DataTypes_1 = require("./DataTypes");
14
15
  if (typeof navigator === 'undefined') {
15
16
  // Test mock only
@@ -231,6 +232,48 @@ var DomUtils;
231
232
  return false;
232
233
  }
233
234
  DomUtils.dimensionEqual = dimensionEqual;
235
+ /**
236
+ * Download file from API fetch response body
237
+ * @param data Data
238
+ * @param suggestedName Suggested file name
239
+ * @param autoDetect Auto detect, false will use link click way
240
+ */
241
+ function downloadFile(data, suggestedName, autoDetect = true) {
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ try {
244
+ if (autoDetect && 'showSaveFilePicker' in globalThis) {
245
+ const handle = yield globalThis.showSaveFilePicker({
246
+ suggestedName
247
+ });
248
+ const stream = yield handle.createWritable();
249
+ if (data instanceof Blob) {
250
+ data.stream().pipeTo(stream);
251
+ }
252
+ else {
253
+ yield data.pipeTo(stream);
254
+ }
255
+ }
256
+ else {
257
+ const url = window.URL.createObjectURL(data instanceof Blob
258
+ ? data
259
+ : yield new Response(data).blob());
260
+ const a = document.createElement('a');
261
+ a.style.display = 'none';
262
+ a.href = url;
263
+ if (suggestedName)
264
+ a.download = suggestedName;
265
+ document.body.appendChild(a);
266
+ a.click();
267
+ a.remove();
268
+ window.URL.revokeObjectURL(url);
269
+ }
270
+ }
271
+ catch (e) {
272
+ console.log(e);
273
+ }
274
+ });
275
+ }
276
+ DomUtils.downloadFile = downloadFile;
234
277
  /**
235
278
  * File to data URL
236
279
  * @param file File
@@ -1,3 +1,4 @@
1
+ /// <reference lib="dom" />
1
2
  import { DataTypes } from './DataTypes';
2
3
  import { FormDataFieldValue, IFormData } from './types/FormData';
3
4
  /**
@@ -50,6 +51,13 @@ export declare namespace DomUtils {
50
51
  * @param d2 Dimension 2
51
52
  */
52
53
  function dimensionEqual(d1?: DOMRect, d2?: DOMRect): boolean;
54
+ /**
55
+ * Download file from API fetch response body
56
+ * @param data Data
57
+ * @param suggestedName Suggested file name
58
+ * @param autoDetect Auto detect, false will use link click way
59
+ */
60
+ function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<void>;
53
61
  /**
54
62
  * File to data URL
55
63
  * @param file File
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ /// <reference lib="dom" />
10
11
  import { DataTypes } from './DataTypes';
11
12
  if (typeof navigator === 'undefined') {
12
13
  // Test mock only
@@ -228,6 +229,48 @@ export var DomUtils;
228
229
  return false;
229
230
  }
230
231
  DomUtils.dimensionEqual = dimensionEqual;
232
+ /**
233
+ * Download file from API fetch response body
234
+ * @param data Data
235
+ * @param suggestedName Suggested file name
236
+ * @param autoDetect Auto detect, false will use link click way
237
+ */
238
+ function downloadFile(data, suggestedName, autoDetect = true) {
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ try {
241
+ if (autoDetect && 'showSaveFilePicker' in globalThis) {
242
+ const handle = yield globalThis.showSaveFilePicker({
243
+ suggestedName
244
+ });
245
+ const stream = yield handle.createWritable();
246
+ if (data instanceof Blob) {
247
+ data.stream().pipeTo(stream);
248
+ }
249
+ else {
250
+ yield data.pipeTo(stream);
251
+ }
252
+ }
253
+ else {
254
+ const url = window.URL.createObjectURL(data instanceof Blob
255
+ ? data
256
+ : yield new Response(data).blob());
257
+ const a = document.createElement('a');
258
+ a.style.display = 'none';
259
+ a.href = url;
260
+ if (suggestedName)
261
+ a.download = suggestedName;
262
+ document.body.appendChild(a);
263
+ a.click();
264
+ a.remove();
265
+ window.URL.revokeObjectURL(url);
266
+ }
267
+ }
268
+ catch (e) {
269
+ console.log(e);
270
+ }
271
+ });
272
+ }
273
+ DomUtils.downloadFile = downloadFile;
231
274
  /**
232
275
  * File to data URL
233
276
  * @param file File
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.85",
3
+ "version": "1.1.86",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/DomUtils.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference lib="dom" />
1
2
  import { DataTypes } from './DataTypes';
2
3
  import { FormDataFieldValue, IFormData } from './types/FormData';
3
4
 
@@ -270,6 +271,53 @@ export namespace DomUtils {
270
271
  return false;
271
272
  }
272
273
 
274
+ /**
275
+ * Download file from API fetch response body
276
+ * @param data Data
277
+ * @param suggestedName Suggested file name
278
+ * @param autoDetect Auto detect, false will use link click way
279
+ */
280
+ export async function downloadFile(
281
+ data: ReadableStream | Blob,
282
+ suggestedName?: string,
283
+ autoDetect: boolean = true
284
+ ) {
285
+ try {
286
+ if (autoDetect && 'showSaveFilePicker' in globalThis) {
287
+ const handle = await (globalThis as any).showSaveFilePicker({
288
+ suggestedName
289
+ });
290
+
291
+ const stream = await handle.createWritable();
292
+
293
+ if (data instanceof Blob) {
294
+ data.stream().pipeTo(stream);
295
+ } else {
296
+ await data.pipeTo(stream);
297
+ }
298
+ } else {
299
+ const url = window.URL.createObjectURL(
300
+ data instanceof Blob
301
+ ? data
302
+ : await new Response(data).blob()
303
+ );
304
+
305
+ const a = document.createElement('a');
306
+ a.style.display = 'none';
307
+ a.href = url;
308
+ if (suggestedName) a.download = suggestedName;
309
+
310
+ document.body.appendChild(a);
311
+ a.click();
312
+ a.remove();
313
+
314
+ window.URL.revokeObjectURL(url);
315
+ }
316
+ } catch (e) {
317
+ console.log(e);
318
+ }
319
+ }
320
+
273
321
  /**
274
322
  * File to data URL
275
323
  * @param file File