Skip to content
Closed
Show file tree
Hide file tree
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
27 changes: 19 additions & 8 deletions src/targets/javascript/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

'use strict'

const headerHelpers = require('../../helpers/headers')
const CodeBuilder = require('../../helpers/code-builder')
const stringifyObject = require('stringify-object')

module.exports = function (source, options) {
const opts = Object.assign(
Expand All @@ -21,30 +23,39 @@ module.exports = function (source, options) {
options
)

const stringifyObject = require('stringify-object')
const code = new CodeBuilder(opts.indent)

options = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this variable to reqOpts so it's not overwriting the options argument of the target.

const reqOpts = {
method: source.method
}

// The `form-data` library automatically adds a `Content-Type` header for `multipart/form-data` content and if we
// add our own here, data won't be correctly transferred.
if (source.postData.mimeType === 'multipart/form-data') {
const contentTypeHeader = headerHelpers.getHeaderName(source.allHeaders, 'content-type')
if (contentTypeHeader) {
// eslint-disable-next-line no-param-reassign
delete source.allHeaders[contentTypeHeader]
}
}

if (Object.keys(source.allHeaders).length) {
options.headers = source.allHeaders
reqOpts.headers = source.allHeaders
}

if (opts.credentials !== null) {
options.credentials = opts.credentials
reqOpts.credentials = opts.credentials
}

switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
options.body = source.postData.paramsObj
reqOpts.body = source.postData.paramsObj
? source.postData.paramsObj
: source.postData.text
break

case 'application/json':
options.body = JSON.stringify(source.postData.jsonObj)
reqOpts.body = JSON.stringify(source.postData.jsonObj)
break

case 'multipart/form-data':
Expand All @@ -63,11 +74,11 @@ module.exports = function (source, options) {

default:
if (source.postData.text) {
options.body = source.postData.text
reqOpts.body = source.postData.text
}
}

code.push('const options = %s;', stringifyObject(options, {
code.push('const options = %s;', stringifyObject(reqOpts, {
indent: opts.indent,
inlineCharacterLimit: 80,
transform: (object, property, originalResult) => {
Expand Down
11 changes: 11 additions & 0 deletions src/targets/node/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict'

const headerHelpers = require('../../helpers/headers')
const stringifyObject = require('stringify-object')
const CodeBuilder = require('../../helpers/code-builder')

Expand All @@ -27,6 +28,16 @@ module.exports = function (source, options) {
method: source.method
}

// The `form-data` library automatically adds a `Content-Type` header for `multipart/form-data` content and if we
// add our own here, data won't be correctly transferred.
if (source.postData.mimeType === 'multipart/form-data') {
const contentTypeHeader = headerHelpers.getHeaderName(source.headersObj, 'content-type')
if (contentTypeHeader) {
// eslint-disable-next-line no-param-reassign
delete source.headersObj[contentTypeHeader]
}
}

if (Object.keys(source.headersObj).length) {
reqOpts.headers = source.headersObj
}
Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/javascript/fetch/multipart-data.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const form = new FormData();
form.append("foo", "Hello World");

const options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
const options = {method: 'POST'};

options.body = form;

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/javascript/fetch/multipart-file.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const form = new FormData();
form.append("foo", "test/fixtures/files/hello.txt");

const options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
const options = {method: 'POST'};

options.body = form;

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/javascript/fetch/multipart-form-data.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const form = new FormData();
form.append("foo", "bar");

const options = {
method: 'POST',
headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
const options = {method: 'POST'};

options.body = form;

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/node/fetch/multipart-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ formData.append('foo', fs.createReadStream('hello.txt'));

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
let options = {method: 'POST'};

options.body = formData;

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/node/fetch/multipart-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ formData.append('foo', fs.createReadStream('test/fixtures/files/hello.txt'));

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
let options = {method: 'POST'};

options.body = formData;

Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/output/node/fetch/multipart-form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ formData.append('foo', 'bar');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
};
let options = {method: 'POST'};

options.body = formData;

Expand Down