Skip to content

Commit

Permalink
feat: updating javascript targets to use const instead of var
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Aug 7, 2020
1 parent 692f540 commit cd034d0
Show file tree
Hide file tree
Showing 85 changed files with 247 additions and 250 deletions.
2 changes: 1 addition & 1 deletion src/targets/javascript/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = function (source, options) {
break

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

source.postData.params.forEach(function (param) {
code.push(
Expand Down
4 changes: 2 additions & 2 deletions src/targets/javascript/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function (source, options) {
break

case 'multipart/form-data':
code.push('var form = new FormData();')
code.push('const 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 || ''))
Expand All @@ -62,7 +62,7 @@ module.exports = function (source, options) {
}
}

code.push('var settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form'))
code.push('const settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form') + ';')
.blank()
.push('$.ajax(settings).done(function (response) {')
.push(1, 'console.log(response);')
Expand Down
8 changes: 4 additions & 4 deletions src/targets/javascript/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ module.exports = function (source, options) {

switch (source.postData.mimeType) {
case 'application/json':
code.push('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
.push(null)
break

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

source.postData.params.forEach(function (param) {
code.push('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
Expand All @@ -42,11 +42,11 @@ module.exports = function (source, options) {
break

default:
code.push('var data = %s;', JSON.stringify(source.postData.text || null))
code.push('const data = %s;', JSON.stringify(source.postData.text || null))
.blank()
}

code.push('var xhr = new XMLHttpRequest();')
code.push('const xhr = new XMLHttpRequest();')

if (opts.cors) {
code.push('xhr.withCredentials = true;')
Expand Down
12 changes: 6 additions & 6 deletions src/targets/node/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ module.exports = function (source, options) {
headers: source.allHeaders
}

code.push('var http = require("%s");', source.uriObj.protocol.replace(':', ''))
code.push('const http = require("%s");', source.uriObj.protocol.replace(':', ''))

code.blank()
.push('var options = %s;', JSON.stringify(reqOpts, null, opts.indent))
.push('const options = %s;', JSON.stringify(reqOpts, null, opts.indent))
.blank()
.push('var req = http.request(options, function (res) {')
.push(1, 'var chunks = [];')
.push('const req = http.request(options, function (res) {')
.push(1, 'const chunks = [];')
.blank()
.push(1, 'res.on("data", function (chunk) {')
.push(2, 'chunks.push(chunk);')
.push(1, '});')
.blank()
.push(1, 'res.on("end", function () {')
.push(2, 'var body = Buffer.concat(chunks);')
.push(2, 'const body = Buffer.concat(chunks);')
.push(2, 'console.log(body.toString());')
.push(1, '});')
.push('});')
Expand All @@ -50,7 +50,7 @@ module.exports = function (source, options) {
switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
if (source.postData.paramsObj) {
code.unshift('var qs = require("querystring");')
code.unshift('const qs = require("querystring");')
code.push('req.write(qs.stringify(%s));', stringifyObject(source.postData.paramsObj, {
indent: ' ',
inlineCharacterLimit: 80
Expand Down
10 changes: 5 additions & 5 deletions src/targets/node/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (source, options) {
var includeFS = false
var code = new CodeBuilder(opts.indent)

code.push('var request = require("request");')
code.push("const request = require('request');")
.blank()

var reqOpts = {
Expand Down Expand Up @@ -90,21 +90,21 @@ module.exports = function (source, options) {
if (source.cookies.length) {
reqOpts.jar = 'JAR'

code.push('var jar = request.jar();')
code.push('const jar = request.jar();')

var url = source.url

source.cookies.forEach(function (cookie) {
code.push('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)
code.push("jar.setCookie(request.cookie('%s=%s'), '%s');", encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)
})
code.blank()
}

if (includeFS) {
code.unshift('var fs = require("fs");')
code.unshift("const fs = require('fs');")
}

code.push('var options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
.blank()

code.push(util.format('request(options, %s', 'function (error, response, body) {'))
Expand Down
19 changes: 11 additions & 8 deletions src/targets/node/unirest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ module.exports = function (source, options) {
var includeFS = false
var code = new CodeBuilder(opts.indent)

code.push('var unirest = require("unirest");')
code.push('const unirest = require("unirest");')
.blank()
.push('var req = unirest("%s", "%s");', source.method, source.url)
.push('const req = unirest("%s", "%s");', source.method, source.url)
.blank()

if (source.cookies.length) {
code.push('var CookieJar = unirest.jar();')
code.push('const CookieJar = unirest.jar();')

source.cookies.forEach(function (cookie) {
code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)
})

code.push('req.jar(CookieJar);')
.blank()
.blank()
}

if (Object.keys(source.queryObj).length) {
Expand All @@ -50,13 +50,15 @@ module.exports = function (source, options) {
case 'application/x-www-form-urlencoded':
if (source.postData.paramsObj) {
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
.blank()
}
break

case 'application/json':
if (source.postData.jsonObj) {
code.push('req.type("json");')
.push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
.blank()
}
break

Expand Down Expand Up @@ -84,20 +86,21 @@ module.exports = function (source, options) {
})

code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))
.blank()
break

default:
if (source.postData.text) {
code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
.blank()
}
}

if (includeFS) {
code.unshift('var fs = require("fs");')
code.unshift('const fs = require("fs");')
}

code.blank()
.push('req.end(function (res) {')
code.push('req.end(function (res) {')
.push(1, 'if (res.error) throw new Error(res.error);')
.blank()
.push(1, 'console.log(res.body);')
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/javascript/fetch/multipart-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "Hello World");

fetch("http://mockbin.com/har", {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/output/javascript/fetch/multipart-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "test/fixtures/files/hello.txt");

fetch("http://mockbin.com/har", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "bar");

fetch("http://mockbin.com/har", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -10,7 +10,7 @@ var settings = {
"foo": "bar",
"hello": "world"
}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/application-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -8,7 +8,7 @@ var settings = {
},
"processData": false,
"data": "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/cookies.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
"method": "POST",
"headers": {
"cookie": "foo=bar; bar=baz"
}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/custom-method.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
"method": "PROPFIND",
"headers": {}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/full.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value",
Expand All @@ -11,7 +11,7 @@ var settings = {
"data": {
"foo": "bar"
}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/headers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -7,7 +7,7 @@ var settings = {
"accept": "application/json",
"x-foo": "Bar"
}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/https.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "https://mockbin.com/har",
"method": "GET",
"headers": {}
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/jsonObj-multiline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -8,7 +8,7 @@ var settings = {
},
"processData": false,
"data": "{\n \"foo\": \"bar\"\n}"
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/output/javascript/jquery/jsonObj-null-value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -8,7 +8,7 @@ var settings = {
},
"processData": false,
"data": "{\"foo\":null}"
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/output/javascript/jquery/multipart-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "Hello World");

var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -11,7 +11,7 @@ var settings = {
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/output/javascript/jquery/multipart-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "test/fixtures/files/hello.txt");

var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -11,7 +11,7 @@ var settings = {
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/output/javascript/jquery/multipart-form-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var form = new FormData();
const form = new FormData();
form.append("foo", "bar");

var settings = {
const settings = {
"async": true,
"crossDomain": true,
"url": "http://mockbin.com/har",
Expand All @@ -11,7 +11,7 @@ var settings = {
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
};

$.ajax(settings).done(function (response) {
console.log(response);
Expand Down
Loading

0 comments on commit cd034d0

Please sign in to comment.