@wordpress/media-utils 5.7.0 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +70 -1
  3. package/build/index.js +43 -11
  4. package/build/index.js.map +1 -1
  5. package/build/utils/flatten-form-data.js +32 -0
  6. package/build/utils/flatten-form-data.js.map +1 -0
  7. package/build/utils/get-mime-types-array.js +27 -0
  8. package/build/utils/get-mime-types-array.js.map +1 -0
  9. package/build/utils/transform-attachment.js +33 -0
  10. package/build/utils/transform-attachment.js.map +1 -0
  11. package/build/utils/types.js +6 -0
  12. package/build/utils/types.js.map +1 -0
  13. package/build/utils/upload-error.js +29 -0
  14. package/build/utils/upload-error.js.map +1 -0
  15. package/build/utils/upload-media.js +53 -152
  16. package/build/utils/upload-media.js.map +1 -1
  17. package/build/utils/upload-to-server.js +34 -0
  18. package/build/utils/upload-to-server.js.map +1 -0
  19. package/build/utils/validate-file-size.js +44 -0
  20. package/build/utils/validate-file-size.js.map +1 -0
  21. package/build/utils/validate-mime-type-for-user.js +41 -0
  22. package/build/utils/validate-mime-type-for-user.js.map +1 -0
  23. package/build/utils/validate-mime-type.js +47 -0
  24. package/build/utils/validate-mime-type.js.map +1 -0
  25. package/build-module/index.js +5 -1
  26. package/build-module/index.js.map +1 -1
  27. package/build-module/utils/flatten-form-data.js +26 -0
  28. package/build-module/utils/flatten-form-data.js.map +1 -0
  29. package/build-module/utils/get-mime-types-array.js +21 -0
  30. package/build-module/utils/get-mime-types-array.js.map +1 -0
  31. package/build-module/utils/transform-attachment.js +27 -0
  32. package/build-module/utils/transform-attachment.js.map +1 -0
  33. package/build-module/utils/types.js +2 -0
  34. package/build-module/utils/types.js.map +1 -0
  35. package/build-module/utils/upload-error.js +22 -0
  36. package/build-module/utils/upload-error.js.map +1 -0
  37. package/build-module/utils/upload-media.js +53 -149
  38. package/build-module/utils/upload-media.js.map +1 -1
  39. package/build-module/utils/upload-to-server.js +26 -0
  40. package/build-module/utils/upload-to-server.js.map +1 -0
  41. package/build-module/utils/validate-file-size.js +38 -0
  42. package/build-module/utils/validate-file-size.js.map +1 -0
  43. package/build-module/utils/validate-mime-type-for-user.js +35 -0
  44. package/build-module/utils/validate-mime-type-for-user.js.map +1 -0
  45. package/build-module/utils/validate-mime-type.js +41 -0
  46. package/build-module/utils/validate-mime-type.js.map +1 -0
  47. package/build-types/components/index.d.ts +2 -0
  48. package/build-types/components/index.d.ts.map +1 -0
  49. package/build-types/components/media-upload/index.d.ts +30 -0
  50. package/build-types/components/media-upload/index.d.ts.map +1 -0
  51. package/build-types/index.d.ts +8 -0
  52. package/build-types/index.d.ts.map +1 -0
  53. package/build-types/utils/flatten-form-data.d.ts +9 -0
  54. package/build-types/utils/flatten-form-data.d.ts.map +1 -0
  55. package/build-types/utils/get-mime-types-array.d.ts +12 -0
  56. package/build-types/utils/get-mime-types-array.d.ts.map +1 -0
  57. package/build-types/utils/transform-attachment.d.ts +11 -0
  58. package/build-types/utils/transform-attachment.d.ts.map +1 -0
  59. package/build-types/utils/types.d.ts +199 -0
  60. package/build-types/utils/types.d.ts.map +1 -0
  61. package/build-types/utils/upload-error.d.ts +19 -0
  62. package/build-types/utils/upload-error.d.ts.map +1 -0
  63. package/build-types/utils/upload-media.d.ts +32 -0
  64. package/build-types/utils/upload-media.d.ts.map +1 -0
  65. package/build-types/utils/upload-to-server.d.ts +3 -0
  66. package/build-types/utils/upload-to-server.d.ts.map +1 -0
  67. package/build-types/utils/validate-file-size.d.ts +8 -0
  68. package/build-types/utils/validate-file-size.d.ts.map +1 -0
  69. package/build-types/utils/validate-mime-type-for-user.d.ts +8 -0
  70. package/build-types/utils/validate-mime-type-for-user.d.ts.map +1 -0
  71. package/build-types/utils/validate-mime-type.d.ts +8 -0
  72. package/build-types/utils/validate-mime-type.d.ts.map +1 -0
  73. package/package.json +8 -7
  74. package/src/index.ts +9 -0
  75. package/src/utils/flatten-form-data.ts +33 -0
  76. package/src/utils/get-mime-types-array.ts +29 -0
  77. package/src/utils/test/flatten-form-data.ts +49 -0
  78. package/src/utils/test/get-mime-types-array.ts +47 -0
  79. package/src/utils/test/upload-error.ts +24 -0
  80. package/src/utils/test/{upload-media.test.js → upload-media.ts} +47 -76
  81. package/src/utils/test/validate-file-size.ts +70 -0
  82. package/src/utils/test/validate-mime-type-for-user.ts +37 -0
  83. package/src/utils/test/validate-mime-type.ts +57 -0
  84. package/src/utils/transform-attachment.ts +24 -0
  85. package/src/utils/types.ts +207 -0
  86. package/src/utils/upload-error.ts +26 -0
  87. package/src/utils/upload-media.ts +149 -0
  88. package/src/utils/upload-to-server.ts +38 -0
  89. package/src/utils/validate-file-size.ts +44 -0
  90. package/src/utils/validate-mime-type-for-user.ts +46 -0
  91. package/src/utils/validate-mime-type.ts +43 -0
  92. package/tsconfig.json +17 -0
  93. package/tsconfig.tsbuildinfo +1 -0
  94. package/build/utils/index.js +0 -13
  95. package/build/utils/index.js.map +0 -1
  96. package/build-module/utils/index.js +0 -2
  97. package/build-module/utils/index.js.map +0 -1
  98. package/src/index.js +0 -2
  99. package/src/utils/index.js +0 -1
  100. package/src/utils/upload-media.js +0 -232
