Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class {
this.url = config.rest_url ? config.rest_url : ( config.url + 'wp-json' )
this.url = this.url.replace( /\/$/, '' )
this.credentials = config.credentials
this.useBody = config.useBody || false

if ( this.credentials ) {
this.oauth = new oauth({
Expand Down Expand Up @@ -195,16 +196,23 @@ export default class {
/**
* Only attach the oauth headers if we have a request token, or it is a request to the `oauth/request` endpoint.
*/
if ( this.oauth && this.config.credentials.token || requestUrls.indexOf( url ) > -1 ) {
headers = {...headers, ...this.oauth.toHeader( oauthData )}
if (!this.useBody && this.oauth && this.config.credentials.token || requestUrls.indexOf(url) > -1) {
headers = { ...headers, ...this.oauth.toHeader(oauthData) }
}

return fetch( url, {
let body = '';
if (this.useBody) {
body = `${qs.stringify(data)}&${qs.stringify(oauthData)}`
} else {
body = ['GET', 'HEAD'].indexOf(method) > -1 ? null : qs.stringify(data)
}

return fetch(url, {
method: method,
headers: headers,
mode: 'cors',
body: ['GET','HEAD'].indexOf( method ) > -1 ? null : qs.stringify( data )
} )
body,
})
.then( response => {
if ( response.headers.get( 'Content-Type' ) && response.headers.get( 'Content-Type' ).indexOf( 'x-www-form-urlencoded' ) > -1 ) {
return response.text().then( text => {
Expand Down