Built motion from commit (unavailable).|2.5.2
[motion2.git] / apidoc / utils / send_sample_request_utils.js
index 5fef426..b7bd361 100755 (executable)
@@ -3,7 +3,7 @@ if (typeof define !== 'function') {
     var define = require('amdefine')(module)
 }
 
-define([], function () {
+define(['lodash'], function (_) {
 
     function handleNestedFields(object, key, params, paramType) {
         var attributes = key.split('.');
@@ -41,11 +41,46 @@ define([], function () {
         return result
     }
 
+    function tryParsingAsType(object, path, type) {
+        var val = _.get(object, path);
+        if (val !== undefined) {
+            if (type === 'Boolean') {
+                if (val === 'true') {
+                    _.set(object, path, true);
+                } else if (val === 'false') {
+                    _.set(object, path, false);
+                } else {
+                    console.warn('Failed to parse object value at path [' + path + ']. Value: (' + val + '). Type: (' + type + ')');
+                }
+            } else if (type === 'Number') {
+                var parsedInt = parseInt(val, 10);
+                if (!_.isNaN(parsedInt)) {
+                    _.set(object, path, parsedInt);
+                } else {
+                    console.warn('Failed to parse object value at path [' + path + ']. Value: (' + val + '). Type: (' + type + ')');
+                }
+            }
+        }
+    }
+
     function handleNestedAndParsingFields(param, paramType) {
         var result = handleArraysAndObjectFields(param, paramType);
         result = handleNestedFieldsForAllParams(result, paramType);
         return result;
     }
 
-    return {handleNestedAndParsingFields};
+    function tryParsingWithTypes(param, paramType) {
+        var result = Object.assign({}, param);
+        Object.keys(paramType).forEach(function (key) {
+            tryParsingAsType(result, key, paramType[key]);
+        });
+        return result;
+    }
+
+    // Converts path params in the {param} format to the accepted :param format, used before inserting the URL params.
+    function convertPathParams(url) {
+        return url.replace(/{(.+?)}/g, ':$1');
+    }
+
+    return {handleNestedAndParsingFields,convertPathParams,tryParsingWithTypes};
 });