hsu-utils 0.0.36 → 0.0.38

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,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.35
3
+ * hsu-utils v0.0.37
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1 +1 @@
1
- export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): void;
1
+ export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
@@ -8,10 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { Typeof } from '..';
11
- function downloadFileByUrl(url, fileName) {
11
+ function downloadFileByUrl(url, fileName, signal) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  try {
14
- const response = yield fetch(url);
14
+ const response = yield fetch(url, { signal });
15
15
  const arrayBuffer = yield response.arrayBuffer();
16
16
  downloadFile(arrayBuffer, fileName);
17
17
  }
@@ -25,18 +25,21 @@ function downloadFileByUrl(url, fileName) {
25
25
  }
26
26
  });
27
27
  }
28
- export default function downloadFile(file, fileName) {
29
- if (typeof file === 'string') {
30
- downloadFileByUrl(file, fileName);
31
- return;
32
- }
33
- const blob = Typeof(file) === 'blob' ? file : new Blob([file]);
34
- const downloadElement = document.createElement('a');
35
- const href = window.URL.createObjectURL(blob);
36
- downloadElement.href = href;
37
- downloadElement.download = decodeURIComponent(fileName || '');
38
- document.body.appendChild(downloadElement);
39
- downloadElement.click();
40
- document.body.removeChild(downloadElement);
41
- window.URL.revokeObjectURL(href);
28
+ export default function downloadFile(file, fileName, signal) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ if (typeof file === 'string') {
31
+ yield downloadFileByUrl(file, fileName, signal);
32
+ }
33
+ else {
34
+ const blob = Typeof(file) === 'blob' ? file : new Blob([file]);
35
+ const downloadElement = document.createElement('a');
36
+ const href = window.URL.createObjectURL(blob);
37
+ downloadElement.href = href;
38
+ downloadElement.download = decodeURIComponent(fileName || '');
39
+ document.body.appendChild(downloadElement);
40
+ downloadElement.click();
41
+ document.body.removeChild(downloadElement);
42
+ window.URL.revokeObjectURL(href);
43
+ }
44
+ });
42
45
  }
@@ -1 +1 @@
1
- export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): void;
1
+ export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
@@ -37,14 +37,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var __1 = require("..");
40
- function downloadFileByUrl(url, fileName) {
40
+ function downloadFileByUrl(url, fileName, signal) {
41
41
  return __awaiter(this, void 0, void 0, function () {
42
42
  var response, arrayBuffer, _a, downloadElement;
43
43
  return __generator(this, function (_b) {
44
44
  switch (_b.label) {
45
45
  case 0:
46
46
  _b.trys.push([0, 3, , 4]);
47
- return [4, fetch(url)];
47
+ return [4, fetch(url, { signal: signal })];
48
48
  case 1:
49
49
  response = _b.sent();
50
50
  return [4, response.arrayBuffer()];
@@ -66,19 +66,31 @@ function downloadFileByUrl(url, fileName) {
66
66
  });
67
67
  });
68
68
  }
69
- function downloadFile(file, fileName) {
70
- if (typeof file === 'string') {
71
- downloadFileByUrl(file, fileName);
72
- return;
73
- }
74
- var blob = (0, __1.Typeof)(file) === 'blob' ? file : new Blob([file]);
75
- var downloadElement = document.createElement('a');
76
- var href = window.URL.createObjectURL(blob);
77
- downloadElement.href = href;
78
- downloadElement.download = decodeURIComponent(fileName || '');
79
- document.body.appendChild(downloadElement);
80
- downloadElement.click();
81
- document.body.removeChild(downloadElement);
82
- window.URL.revokeObjectURL(href);
69
+ function downloadFile(file, fileName, signal) {
70
+ return __awaiter(this, void 0, void 0, function () {
71
+ var blob, downloadElement, href;
72
+ return __generator(this, function (_a) {
73
+ switch (_a.label) {
74
+ case 0:
75
+ if (!(typeof file === 'string')) return [3, 2];
76
+ return [4, downloadFileByUrl(file, fileName, signal)];
77
+ case 1:
78
+ _a.sent();
79
+ return [3, 3];
80
+ case 2:
81
+ blob = (0, __1.Typeof)(file) === 'blob' ? file : new Blob([file]);
82
+ downloadElement = document.createElement('a');
83
+ href = window.URL.createObjectURL(blob);
84
+ downloadElement.href = href;
85
+ downloadElement.download = decodeURIComponent(fileName || '');
86
+ document.body.appendChild(downloadElement);
87
+ downloadElement.click();
88
+ document.body.removeChild(downloadElement);
89
+ window.URL.revokeObjectURL(href);
90
+ _a.label = 3;
91
+ case 3: return [2];
92
+ }
93
+ });
94
+ });
83
95
  }
84
96
  exports.default = downloadFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",