Skip to content

Commit a2bad64

Browse files
author
Olavo Parno
committed
chore: update dependencies and improve type safety
1 parent 21c00f6 commit a2bad64

File tree

7 files changed

+3412
-2667
lines changed

7 files changed

+3412
-2667
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
| Statements | Branches | Functions | Lines |
1010
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
11-
| ![Statements](https://img.shields.io/badge/statements-87.38%25-yellow.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-73.17%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-81.81%25-yellow.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-88%25-yellow.svg?style=flat&logo=jest) |
11+
| ![Statements](https://img.shields.io/badge/statements-87.5%25-yellow.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-73.17%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-81.81%25-yellow.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-88.11%25-yellow.svg?style=flat&logo=jest) |
1212

1313
## Table of Contents
1414

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
]
4949
},
5050
"peerDependencies": {
51-
"react": "^17.0.2 || ^18"
51+
"react": "^17.0.2 || ^18.0.0 || ^19.0.0"
5252
},
5353
"devDependencies": {
5454
"@babel/core": "^7.16.12",
@@ -88,6 +88,6 @@
8888
"standard-version": "^9.3.2",
8989
"tslib": "^2.3.1",
9090
"typescript": "^4.5.5",
91-
"web-streams-polyfill": "^3.2.0"
91+
"web-streams-polyfill": "^4.1.0"
9292
}
9393
}

Diff for: src/__tests__/index.spec.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import {
44
ReadableStream,
55
ReadableStreamDefaultReadResult,
6-
} from 'web-streams-polyfill/ponyfill';
6+
} from 'web-streams-polyfill';
77
import { renderHook, act } from '@testing-library/react-hooks';
88
import useDownloader, { jsDownload } from '../index';
99
import { WindowDownloaderEmbedded } from '../types';
@@ -21,7 +21,8 @@ const expectedKeys = [
2121
beforeAll(() => {
2222
global.window.fetch = fetch as Extract<WindowOrWorkerGlobalScope, 'fetch'>;
2323
global.Response = Response;
24-
global.ReadableStream = ReadableStream;
24+
global.ReadableStream =
25+
ReadableStream as unknown as typeof global.ReadableStream;
2526
});
2627

2728
describe('useDownloader successes', () => {
@@ -240,10 +241,14 @@ describe('useDownloader failures', () => {
240241

241242
describe('Tests without msSaveBlob', () => {
242243
beforeAll(() => {
243-
(window as unknown as WindowDownloaderEmbedded).navigator.msSaveBlob =
244-
undefined;
245-
window.URL.createObjectURL = () => null;
246-
window.URL.revokeObjectURL = () => null;
244+
const currentWindow = window as unknown as WindowDownloaderEmbedded;
245+
246+
currentWindow.navigator.msSaveBlob = undefined;
247+
248+
if (currentWindow.URL) {
249+
currentWindow.URL.createObjectURL = () => null;
250+
currentWindow.URL.revokeObjectURL = () => null;
251+
}
247252
});
248253

249254
it('should test with URL and being revoked', async () => {
@@ -262,17 +267,22 @@ describe('useDownloader failures', () => {
262267
});
263268

264269
it('should test with URL via webkitURL', () => {
265-
window.URL = undefined;
266-
window.webkitURL.createObjectURL = () => null;
270+
const currentWindow = window as unknown as WindowDownloaderEmbedded;
267271

268-
const createObjectWebkitURLSpy = jest.spyOn(
269-
window.webkitURL,
270-
'createObjectURL'
271-
);
272+
currentWindow.URL = undefined;
272273

273-
jsDownload(new Blob(['abcde']), 'test');
274+
if (currentWindow.webkitURL) {
275+
currentWindow.webkitURL.createObjectURL = () => null;
276+
277+
const createObjectWebkitURLSpy = jest.spyOn(
278+
window.webkitURL,
279+
'createObjectURL'
280+
);
281+
282+
jsDownload(new Blob(['abcde']), 'test');
274283

275-
expect(createObjectWebkitURLSpy).toHaveBeenCalled();
284+
expect(createObjectWebkitURLSpy).toHaveBeenCalled();
285+
}
276286
});
277287
});
278288
});

Diff for: src/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ export const jsDownload = (
9898
type: mime || 'application/octet-stream',
9999
});
100100

101+
const currentWindow = window as unknown as WindowDownloaderEmbedded;
102+
101103
if (
102-
typeof (window as unknown as WindowDownloaderEmbedded).navigator
104+
typeof currentWindow.navigator
103105
.msSaveBlob !== 'undefined'
104106
) {
105-
return (window as unknown as WindowDownloaderEmbedded).navigator.msSaveBlob(
107+
return currentWindow.navigator.msSaveBlob(
106108
blob,
107109
filename
108110
);
@@ -168,8 +170,8 @@ export default function useDownloader(
168170
'The user aborted a request.': 'Download timed out',
169171
};
170172
setError(() => {
171-
const resolvedError = errorMap[err.message]
172-
? errorMap[err.message]
173+
const resolvedError = errorMap[err.message as keyof typeof errorMap]
174+
? errorMap[err.message as keyof typeof errorMap]
173175
: err.message;
174176

175177
return { errorMessage: resolvedError };

Diff for: src/types.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,16 @@ export interface ResolverProps {
7979
}
8080

8181
interface CustomNavigator extends Navigator {
82-
msSaveBlob: (blob?: Blob, filename?: string) => boolean | NodeJS.Timeout;
82+
msSaveBlob?: (blob?: Blob, filename?: string) => boolean | NodeJS.Timeout;
83+
}
84+
85+
interface CustomURL extends URL {
86+
createObjectURL: (blob: Blob) => string | null;
87+
revokeObjectURL: (url: string) => void | null;
8388
}
8489

8590
export interface WindowDownloaderEmbedded extends Window {
8691
navigator: CustomNavigator;
92+
URL?: CustomURL
93+
webkitURL?: CustomURL
8794
}

Diff for: tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"noImplicitThis": true,
1818
"noImplicitAny": true,
1919
"strictNullChecks": true,
20-
"suppressImplicitAnyIndexErrors": true,
2120
"noUnusedLocals": true,
2221
"noUnusedParameters": true,
2322
"skipLibCheck": true,

0 commit comments

Comments
 (0)