|
| 1 | +const CodeBuilder = require('../../helpers/code-builder') |
| 2 | + |
| 3 | +module.exports = ({ uriObj, queryObj, method: rawMethod, postData, allHeaders }) => { |
| 4 | + const code = new CodeBuilder() |
| 5 | + |
| 6 | + // To support custom methods we check for the supported methods |
| 7 | + // and if doesn't exist then we build a custom class for it |
| 8 | + const method = rawMethod.toUpperCase() |
| 9 | + const methods = [ |
| 10 | + 'GET', |
| 11 | + 'POST', |
| 12 | + 'HEAD', |
| 13 | + 'DELETE', |
| 14 | + 'PATCH', |
| 15 | + 'PUT', |
| 16 | + 'OPTIONS', |
| 17 | + 'COPY', |
| 18 | + 'LOCK', |
| 19 | + 'UNLOCK', |
| 20 | + 'MOVE', |
| 21 | + 'TRACE' |
| 22 | + ] |
| 23 | + |
| 24 | + if (!methods.includes(method)) { |
| 25 | + code.push(`# Faraday cannot currently run ${method} requests. Please use another client.`) |
| 26 | + return code.join() |
| 27 | + } |
| 28 | + |
| 29 | + code.push("require 'faraday'") |
| 30 | + code.blank() |
| 31 | + |
| 32 | + // Write body to beginning of script |
| 33 | + if (postData.mimeType === 'application/x-www-form-urlencoded') { |
| 34 | + if (postData.params) { |
| 35 | + code.push('data = {') |
| 36 | + postData.params.forEach(param => { |
| 37 | + code.push(` :${param.name} => ${JSON.stringify(param.value)},`) |
| 38 | + }) |
| 39 | + code.push('}') |
| 40 | + code.blank() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + code.push('conn = Faraday.new(') |
| 45 | + code.push(` url: '${uriObj.protocol}//${uriObj.host}',`) |
| 46 | + if (allHeaders['content-type'] || allHeaders['Content-Type']) { |
| 47 | + code.push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`) |
| 48 | + } |
| 49 | + code.push(')') |
| 50 | + |
| 51 | + code.blank() |
| 52 | + code.push(`response = conn.${method.toLowerCase()}('${uriObj.pathname}') do |req|`) |
| 53 | + |
| 54 | + const headers = Object.keys(allHeaders) |
| 55 | + if (headers.length) { |
| 56 | + headers.forEach(key => { |
| 57 | + if (key.toLowerCase() !== 'content-type') { |
| 58 | + code.push(" req.headers['%qs'] = '%qs'", key, allHeaders[key]) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + Object.keys(queryObj).forEach(name => { |
| 64 | + const value = queryObj[name] |
| 65 | + if (Array.isArray(value)) { |
| 66 | + code.push(` req.params['%qs'] = ${JSON.stringify(value)}`, name) |
| 67 | + } else { |
| 68 | + code.push(" req.params['%qs'] = '%qs'", name, value) |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + switch (postData.mimeType) { |
| 73 | + case 'application/x-www-form-urlencoded': |
| 74 | + if (postData.params) { |
| 75 | + code.push(' req.body = URI.encode_www_form(data)') |
| 76 | + } |
| 77 | + break |
| 78 | + |
| 79 | + case 'application/json': |
| 80 | + if (postData.jsonObj) { |
| 81 | + code.push(` req.body = ${JSON.stringify(postData.text)}`) |
| 82 | + } |
| 83 | + break |
| 84 | + |
| 85 | + default: |
| 86 | + if (postData.text) { |
| 87 | + code.push(` req.body = ${JSON.stringify(postData.text)}`) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + code.push('end') |
| 92 | + code.blank() |
| 93 | + code.push('puts response.status') |
| 94 | + code.push('puts response.body') |
| 95 | + |
| 96 | + return code.join() |
| 97 | +} |
| 98 | + |
| 99 | +module.exports.info = { |
| 100 | + key: 'faraday', |
| 101 | + title: 'faraday', |
| 102 | + link: 'https://github.com/lostisland/faraday', |
| 103 | + description: 'Faraday HTTP client' |
| 104 | +} |
0 commit comments