|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for Node.js using Unirest. |
| 4 | + * |
| 5 | + * @author |
| 6 | + * @AhmadNassri |
| 7 | + * |
| 8 | + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict' |
| 12 | + |
| 13 | +var CodeBuilder = require('../../helpers/code-builder') |
| 14 | + |
| 15 | +module.exports = function (source, options) { |
| 16 | + var opts = Object.assign({ |
| 17 | + indent: ' ' |
| 18 | + }, options) |
| 19 | + |
| 20 | + var includeFS = false |
| 21 | + var code = new CodeBuilder(opts.indent) |
| 22 | + |
| 23 | + code.push('const axios = = require("axios");') |
| 24 | + .blank() |
| 25 | + .push('var req = unirest("%s", "%s");', source.method, source.url) |
| 26 | + .blank() |
| 27 | + |
| 28 | + // if (source.cookies.length) { |
| 29 | + // code.push('var CookieJar = unirest.jar();') |
| 30 | + |
| 31 | + // source.cookies.forEach(function (cookie) { |
| 32 | + // code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url) |
| 33 | + // }) |
| 34 | + |
| 35 | + // code.push('req.jar(CookieJar);') |
| 36 | + // .blank() |
| 37 | + // } |
| 38 | + |
| 39 | + // if (Object.keys(source.queryObj).length) { |
| 40 | + // code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)) |
| 41 | + // .blank() |
| 42 | + // } |
| 43 | + |
| 44 | + // if (Object.keys(source.headersObj).length) { |
| 45 | + // code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)) |
| 46 | + // .blank() |
| 47 | + // } |
| 48 | + |
| 49 | + // switch (source.postData.mimeType) { |
| 50 | + // case 'application/x-www-form-urlencoded': |
| 51 | + // if (source.postData.paramsObj) { |
| 52 | + // code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)) |
| 53 | + // } |
| 54 | + // break |
| 55 | + |
| 56 | + // case 'application/json': |
| 57 | + // if (source.postData.jsonObj) { |
| 58 | + // code.push('req.type("json");') |
| 59 | + // .push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)) |
| 60 | + // } |
| 61 | + // break |
| 62 | + |
| 63 | + // case 'multipart/form-data': |
| 64 | + // var multipart = [] |
| 65 | + |
| 66 | + // source.postData.params.forEach(function (param) { |
| 67 | + // var part = {} |
| 68 | + |
| 69 | + // if (param.fileName && !param.value) { |
| 70 | + // includeFS = true |
| 71 | + |
| 72 | + // part.body = 'fs.createReadStream("' + param.fileName + '")' |
| 73 | + // } else if (param.value) { |
| 74 | + // part.body = param.value |
| 75 | + // } |
| 76 | + |
| 77 | + // if (part.body) { |
| 78 | + // if (param.contentType) { |
| 79 | + // part['content-type'] = param.contentType |
| 80 | + // } |
| 81 | + |
| 82 | + // multipart.push(part) |
| 83 | + // } |
| 84 | + // }) |
| 85 | + |
| 86 | + // code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)) |
| 87 | + // break |
| 88 | + |
| 89 | + // default: |
| 90 | + // if (source.postData.text) { |
| 91 | + // code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)) |
| 92 | + // } |
| 93 | + // } |
| 94 | + |
| 95 | + // if (includeFS) { |
| 96 | + // code.unshift('var fs = require("fs");') |
| 97 | + // } |
| 98 | + |
| 99 | + // code.blank() |
| 100 | + // .push('req.end(function (res) {') |
| 101 | + // .push(1, 'if (res.error) throw new Error(res.error);') |
| 102 | + // .blank() |
| 103 | + // .push(1, 'console.log(res.body);') |
| 104 | + // .push('});') |
| 105 | + // .blank() |
| 106 | + |
| 107 | + return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")') |
| 108 | +} |
| 109 | + |
| 110 | +module.exports.info = { |
| 111 | + key: 'axios', |
| 112 | + title: 'Axios', |
| 113 | + link: 'https://github.com/axios/axios', |
| 114 | + description: 'Promise based HTTP client for the browser and node.js - axios/axios.' |
| 115 | +} |
0 commit comments