Skip to content
Merged
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
29 changes: 18 additions & 11 deletions src/targets/javascript/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ module.exports = function (source, options) {
options
)

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

options = {
method: source.method,
headers: source.allHeaders
method: source.method
}

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

if (opts.credentials !== null) {
Expand Down Expand Up @@ -63,15 +67,18 @@ module.exports = function (source, options) {
}
}

code
.push(`fetch("${source.fullUrl}", ${JSON.stringify(options, null, opts.indent)})`)
.push('.then(response => response.json())')
.push('.then(response => {')
.push(1, 'console.log(response);')
.push('})')
.push('.catch(err => {')
.push(1, 'console.error(err);')
.push('});')
code.push('const options = %s;', stringifyObject(options, { indent: opts.indent, inlineCharacterLimit: 80 }))
.blank()

if (source.postData.mimeType === 'multipart/form-data') {
code.push('options.body = form;')
.blank()
}

code.push("fetch('%s', options)", source.fullUrl)
.push(1, '.then(response => response.json())')
.push(1, '.then(response => console.log(response))')
.push(1, '.catch(err => console.error(err));')

return code.join()
}
Expand Down
27 changes: 10 additions & 17 deletions test/fixtures/output/javascript/fetch/application-form-encoded.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"body": {
"foo": "bar",
"hello": "world"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'application/x-www-form-urlencoded'},
body: {foo: 'bar', hello: 'world'}
};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 10 additions & 14 deletions test/fixtures/output/javascript/fetch/application-json.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'application/json'},
body: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
19 changes: 6 additions & 13 deletions test/fixtures/output/javascript/fetch/cookies.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"cookie": "foo=bar; bar=baz"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'POST', headers: {cookie: 'foo=bar; bar=baz'}};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
17 changes: 6 additions & 11 deletions test/fixtures/output/javascript/fetch/custom-method.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
fetch("http://mockbin.com/har", {
"method": "PROPFIND",
"headers": {}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'PROPFIND'};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
30 changes: 13 additions & 17 deletions test/fixtures/output/javascript/fetch/full.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
fetch("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value", {
"method": "POST",
"headers": {
"cookie": "foo=bar; bar=baz",
"accept": "application/json",
"content-type": "application/x-www-form-urlencoded"
const options = {
method: 'POST',
headers: {
cookie: 'foo=bar; bar=baz',
accept: 'application/json',
'content-type': 'application/x-www-form-urlencoded'
},
"body": {
"foo": "bar"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
body: {foo: 'bar'}
};

fetch('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
20 changes: 6 additions & 14 deletions test/fixtures/output/javascript/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
fetch("http://mockbin.com/har", {
"method": "GET",
"headers": {
"accept": "application/json",
"x-foo": "Bar"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'GET', headers: {accept: 'application/json', 'x-foo': 'Bar'}};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
17 changes: 6 additions & 11 deletions test/fixtures/output/javascript/fetch/https.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
fetch("https://mockbin.com/har", {
"method": "GET",
"headers": {}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'GET'};

fetch('https://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 10 additions & 14 deletions test/fixtures/output/javascript/fetch/jsonObj-multiline.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": "{\"foo\":\"bar\"}"
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'application/json'},
body: '{"foo":"bar"}'
};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 10 additions & 14 deletions test/fixtures/output/javascript/fetch/jsonObj-null-value.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": "{\"foo\":null}"
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'application/json'},
body: '{"foo":null}'
};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 11 additions & 13 deletions test/fixtures/output/javascript/fetch/multipart-data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const form = new FormData();
form.append("foo", "Hello World");

fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "multipart/form-data; boundary=---011000010111000001101001"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = form;

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 11 additions & 13 deletions test/fixtures/output/javascript/fetch/multipart-file.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const form = new FormData();
form.append("foo", "test/fixtures/files/hello.txt");

fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "multipart/form-data; boundary=---011000010111000001101001"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = form;

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
24 changes: 11 additions & 13 deletions test/fixtures/output/javascript/fetch/multipart-form-data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const form = new FormData();
form.append("foo", "bar");

fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"Content-Type": "multipart/form-data; boundary=---011000010111000001101001"
}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {
method: 'POST',
headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = form;

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
17 changes: 6 additions & 11 deletions test/fixtures/output/javascript/fetch/query.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
fetch("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value", {
"method": "GET",
"headers": {}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'GET'};

fetch('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
17 changes: 6 additions & 11 deletions test/fixtures/output/javascript/fetch/short.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
fetch("http://mockbin.com/har", {
"method": "GET",
"headers": {}
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'GET'};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
20 changes: 6 additions & 14 deletions test/fixtures/output/javascript/fetch/text-plain.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
fetch("http://mockbin.com/har", {
"method": "POST",
"headers": {
"content-type": "text/plain"
},
"body": "Hello World"
})
.then(response => response.json())
.then(response => {
console.log(response);
})
.catch(err => {
console.error(err);
});
const options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'};

fetch('http://mockbin.com/har', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));