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
74 changes: 74 additions & 0 deletions src/targets/javascript/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @description
* HTTP code snippet generator for fetch
*
* @author
* @pmdroid
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/

'use strict'

var util = require('util')
var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
var opts = util._extend({
indent: ' ',
credentials: null
}, options)

var code = new CodeBuilder(opts.indent)

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

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

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

case 'application/json':
options.body = source.postData.jsonObj
break

case 'multipart/form-data':
code.push('var form = new FormData();')

source.postData.params.forEach(function (param) {
code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
})

code.blank()
break

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

code.push('fetch(\'' + source.fullUrl + '\', ' + JSON.stringify(options, null, opts.indent) + ')')
.push('.then(response => {')
.push(1, 'console.log(response);')
.push('})')
.push('.catch(err => {')
.push(1, 'console.log(err);')
.push('});')

return code.join()
}

module.exports.info = {
key: 'fetch',
title: 'fetch',
link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
description: 'Perform an asynchronous HTTP requests with fetch'
}
1 change: 1 addition & 0 deletions src/targets/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports = {
},

jquery: require('./jquery'),
fetch: require('./fetch'),
xhr: require('./xhr')
}
6 changes: 6 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
"link": "http://api.jquery.com/jquery.ajax/",
"description": "Perform an asynchronous HTTP (Ajax) requests with jQuery"
},
{
"key": "fetch",
"title": "fetch",
"link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",
"description": "Perform an asynchronous HTTP requests with fetch"
},
{
"key": "xhr",
"title": "XMLHttpRequest",
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/output/javascript/fetch/application-form-encoded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fetch('http://mockbin.com/har', {
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"body": {
"foo": "bar",
"hello": "world"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
32 changes: 32 additions & 0 deletions test/fixtures/output/javascript/fetch/application-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 => {
console.log(response);
})
.catch(err => {
console.log(err);
});
12 changes: 12 additions & 0 deletions test/fixtures/output/javascript/fetch/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fetch('http://mockbin.com/har', {
"method": "POST",
"headers": {
"cookie": "foo=bar; bar=baz"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
10 changes: 10 additions & 0 deletions test/fixtures/output/javascript/fetch/custom-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fetch('http://mockbin.com/har', {
"method": "PROPFIND",
"headers": {}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
17 changes: 17 additions & 0 deletions test/fixtures/output/javascript/fetch/full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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"
},
"body": {
"foo": "bar"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
13 changes: 13 additions & 0 deletions test/fixtures/output/javascript/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fetch('http://mockbin.com/har', {
"method": "GET",
"headers": {
"accept": "application/json",
"x-foo": "Bar"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
10 changes: 10 additions & 0 deletions test/fixtures/output/javascript/fetch/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fetch('https://mockbin.com/har', {
"method": "GET",
"headers": {}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
15 changes: 15 additions & 0 deletions test/fixtures/output/javascript/fetch/jsonObj-null-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fetch('http://mockbin.com/har', {
"method": "POST",
"headers": {
"content-type": "application/json"
},
"body": {
"foo": null
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
15 changes: 15 additions & 0 deletions test/fixtures/output/javascript/fetch/multipart-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var 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 => {
console.log(response);
})
.catch(err => {
console.log(err);
});
15 changes: 15 additions & 0 deletions test/fixtures/output/javascript/fetch/multipart-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var 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 => {
console.log(response);
})
.catch(err => {
console.log(err);
});
15 changes: 15 additions & 0 deletions test/fixtures/output/javascript/fetch/multipart-form-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var form = new FormData();
form.append("foo", "bar");

fetch('http://mockbin.com/har', {
"method": "POST",
"headers": {
"content-type": "multipart/form-data; boundary=---011000010111000001101001"
}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
10 changes: 10 additions & 0 deletions test/fixtures/output/javascript/fetch/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fetch('http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value', {
"method": "GET",
"headers": {}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
10 changes: 10 additions & 0 deletions test/fixtures/output/javascript/fetch/short.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fetch('http://mockbin.com/har', {
"method": "GET",
"headers": {}
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
13 changes: 13 additions & 0 deletions test/fixtures/output/javascript/fetch/text-plain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fetch('http://mockbin.com/har', {
"method": "POST",
"headers": {
"content-type": "text/plain"
},
"body": "Hello World"
})
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
3 changes: 3 additions & 0 deletions test/targets/javascript/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = function (snippet, fixtures) {}