Skip to content

Commit

Permalink
Blob: Convert package to TypeScript (WordPress#62569)
Browse files Browse the repository at this point in the history
Co-authored-by: jpstevens <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: up1512001 <[email protected]>
  • Loading branch information
5 people authored and patil-vipul committed Jun 17, 2024
1 parent 5bf007d commit 3c52da7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
4 changes: 4 additions & 0 deletions packages/blob/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- Refactor to TypeScript ([#62569](https://github.com/WordPress/gutenberg/pull/62569)).

## 4.0.0 (2024-05-31)

### Breaking Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/blob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _Parameters_

_Returns_

- `File|undefined`: The file for the blob URL.
- `File | undefined`: The file for the blob URL.

### getBlobTypeByURL

Expand All @@ -73,15 +73,15 @@ _Parameters_

_Returns_

- `string|undefined`: The blob type.
- `string | undefined`: The blob type.

### isBlobURL

Check whether a url is a blob url.

_Parameters_

- _url_ `string|undefined`: The URL.
- _url_ `string | undefined`: The URL.

_Returns_

Expand Down
45 changes: 23 additions & 22 deletions packages/blob/src/index.js → packages/blob/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/**
* @type {Record<string, File|undefined>}
*/
const cache = {};
const cache: Record< string, File > = {};

/**
* Create a blob URL from a file.
*
* @param {File} file The file to create a blob URL for.
* @param file The file to create a blob URL for.
*
* @return {string} The blob URL.
* @return The blob URL.
*/
export function createBlobURL( file ) {
export function createBlobURL( file: File ): string {
const url = window.URL.createObjectURL( file );

cache[ url ] = file;
Expand All @@ -23,11 +20,11 @@ export function createBlobURL( file ) {
* `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
* `undefined`.
*
* @param {string} url The blob URL.
* @param url The blob URL.
*
* @return {File|undefined} The file for the blob URL.
* @return The file for the blob URL.
*/
export function getBlobByURL( url ) {
export function getBlobByURL( url: string ): File | undefined {
return cache[ url ];
}

Expand All @@ -36,20 +33,20 @@ export function getBlobByURL( url ) {
* `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
* `undefined`.
*
* @param {string} url The blob URL.
* @param url The blob URL.
*
* @return {string|undefined} The blob type.
* @return 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' ).
}

/**
* Remove the resource and file cache from memory.
*
* @param {string} url The blob URL.
* @param url The blob URL.
*/
export function revokeBlobURL( url ) {
export function revokeBlobURL( url: string ): void {
if ( cache[ url ] ) {
window.URL.revokeObjectURL( url );
}
Expand All @@ -60,11 +57,11 @@ export function revokeBlobURL( url ) {
/**
* Check whether a url is a blob url.
*
* @param {string|undefined} url The URL.
* @param url The URL.
*
* @return {boolean} Is the url a blob url?
* @return Is the url a blob url?
*/
export function isBlobURL( url ) {
export function isBlobURL( url: string | undefined ): boolean {
if ( ! url || ! url.indexOf ) {
return false;
}
Expand All @@ -90,11 +87,15 @@ export function isBlobURL( url ) {
* downloadBlob( filename, fileContent, 'application/json' );
* ```
*
* @param {string} filename File name.
* @param {BlobPart} content File content (BufferSource | Blob | string).
* @param {string} contentType (Optional) File mime type. Default is `''`.
* @param filename File name.
* @param content File content (BufferSource | Blob | string).
* @param 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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:"', () => {
Expand All @@ -13,17 +13,23 @@ describe( 'isBlobURL', () => {
} );

it( 'returns false if the url is not defined', () => {
expect( isBlobURL() ).toBe( false );
expect(
// @ts-expect-error This is not a valid call according to types.
isBlobURL()
).toBe( false );
} );
} );

describe( 'getBlobTypeByURL', () => {
it( 'returns undefined if the blob is not found', () => {
expect( getBlobTypeByURL( 'blob:notexisting' ) ).toBe( undefined );
expect( getBlobTypeByURL( 'blob:notexisting' ) ).toBeUndefined();
} );

it( 'returns undefined if the url is not defined', () => {
expect( getBlobTypeByURL() ).toBe( undefined );
expect(
// @ts-expect-error This is not a valid call according to types.
getBlobTypeByURL()
).toBeUndefined();
} );
} );

Expand All @@ -36,13 +42,17 @@ 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
// @ts-expect-error This is not a valid URL object.
window.URL = {
createObjectURL,
revokeObjectURL,
Expand Down

0 comments on commit 3c52da7

Please sign in to comment.