-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Media Utils: Add TypeScript support and export more utils (#64784)
Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
- Loading branch information
1 parent
8b452c2
commit f4cad96
Showing
27 changed files
with
1,074 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Determines whether the passed argument appears to be a plain object. | ||
* | ||
* @param data The object to inspect. | ||
*/ | ||
function isPlainObject( data: unknown ): data is Record< string, unknown > { | ||
return ( | ||
data !== null && | ||
typeof data === 'object' && | ||
Object.getPrototypeOf( data ) === Object.prototype | ||
); | ||
} | ||
|
||
/** | ||
* 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 ( isPlainObject( data ) ) { | ||
for ( const [ name, value ] of Object.entries( data ) ) { | ||
flattenFormData( formData, `${ key }[${ name }]`, value ); | ||
} | ||
} else if ( data !== undefined ) { | ||
formData.append( key, String( data ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }` | ||
), | ||
]; | ||
} | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { flattenFormData } from '../flatten-form-data'; | ||
|
||
describe( 'flattenFormData', () => { | ||
it( 'should flatten nested data structure', () => { | ||
const data = new FormData(); | ||
|
||
class RichTextData { | ||
toString() { | ||
return 'i am rich text'; | ||
} | ||
} | ||
|
||
const additionalData = { | ||
foo: null, | ||
bar: 1234, | ||
meta: { | ||
nested: 'foo', | ||
dothis: true, | ||
dothat: false, | ||
supermeta: { | ||
nested: 'baz', | ||
}, | ||
}, | ||
customClass: new RichTextData(), | ||
}; | ||
|
||
for ( const [ key, value ] of Object.entries( additionalData ) ) { | ||
flattenFormData( | ||
data, | ||
key, | ||
value as Parameters< typeof flattenFormData >[ 2 ] | ||
); | ||
} | ||
|
||
const actual = Object.fromEntries( data.entries() ); | ||
expect( actual ).toStrictEqual( { | ||
bar: '1234', | ||
foo: 'null', | ||
'meta[dothat]': 'false', | ||
'meta[dothis]': 'true', | ||
'meta[nested]': 'foo', | ||
'meta[supermeta][nested]': 'baz', | ||
customClass: 'i am rich text', | ||
} ); | ||
} ); | ||
} ); |
47 changes: 47 additions & 0 deletions
47
packages/media-utils/src/utils/test/get-mime-types-array.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |
Oops, something went wrong.