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