Skip to content

Commit

Permalink
Export more utils from media-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Aug 25, 2024
1 parent c48c800 commit 6a73fa4
Show file tree
Hide file tree
Showing 25 changed files with 957 additions and 312 deletions.
1 change: 1 addition & 0 deletions packages/editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{ "path": "../i18n" },
{ "path": "../icons" },
{ "path": "../keycodes" },
{ "path": "../media-utils" },
{ "path": "../notices" },
{ "path": "../plugins" },
{ "path": "../private-apis" },
Expand Down
4 changes: 4 additions & 0 deletions packages/media-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Rewrite in TypeScript, exporting all the individual utility functions.

## 5.6.0 (2024-08-21)

## 5.5.0 (2024-08-07)
Expand Down
3 changes: 2 additions & 1 deletion packages/media-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/url"
"directory": "packages/media-utils"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
Expand All @@ -25,6 +25,7 @@
},
"main": "build/index.js",
"module": "build-module/index.js",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/api-fetch": "file:../api-fetch",
Expand Down
2 changes: 0 additions & 2 deletions packages/media-utils/src/index.js

This file was deleted.

9 changes: 9 additions & 0 deletions packages/media-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './components';

export { uploadMedia } from './utils/upload-media';
export { transformAttachment } from './utils/transform-attachment';
export { validateFileSize } from './utils/validate-file-size';
export { validateMimeType } from './utils/validate-mime-type';
export { validateMimeTypeForUser } from './utils/validate-mime-type-for-user';

export type { Attachment, RestAttachment } from './utils/types';
29 changes: 29 additions & 0 deletions packages/media-utils/src/utils/flatten-form-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Recursively flatten data passed to form data, to allow using multi-level objects.
*
* @param {FormData} formData Form data object.
* @param {string} key Key to amend to form data object
* @param {string|Object} data Data to be amended to form data.
*/
export function flattenFormData(
formData: FormData,
key: string,
data: string | undefined | Record< string, string >
) {
if (
typeof data === 'object' &&
Object.getPrototypeOf( data ) === Object.prototype
) {
for ( const name in data ) {
if ( Object.prototype.hasOwnProperty.call( data, name ) ) {
flattenFormData(
formData,
`${ key }[${ name }]`,
data[ name ]
);
}
}
} else if ( data !== undefined ) {
formData.append( key, String( data ) );
}
}
29 changes: 29 additions & 0 deletions packages/media-utils/src/utils/get-mime-types-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Browsers may use unexpected mime types, and they differ from browser to browser.
* This function computes a flexible array of mime types from the mime type structured provided by the server.
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
*
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
* Extensions are keys separated by '|' and values are mime types associated with an extension.
*
* @return An array of mime types or null
*/
export function getMimeTypesArray(
wpMimeTypesObject?: Record< string, string > | null
) {
if ( ! wpMimeTypesObject ) {
return null;
}
return Object.entries( wpMimeTypesObject ).flatMap(
( [ extensionsString, mime ] ) => {
const [ type ] = mime.split( '/' );
const extensions = extensionsString.split( '|' );
return [
mime,
...extensions.map(
( extension ) => `${ type }/${ extension }`
),
];
}
);
}
1 change: 0 additions & 1 deletion packages/media-utils/src/utils/index.js

This file was deleted.

47 changes: 47 additions & 0 deletions packages/media-utils/src/utils/test/get-mime-types-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Internal dependencies
*/
import { getMimeTypesArray } from '../get-mime-types-array';

describe( 'getMimeTypesArray', () => {
it( 'should return null if it is "falsy" e.g: undefined or null', () => {
expect( getMimeTypesArray( null ) ).toEqual( null );
expect( getMimeTypesArray( undefined ) ).toEqual( null );
} );

it( 'should return an empty array if an empty object is passed', () => {
expect( getMimeTypesArray( {} ) ).toEqual( [] );
} );

it( 'should return the type plus a new mime type with type and subtype with the extension if a type is passed', () => {
expect( getMimeTypesArray( { ext: 'chicken' } ) ).toEqual( [
'chicken',
'chicken/ext',
] );
} );

it( 'should return the mime type passed and a new mime type with type and the extension as subtype', () => {
expect( getMimeTypesArray( { ext: 'chicken/ribs' } ) ).toEqual( [
'chicken/ribs',
'chicken/ext',
] );
} );

it( 'should return the mime type passed and an additional mime type per extension supported', () => {
expect( getMimeTypesArray( { 'jpg|jpeg|jpe': 'image/jpeg' } ) ).toEqual(
[ 'image/jpeg', 'image/jpg', 'image/jpeg', 'image/jpe' ]
);
} );

it( 'should handle multiple mime types', () => {
expect(
getMimeTypesArray( { 'ext|aaa': 'chicken/ribs', aaa: 'bbb' } )
).toEqual( [
'chicken/ribs',
'chicken/ext',
'chicken/aaa',
'bbb',
'bbb/aaa',
] );
} );
} );
24 changes: 24 additions & 0 deletions packages/media-utils/src/utils/test/upload-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Internal dependencies
*/
import { UploadError } from '../upload-error';

describe( 'UploadError', () => {
it( 'holds error code and file name', () => {
const file = new File( [], 'example.jpg', {
lastModified: 1234567891,
type: 'image/jpeg',
} );

const error = new UploadError( {
code: 'some_error',
message: 'An error occurred',
file,
} );

expect( error ).toStrictEqual( expect.any( Error ) );
expect( error.code ).toBe( 'some_error' );
expect( error.message ).toBe( 'An error occurred' );
expect( error.file ).toBe( file );
} );
} );
Loading

0 comments on commit 6a73fa4

Please sign in to comment.