Skip to content

Commit e4219e8

Browse files
committed
Use FormData to support media uploads in fetch transport
1 parent 36c89fc commit e4219e8

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

fetch/fetch-transport.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
'use strict';
55

66
const fetch = require( 'isomorphic-unfetch' );
7-
// const FormData = require( 'form-data' );
7+
const FormData = require( 'form-data' );
8+
const fs = require( 'fs' );
89

910
const objectReduce = require( '../lib/util/object-reduce' );
1011
const { createPaginationObject } = require( '../lib/pagination' );
@@ -171,21 +172,25 @@ function _httpGet( wpreq ) {
171172
* @returns {Promise} A promise to the results of the HTTP request
172173
*/
173174
function _httpPost( wpreq, data = {} ) {
174-
// if ( wpreq._attachment ) {
175-
// // Data must be form-encoded alongside image attachment
176-
// const form = new FormData();
177-
// data = objectReduce(
178-
// data,
179-
// ( form, value, key ) => form.append( key, value ),
180-
// // TODO: Probably need to read in the file if a string path is given
181-
// form.append( 'file', wpreq._attachment, wpreq._attachmentName )
182-
// );
183-
// config = objectReduce(
184-
// form.getHeaders(),
185-
// ( config, value, key ) => _setHeader( config, key, value ),
186-
// config
187-
// );
188-
// }
175+
let file = wpreq._attachment;
176+
if ( file ) {
177+
// Handle files provided as a path string
178+
if ( typeof file === 'string' ) {
179+
file = fs.createReadStream( file );
180+
}
181+
182+
// Build the form data object
183+
const form = new FormData();
184+
form.append( 'file', file, wpreq._attachmentName );
185+
Object.keys( data ).forEach( key => form.append( key, data[ key ] ) );
186+
187+
// Fire off the media upload request
188+
return send( wpreq, {
189+
method: 'POST',
190+
redirect: 'follow',
191+
body: form,
192+
} );
193+
}
189194

190195
return send( wpreq, {
191196
method: 'POST',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"lint": "npm run eslint",
5454
"watch": "jest --watch",
5555
"test:all": "jest",
56-
"test:unit": "jest tests/unit superagent/tests/unit",
56+
"test:unit": "jest tests/unit superagent/tests/unit fetch/tests/unit",
5757
"test:integration": "jest tests/integration",
5858
"test:ci": "npm run eslint && jest tests/unit --coverage",
5959
"pretest": "npm run lint || true",
@@ -65,7 +65,7 @@
6565
"qs": "^6.6.0"
6666
},
6767
"optionalDependencies": {
68-
"form-data": "^2.3.3",
68+
"form-data": "^2.5.0",
6969
"isomorphic-unfetch": "^3.0.0",
7070
"superagent": "^4.1.0"
7171
},

tests/integration/media.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const expectedResults = {
5858

5959
describe.each( [
6060
[ 'wpapi/superagent', require( '../../superagent' ) ],
61+
[ 'wpapi/fetch', require( '../../fetch' ) ],
6162
] )( '%s: media()', ( transportName, WPAPI ) => {
6263
let wp;
6364
let authenticated;

tests/integration/posts.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,7 @@ describe.each( [
670670
return expect( prom ).resolves.toBe( SUCCESS );
671671
}, 10000 );
672672

673-
// TODO: Un-skip once image uploading is reinstated.
674-
it.skip( 'can create a post with tags, categories and featured media', () => {
673+
it( 'can create a post with tags, categories and featured media', () => {
675674
let id;
676675
let mediaId;
677676
const filePath = path.join( __dirname, 'assets/emilygarfield-untitled.jpg' );

0 commit comments

Comments
 (0)