@@ -0,0 +1,199 @@
1
+ /**
2
+ * A media attachment object in a REST API context.
3
+ *
4
+ * Simplified version of what's defined in the wp-types package.
5
+ *
6
+ * @see https://www.npmjs.com/package/wp-types
7
+ */
8
+ interface WP_REST_API_Attachment {
9
+ /**
10
+ * Unique identifier for the attachment.
11
+ */
12
+ id: number;
13
+ /**
14
+ * The ID of the featured media for the post.
15
+ */
16
+ featured_media: number;
17
+ /**
18
+ * URL to the attachment.
19
+ */
20
+ link: string;
21
+ /**
22
+ * The date the attachment was published, in the site's timezone.
23
+ */
24
+ date: string;
25
+ /**
26
+ * The date the attachment was published, as GMT.
27
+ */
28
+ date_gmt: string;
29
+ /**
30
+ * The date the attachment was last modified, in the site's timezone.
31
+ */
32
+ modified: string;
33
+ /**
34
+ * The date the attachment was last modified, as GMT.
35
+ */
36
+ modified_gmt: string;
37
+ /**
38
+ * An alphanumeric identifier for the attachment unique to its type.
39
+ */
40
+ slug: string;
41
+ /**
42
+ * A named status for the attachment.
43
+ */
44
+ status: string;
45
+ /**
46
+ * Type of Post for the attachment.
47
+ */
48
+ type: 'attachment';
49
+ /**
50
+ * Alternative text to display when attachment is not displayed.
51
+ */
52
+ alt_text: string;
53
+ /**
54
+ * The attachment caption.
55
+ */
56
+ caption: {
57
+ /**
58
+ * Caption for the attachment, as it exists in the database. Only present when using the 'edit' context.
59
+ */
60
+ raw?: string;
61
+ /**
62
+ * HTML caption for the attachment, transformed for display.
63
+ */
64
+ rendered: string;
65
+ };
66
+ /**
67
+ * The attachment description.
68
+ */
69
+ description: {
70
+ /**
71
+ * Description for the attachment, as it exists in the database. Only present when using the 'edit' context.
72
+ */
73
+ raw?: string;
74
+ /**
75
+ * HTML description for the attachment, transformed for display.
76
+ */
77
+ rendered: string;
78
+ };
79
+ /**
80
+ * Attachment type.
81
+ */
82
+ media_type: 'image' | 'file';
83
+ /**
84
+ * The attachment MIME type.
85
+ */
86
+ mime_type: string;
87
+ /**
88
+ * Details about the media file, specific to its type.
89
+ */
90
+ media_details: {
91
+ [k: string]: unknown;
92
+ };
93
+ /**
94
+ * The ID for the associated post of the attachment.
95
+ */
96
+ post: number | null;
97
+ /**
98
+ * URL to the original attachment file.
99
+ */
100
+ source_url: string;
101
+ /**
102
+ * List of the missing image sizes of the attachment. Only present when using the 'edit' context.
103
+ */
104
+ missing_image_sizes?: string[];
105
+ /**
106
+ * Permalink template for the attachment. Only present when using the 'edit' context and the post type is public.
107
+ */
108
+ permalink_template?: string;
109
+ /**
110
+ * Slug automatically generated from the attachment title. Only present when using the 'edit' context and the post type is public.
111
+ */
112
+ generated_slug?: string;
113
+ /**
114
+ * An array of the class names for the post container element.
115
+ */
116
+ class_list: string[];
117
+ /**
118
+ * The title for the attachment.
119
+ */
120
+ title: {
121
+ /**
122
+ * Title for the attachment, as it exists in the database. Only present when using the 'edit' context.
123
+ */
124
+ raw?: string;
125
+ /**
126
+ * HTML title for the attachment, transformed for display.
127
+ */
128
+ rendered: string;
129
+ };
130
+ /**
131
+ * The ID for the author of the attachment.
132
+ */
133
+ author: number;
134
+ /**
135
+ * Whether or not comments are open on the attachment.
136
+ */
137
+ comment_status: string;
138
+ /**
139
+ * Whether or not the attachment can be pinged.
140
+ */
141
+ ping_status: string;
142
+ /**
143
+ * Meta fields.
144
+ */
145
+ meta: [] | {
146
+ [k: string]: unknown;
147
+ };
148
+ /**
149
+ * The theme file to use to display the attachment.
150
+ */
151
+ template: string;
152
+ _links: {
153
+ [k: string]: {
154
+ href: string;
155
+ embeddable?: boolean;
156
+ [k: string]: unknown;
157
+ }[];
158
+ };
159
+ /**
160
+ * The embedded representation of relations. Only present when the '_embed' query parameter is set.
161
+ */
162
+ _embedded?: {
163
+ /**
164
+ * The author of the post.
165
+ */
166
+ author: unknown[];
167
+ /**
168
+ * The featured image post.
169
+ */
170
+ 'wp:featuredmedia'?: WP_REST_API_Attachment[];
171
+ [k: string]: unknown;
172
+ };
173
+ [k: string]: unknown;
174
+ }
175
+ /**
176
+ * REST API attachment object with additional fields added by this project.
177
+ */
178
+ export interface RestAttachment extends WP_REST_API_Attachment {
179
+ }
180
+ type BetterOmit<T, K extends PropertyKey> = {
181
+ [P in keyof T as P extends K ? never : P]: T[P];
182
+ };
183
+ /**
184
+ * Transformed attachment object.
185
+ */
186
+ export type Attachment = BetterOmit<RestAttachment, 'alt_text' | 'source_url' | 'caption' | 'title'> & {
187
+ alt: WP_REST_API_Attachment['alt_text'];
188
+ caption: WP_REST_API_Attachment['caption']['raw'] & string;
189
+ title: WP_REST_API_Attachment['title']['raw'];
190
+ url: WP_REST_API_Attachment['source_url'];
191
+ poster?: WP_REST_API_Attachment['source_url'];
192
+ };
193
+ export type OnChangeHandler = (attachments: Partial<Attachment>[]) => void;
194
+ export type OnSuccessHandler = (attachments: Partial<Attachment>[]) => void;
195
+ export type OnErrorHandler = (error: Error) => void;
196
+ export type CreateRestAttachment = Partial<RestAttachment>;
197
+ export type AdditionalData = BetterOmit<CreateRestAttachment, 'meta'>;
198
+ export {};
199
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,19 @@
1
+ interface UploadErrorArgs {
2
+ code: string;
3
+ message: string;
4
+ file: File;
5
+ cause?: Error;
6
+ }
7
+ /**
8
+ * MediaError class.
9
+ *
10
+ * Small wrapper around the `Error` class
11
+ * to hold an error code and a reference to a file object.
12
+ */
13
+ export declare class UploadError extends Error {
14
+ code: string;
15
+ file: File;
16
+ constructor({ code, message, file, cause }: UploadErrorArgs);
17
+ }
18
+ export {};
19
+ //# sourceMappingURL=upload-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-error.d.ts","sourceRoot":"","sources":["../../src/utils/upload-error.ts"],"names":[],"mappings":"AAAA,UAAU,eAAe;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,CAAC,EAAE,KAAK,CAAC;CACd;AAED;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,CAAC;gBAEE,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,eAAe;CAQ5D"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { AdditionalData, OnChangeHandler, OnErrorHandler, OnSuccessHandler } from './types';
5
+ interface UploadMediaArgs {
6
+ additionalData?: AdditionalData;
7
+ allowedTypes?: string[];
8
+ filesList: File[];
9
+ maxUploadFileSize?: number;
10
+ onError?: OnErrorHandler;
11
+ onFileChange?: OnChangeHandler;
12
+ onSuccess?: OnSuccessHandler;
13
+ wpAllowedMimeTypes?: Record<string, string> | null;
14
+ signal?: AbortSignal;
15
+ }
16
+ /**
17
+ * Upload a media file when the file upload button is activated
18
+ * or when adding a file to the editor via drag & drop.
19
+ *
20
+ * @param $0 Parameters object passed to the function.
21
+ * @param $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
22
+ * @param $0.additionalData Additional data to include in the request.
23
+ * @param $0.filesList List of files.
24
+ * @param $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
25
+ * @param $0.onError Function called when an error happens.
26
+ * @param $0.onFileChange Function called each time a file or a temporary representation of the file is available.
27
+ * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
28
+ * @param $0.signal Abort signal.
29
+ */
30
+ export declare function uploadMedia({ wpAllowedMimeTypes, allowedTypes, additionalData, filesList, maxUploadFileSize, onError, onFileChange, signal, }: UploadMediaArgs): void;
31
+ export {};
32
+ //# sourceMappingURL=upload-media.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-media.d.ts","sourceRoot":"","sources":["../../src/utils/upload-media.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,OAAO,KAAK,EACX,cAAc,EAEd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,MAAM,SAAS,CAAC;AAOjB,UAAU,eAAe;IAExB,cAAc,CAAC,EAAE,cAAc,CAAC;IAEhC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB,SAAS,EAAE,IAAI,EAAE,CAAC;IAElB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAE3B,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB,YAAY,CAAC,EAAE,eAAe,CAAC;IAE/B,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAE7B,kBAAkB,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,GAAG,IAAI,CAAC;IAErD,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAE,EAC5B,kBAAkB,EAClB,YAAY,EACZ,cAAmB,EACnB,SAAS,EACT,iBAAiB,EACjB,OAAO,EACP,YAAY,EACZ,MAAM,GACN,EAAE,eAAe,QAkFjB"}
@@ -0,0 +1,3 @@
1
+ import type { CreateRestAttachment } from './types';
2
+ export declare function uploadToServer(file: File, additionalData?: CreateRestAttachment, signal?: AbortSignal): Promise<import("./types").Attachment>;
3
+ //# sourceMappingURL=upload-to-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-to-server.d.ts","sourceRoot":"","sources":["../../src/utils/upload-to-server.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,oBAAoB,EAAkB,MAAM,SAAS,CAAC;AAEpE,wBAAsB,cAAc,CACnC,IAAI,EAAE,IAAI,EACV,cAAc,GAAE,oBAAyB,EACzC,MAAM,CAAC,EAAE,WAAW,yCAsBpB"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Verifies whether the file is within the file upload size limits for the site.
3
+ *
4
+ * @param file File object.
5
+ * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.
6
+ */
7
+ export declare function validateFileSize(file: File, maxUploadFileSize?: number): void;
8
+ //# sourceMappingURL=validate-file-size.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-file-size.d.ts","sourceRoot":"","sources":["../../src/utils/validate-file-size.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAE,IAAI,EAAE,IAAI,EAAE,iBAAiB,CAAC,EAAE,MAAM,QA2BvE"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Verifies if the user is allowed to upload this mime type.
3
+ *
4
+ * @param file File object.
5
+ * @param wpAllowedMimeTypes List of allowed mime types and file extensions.
6
+ */
7
+ export declare function validateMimeTypeForUser(file: File, wpAllowedMimeTypes?: Record<string, string> | null): void;
8
+ //# sourceMappingURL=validate-mime-type-for-user.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-mime-type-for-user.d.ts","sourceRoot":"","sources":["../../src/utils/validate-mime-type-for-user.ts"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACtC,IAAI,EAAE,IAAI,EACV,kBAAkB,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,GAAG,IAAI,QA0BpD"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Verifies if the caller (e.g. a block) supports this mime type.
3
+ *
4
+ * @param file File object.
5
+ * @param allowedTypes List of allowed mime types.
6
+ */
7
+ export declare function validateMimeType(file: File, allowedTypes?: string[]): void;
8
+ //# sourceMappingURL=validate-mime-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-mime-type.d.ts","sourceRoot":"","sources":["../../src/utils/validate-mime-type.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAE,IAAI,EAAE,IAAI,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,QA0BpE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/media-utils",
3
- "version": "5.7.0",
3
+ "version": "5.8.0",
4
4
  "description": "WordPress Media Upload Utils.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -14,7 +14,7 @@
