Skip to content

Commit 3cf4b47

Browse files
author
Sefi Krausz
committed
changes to axios target
1 parent d80813f commit 3cf4b47

1 file changed

Lines changed: 56 additions & 84 deletions

File tree

‎src/targets/node/axios.js‎

Lines changed: 56 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,78 @@
1010

1111
'use strict'
1212

13-
var CodeBuilder = require('../../helpers/code-builder')
13+
let CodeBuilder = require('../../helpers/code-builder')
1414

1515
module.exports = function (source, options) {
16-
var opts = Object.assign({
16+
let opts = Object.assign({
1717
indent: ' '
1818
}, options)
1919

20-
var includeFS = false
21-
var code = new CodeBuilder(opts.indent)
20+
let includeFS = false
21+
let code = new CodeBuilder(opts.indent)
2222

2323
code.push('const axios = require("axios");')
2424
.blank()
25-
// .push('var req = unirest("%s", "%s");', source.method, source.url)
26-
// .blank()
2725

28-
let requestOptions = {
26+
const requestOptions = {
2927
method: `${source.method}`,
3028
headers: {
3129
'content-type': `${source.postData.mimeType}`,
3230
...(Object.keys(source.headersObj).length && source.headersObj)
3331
},
34-
params: Object.keys(source.queryObj).length && source.queryObj,
35-
url: `${source.url}`,
32+
params: Object.keys(source.queryObj).length ? source.queryObj : undefined,
33+
url: `${source.url}`
3634
};
3735

36+
switch (source.postData.mimeType) {
37+
case 'application/x-www-form-urlencoded':
38+
if (source.postData.paramsObj) {
39+
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
40+
}
41+
break
42+
43+
case 'application/json':
44+
if (source.postData.jsonObj) {
45+
code.push('req.type("json");')
46+
.push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
47+
}
48+
break
49+
50+
case 'multipart/form-data':
51+
var multipart = []
52+
53+
source.postData.params.forEach(function (param) {
54+
var part = {}
55+
56+
if (param.fileName && !param.value) {
57+
includeFS = true
58+
59+
part.body = 'fs.createReadStream("' + param.fileName + '")'
60+
} else if (param.value) {
61+
part.body = param.value
62+
}
63+
64+
if (part.body) {
65+
if (param.contentType) {
66+
part['content-type'] = param.contentType
67+
}
68+
69+
multipart.push(part)
70+
}
71+
})
72+
73+
code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))
74+
break
75+
76+
default:
77+
if (source.postData.text) {
78+
code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
79+
}
80+
}
81+
82+
// if (includeFS) {
83+
// code.unshift('var fs = require("fs");')
84+
// }
3885
code.push(`axios(${
3986
JSON.stringify(requestOptions)
4087
})
@@ -44,81 +91,6 @@ module.exports = function (source, options) {
4491
.catch((error)=>{
4592
console.log(error)
4693
})`)
47-
// .push(`.then((response)=>{
48-
// console.log(response)
49-
// })`)
50-
// .push(`.catch((error)=>{
51-
// console.log(error)
52-
// })`)
53-
54-
//TODO implement params
55-
// if (Object.keys(source.queryObj).length) {
56-
// code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent))
57-
// .blank()
58-
// }
59-
60-
// if (Object.keys(source.headersObj).length) {
61-
// code.push('req.headers(%s);', JSON.stringify(source.headersObj))
62-
63-
// }
64-
65-
// switch (source.postData.mimeType) {
66-
// case 'application/x-www-form-urlencoded':
67-
// if (source.postData.paramsObj) {
68-
// code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
69-
// }
70-
// break
71-
72-
// case 'application/json':
73-
// if (source.postData.jsonObj) {
74-
// code.push('req.type("json");')
75-
// .push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
76-
// }
77-
// break
78-
79-
// case 'multipart/form-data':
80-
// var multipart = []
81-
82-
// source.postData.params.forEach(function (param) {
83-
// var part = {}
84-
85-
// if (param.fileName && !param.value) {
86-
// includeFS = true
87-
88-
// part.body = 'fs.createReadStream("' + param.fileName + '")'
89-
// } else if (param.value) {
90-
// part.body = param.value
91-
// }
92-
93-
// if (part.body) {
94-
// if (param.contentType) {
95-
// part['content-type'] = param.contentType
96-
// }
97-
98-
// multipart.push(part)
99-
// }
100-
// })
101-
102-
// code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))
103-
// break
104-
105-
// default:
106-
// if (source.postData.text) {
107-
// code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
108-
// }
109-
// }
110-
111-
// if (includeFS) {
112-
// code.unshift('var fs = require("fs");')
113-
// }
114-
115-
// code.blank()
116-
// .push('req.end(function (res) {')
117-
// .push(1, 'if (res.error) throw new Error(res.error);')
118-
// .blank()
119-
// .push(1, 'console.log(res.body);')
120-
// .push('});')
121-
// .blank()
12294

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

0 commit comments

Comments
 (0)