diff --git a/packages/blob/src/index.js b/packages/blob/src/index.ts similarity index 82% rename from packages/blob/src/index.js rename to packages/blob/src/index.ts index 2493f81fc4c65..5f55d16c3f754 100644 --- a/packages/blob/src/index.js +++ b/packages/blob/src/index.ts @@ -1,7 +1,9 @@ +type BlobPart = BufferSource | Blob | string; + /** * @type {Record} */ -const cache = {}; +const cache: Record< string, File | undefined > = {}; /** * Create a blob URL from a file. @@ -10,7 +12,7 @@ const cache = {}; * * @return {string} The blob URL. */ -export function createBlobURL( file ) { +export function createBlobURL( file: File ): string { const url = window.URL.createObjectURL( file ); cache[ url ] = file; @@ -27,7 +29,7 @@ export function createBlobURL( file ) { * * @return {File|undefined} The file for the blob URL. */ -export function getBlobByURL( url ) { +export function getBlobByURL( url: string ): File | undefined { return cache[ url ]; } @@ -40,7 +42,7 @@ export function getBlobByURL( url ) { * * @return {string|undefined} The blob type. */ -export function getBlobTypeByURL( url ) { +export function getBlobTypeByURL( url: string ): string | undefined { return getBlobByURL( url )?.type.split( '/' )[ 0 ]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ). } @@ -49,7 +51,7 @@ export function getBlobTypeByURL( url ) { * * @param {string} url The blob URL. */ -export function revokeBlobURL( url ) { +export function revokeBlobURL( url: string ): void { if ( cache[ url ] ) { window.URL.revokeObjectURL( url ); } @@ -64,7 +66,7 @@ export function revokeBlobURL( url ) { * * @return {boolean} Is the url a blob url? */ -export function isBlobURL( url ) { +export function isBlobURL( url: string | undefined ): boolean { if ( ! url || ! url.indexOf ) { return false; } @@ -94,7 +96,11 @@ export function isBlobURL( url ) { * @param {BlobPart} content File content (BufferSource | Blob | string). * @param {string} contentType (Optional) File mime type. Default is `''`. */ -export function downloadBlob( filename, content, contentType = '' ) { +export function downloadBlob( + filename: string, + content: BlobPart, + contentType: string = '' +): void { if ( ! filename || ! content ) { return; } diff --git a/packages/blob/src/test/index.js b/packages/blob/src/test/index.ts similarity index 85% rename from packages/blob/src/test/index.js rename to packages/blob/src/test/index.ts index 47dcb5019ee25..04ba9700ce5b7 100644 --- a/packages/blob/src/test/index.js +++ b/packages/blob/src/test/index.ts @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { isBlobURL, getBlobTypeByURL, downloadBlob } from '../'; +import { isBlobURL, getBlobTypeByURL, downloadBlob } from '..'; describe( 'isBlobURL', () => { it( 'returns true if the url starts with "blob:"', () => { @@ -13,6 +13,8 @@ describe( 'isBlobURL', () => { } ); it( 'returns false if the url is not defined', () => { + // calling isBlobURL without a URL is not type compliant, so ignore it + // @ts-ignore expect( isBlobURL() ).toBe( false ); } ); } ); @@ -23,6 +25,8 @@ describe( 'getBlobTypeByURL', () => { } ); it( 'returns undefined if the url is not defined', () => { + // calling getBlobTypeByURL without a URL is not type compliant, so ignore it + // @ts-ignore expect( getBlobTypeByURL() ).toBe( undefined ); } ); } ); @@ -36,17 +40,18 @@ describe( 'downloadBlob', () => { const createElementSpy = jest .spyOn( global.document, 'createElement' ) .mockReturnValue( mockAnchorElement ); + const mockBlob = jest.fn(); - const blobSpy = jest.spyOn( window, 'Blob' ).mockReturnValue( mockBlob ); + const blobSpy = jest + .spyOn( window, 'Blob' ) + .mockReturnValue( mockBlob as unknown as Blob ); jest.spyOn( document.body, 'appendChild' ); jest.spyOn( document.body, 'removeChild' ); beforeEach( () => { // Can't seem to spy on these static methods. They are `undefined`. // Possibly overwritten: https://github.com/WordPress/gutenberg/blob/trunk/packages/jest-preset-default/scripts/setup-globals.js#L5 - window.URL = { - createObjectURL, - revokeObjectURL, - }; + window.URL.createObjectURL = createObjectURL; + window.URL.revokeObjectURL = revokeObjectURL; } ); afterAll( () => {