14
14
  "repository": {
15
15
  "type": "git",
16
16
  "url": "https://github.com/WordPress/gutenberg.git",
17
- "directory": "packages/url"
17
+ "directory": "packages/media-utils"
18
18
  },
19
19
  "bugs": {
20
20
  "url": "https://github.com/WordPress/gutenberg/issues"
@@ -25,15 +25,16 @@
25
25
  },
26
26
  "main": "build/index.js",
27
27
  "module": "build-module/index.js",
28
+ "types": "build-types",
28
29
  "dependencies": {
29
30
  "@babel/runtime": "^7.16.0",
30
- "@wordpress/api-fetch": "^7.7.0",
31
- "@wordpress/blob": "^4.7.0",
32
- "@wordpress/element": "^6.7.0",
33
- "@wordpress/i18n": "^5.7.0"
31
+ "@wordpress/api-fetch": "^7.8.0",
32
+ "@wordpress/blob": "^4.8.0",
33
+ "@wordpress/element": "^6.8.0",
34
+ "@wordpress/i18n": "^5.8.0"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"
37
38
  },
38
- "gitHead": "c90d920de07c53dff82c5914635b56fafa503b7f"
39
+ "gitHead": "cecf5e14d317aa67407f77a7e5c8b6a43016bd42"
39
40
  }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './components';
