Skip to content

Commit 225d9dc

Browse files
hirenoblejgiovaresco
authored andcommitted
Adding node-fetch target (#180)
@hirenoble is the original author (#154). I've just resolve conflicts and fix tests that were failing. @develohpanda noticed that JSON body should be stringified (https://github.com/node-fetch/node-fetch#post-with-json) And looking at fetch options (https://github.com/node-fetch/node-fetch#fetch-options) I noticed that `json` attribute does not exist, so I've removed it. Co-authored-by: Julien Giovaresco <dev@giovaresco.fr>
1 parent e2fa910 commit 225d9dc

19 files changed

+325
-1
lines changed

src/targets/node/fetch.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Node.js using node-fetch.
4+
*
5+
* @author
6+
* @hirenoble
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 stringifyObject = require('stringify-object')
14+
var CodeBuilder = require('../../helpers/code-builder')
15+
16+
module.exports = function (source, options) {
17+
var opts = Object.assign({
18+
indent: ' '
19+
}, options)
20+
21+
var includeFS = false
22+
var code = new CodeBuilder(opts.indent)
23+
24+
code.push('const fetch = require(\'node-fetch\');')
25+
var url = source.url
26+
var reqOpts = {
27+
method: source.method
28+
}
29+
30+
if (Object.keys(source.queryObj).length) {
31+
reqOpts.qs = source.queryObj
32+
}
33+
34+
if (Object.keys(source.headersObj).length) {
35+
reqOpts.headers = source.headersObj
36+
}
37+
38+
switch (source.postData.mimeType) {
39+
case 'application/x-www-form-urlencoded':
40+
reqOpts.body = source.postData.paramsObj
41+
break
42+
43+
case 'application/json':
44+
if (source.postData.jsonObj) {
45+
reqOpts.body = JSON.stringify(source.postData.jsonObj)
46+
}
47+
break
48+
49+
case 'multipart/form-data':
50+
code.unshift('const FormData = require(\'form-data\');')
51+
code.push('const formData = new FormData();')
52+
source.postData.params.forEach(function (param) {
53+
if (!param.fileName && !param.fileName && !param.contentType) {
54+
code.push('formData.append(\'' + param.name + '\',\'' + param.value + '\');')
55+
return
56+
}
57+
58+
if (param.fileName) {
59+
includeFS = true
60+
code.blank()
61+
code.push('formData.append(\'' + param.name + '\', fs.createReadStream(\'' + param.fileName + '\'));')
62+
}
63+
})
64+
break
65+
66+
default:
67+
if (source.postData.text) {
68+
reqOpts.body = source.postData.text
69+
}
70+
}
71+
72+
// construct cookies argument
73+
if (source.cookies.length) {
74+
var cookies = ''
75+
source.cookies.forEach(function (cookie) {
76+
cookies = cookies + encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) + '; '
77+
})
78+
if (reqOpts.headers) {
79+
reqOpts.headers.cookie = cookies
80+
} else {
81+
reqOpts.headers = {}
82+
reqOpts.headers.cookie = cookies
83+
}
84+
}
85+
code.blank()
86+
code.push('let url = \'' + url + '\';')
87+
.blank()
88+
code.push('let options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
89+
.blank()
90+
91+
if (includeFS) {
92+
code.unshift('const fs = require(\'fs\');')
93+
}
94+
if (source.postData.mimeType === 'multipart/form-data') {
95+
code.push('options.body = formData;')
96+
.blank()
97+
}
98+
code.push('fetch(url, options)')
99+
.push(1, '.then(res => res.json())')
100+
.push(1, '.then(json => console.log(json))')
101+
.push(1, '.catch(err => console.error(\'error:\' + err));')
102+
103+
return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
104+
}
105+
106+
module.exports.info = {
107+
key: 'fetch',
108+
title: 'Fetch',
109+
link: 'https://github.com/bitinn/node-fetch',
110+
description: 'Simplified HTTP node-fetch client'
111+
}

src/targets/node/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
native: require('./native'),
1212
request: require('./request'),
1313
unirest: require('./unirest'),
14-
axios: require('./axios')
14+
axios: require('./axios'),
15+
fetch: require('./fetch')
1516
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
"title": "Axios",
5555
"link": "https://github.com/axios/axios",
5656
"description": "Promise based HTTP client for the browser and node.js"
57+
},
58+
{
59+
"key": "fetch",
60+
"title": "Fetch",
61+
"link": "https://github.com/bitinn/node-fetch",
62+
"description": "Simplified HTTP node-fetch client"
5763
}
5864
]
5965
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {
6+
method: 'POST',
7+
headers: {'content-type': 'application/x-www-form-urlencoded'},
8+
body: {foo: 'bar', hello: 'world'}
9+
};
10+
11+
fetch(url, options)
12+
.then(res => res.json())
13+
.then(json => console.log(json))
14+
.catch(err => console.error('error:' + err));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {
6+
method: 'POST',
7+
headers: {'content-type': 'application/json'},
8+
body: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
9+
};
10+
11+
fetch(url, options)
12+
.then(res => res.json())
13+
.then(json => console.log(json))
14+
.catch(err => console.error('error:' + err));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {method: 'POST', headers: {cookie: 'foo=bar; bar=baz; '}};
6+
7+
fetch(url, options)
8+
.then(res => res.json())
9+
.then(json => console.log(json))
10+
.catch(err => console.error('error:' + err));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {method: 'PROPFIND'};
6+
7+
fetch(url, options)
8+
.then(res => res.json())
9+
.then(json => console.log(json))
10+
.catch(err => console.error('error:' + err));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {
6+
method: 'POST',
7+
qs: {foo: ['bar', 'baz'], baz: 'abc', key: 'value'},
8+
headers: {
9+
accept: 'application/json',
10+
'content-type': 'application/x-www-form-urlencoded',
11+
cookie: 'foo=bar; bar=baz; '
12+
},
13+
body: {foo: 'bar'}
14+
};
15+
16+
fetch(url, options)
17+
.then(res => res.json())
18+
.then(json => console.log(json))
19+
.catch(err => console.error('error:' + err));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'http://mockbin.com/har';
4+
5+
let options = {method: 'GET', headers: {accept: 'application/json', 'x-foo': 'Bar'}};
6+
7+
fetch(url, options)
8+
.then(res => res.json())
9+
.then(json => console.log(json))
10+
.catch(err => console.error('error:' + err));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetch = require('node-fetch');
2+
3+
let url = 'https://mockbin.com/har';
4+
5+
let options = {method: 'GET'};
6+
7+
fetch(url, options)
8+
.then(res => res.json())
9+
.then(json => console.log(json))
10+
.catch(err => console.error('error:' + err));

0 commit comments

Comments
 (0)