Skip to content

Commit dc78b0a

Browse files
feat(target) add browser fetch api target (#128)
Fixes #90
1 parent 98f4471 commit dc78b0a

File tree

18 files changed

+296
-0
lines changed

18 files changed

+296
-0
lines changed

src/targets/javascript/fetch.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for fetch
4+
*
5+
* @author
6+
* @pmdroid
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var CodeBuilder = require('../../helpers/code-builder')
14+
15+
module.exports = function (source, options) {
16+
var opts = Object.assign(
17+
{
18+
indent: ' ',
19+
credentials: null
20+
},
21+
options
22+
)
23+
24+
var code = new CodeBuilder(opts.indent)
25+
26+
options = {
27+
method: source.method,
28+
headers: source.allHeaders
29+
}
30+
31+
if (opts.credentials !== null) {
32+
options.credentials = opts.credentials
33+
}
34+
35+
switch (source.postData.mimeType) {
36+
case 'application/x-www-form-urlencoded':
37+
options.body = source.postData.paramsObj
38+
? source.postData.paramsObj
39+
: source.postData.text
40+
break
41+
42+
case 'application/json':
43+
options.body = source.postData.jsonObj
44+
break
45+
46+
case 'multipart/form-data':
47+
code.push('var form = new FormData();')
48+
49+
source.postData.params.forEach(function (param) {
50+
code.push(
51+
'form.append(%s, %s);',
52+
JSON.stringify(param.name),
53+
JSON.stringify(param.value || param.fileName || '')
54+
)
55+
})
56+
57+
code.blank()
58+
break
59+
60+
default:
61+
if (source.postData.text) {
62+
options.body = source.postData.text
63+
}
64+
}
65+
66+
code
67+
.push(`fetch("${source.fullUrl}", ${JSON.stringify(options, null, opts.indent)})`)
68+
.push('.then(response => {')
69+
.push(1, 'console.log(response);')
70+
.push('})')
71+
.push('.catch(err => {')
72+
.push(1, 'console.log(err);')
73+
.push('});')
74+
75+
return code.join()
76+
}
77+
78+
module.exports.info = {
79+
key: 'fetch',
80+
title: 'fetch',
81+
link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
82+
description: 'Perform asynchronous HTTP requests with the Fetch API'
83+
}

src/targets/javascript/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module.exports = {
99
},
1010

1111
jquery: require('./jquery'),
12+
fetch: require('./fetch'),
1213
xhr: require('./xhr')
1314
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
"link": "http://api.jquery.com/jquery.ajax/",
6464
"description": "Perform an asynchronous HTTP (Ajax) requests with jQuery"
6565
},
66+
{
67+
"key": "fetch",
68+
"title": "fetch",
69+
"link": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",
70+
"description": "Perform asynchronous HTTP requests with the Fetch API"
71+
},
6672
{
6773
"key": "xhr",
6874
"title": "XMLHttpRequest",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 => {
12+
console.log(response);
13+
})
14+
.catch(err => {
15+
console.log(err);
16+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
fetch("http://mockbin.com/har", {
2+
"method": "POST",
3+
"headers": {
4+
"content-type": "application/json"
5+
},
6+
"body": {
7+
"number": 1,
8+
"string": "f\"oo",
9+
"arr": [
10+
1,
11+
2,
12+
3
13+
],
14+
"nested": {
15+
"a": "b"
16+
},
17+
"arr_mix": [
18+
1,
19+
"a",
20+
{
21+
"arr_mix_nested": {}
22+
}
23+
],
24+
"boolean": false
25+
}
26+
})
27+
.then(response => {
28+
console.log(response);
29+
})
30+
.catch(err => {
31+
console.log(err);
32+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fetch("http://mockbin.com/har", {
2+
"method": "POST",
3+
"headers": {
4+
"cookie": "foo=bar; bar=baz"
5+
}
6+
})
7+
.then(response => {
8+
console.log(response);
9+
})
10+
.catch(err => {
11+
console.log(err);
12+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fetch("http://mockbin.com/har", {
2+
"method": "PROPFIND",
3+
"headers": {}
4+
})
5+
.then(response => {
6+
console.log(response);
7+
})
8+
.catch(err => {
9+
console.log(err);
10+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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"
7+
},
8+
"body": {
9+
"foo": "bar"
10+
}
11+
})
12+
.then(response => {
13+
console.log(response);
14+
})
15+
.catch(err => {
16+
console.log(err);
17+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fetch("http://mockbin.com/har", {
2+
"method": "GET",
3+
"headers": {
4+
"accept": "application/json",
5+
"x-foo": "Bar"
6+
}
7+
})
8+
.then(response => {
9+
console.log(response);
10+
})
11+
.catch(err => {
12+
console.log(err);
13+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fetch("https://mockbin.com/har", {
2+
"method": "GET",
3+
"headers": {}
4+
})
5+
.then(response => {
6+
console.log(response);
7+
})
8+
.catch(err => {
9+
console.log(err);
10+
});

0 commit comments

Comments
 (0)