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
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ module.exports.addTargetClient = function (target, client) {
throw new Error('The supplied custom target client must contain an `info` object.')
} else if (!('key' in client.info) || !('title' in client.info)) {
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
} else if (targets[target].hasOwnProperty(client.info.key)) {
throw new Error('The supplied custom target client already exists, please use a different key')
}

targets[target][client.info.key] = client
Expand Down
89 changes: 89 additions & 0 deletions src/targets/javascript/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @description
* HTTP code snippet generator for Javascript & Node.js using Axios.
*
* @author
* @rohit-gohri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'

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

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

var code = new CodeBuilder(opts.indent)

code.push('import axios from "axios";')
.blank()

var reqOpts = {
method: source.method,
url: source.url
}

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

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

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

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

case 'multipart/form-data':
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 || '')
)
})

code.blank()

reqOpts.data = '[form]'
break

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

code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form'))
.blank()

code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
.push(1, 'console.log(response.data);')
.push('}).catch(%s', 'function (error) {')
.push(1, 'console.error(error);')
.push('});')

return code.join()
}

module.exports.info = {
key: 'axios',
title: 'Axios',
link: 'https://github.com/axios/axios',
description: 'Promise based HTTP client for the browser and node.js'
}
3 changes: 2 additions & 1 deletion src/targets/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {

jquery: require('./jquery'),
fetch: require('./fetch'),
xhr: require('./xhr')
xhr: require('./xhr'),
axios: require('./axios')
}
73 changes: 73 additions & 0 deletions src/targets/node/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @description
* HTTP code snippet generator for Javascript & Node.js using Axios.
*
* @author
* @rohit-gohri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'

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

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

var code = new CodeBuilder(opts.indent)

code.push('var axios = require("axios").default;')
.blank()

var reqOpts = {
method: source.method,
url: source.url
}

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

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

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

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

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

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

code.push(util.format('axios.request(options).then(%s', 'function (response) {'))
.push(1, 'console.log(response.data);')
.push('}).catch(%s', 'function (error) {')
.push(1, 'console.error(error);')
.push('});')

return code.join()
}

module.exports.info = {
key: 'axios',
title: 'Axios',
link: 'https://github.com/axios/axios',
description: 'Promise based HTTP client for the browser and node.js'
}
3 changes: 2 additions & 1 deletion src/targets/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {

native: require('./native'),
request: require('./request'),
unirest: require('./unirest')
unirest: require('./unirest'),
axios: require('./axios')
}
12 changes: 12 additions & 0 deletions test/fixtures/available-targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
"title": "Unirest",
"link": "http://unirest.io/nodejs.html",
"description": "Lightweight HTTP Request Client Library"
},
{
"key": "axios",
"title": "Axios",
"link": "https://github.com/axios/axios",
"description": "Promise based HTTP client for the browser and node.js"
}
]
},
Expand All @@ -74,6 +80,12 @@
"title": "XMLHttpRequest",
"link": "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest",
"description": "W3C Standard API that provides scripted client functionality"
},
{
"key": "axios",
"title": "Axios",
"link": "https://github.com/axios/axios",
"description": "Promise based HTTP client for the browser and node.js"
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/output/javascript/axios/application-form-encoded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'application/x-www-form-urlencoded'},
data: {foo: 'bar', hello: 'world'}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
21 changes: 21 additions & 0 deletions test/fixtures/output/javascript/axios/application-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'application/json'},
data: {
number: 1,
string: 'f"oo',
arr: [1, 2, 3],
nested: {a: 'b'},
arr_mix: [1, 'a', {arr_mix_nested: {}}],
boolean: false
}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
13 changes: 13 additions & 0 deletions test/fixtures/output/javascript/axios/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {cookie: 'foo=bar; bar=baz'}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
9 changes: 9 additions & 0 deletions test/fixtures/output/javascript/axios/custom-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import axios from "axios";

const options = {method: 'PROPFIND', url: 'http://mockbin.com/har'};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
19 changes: 19 additions & 0 deletions test/fixtures/output/javascript/axios/full.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
params: {foo: ['bar', 'baz'], baz: 'abc', key: 'value'},
headers: {
cookie: 'foo=bar; bar=baz',
accept: 'application/json',
'content-type': 'application/x-www-form-urlencoded'
},
data: {foo: 'bar'}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
13 changes: 13 additions & 0 deletions test/fixtures/output/javascript/axios/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from "axios";

const options = {
method: 'GET',
url: 'http://mockbin.com/har',
headers: {accept: 'application/json', 'x-foo': 'Bar'}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
9 changes: 9 additions & 0 deletions test/fixtures/output/javascript/axios/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import axios from "axios";

const options = {method: 'GET', url: 'https://mockbin.com/har'};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
14 changes: 14 additions & 0 deletions test/fixtures/output/javascript/axios/jsonObj-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'application/json'},
data: {foo: 'bar'}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
14 changes: 14 additions & 0 deletions test/fixtures/output/javascript/axios/jsonObj-null-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from "axios";

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'application/json'},
data: {foo: null}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
17 changes: 17 additions & 0 deletions test/fixtures/output/javascript/axios/multipart-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import axios from "axios";

const form = new FormData();
form.append("foo", "Hello World");

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'},
data: '[form]'
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
17 changes: 17 additions & 0 deletions test/fixtures/output/javascript/axios/multipart-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import axios from "axios";

const form = new FormData();
form.append("foo", "test/fixtures/files/hello.txt");

const options = {
method: 'POST',
url: 'http://mockbin.com/har',
headers: {'content-type': 'multipart/form-data; boundary=---011000010111000001101001'},
data: '[form]'
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
Loading