Skip to content

Commit e631159

Browse files
authored
Merge pull request #176 from rohit-gohri/axios
Add Axios Target
2 parents 68cfbd0 + 40cde9d commit e631159

39 files changed

+610
-3
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ module.exports.addTargetClient = function (target, client) {
242242
throw new Error('The supplied custom target client must contain an `info` object.')
243243
} else if (!('key' in client.info) || !('title' in client.info)) {
244244
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
245+
} else if (targets[target].hasOwnProperty(client.info.key)) {
246+
throw new Error('The supplied custom target client already exists, please use a different key')
245247
}
246248

247249
targets[target][client.info.key] = client

src/targets/javascript/axios.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Javascript & Node.js using Axios.
4+
*
5+
* @author
6+
* @rohit-gohri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
'use strict'
11+
12+
var util = require('util')
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 code = new CodeBuilder(opts.indent)
22+
23+
code.push('import axios from "axios";')
24+
.blank()
25+
26+
var reqOpts = {
27+
method: source.method,
28+
url: source.url
29+
}
30+
31+
if (Object.keys(source.queryObj).length) {
32+
reqOpts.params = source.queryObj
33+
}
34+
35+
if (Object.keys(source.allHeaders).length) {
36+
reqOpts.headers = source.allHeaders
37+
}
38+
39+
switch (source.postData.mimeType) {
40+
case 'application/x-www-form-urlencoded':
41+
reqOpts.data = source.postData.paramsObj
42+
break
43+
44+
case 'application/json':
45+
if (source.postData.jsonObj) {
46+
reqOpts.data = source.postData.jsonObj
47+
}
48+
break
49+
50+
case 'multipart/form-data':
51+
code.push('const form = new FormData();')
52+
53+
source.postData.params.forEach(function (param) {
54+
code.push(
55+
'form.append(%s, %s);',
56+
JSON.stringify(param.name),
57+
JSON.stringify(param.value || param.fileName || '')
58+
)
59+
})
60+
61+
code.blank()
62+
63+
reqOpts.data = '[form]'
64+
break
65+
66+
default:
67+
if (source.postData.text) {
68+
reqOpts.data = source.postData.text
69+
}
70+
}
71+
72+
code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form'))
73+
.blank()
74+
75+
code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
76+
.push(1, 'console.log(response.data);')
77+
.push('}).catch(%s', 'function (error) {')
78+
.push(1, 'console.error(error);')
79+
.push('});')
80+
81+
return code.join()
82+
}
83+
84+
module.exports.info = {
85+
key: 'axios',
86+
title: 'Axios',
87+
link: 'https://github.com/axios/axios',
88+
description: 'Promise based HTTP client for the browser and node.js'
89+
}

src/targets/javascript/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010

1111
jquery: require('./jquery'),
1212
fetch: require('./fetch'),
13-
xhr: require('./xhr')
13+
xhr: require('./xhr'),
14+
axios: require('./axios')
1415
}

src/targets/node/axios.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for Javascript & Node.js using Axios.
4+
*
5+
* @author
6+
* @rohit-gohri
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
'use strict'
11+
12+
var util = require('util')
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 code = new CodeBuilder(opts.indent)
22+
23+
code.push('var axios = require("axios").default;')
24+
.blank()
25+
26+
var reqOpts = {
27+
method: source.method,
28+
url: source.url
29+
}
30+
31+
if (Object.keys(source.queryObj).length) {
32+
reqOpts.params = source.queryObj
33+
}
34+
35+
if (Object.keys(source.allHeaders).length) {
36+
reqOpts.headers = source.allHeaders
37+
}
38+
39+
switch (source.postData.mimeType) {
40+
case 'application/x-www-form-urlencoded':
41+
reqOpts.data = source.postData.paramsObj
42+
break
43+
44+
case 'application/json':
45+
if (source.postData.jsonObj) {
46+
reqOpts.data = source.postData.jsonObj
47+
}
48+
break
49+
50+
default:
51+
if (source.postData.text) {
52+
reqOpts.data = source.postData.text
53+
}
54+
}
55+
56+
code.push('var options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
57+
.blank()
58+
59+
code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
60+
.push(1, 'console.log(response.data);')
61+
.push('}).catch(%s', 'function (error) {')
62+
.push(1, 'console.error(error);')
63+
.push('});')
64+
65+
return code.join()
66+
}
67+
68+
module.exports.info = {
69+
key: 'axios',
70+
title: 'Axios',
71+
link: 'https://github.com/axios/axios',
72+
description: 'Promise based HTTP client for the browser and node.js'
73+
}

src/targets/node/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010

1111
native: require('./native'),
1212
request: require('./request'),
13-
unirest: require('./unirest')
13+
unirest: require('./unirest'),
14+
axios: require('./axios')
1415
}

test/fixtures/available-targets.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
"title": "Unirest",
4949
"link": "http://unirest.io/nodejs.html",
5050
"description": "Lightweight HTTP Request Client Library"
51+
},
52+
{
53+
"key": "axios",
54+
"title": "Axios",
55+
"link": "https://github.com/axios/axios",
56+
"description": "Promise based HTTP client for the browser and node.js"
5157
}
5258
]
5359
},
@@ -74,6 +80,12 @@
7480
"title": "XMLHttpRequest",
7581
"link": "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest",
7682
"description": "W3C Standard API that provides scripted client functionality"
83+
},
84+
{
85+
"key": "axios",
86+
"title": "Axios",
87+
"link": "https://github.com/axios/axios",
88+
"description": "Promise based HTTP client for the browser and node.js"
7789
}
7890
]
7991
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import axios from "axios";
2+
3+
const options = {
4+
method: 'POST',
5+
url: 'http://mockbin.com/har',
6+
headers: {'content-type': 'application/x-www-form-urlencoded'},
7+
data: {foo: 'bar', hello: 'world'}
8+
};
9+
10+
axios.request(options).then(function (response) {
11+
console.log(response.data);
12+
}).catch(function (error) {
13+
console.error(error);
14+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import axios from "axios";
2+
3+
const options = {
4+
method: 'POST',
5+
url: 'http://mockbin.com/har',
6+
headers: {'content-type': 'application/json'},
7+
data: {
8+
number: 1,
9+
string: 'f"oo',
10+
arr: [1, 2, 3],
11+
nested: {a: 'b'},
12+
arr_mix: [1, 'a', {arr_mix_nested: {}}],
13+
boolean: false
14+
}
15+
};
16+
17+
axios.request(options).then(function (response) {
18+
console.log(response.data);
19+
}).catch(function (error) {
20+
console.error(error);
21+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import axios from "axios";
2+
3+
const options = {
4+
method: 'POST',
5+
url: 'http://mockbin.com/har',
6+
headers: {cookie: 'foo=bar; bar=baz'}
7+
};
8+
9+
axios.request(options).then(function (response) {
10+
console.log(response.data);
11+
}).catch(function (error) {
12+
console.error(error);
13+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import axios from "axios";
2+
3+
const options = {method: 'PROPFIND', url: 'http://mockbin.com/har'};
4+
5+
axios.request(options).then(function (response) {
6+
console.log(response.data);
7+
}).catch(function (error) {
8+
console.error(error);
9+
});

0 commit comments

Comments
 (0)