UNPKG

httpsnippet-fsless

Version:

HTTP Request snippet generator for *most* languages without using node-fs

73 lines (58 loc) 2.13 kB
/** * @description * HTTP code snippet generator for the Shell using cURL. * * @author * @AhmadNassri * * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. */ 'use strict' var util = require('util') var helpers = require('../../helpers/shell') var CodeBuilder = require('../../helpers/code-builder') module.exports = function (source, options) { var opts = util._extend({ indent: ' ', short: false }, options) var code = new CodeBuilder(opts.indent, opts.indent !== false ? ' \\\n' + opts.indent : ' ') code.push('curl %s %s', opts.short ? '-X' : '--request', source.method) .push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl))) if (source.httpVersion === 'HTTP/1.0') { code.push(opts.short ? '-0' : '--http1.0') } // construct headers Object.keys(source.headersObj).sort().map(function (key) { var header = util.format('%s: %s', key, source.headersObj[key]) code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header)) }) if (source.allHeaders.cookie) { code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie)) } // construct post params switch (source.postData.mimeType) { case 'multipart/form-data': source.postData.params.map(function (param) { var post = util.format('%s=%s', param.name, param.value) if (param.fileName && !param.value) { post = util.format('%s=@%s', param.name, param.fileName) } code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post)) }) break default: // raw request body if (source.postData.text) { var postData = helpers.quote(source.postData.text); code.push('%s %s', opts.short ? '-d' : '--data', options.escape ? helpers.escape(postData) : postData) } } return code.join() } module.exports.info = { key: 'curl', title: 'cURL', link: 'http://curl.haxx.se/', description: 'cURL is a command line tool and library for transferring data with URL syntax' }