Skip to content

Commit e5c2281

Browse files
committed
adding JavaScript target (XHR + jQuery)
1 parent 33afa54 commit e5c2281

29 files changed

Lines changed: 517 additions & 10 deletions

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"devDependencies": {
3737
"codeclimate-test-reporter": "0.0.4",
3838
"glob": "^5.0.3",
39-
"husky": "^0.7.0",
4039
"istanbul": "^0.3.8",
4140
"mocha": "^2.2.1",
4241
"should": "^5.2.0"

src/targets/javascript/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

src/targets/javascript/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'javascript',
5+
title: 'JavaScript',
6+
extname: '.js',
7+
default: 'xhr'
8+
}

src/targets/javascript/jquery.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for native XMLHttpRequest
4+
*
5+
* @author
6+
* @AhmadNassri
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 util = require('util')
14+
15+
module.exports = function (source, options) {
16+
var opts = util._extend({
17+
indent: ' '
18+
}, options)
19+
20+
var code = []
21+
22+
var settings = {
23+
async: true,
24+
crossDomain: true,
25+
url: source.fullUrl,
26+
method: source.method,
27+
headers: source.allHeaders
28+
}
29+
30+
switch (source.postData.mimeType) {
31+
case 'application/x-www-form-urlencoded':
32+
settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text
33+
break
34+
35+
case 'application/json':
36+
settings.processData = false
37+
settings.data = source.postData.text
38+
break
39+
40+
case 'multipart/form-data':
41+
code.push('var form = new FormData();')
42+
43+
source.postData.params.map(function (param) {
44+
code.push(util.format('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || '')))
45+
})
46+
47+
settings.processData = false
48+
settings.contentType = false
49+
settings.mimeType = 'multipart/form-data'
50+
settings.data = '[form]'
51+
52+
// remove the contentType header
53+
if (~settings.headers['content-type'].indexOf('boundary')) {
54+
delete settings.headers['content-type']
55+
}
56+
code.push(null)
57+
break
58+
59+
default:
60+
if (source.postData.text) {
61+
settings.data = source.postData.text
62+
}
63+
}
64+
65+
code.push('var settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form'))
66+
67+
code.push(null)
68+
69+
code.push('$.ajax(settings).done(function (response) {\n\tconsole.log(response);\n});'.replace(/\t/g, opts.indent))
70+
71+
return code.join('\n')
72+
}
73+
74+
module.exports.info = {
75+
key: 'jquery',
76+
title: 'jQuery',
77+
link: 'http://api.jquery.com/jquery.ajax/',
78+
description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery'
79+
}

src/targets/javascript/xhr.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for native XMLHttpRequest
4+
*
5+
* @author
6+
* @AhmadNassri
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 util = require('util')
14+
15+
module.exports = function (source, options) {
16+
var opts = util._extend({
17+
indent: ' ',
18+
cors: true
19+
}, options)
20+
21+
var code = []
22+
23+
switch (source.postData.mimeType) {
24+
case 'application/json':
25+
code.push(util.format('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)))
26+
code.push(null)
27+
break
28+
29+
case 'multipart/form-data':
30+
code.push('var data = new FormData();')
31+
32+
source.postData.params.map(function (param) {
33+
code.push(util.format('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || '')))
34+
})
35+
36+
// remove the contentType header
37+
if (source.allHeaders['content-type'].indexOf('boundary')) {
38+
delete source.allHeaders['content-type']
39+
}
40+
41+
code.push(null)
42+
break
43+
44+
default:
45+
code.push(util.format('var data = %s;', JSON.stringify(source.postData.text || null)))
46+
code.push(null)
47+
}
48+
49+
code.push('var xhr = new XMLHttpRequest();')
50+
51+
if (opts.cors) {
52+
code.push('xhr.withCredentials = true;')
53+
}
54+
55+
code.push(null)
56+
57+
code.push('xhr.addEventListener("readystatechange", function () {\n\tif (this.readyState === this.DONE) {\n\t\tconsole.log(this.responseText);\n\t}\n});'.replace(/\t/g, opts.indent))
58+
59+
code.push(null)
60+
61+
code.push(util.format('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl)))
62+
63+
Object.keys(source.allHeaders).map(function (key) {
64+
code.push(util.format('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key])))
65+
})
66+
67+
code.push(null)
68+
69+
code.push('xhr.send(data);')
70+
71+
return code.join('\n')
72+
}
73+
74+
module.exports.info = {
75+
key: 'xhr',
76+
title: 'XMLHttpRequest',
77+
link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
78+
description: 'W3C Standard API that provides scripted client functionality'
79+
}

test/fixtures/available-targets.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@
5151
}
5252
]
5353
},
54+
{
55+
"key": "javascript",
56+
"title": "JavaScript",
57+
"extname": ".js",
58+
"default": "xhr",
59+
"clients": [
60+
{
61+
"key": "jquery",
62+
"title": "jQuery",
63+
"link": "http://api.jquery.com/jquery.ajax/",
64+
"description": "Perform an asynchronous HTTP (Ajax) requests with jQuery"
65+
},
66+
{
67+
"key": "xhr",
68+
"title": "XMLHttpRequest",
69+
"link": "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest",
70+
"description": "W3C Standard API that provides scripted client functionality"
71+
}
72+
]
73+
},
5474
{
5575
"key": "ocaml",
5676
"title": "OCaml",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har",
5+
"method": "POST",
6+
"headers": {
7+
"content-type": "application/x-www-form-urlencoded"
8+
},
9+
"data": {
10+
"foo": "bar",
11+
"hello": "world"
12+
}
13+
}
14+
15+
$.ajax(settings).done(function (response) {
16+
console.log(response);
17+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har",
5+
"method": "POST",
6+
"headers": {
7+
"content-type": "application/json"
8+
},
9+
"processData": false,
10+
"data": "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }"
11+
}
12+
13+
$.ajax(settings).done(function (response) {
14+
console.log(response);
15+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har",
5+
"method": "POST",
6+
"headers": {
7+
"cookie": "foo=bar; bar=baz"
8+
}
9+
}
10+
11+
$.ajax(settings).done(function (response) {
12+
console.log(response);
13+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var settings = {
2+
"async": true,
3+
"crossDomain": true,
4+
"url": "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value",
5+
"method": "POST",
6+
"headers": {
7+
"cookie": "foo=bar; bar=baz",
8+
"accept": "application/json",
9+
"content-type": "application/x-www-form-urlencoded"
10+
},
11+
"data": {
12+
"foo": "bar"
13+
}
14+
}
15+
16+
$.ajax(settings).done(function (response) {
17+
console.log(response);
18+
});

0 commit comments

Comments
 (0)