2
+
3
+ export { uploadMedia } from './utils/upload-media';
4
+ export { transformAttachment } from './utils/transform-attachment';
5
+ export { validateFileSize } from './utils/validate-file-size';
6
+ export { validateMimeType } from './utils/validate-mime-type';
7
+ export { validateMimeTypeForUser } from './utils/validate-mime-type-for-user';
8
+
9
+ export type { Attachment, RestAttachment } from './utils/types';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Determines whether the passed argument appears to be a plain object.
3
+ *
4
+ * @param data The object to inspect.
5
+ */
6
+ function isPlainObject( data: unknown ): data is Record< string, unknown > {
7
+ return (
8
+ data !== null &&
9
+ typeof data === 'object' &&
10
+ Object.getPrototypeOf( data ) === Object.prototype
11
+ );
12
+ }
13
+
14
+ /**
15
+ * Recursively flatten data passed to form data, to allow using multi-level objects.
16
+ *
17
+ * @param {FormData} formData Form data object.
18
+ * @param {string} key Key to amend to form data object
19
+ * @param {string|Object} data Data to be amended to form data.
20
+ */
21
+ export function flattenFormData(
22
+ formData: FormData,
23
+ key: string,
24
+ data: string | undefined | Record< string, string >
25
+ ) {
26
+ if ( isPlainObject( data ) ) {
27
+ for ( const [ name, value ] of Object.entries( data ) ) {
28
+ flattenFormData( formData, `${ key }[${ name }]`, value );
29
+ }
30
+ } else if ( data !== undefined ) {
31
+ formData.append( key, String( data ) );
32
+ }
33
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Browsers may use unexpected mime types, and they differ from browser to browser.
3
+ * This function computes a flexible array of mime types from the mime type structured provided by the server.
4
+ * Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
5
+ *
6
+ * @param {?Object} wpMimeTypesObject Mime type object received from the server.
7
+ * Extensions are keys separated by '|' and values are mime types associated with an extension.
8
+ *
9
+ * @return An array of mime types or null
10
+ */
11
+ export function getMimeTypesArray(
12
+ wpMimeTypesObject?: Record< string, string > | null
13
+ ) {
14
+ if ( ! wpMimeTypesObject ) {
15
+ return null;
16
+ }
17
+ return Object.entries( wpMimeTypesObject ).flatMap(
18
+ ( [ extensionsString, mime ] ) => {
19
+ const [ type ] = mime.split( '/' );
20
+ const extensions = extensionsString.split( '|' );
21
+ return [
22
+ mime,
23
+ ...extensions.map(
24
+ ( extension ) => `${ type }/${ extension }`
25
+ ),
26
+ ];
27
+ }
28
+ );
29
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { flattenFormData } from '../flatten-form-data';
5
+
6
+ describe( 'flattenFormData', () => {
7
+ it( 'should flatten nested data structure', () => {
8
+ const data = new FormData();
9
+
10
+ class RichTextData {
11
+ toString() {
12
+ return 'i am rich text';
13
+ }
14
+ }
15
+
16
+ const additionalData = {
17
+ foo: null,
18
+ bar: 1234,
19
+ meta: {
20
+ nested: 'foo',
21
+ dothis: true,
22
+ dothat: false,
23
+ supermeta: {
24
+ nested: 'baz',
25
+ },
26
+ },
27
+ customClass: new RichTextData(),
28
+ };
29
+
30
+ for ( const [ key, value ] of Object.entries( additionalData ) ) {
31
+ flattenFormData(
32
+ data,
33
+ key,
34
+ value as Parameters< typeof flattenFormData >[ 2 ]
35
+ );
36
+ }
37
+
38
+ const actual = Object.fromEntries( data.entries() );
39
+ expect( actual ).toStrictEqual( {
40
+ bar: '1234',
41
+ foo: 'null',
42
+ 'meta[dothat]': 'false',
43
+ 'meta[dothis]': 'true',
44
+ 'meta[nested]': 'foo',
45
+ 'meta[supermeta][nested]': 'baz',
46
+ customClass: 'i am rich text',
47
+ } );
48
+ } );
49
+ } );
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { getMimeTypesArray } from '../get-mime-types-array';
5
+
6
+ describe( 'getMimeTypesArray', () => {
7
+ it( 'should return null if it is "falsy" e.g: undefined or null', () => {
8
+ expect( getMimeTypesArray( null ) ).toEqual( null );
9
+ expect( getMimeTypesArray( undefined ) ).toEqual( null );
10
+ } );
11
+
12
+ it( 'should return an empty array if an empty object is passed', () => {
13
+ expect( getMimeTypesArray( {} ) ).toEqual( [] );
14
+ } );
15
+
16
+ it( 'should return the type plus a new mime type with type and subtype with the extension if a type is passed', () => {
17
+ expect( getMimeTypesArray( { ext: 'chicken' } ) ).toEqual( [
18
+ 'chicken',
19
+ 'chicken/ext',
20
+ ] );
21
+ } );
22
+
23
+ it( 'should return the mime type passed and a new mime type with type and the extension as subtype', () => {
24
+ expect( getMimeTypesArray( { ext: 'chicken/ribs' } ) ).toEqual( [
25
+ 'chicken/ribs',
26
+ 'chicken/ext',
27
+ ] );
28
+ } );
29
+
30
+ it( 'should return the mime type passed and an additional mime type per extension supported', () => {
31
+ expect( getMimeTypesArray( { 'jpg|jpeg|jpe': 'image/jpeg' } ) ).toEqual(
32
+ [ 'image/jpeg', 'image/jpg', 'image/jpeg', 'image/jpe' ]
33
+ );
34
+ } );
35
+
36
+ it( 'should handle multiple mime types', () => {
37
+ expect(
38
+ getMimeTypesArray( { 'ext|aaa': 'chicken/ribs', aaa: 'bbb' } )
39
+ ).toEqual( [
40
+ 'chicken/ribs',
41
+ 'chicken/ext',
42
+ 'chicken/aaa',
43
+ 'bbb',
44
+ 'bbb/aaa',
45
+ ] );
46
+ } );
47
+ } );
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { UploadError } from '../upload-error';
5
+
6
+ describe( 'UploadError', () => {
7
+ it( 'holds error code and file name', () => {
8
+ const file = new File( [], 'example.jpg', {
9
+ lastModified: 1234567891,
10
+ type: 'image/jpeg',
11
+ } );
12
+
13
+ const error = new UploadError( {
14
+ code: 'some_error',
15
+ message: 'An error occurred',
16
+ file,
17
+ } );
18
+
19
+ expect( error ).toStrictEqual( expect.any( Error ) );
20
+ expect( error.code ).toBe( 'some_error' );
21
+ expect( error.message ).toBe( 'An error occurred' );
22
+ expect( error.file ).toBe( file );
23
+ } );
24
+ } );