util.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. var self = module.exports = {
  3. /**
  4. * sanitizes input string by handling escape characters eg: converts '''' to '\'\'', (" to \" and \ to \\ )
  5. * and trim input if required
  6. *
  7. * @param {String} inputString
  8. * @param {Boolean} [trim] - indicates whether to trim string or not
  9. * @param {Boolean} [doubleQuotes] - indicates whether to escape double quotes(") and backslash(\\)
  10. * @param {Boolean} [backSlash] - indicates whether to escape backslash(\\)
  11. * @returns {String}
  12. */
  13. sanitize: function (inputString, trim, doubleQuotes, backSlash) {
  14. if (typeof inputString !== 'string') {
  15. return '';
  16. }
  17. if (backSlash) {
  18. inputString = inputString.replace(/\\/g, '\\\\');
  19. }
  20. if (doubleQuotes) {
  21. inputString = inputString.replace(/"/g, '\\"');
  22. }
  23. // for curl escaping of single quotes inside single quotes involves changing of ' to '\''
  24. inputString = inputString.replace(/'/g, "'\\''"); // eslint-disable-line quotes
  25. return trim ? inputString.trim() : inputString;
  26. },
  27. form: function (option, format) {
  28. if (format) {
  29. switch (option) {
  30. case '-s':
  31. return '--silent';
  32. case '-L':
  33. return '--location';
  34. case '-m':
  35. return '--max-time';
  36. case '-I':
  37. return '--head';
  38. case '-X':
  39. return '--request';
  40. case '-H':
  41. return '--header';
  42. case '-d':
  43. return '--data';
  44. case '-F':
  45. return '--form';
  46. default:
  47. return '';
  48. }
  49. }
  50. else {
  51. return option;
  52. }
  53. },
  54. /**
  55. * sanitizes input options
  56. *
  57. * @param {Object} options - Options provided by the user
  58. * @param {Array} optionsArray - options array received from getOptions function
  59. *
  60. * @returns {Object} - Sanitized options object
  61. */
  62. sanitizeOptions: function (options, optionsArray) {
  63. var result = {}, defaultOptions = {}, id;
  64. optionsArray.forEach((option) => {
  65. defaultOptions[option.id] = {
  66. default: option.default,
  67. type: option.type
  68. };
  69. if (option.type === 'enum') {
  70. defaultOptions[option.id].availableOptions = option.availableOptions;
  71. }
  72. });
  73. for (id in options) {
  74. if (options.hasOwnProperty(id)) {
  75. if (defaultOptions[id] === undefined) {
  76. continue;
  77. }
  78. switch (defaultOptions[id].type) {
  79. case 'boolean':
  80. if (typeof options[id] !== 'boolean') {
  81. result[id] = defaultOptions[id].default;
  82. }
  83. else {
  84. result[id] = options[id];
  85. }
  86. break;
  87. case 'positiveInteger':
  88. if (typeof options[id] !== 'number' || options[id] < 0) {
  89. result[id] = defaultOptions[id].default;
  90. }
  91. else {
  92. result[id] = options[id];
  93. }
  94. break;
  95. case 'enum':
  96. if (!defaultOptions[id].availableOptions.includes(options[id])) {
  97. result[id] = defaultOptions[id].default;
  98. }
  99. else {
  100. result[id] = options[id];
  101. }
  102. break;
  103. default:
  104. result[id] = options[id];
  105. }
  106. }
  107. }
  108. for (id in defaultOptions) {
  109. if (defaultOptions.hasOwnProperty(id)) {
  110. if (result[id] === undefined) {
  111. result[id] = defaultOptions[id].default;
  112. }
  113. }
  114. }
  115. return result;
  116. },
  117. /**
  118. *
  119. * @param {*} urlObject The request sdk request.url object
  120. * @returns {String} The final string after parsing all the parameters of the url including
  121. * protocol, auth, host, port, path, query, hash
  122. * This will be used because the url.toString() method returned the URL with non encoded query string
  123. * and hence a manual call is made to getQueryString() method with encode option set as true.
  124. */
  125. getUrlStringfromUrlObject: function (urlObject) {
  126. var url = '';
  127. if (!urlObject) {
  128. return url;
  129. }
  130. if (urlObject.protocol) {
  131. url += (urlObject.protocol.endsWith('://') ? urlObject.protocol : urlObject.protocol + '://');
  132. }
  133. if (urlObject.auth && urlObject.auth.user) {
  134. url = url + ((urlObject.auth.password) ?
  135. // ==> username:password@
  136. urlObject.auth.user + ':' + urlObject.auth.password : urlObject.auth.user) + '@';
  137. }
  138. if (urlObject.host) {
  139. url += urlObject.getHost();
  140. }
  141. if (urlObject.port) {
  142. url += ':' + urlObject.port.toString();
  143. }
  144. if (urlObject.path) {
  145. url += urlObject.getPath();
  146. }
  147. if (urlObject.query && urlObject.query.count()) {
  148. let queryString = urlObject.getQueryString({ ignoreDisabled: true, encode: true });
  149. queryString && (url += '?' + queryString);
  150. }
  151. if (urlObject.hash) {
  152. url += '#' + urlObject.hash;
  153. }
  154. return self.sanitize(url);
  155. },
  156. /**
  157. *
  158. * @param {Array} array - form data array
  159. * @param {String} key - key of form data param
  160. * @param {String} type - type of form data param(file/text)
  161. * @param {String} val - value/src property of form data param
  162. * @param {String} disabled - Boolean denoting whether the param is disabled or not
  163. * @param {String} contentType - content type header of the param
  164. *
  165. * Appends a single param to form data array
  166. */
  167. addFormParam: function (array, key, type, val, disabled, contentType) {
  168. if (type === 'file') {
  169. array.push({
  170. key: key,
  171. type: type,
  172. src: val,
  173. disabled: disabled,
  174. contentType: contentType
  175. });
  176. }
  177. else {
  178. array.push({
  179. key: key,
  180. type: type,
  181. value: val,
  182. disabled: disabled,
  183. contentType: contentType
  184. });
  185. }
  186. }
  187. };