send_sample_request_utils.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //this block is used to make this module works with Node (CommonJS module format)
  2. if (typeof define !== 'function') {
  3. var define = require('amdefine')(module)
  4. }
  5. define(['lodash'], function (_) {
  6. var log = console;
  7. function handleNestedFields(object, key, params, paramType) {
  8. var attributes = key.split('.');
  9. var field = attributes[0];
  10. params.push(field);
  11. if (attributes.length > 1 && paramType[params.join('.')] == 'Object') {
  12. var nestedField = attributes.slice(1).join('.');
  13. if (!object[field])
  14. object[field] = {};
  15. if (typeof object[field] == 'object') {
  16. object[field][nestedField] = object[key];
  17. delete object[key];
  18. handleNestedFields(object[field], nestedField, params, paramType);
  19. }
  20. }
  21. }
  22. function handleNestedFieldsForAllParams(param, paramType) {
  23. var result = Object.assign({}, param);
  24. Object.keys(result).forEach(function (key) {
  25. handleNestedFields(result, key, [], paramType);
  26. });
  27. return result
  28. }
  29. function handleArraysAndObjectFields(param, paramType) {
  30. var result = Object.assign({}, param);
  31. Object.keys(paramType).forEach(function (key) {
  32. if (result[key] && (paramType[key].endsWith('[]') || paramType[key] === 'Object')) {
  33. try {
  34. result[key] = JSON.parse(result[key]);
  35. } catch (e) {;}
  36. }
  37. });
  38. return result
  39. }
  40. function tryParsingAsType(object, path, type) {
  41. var val = _.get(object, path);
  42. if (val !== undefined) {
  43. if (type === 'Boolean') {
  44. if (val === 'true') {
  45. _.set(object, path, true);
  46. } else if (val === 'false') {
  47. _.set(object, path, false);
  48. } else {
  49. log.warn('Failed to parse object value at path [' + path + ']. Value: (' + val + '). Type: (' + type + ')');
  50. }
  51. } else if (type === 'Number') {
  52. var parsedInt = parseInt(val, 10);
  53. if (!_.isNaN(parsedInt)) {
  54. _.set(object, path, parsedInt);
  55. } else {
  56. log.warn('Failed to parse object value at path [' + path + ']. Value: (' + val + '). Type: (' + type + ')');
  57. }
  58. }
  59. }
  60. }
  61. function handleNestedAndParsingFields(param, paramType) {
  62. var result = handleArraysAndObjectFields(param, paramType);
  63. result = handleNestedFieldsForAllParams(result, paramType);
  64. return result;
  65. }
  66. function tryParsingWithTypes(param, paramType) {
  67. var result = Object.assign({}, param);
  68. Object.keys(paramType).forEach(function (key) {
  69. tryParsingAsType(result, key, paramType[key]);
  70. });
  71. return result;
  72. }
  73. // Converts path params in the {param} format to the accepted :param format, used before inserting the URL params.
  74. function convertPathParams(url) {
  75. return url.replace(/{(.+?)}/g, ':$1');
  76. }
  77. function setLogger(logger) {
  78. log = logger;
  79. }
  80. return {
  81. handleNestedAndParsingFields,
  82. convertPathParams,
  83. tryParsingWithTypes,
  84. setLogger
  85. };
  86. });