|
| 1 | +const _ = require('lodash'), |
| 2 | + sanitizeOptions = require('./util/sanitize').sanitizeOptions, |
| 3 | + { parseHeader, parseBody } = require('./util/parseRequest'), |
| 4 | + { addDefaultContentType, formatFormData } = require('./util/formatRequest'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Returns snippet for Rust reqwest by parsing data from Postman-SDK request object |
| 8 | + * |
| 9 | + * @param {Object} request - Postman SDK request object |
| 10 | + * @param {String} indentation - indentation required for code snippet |
| 11 | + * @param {Object} options - Options for code generation |
| 12 | + * |
| 13 | + * @returns {String} - Rust reqwest code snippet for given request object |
| 14 | + */ |
| 15 | +function makeSnippet (request, indentation, options) { |
| 16 | + // Build the client - set timeout and redirect policy |
| 17 | + let snippet = '#[tokio::main]\n'; |
| 18 | + snippet += 'async fn main() -> Result<(), Box<dyn std::error::Error>> {\n'; |
| 19 | + snippet += `${indentation}let client = reqwest::Client::builder()\n`; |
| 20 | + |
| 21 | + // Disable redirects if option is set |
| 22 | + if (_.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect) === false) { |
| 23 | + snippet += `${indentation.repeat(2)}.redirect(reqwest::redirect::Policy::none())\n`; |
| 24 | + } |
| 25 | + |
| 26 | + snippet += `${indentation.repeat(2)}.build()?;\n\n`; |
| 27 | + |
| 28 | + addDefaultContentType(request); |
| 29 | + request.body && request.body.mode === 'formdata' && formatFormData(request); |
| 30 | + |
| 31 | + const body = request.body && request.body.toJSON(), |
| 32 | + contentType = request.headers.get('Content-Type'), |
| 33 | + { headerSnippet, requestHeaderSnippet } = parseHeader(request, indentation), |
| 34 | + { bodySnippet, requestBodySnippet } = parseBody(body, options.trimRequestBody, indentation, contentType); |
| 35 | + |
| 36 | + snippet += headerSnippet; |
| 37 | + snippet += bodySnippet; |
| 38 | + |
| 39 | + // Create the request and add headers and body |
| 40 | + let requestSnippet = `${indentation}let method = "${request.method}";\n`; |
| 41 | + requestSnippet += `${indentation}let request = client.request(reqwest::Method::from_bytes(method.as_bytes())?, `; |
| 42 | + requestSnippet += `"${request.url.toString()}")\n`; |
| 43 | + requestSnippet += requestHeaderSnippet; |
| 44 | + requestSnippet += requestBodySnippet; |
| 45 | + |
| 46 | + // Set request timeout |
| 47 | + if (options.requestTimeout !== 0) { |
| 48 | + requestSnippet += `${indentation.repeat(2)}.timeout(std::time::Duration::from_millis(${options.requestTimeout}))\n`; |
| 49 | + } |
| 50 | + |
| 51 | + requestSnippet = requestSnippet.slice(0, -1) + ';\n\n'; |
| 52 | + |
| 53 | + snippet += requestSnippet; |
| 54 | + snippet += `${indentation}let response = request.send().await?;\n`; |
| 55 | + snippet += `${indentation}let body = response.text().await?;\n\n`; |
| 56 | + |
| 57 | + snippet += `${indentation}println!("{}", body);\n\n`; |
| 58 | + snippet += `${indentation}Ok(())\n}`; |
| 59 | + |
| 60 | + return snippet; |
| 61 | +} |
| 62 | + |
| 63 | +const self = module.exports = { |
| 64 | + /** |
| 65 | + * Used to return options which are specific to a particular plugin |
| 66 | + * |
| 67 | + * @returns {Array} |
| 68 | + */ |
| 69 | + getOptions: function () { |
| 70 | + return [ |
| 71 | + { |
| 72 | + name: 'Set indentation count', |
| 73 | + id: 'indentCount', |
| 74 | + type: 'positiveInteger', |
| 75 | + default: 4, |
| 76 | + description: 'Set the number of indentation characters to add per code level' |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'Set indentation type', |
| 80 | + id: 'indentType', |
| 81 | + type: 'enum', |
| 82 | + availableOptions: ['Tab', 'Space'], |
| 83 | + default: 'Space', |
| 84 | + description: 'Select the character used to indent lines of code' |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'Set request timeout', |
| 88 | + id: 'requestTimeout', |
| 89 | + type: 'positiveInteger', |
| 90 | + default: 0, |
| 91 | + description: 'Set number of milliseconds the request should wait for a response' + |
| 92 | + ' before timing out (use 0 for infinity)' |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'Follow redirects', |
| 96 | + id: 'followRedirect', |
| 97 | + type: 'boolean', |
| 98 | + default: true, |
| 99 | + description: 'Automatically follow HTTP redirects' |
| 100 | + }, |
| 101 | + { |
| 102 | + name: 'Trim request body fields', |
| 103 | + id: 'trimRequestBody', |
| 104 | + type: 'boolean', |
| 105 | + default: false, |
| 106 | + description: 'Remove white space and additional lines that may affect the server\'s response' |
| 107 | + }]; |
| 108 | + }, |
| 109 | + |
| 110 | + /** |
| 111 | + * Used to convert the postman sdk-request object to rust snippet |
| 112 | + * |
| 113 | + * @param {Object} request - postman SDK-request object |
| 114 | + * @param {Object} options |
| 115 | + * @param {String} options.indentType - type of indentation eg: Space / Tab (default: Space) |
| 116 | + * @param {Number} options.indentCount - frequency of indent (default: 4 for indentType: Space, |
| 117 | + default: 1 for indentType: Tab) |
| 118 | + * @param {Number} options.requestTimeout : time in milli-seconds after which request will bail out |
| 119 | + (default: 0 -> never bail out) |
| 120 | + * @param {Boolean} options.trimRequestBody : whether to trim request body fields (default: false) |
| 121 | + * @param {Boolean} options.followRedirect : whether to allow redirects of a request |
| 122 | + * @param {Function} callback - function with parameters (error, snippet) |
| 123 | + */ |
| 124 | + convert: function (request, options, callback) { |
| 125 | + if (!_.isFunction(callback)) { |
| 126 | + throw new Error('Rust~reqwest-convert: Callback is not a function'); |
| 127 | + } |
| 128 | + options = sanitizeOptions(options, self.getOptions()); |
| 129 | + |
| 130 | + // String representing value of indentation required |
| 131 | + let indentString; |
| 132 | + |
| 133 | + indentString = options.indentType === 'Tab' ? '\t' : ' '; |
| 134 | + indentString = indentString.repeat(options.indentCount); |
| 135 | + |
| 136 | + return callback(null, makeSnippet(request, indentString, options)); |
| 137 | + } |
| 138 | +}; |
0 commit comments