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

'use strict'

var stringifyObject = require('stringify-object')
var CodeBuilder = require('../../helpers/code-builder')

module.exports = function (source, options) {
var opts = Object.assign({
indent: ' '
}, options)

var includeFS = false
var code = new CodeBuilder(opts.indent)

code.push('const fetch = require(\'node-fetch\');')
var url = source.url
var reqOpts = {
method: source.method
}

if (Object.keys(source.queryObj).length) {
reqOpts.qs = source.queryObj
}

if (Object.keys(source.headersObj).length) {
reqOpts.headers = source.headersObj
}

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

case 'application/json':
if (source.postData.jsonObj) {
reqOpts.body = JSON.stringify(source.postData.jsonObj)
}
break

case 'multipart/form-data':
code.unshift('const FormData = require(\'form-data\');')
code.push('const formData = new FormData();')
source.postData.params.forEach(function (param) {
if (!param.fileName && !param.fileName && !param.contentType) {
code.push('formData.append(\'' + param.name + '\',\'' + param.value + '\');')
return
}

if (param.fileName) {
includeFS = true
code.blank()
code.push('formData.append(\'' + param.name + '\', fs.createReadStream(\'' + param.fileName + '\'));')
}
})
break

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

// construct cookies argument
if (source.cookies.length) {
var cookies = ''
source.cookies.forEach(function (cookie) {
cookies = cookies + encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value) + '; '
})
if (reqOpts.headers) {
reqOpts.headers.cookie = cookies
} else {
reqOpts.headers = {}
reqOpts.headers.cookie = cookies
}
}
code.blank()
code.push('let url = \'' + url + '\';')
.blank()
code.push('let options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
.blank()

if (includeFS) {
code.unshift('const fs = require(\'fs\');')
}
if (source.postData.mimeType === 'multipart/form-data') {
code.push('options.body = formData;')
.blank()
}
code.push('fetch(url, options)')
.push(1, '.then(res => res.json())')
.push(1, '.then(json => console.log(json))')
.push(1, '.catch(err => console.error(\'error:\' + err));')

return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
}

module.exports.info = {
key: 'fetch',
title: 'Fetch',
link: 'https://github.com/bitinn/node-fetch',
description: 'Simplified HTTP node-fetch client'
}
3 changes: 2 additions & 1 deletion src/targets/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
native: require('./native'),
request: require('./request'),
unirest: require('./unirest'),
axios: require('./axios')
axios: require('./axios'),
fetch: require('./fetch')
}
6 changes: 6 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"title": "Axios",
"link": "https://github.com/axios/axios",
"description": "Promise based HTTP client for the browser and node.js"
},
{
"key": "fetch",
"title": "Fetch",
"link": "https://github.com/bitinn/node-fetch",
"description": "Simplified HTTP node-fetch client"
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/output/node/fetch/application-form-encoded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'application/x-www-form-urlencoded'},
body: {foo: 'bar', hello: 'world'}
};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
14 changes: 14 additions & 0 deletions test/fixtures/output/node/fetch/application-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let 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(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'POST', headers: {cookie: 'foo=bar; bar=baz; '}};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/custom-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'PROPFIND'};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
19 changes: 19 additions & 0 deletions test/fixtures/output/node/fetch/full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
qs: {foo: ['bar', 'baz'], baz: 'abc', key: 'value'},
headers: {
accept: 'application/json',
'content-type': 'application/x-www-form-urlencoded',
cookie: 'foo=bar; bar=baz; '
},
body: {foo: 'bar'}
};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'GET', headers: {accept: 'application/json', 'x-foo': 'Bar'}};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'https://mockbin.com/har';

let options = {method: 'GET'};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
14 changes: 14 additions & 0 deletions test/fixtures/output/node/fetch/jsonObj-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'application/json'},
body: '{"foo":"bar"}'
};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
14 changes: 14 additions & 0 deletions test/fixtures/output/node/fetch/jsonObj-null-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'application/json'},
body: '{"foo":null}'
};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
20 changes: 20 additions & 0 deletions test/fixtures/output/node/fetch/multipart-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();

formData.append('foo', fs.createReadStream('hello.txt'));

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = formData;

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
20 changes: 20 additions & 0 deletions test/fixtures/output/node/fetch/multipart-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();

formData.append('foo', fs.createReadStream('test/fixtures/files/hello.txt'));

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = formData;

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
18 changes: 18 additions & 0 deletions test/fixtures/output/node/fetch/multipart-form-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();
formData.append('foo','bar');

let url = 'http://mockbin.com/har';

let options = {
method: 'POST',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = formData;

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'GET', qs: {foo: ['bar', 'baz'], baz: 'abc', key: 'value'}};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/short.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'GET'};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
10 changes: 10 additions & 0 deletions test/fixtures/output/node/fetch/text-plain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

let url = 'http://mockbin.com/har';

let options = {method: 'POST', headers: {'content-type': 'text/plain'}, body: 'Hello World'};

fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
3 changes: 3 additions & 0 deletions test/targets/node/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

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