|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * HTTP code snippet generator for Clojure using clj-http. |
| 4 | + * |
| 5 | + * @author |
| 6 | + * @tggreene |
| 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 | +var Keyword = function (name) { |
| 16 | + this.name = name |
| 17 | +} |
| 18 | + |
| 19 | +Keyword.prototype.toString = function () { |
| 20 | + return ':' + this.name |
| 21 | +} |
| 22 | + |
| 23 | +var File = function (path) { |
| 24 | + this.path = path |
| 25 | +} |
| 26 | + |
| 27 | +File.prototype.toString = function () { |
| 28 | + return '(clojure.java.io/file "' + this.path + '")' |
| 29 | +} |
| 30 | + |
| 31 | +var jsType = function (x) { |
| 32 | + return (typeof x !== 'undefined') |
| 33 | + ? x.constructor.name.toLowerCase() |
| 34 | + : null |
| 35 | +} |
| 36 | + |
| 37 | +var objEmpty = function (x) { |
| 38 | + return (jsType(x) === 'object') |
| 39 | + ? Object.keys(x).length === 0 |
| 40 | + : false |
| 41 | +} |
| 42 | + |
| 43 | +var filterEmpty = function (m) { |
| 44 | + Object.keys(m) |
| 45 | + .filter(function (x) { return objEmpty(m[x]) }) |
| 46 | + .forEach(function (x) { delete m[x] }) |
| 47 | + return m |
| 48 | +} |
| 49 | + |
| 50 | +var padBlock = function (x, s) { |
| 51 | + var padding = Array.apply(null, Array(x)) |
| 52 | + .map(function (_) { |
| 53 | + return ' ' |
| 54 | + }) |
| 55 | + .join('') |
| 56 | + return s.replace(/\n/g, '\n' + padding) |
| 57 | +} |
| 58 | + |
| 59 | +var jsToEdn = function (js) { |
| 60 | + switch (jsType(js)) { |
| 61 | + default: // 'number' 'boolean' |
| 62 | + return js.toString() |
| 63 | + case 'string': |
| 64 | + return '"' + js.replace(/\"/g, '\\"') + '"' |
| 65 | + case 'file': |
| 66 | + return js.toString() |
| 67 | + case 'keyword': |
| 68 | + return js.toString() |
| 69 | + case 'null': |
| 70 | + return 'nil' |
| 71 | + case 'regexp': |
| 72 | + return '#"' + js.source + '"' |
| 73 | + case 'object': // simple vertical format |
| 74 | + var obj = Object.keys(js) |
| 75 | + .reduce(function (acc, key) { |
| 76 | + var val = padBlock(key.length + 2, jsToEdn(js[key])) |
| 77 | + return acc + ':' + key + ' ' + val + '\n ' |
| 78 | + }, '') |
| 79 | + .trim() |
| 80 | + return '{' + padBlock(1, obj) + '}' |
| 81 | + case 'array': // simple horizontal format |
| 82 | + var arr = js.reduce(function (acc, val) { |
| 83 | + return acc + ' ' + jsToEdn(val) |
| 84 | + }, '').trim() |
| 85 | + return '[' + padBlock(1, arr) + ']' |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +module.exports = function (source, options) { |
| 90 | + var code = new CodeBuilder(options) |
| 91 | + var methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'] |
| 92 | + |
| 93 | + if (methods.indexOf(source.method.toLowerCase()) === -1) { |
| 94 | + return code.push('Method not supported').join() |
| 95 | + } |
| 96 | + |
| 97 | + var params = {headers: source.allHeaders, |
| 98 | + 'query-params': source.queryObj} |
| 99 | + |
| 100 | + switch (source.postData.mimeType) { |
| 101 | + case 'application/json': |
| 102 | + params['content-type'] = new Keyword('json') |
| 103 | + params['form-params'] = source.postData.jsonObj |
| 104 | + delete params.headers['content-type'] |
| 105 | + break |
| 106 | + case 'application/x-www-form-urlencoded': |
| 107 | + params['form-params'] = source.postData.paramsObj |
| 108 | + delete params.headers['content-type'] |
| 109 | + break |
| 110 | + case 'text/plain': |
| 111 | + params.body = source.postData.text |
| 112 | + delete params.headers['content-type'] |
| 113 | + break |
| 114 | + case 'multipart/form-data': |
| 115 | + params.multipart = source.postData.params.map(function (x) { |
| 116 | + if (x.fileName && !x.value) { |
| 117 | + return {name: x.name, |
| 118 | + content: new File(x.fileName)} |
| 119 | + } else { |
| 120 | + return {name: x.name, |
| 121 | + content: x.value} |
| 122 | + } |
| 123 | + }) |
| 124 | + delete params.headers['content-type'] |
| 125 | + break |
| 126 | + } |
| 127 | + |
| 128 | + switch (params.headers.accept) { |
| 129 | + case 'application/json': |
| 130 | + params.accept = new Keyword('json') |
| 131 | + delete params.headers.accept |
| 132 | + break |
| 133 | + } |
| 134 | + |
| 135 | + code.push('(require \'[clj-http.client :as client])\n') |
| 136 | + |
| 137 | + if (objEmpty(filterEmpty(params))) { |
| 138 | + code.push('(client/%s "%s")', source.method.toLowerCase(), source.url) |
| 139 | + } else { |
| 140 | + code.push('(client/%s "%s" %s)', source.method.toLowerCase(), source.url, padBlock(11 + source.method.length + source.url.length, jsToEdn(filterEmpty(params)))) |
| 141 | + } |
| 142 | + |
| 143 | + return code.join() |
| 144 | +} |
| 145 | + |
| 146 | +module.exports.info = { |
| 147 | + key: 'clj_http', |
| 148 | + title: 'clj-http', |
| 149 | + link: 'https://github.com/dakrone/clj-http', |
| 150 | + description: 'An idiomatic clojure http client wrapping the apache client.' |
| 151 | +} |
0 commit comments