|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for the Shell using cURL. |
| 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 util = require('util') |
| 14 | +var helpers = require('../httpsnippet/helpers/shell') |
| 15 | +var CodeBuilder = require('../httpsnippet/helpers/code-builder') |
| 16 | + |
| 17 | +module.exports = function (source, options) { |
| 18 | + var opts = util._extend({ |
| 19 | + indent: ' ', |
| 20 | + short: false, |
| 21 | + binary: false |
| 22 | + }, options) |
| 23 | + |
| 24 | + var code = new CodeBuilder(opts.indent, opts.indent !== false ? ' \\\n' + opts.indent : ' ') |
| 25 | + |
| 26 | + code.push('curl %s %s', opts.short ? '-X' : '--request', source.method) |
| 27 | + .push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl))) |
| 28 | + |
| 29 | + if (source.httpVersion === 'HTTP/1.0') { |
| 30 | + code.push(opts.short ? '-0' : '--http1.0') |
| 31 | + } |
| 32 | + |
| 33 | + // construct headers |
| 34 | + Object.keys(source.headersObj).sort().forEach(function (key) { |
| 35 | + var header = util.format('%s: %s', key, source.headersObj[key]) |
| 36 | + code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header)) |
| 37 | + }) |
| 38 | + |
| 39 | + if (source.allHeaders.cookie) { |
| 40 | + code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie)) |
| 41 | + } |
| 42 | + |
| 43 | + // construct post params |
| 44 | + switch (source.postData.mimeType) { |
| 45 | + case 'multipart/form-data': |
| 46 | + source.postData.params.map(function (param) { |
| 47 | + var post = util.format('%s=%s', param.name, param.value) |
| 48 | + |
| 49 | + if (param.fileName && !param.value) { |
| 50 | + post = util.format('%s=@%s', param.name, param.fileName) |
| 51 | + } |
| 52 | + |
| 53 | + code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post)) |
| 54 | + }) |
| 55 | + break |
| 56 | + |
| 57 | + case 'application/x-www-form-urlencoded': |
| 58 | + if (source.postData.params) { |
| 59 | + source.postData.params.map(function (param) { |
| 60 | + code.push( |
| 61 | + '%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'), |
| 62 | + helpers.quote(util.format('%s=%s', param.name, param.value)) |
| 63 | + ) |
| 64 | + }) |
| 65 | + } else { |
| 66 | + code.push( |
| 67 | + '%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'), |
| 68 | + helpers.escape(helpers.quote(source.postData.text)) |
| 69 | + ) |
| 70 | + } |
| 71 | + break |
| 72 | + |
| 73 | + default: |
| 74 | + // raw request body |
| 75 | + if (source.postData.text) { |
| 76 | + code.push( |
| 77 | + '%s %s', opts.binary ? '--data-binary' : (opts.short ? '-d' : '--data'), |
| 78 | + helpers.escape(helpers.quote(source.postData.text)) |
| 79 | + ) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return code.join() |
| 84 | +} |
| 85 | + |
| 86 | +module.exports.info = { |
| 87 | + key: 'curl', |
| 88 | + title: 'cURL', |
| 89 | + link: 'http://curl.haxx.se/', |
| 90 | + description: 'cURL is a command line tool and library for transferring data with URL syntax' |
| 91 | +} |
0 commit comments