|
| 1 | +var sdk = require('postman-collection'), |
| 2 | + labelList = require('./assets/languageLabels.json'), |
| 3 | + languageMap = require('./assets/languages.js'); |
| 4 | + |
| 5 | +module.exports = { |
| 6 | + /** |
| 7 | + * Gets the options specific to a given language. |
| 8 | + * |
| 9 | + * @param {Object} language - language key provided by getLanguageList function |
| 10 | + * @param {Array} variant - variant key provided by getLanguageList function |
| 11 | + * @param {Function} callback - callback function with arguments (error, object) |
| 12 | + */ |
| 13 | + getOptions (language, variant, callback) { |
| 14 | + var validCodegen = languageMap.filter((codegen) => { |
| 15 | + var lang = codegen.lang.trim(), |
| 16 | + currentVariant = codegen.variant.trim(); |
| 17 | + return language === lang.toLowerCase() && variant.toLowerCase() === currentVariant.toLowerCase(); |
| 18 | + }); |
| 19 | + |
| 20 | + validCodegen.forEach((codegen) => { |
| 21 | + main = codegen.main; |
| 22 | + if (typeof main.getOptions !== 'function') { |
| 23 | + return callback('Codegen~getOptions: getOptions is not a function'); |
| 24 | + } |
| 25 | + if (!main.getOptions) { |
| 26 | + return callback('Codegen~convert: Could not find condegen corresponding to provided language, variant pair'); |
| 27 | + } |
| 28 | + |
| 29 | + return callback(null, main.getOptions()); |
| 30 | + }); |
| 31 | + }, |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns an object of supported languages |
| 35 | + * |
| 36 | + */ |
| 37 | + getLanguageList () { |
| 38 | + var langMap = {}, |
| 39 | + supportedLanguages = []; |
| 40 | + languageMap.forEach((codegen) => { |
| 41 | + var lang = codegen.lang.trim(), |
| 42 | + syntax_mode = codegen.syntax_mode.trim(), |
| 43 | + variant = codegen.variant.trim(); |
| 44 | + lang = lang.toLowerCase(); |
| 45 | + if (!langMap[lang]) { |
| 46 | + langMap[lang] = { |
| 47 | + key: lang, |
| 48 | + label: labelList[lang] ? labelList[lang] : lang, |
| 49 | + syntax_mode: syntax_mode.toLowerCase(), |
| 50 | + variants: [ |
| 51 | + { |
| 52 | + key: variant |
| 53 | + } |
| 54 | + ] |
| 55 | + }; |
| 56 | + } |
| 57 | + else { |
| 58 | + langMap[lang].variants.push({ |
| 59 | + key: variant |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + supportedLanguages = Object.keys(langMap).map(function (lang) { |
| 65 | + return langMap[lang]; |
| 66 | + }); |
| 67 | + |
| 68 | + return supportedLanguages; |
| 69 | + }, |
| 70 | + |
| 71 | + /** |
| 72 | + * Converts a request to a preferred language snippet |
| 73 | + * |
| 74 | + * @param {Object} language - language key provided by getLanguageList function |
| 75 | + * @param {Array} variant - variant key provided by getLanguageList function |
| 76 | + * @param {String} request - valid postman request |
| 77 | + * @param {Object} [options] - contains convert level options |
| 78 | + * @param {Number} [options.indentType] - indentation based on tab or spaces |
| 79 | + * @param {Number} [options.indentCount] - count/frequency of indentType |
| 80 | + * @param {Number} [options.requestTimeout] : time in milli-seconds after which request will bail out |
| 81 | + * @param {Boolean} [options.requestBodyTrim] : whether to trim request body fields |
| 82 | + * @param {Boolean} [options.addCacheHeader] : whether to add cache-control header to postman SDK-request |
| 83 | + * @param {Boolean} [options.followRedirect] : whether to allow redirects of a request |
| 84 | + * @param {Function} callback - callback function with arguments (error, string) |
| 85 | + */ |
| 86 | + convert (language, variant, request, options, callback) { |
| 87 | + var convert, main; |
| 88 | + |
| 89 | + if (!sdk.Request.isRequest(request)) { |
| 90 | + return callback('Codegen~convert: Invalid request'); |
| 91 | + } |
| 92 | + |
| 93 | + languageMap.forEach((codegen) => { |
| 94 | + var lang = codegen.lang.trim(), |
| 95 | + currentVariant = codegen.variant.trim(); |
| 96 | + if (language.toLowerCase() === lang.toLowerCase() && variant.toLowerCase() === currentVariant.toLowerCase()) { |
| 97 | + main = codegen.main; |
| 98 | + convert = main.convert; |
| 99 | + |
| 100 | + if (typeof convert !== 'function') { |
| 101 | + return callback('Codegen~convert: Convert is not a function'); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + if (!convert) { |
| 106 | + return callback('Codegen~convert: Could not find condegen corresponding to provided language, variant pair'); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + convert(request, options, function (err, snippet) { |
| 111 | + if (err) { |
| 112 | + return callback(err); |
| 113 | + } |
| 114 | + |
| 115 | + return callback(null, snippet); |
| 116 | + }); |
| 117 | + } |
| 118 | +}; |
0 commit comments