Code coverage report for src/targets/node/unirest.js

Statements: 98.15% (53 / 54)      Branches: 82.14% (23 / 28)      Functions: 100% (3 / 3)      Lines: 98.15% (53 / 54)      Ignored: none     

All files » src/targets/node/ » unirest.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104    1   1 10       10 10   10 10   10 2   2 4     2 2     10 2 2     10 7 7     10   2 2   2     1 1 1   1     3   3 3   3 1   1 2 2     3 3 2     3       3 3     4         10 1     10 10 10 10 10 10 10   10     1            
'use strict'
 
var util = require('util')
 
module.exports = function (source, options) {
  var opts = util._extend({
    indent: '  '
  }, options)
 
  var includeFS = false
  var code = ['var unirest = require("unirest");', null]
 
  code.push(util.format('var req = unirest("%s", "%s");', source.method, source.url))
  code.push(null)
 
  if (source.cookies.length) {
    code.push('var CookieJar = unirest.jar();')
 
    source.cookies.forEach(function (cookie) {
      code.push(util.format('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url))
    })
 
    code.push('req.jar(CookieJar);')
    code.push(null)
  }
 
  if (Object.keys(source.queryObj).length) {
    code.push(util.format('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)))
    code.push(null)
  }
 
  if (Object.keys(source.headersObj).length) {
    code.push(util.format('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)))
    code.push(null)
  }
 
  switch (source.postData.mimeType) {
    case 'application/x-www-form-urlencoded':
      Eif (source.postData.paramsObj) {
        code.push(util.format('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)))
      }
      break
 
    case 'application/json':
      Eif (source.postData.jsonObj) {
        code.push('req.type("json");')
        code.push(util.format('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)))
      }
      break
 
    case 'multipart/form-data':
      var multipart = []
 
      source.postData.params.forEach(function (param) {
        var part = {}
 
        if (param.fileName && !param.value) {
          includeFS = true
 
          part.body = 'fs.createReadStream("' + param.fileName + '")'
        } else Eif (param.value) {
          part.body = param.value
        }
 
        Eif (part.body) {
          if (param.contentType) {
            part['content-type'] = param.contentType
          }
 
          multipart.push(part)
        }
      })
 
      code.push(util.format('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)))
      break
 
    default:
      Iif (source.postData.text) {
        code.push(opts.indent + util.format('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)))
      }
  }
 
  if (includeFS) {
    code.unshift('var fs = require("fs");')
  }
 
  code.push(null)
  code.push('req.end(function (res) {')
  code.push(opts.indent + 'if (res.error) throw new Error(res.error);')
  code.push(null)
  code.push(opts.indent + 'console.log(res.body);')
  code.push('});')
  code.push(null)
 
  return code.join('\n').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")')
}
 
module.exports.info = {
  key: 'unirest',
  title: 'Unirest',
  link: 'http://unirest.io/',
  description: 'Lightweight HTTP Request Client Library'
}