hsu-utils 0.0.37 → 0.0.39

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.36
3
+ * hsu-utils v0.0.38
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1 +1 @@
1
- export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): Promise<void>;
1
+ export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
@@ -8,27 +8,29 @@ 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
  }
18
- catch (_a) {
19
- const downloadElement = document.createElement('a');
20
- downloadElement.href = url;
21
- downloadElement.download = decodeURIComponent(fileName || '');
22
- document.body.appendChild(downloadElement);
23
- downloadElement.click();
24
- document.body.removeChild(downloadElement);
18
+ catch (error) {
19
+ if ((error === null || error === void 0 ? void 0 : error.name) !== 'AbortError') {
20
+ const downloadElement = document.createElement('a');
21
+ downloadElement.href = url;
22
+ downloadElement.download = decodeURIComponent(fileName || '');
23
+ document.body.appendChild(downloadElement);
24
+ downloadElement.click();
25
+ document.body.removeChild(downloadElement);
26
+ }
25
27
  }
26
28
  });
27
29
  }
28
- export default function downloadFile(file, fileName) {
30
+ export default function downloadFile(file, fileName, signal) {
29
31
  return __awaiter(this, void 0, void 0, function* () {
30
32
  if (typeof file === 'string') {
31
- yield downloadFileByUrl(file, fileName);
33
+ yield downloadFileByUrl(file, fileName, signal);
32
34
  }
33
35
  else {
34
36
  const blob = Typeof(file) === 'blob' ? file : new Blob([file]);
@@ -1 +1 @@
1
- export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string): Promise<void>;
1
+ export default function downloadFile(file: ArrayBuffer | Blob | string, fileName?: string, signal?: AbortSignal): Promise<void>;
@@ -37,43 +37,45 @@ 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
- var response, arrayBuffer, _a, downloadElement;
43
- return __generator(this, function (_b) {
44
- switch (_b.label) {
42
+ var response, arrayBuffer, error_1, downloadElement;
43
+ return __generator(this, function (_a) {
44
+ switch (_a.label) {
45
45
  case 0:
46
- _b.trys.push([0, 3, , 4]);
47
- return [4, fetch(url)];
46
+ _a.trys.push([0, 3, , 4]);
47
+ return [4, fetch(url, { signal: signal })];
48
48
  case 1:
49
- response = _b.sent();
49
+ response = _a.sent();
50
50
  return [4, response.arrayBuffer()];
51
51
  case 2:
52
- arrayBuffer = _b.sent();
52
+ arrayBuffer = _a.sent();
53
53
  downloadFile(arrayBuffer, fileName);
54
54
  return [3, 4];
55
55
  case 3:
56
- _a = _b.sent();
57
- downloadElement = document.createElement('a');
58
- downloadElement.href = url;
59
- downloadElement.download = decodeURIComponent(fileName || '');
60
- document.body.appendChild(downloadElement);
61
- downloadElement.click();
62
- document.body.removeChild(downloadElement);
56
+ error_1 = _a.sent();
57
+ if ((error_1 === null || error_1 === void 0 ? void 0 : error_1.name) !== 'AbortError') {
58
+ downloadElement = document.createElement('a');
59
+ downloadElement.href = url;
60
+ downloadElement.download = decodeURIComponent(fileName || '');
61
+ document.body.appendChild(downloadElement);
62
+ downloadElement.click();
63
+ document.body.removeChild(downloadElement);
64
+ }
63
65
  return [3, 4];
64
66
  case 4: return [2];
65
67
  }
66
68
  });
67
69
  });
68
70
  }
69
- function downloadFile(file, fileName) {
71
+ function downloadFile(file, fileName, signal) {
70
72
  return __awaiter(this, void 0, void 0, function () {
71
73
  var blob, downloadElement, href;
72
74
  return __generator(this, function (_a) {
73
75
  switch (_a.label) {
74
76
  case 0:
75
77
  if (!(typeof file === 'string')) return [3, 2];
76
- return [4, downloadFileByUrl(file, fileName)];
78
+ return [4, downloadFileByUrl(file, fileName, signal)];
77
79
  case 1:
78
80
  _a.sent();
79
81
  return [3, 3];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",