1212
1313let CodeBuilder = require ( '../../helpers/code-builder' )
1414
15+ const contentBodyFactory = ( contentType , postData , includeFS = false ) => {
16+ switch ( contentType ) {
17+ case 'application/x-www-form-urlencoded' :
18+ return [ postData . paramsObj , includeFS ]
19+ case 'application/json' :
20+ return [ postData . jsonObj , includeFS ]
21+ case 'multipart/form-data' :
22+ let multipart = [ ]
23+
24+ postData . params . forEach ( ( param ) => {
25+ let part = { }
26+
27+ if ( param . fileName && ! param . value ) {
28+ includeFS = true
29+
30+ part . body = 'fs.createReadStream("' + param . fileName + '")'
31+ } else if ( param . value ) {
32+ part . body = param . value
33+ }
34+
35+ if ( part . body ) {
36+ if ( param . contentType ) {
37+ part [ 'content-type' ] = param . contentType
38+ }
39+
40+ multipart . push ( part )
41+ }
42+ } )
43+ return [ multipart , includeFS ]
44+ default :
45+ if ( postData . text ) {
46+ return [ postData . text , includeFS ]
47+ }
48+ return null
49+ }
50+
51+ }
1552module . exports = function ( source , options ) {
1653 let opts = Object . assign ( {
1754 indent : ' '
1855 } , options )
1956
20- let includeFS = false
57+
2158 let code = new CodeBuilder ( opts . indent )
2259
2360 code . push ( 'const axios = require("axios");' )
@@ -31,57 +68,17 @@ module.exports = function (source, options) {
3168 } ,
3269 params : Object . keys ( source . queryObj ) . length ? source . queryObj : undefined ,
3370 url : `${ source . url } `
34- } ;
3571
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- // }
72+ } ;
73+ const { postData } = source
74+ const { mimeType } = postData
75+ const [ data , includeFS ] = contentBodyFactory ( mimeType , postData )
76+ if ( data ) {
77+ requestOptions [ 'data' ] = data
78+ }
79+ if ( includeFS ) {
80+ code . unshift ( 'const fs = require("fs");' )
81+ }
8582 code . push ( `axios(${
8683 JSON . stringify ( requestOptions )
8784 } )
0 commit comments