Skip to content

Commit 200b1fd

Browse files
erunionreynolek
andauthored
fix: bug where FormData in the js fetch target wasn't applied (#202)
* fix: bug where formData in the js fetch target wasn't applied * fix(fetch.js) - Removed a variable that was needed * fix(fetch.js) - Linting Co-authored-by: Eric Reynolds <[email protected]>
1 parent 03a44e6 commit 200b1fd

File tree

16 files changed

+146
-211
lines changed

16 files changed

+146
-211
lines changed

src/targets/javascript/fetch.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ module.exports = function (source, options) {
2121
options
2222
)
2323

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

2627
options = {
27-
method: source.method,
28-
headers: source.allHeaders
28+
method: source.method
29+
}
30+
31+
if (Object.keys(source.allHeaders).length) {
32+
options.headers = source.allHeaders
2933
}
3034

3135
if (opts.credentials !== null) {
@@ -63,15 +67,18 @@ module.exports = function (source, options) {
6367
}
6468
}
6569

66-
code
67-
.push(`fetch("${source.fullUrl}", ${JSON.stringify(options, null, opts.indent)})`)
68-
.push('.then(response => response.json())')
69-
.push('.then(response => {')
70-
.push(1, 'console.log(response);')
71-
.push('})')
72-
.push('.catch(err => {')
73-
.push(1, 'console.error(err);')
74-
.push('});')
70+
code.push('const options = %s;', stringifyObject(options, { indent: opts.indent, inlineCharacterLimit: 80 }))
71+
.blank()
72+
73+
if (source.postData.mimeType === 'multipart/form-data') {
74+
code.push('options.body = form;')
75+
.blank()
76+
}
77+
78+
code.push("fetch('%s', options)", source.fullUrl)
79+
.push(1, '.then(response => response.json())')
80+
.push(1, '.then(response => console.log(response))')
81+
.push(1, '.catch(err => console.error(err));')
7582

7683
return code.join()
7784
}
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "POST",
3-
"headers": {
4-
"content-type": "application/x-www-form-urlencoded"
5-
},
6-
"body": {
7-
"foo": "bar",
8-
"hello": "world"
9-
}
10-
})
11-
.then(response => response.json())
12-
.then(response => {
13-
console.log(response);
14-
})
15-
.catch(err => {
16-
console.error(err);
17-
});
1+
const options = {
2+
method: 'POST',
3+
headers: {'content-type': 'application/x-www-form-urlencoded'},
4+
body: {foo: 'bar', hello: 'world'}
5+
};
6+
7+
fetch('http://mockbin.com/har', options)
8+
.then(response => response.json())
9+
.then(response => console.log(response))
10+
.catch(err => console.error(err));
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "POST",
3-
"headers": {
4-
"content-type": "application/json"
5-
},
6-
"body": "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
7-
})
8-
.then(response => response.json())
9-
.then(response => {
10-
console.log(response);
11-
})
12-
.catch(err => {
13-
console.error(err);
14-
});
1+
const options = {
2+
method: 'POST',
3+
headers: {'content-type': 'application/json'},
4+
body: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
5+
};
6+
7+
fetch('http://mockbin.com/har', options)
8+
.then(response => response.json())
9+
.then(response => console.log(response))
10+
.catch(err => console.error(err));
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "POST",
3-
"headers": {
4-
"cookie": "foo=bar; bar=baz"
5-
}
6-
})
7-
.then(response => response.json())
8-
.then(response => {
9-
console.log(response);
10-
})
11-
.catch(err => {
12-
console.error(err);
13-
});
1+
const options = {method: 'POST', headers: {cookie: 'foo=bar; bar=baz'}};
2+
3+
fetch('http://mockbin.com/har', options)
4+
.then(response => response.json())
5+
.then(response => console.log(response))
6+
.catch(err => console.error(err));
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "PROPFIND",
3-
"headers": {}
4-
})
5-
.then(response => response.json())
6-
.then(response => {
7-
console.log(response);
8-
})
9-
.catch(err => {
10-
console.error(err);
11-
});
1+
const options = {method: 'PROPFIND'};
2+
3+
fetch('http://mockbin.com/har', options)
4+
.then(response => response.json())
5+
.then(response => console.log(response))
6+
.catch(err => console.error(err));
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
fetch("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value", {
2-
"method": "POST",
3-
"headers": {
4-
"cookie": "foo=bar; bar=baz",
5-
"accept": "application/json",
6-
"content-type": "application/x-www-form-urlencoded"
1+
const options = {
2+
method: 'POST',
3+
headers: {
4+
cookie: 'foo=bar; bar=baz',
5+
accept: 'application/json',
6+
'content-type': 'application/x-www-form-urlencoded'
77
},
8-
"body": {
9-
"foo": "bar"
10-
}
11-
})
12-
.then(response => response.json())
13-
.then(response => {
14-
console.log(response);
15-
})
16-
.catch(err => {
17-
console.error(err);
18-
});
8+
body: {foo: 'bar'}
9+
};
10+
11+
fetch('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', options)
12+
.then(response => response.json())
13+
.then(response => console.log(response))
14+
.catch(err => console.error(err));
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "GET",
3-
"headers": {
4-
"accept": "application/json",
5-
"x-foo": "Bar"
6-
}
7-
})
8-
.then(response => response.json())
9-
.then(response => {
10-
console.log(response);
11-
})
12-
.catch(err => {
13-
console.error(err);
14-
});
1+
const options = {method: 'GET', headers: {accept: 'application/json', 'x-foo': 'Bar'}};
2+
3+
fetch('http://mockbin.com/har', options)
4+
.then(response => response.json())
5+
.then(response => console.log(response))
6+
.catch(err => console.error(err));
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
fetch("https://mockbin.com/har", {
2-
"method": "GET",
3-
"headers": {}
4-
})
5-
.then(response => response.json())
6-
.then(response => {
7-
console.log(response);
8-
})
9-
.catch(err => {
10-
console.error(err);
11-
});
1+
const options = {method: 'GET'};
2+
3+
fetch('https://mockbin.com/har', options)
4+
.then(response => response.json())
5+
.then(response => console.log(response))
6+
.catch(err => console.error(err));
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "POST",
3-
"headers": {
4-
"content-type": "application/json"
5-
},
6-
"body": "{\"foo\":\"bar\"}"
7-
})
8-
.then(response => response.json())
9-
.then(response => {
10-
console.log(response);
11-
})
12-
.catch(err => {
13-
console.error(err);
14-
});
1+
const options = {
2+
method: 'POST',
3+
headers: {'content-type': 'application/json'},
4+
body: '{"foo":"bar"}'
5+
};
6+
7+
fetch('http://mockbin.com/har', options)
8+
.then(response => response.json())
9+
.then(response => console.log(response))
10+
.catch(err => console.error(err));
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
fetch("http://mockbin.com/har", {
2-
"method": "POST",
3-
"headers": {
4-
"content-type": "application/json"
5-
},
6-
"body": "{\"foo\":null}"
7-
})
8-
.then(response => response.json())
9-
.then(response => {
10-
console.log(response);
11-
})
12-
.catch(err => {
13-
console.error(err);
14-
});
1+
const options = {
2+
method: 'POST',
3+
headers: {'content-type': 'application/json'},
4+
body: '{"foo":null}'
5+
};
6+
7+
fetch('http://mockbin.com/har', options)
8+
.then(response => response.json())
9+
.then(response => console.log(response))
10+
.catch(err => console.error(err));

0 commit comments

Comments
 (0)