UNPKG

@hoppscotch/httpsnippet

Version:

HTTP Request snippet generator for *most* languages

57 lines 2.4 kB
"use strict"; exports.__esModule = true; exports.native = void 0; /** * @description * HTTP code snippet generator for native Crystal * * @author * @18183883296 * * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. */ var code_builder_1 = require("../../../helpers/code-builder"); var escape_1 = require("../../../helpers/escape"); exports.native = { info: { key: 'native', title: 'http::client', link: 'https://crystal-lang.org/api/master/HTTP/Client.html', description: 'Crystal HTTP client' }, convert: function (_a, options) { var rawMethod = _a.method, fullUrl = _a.fullUrl, postData = _a.postData, allHeaders = _a.allHeaders; if (options === void 0) { options = {}; } var _b = options.insecureSkipVerify, insecureSkipVerify = _b === void 0 ? false : _b; var _c = new code_builder_1.CodeBuilder(), push = _c.push, blank = _c.blank, join = _c.join; push('require "http/client"'); blank(); push("url = \"".concat(fullUrl, "\"")); var headers = Object.keys(allHeaders); if (headers.length) { push('headers = HTTP::Headers{'); headers.forEach(function (key) { push(" \"".concat(key, "\" => \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\"")); }); push('}'); } if (postData.text) { push("reqBody = ".concat(JSON.stringify(postData.text))); } blank(); var method = rawMethod.toUpperCase(); var methods = ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT', 'OPTIONS']; var headersContext = headers.length ? ', headers: headers' : ''; var bodyContext = postData.text ? ', body: reqBody' : ''; var sslContext = insecureSkipVerify ? ', tls: OpenSSL::SSL::Context::Client.insecure' : ''; if (methods.includes(method)) { push("response = HTTP::Client.".concat(method.toLowerCase(), " url").concat(headersContext).concat(bodyContext).concat(sslContext)); } else { push("response = HTTP::Client.exec \"".concat(method, "\", url").concat(headersContext).concat(bodyContext).concat(sslContext)); } push('puts response.body'); return join(); } }; //# sourceMappingURL=client.js.map