X-Git-Url: http://repos.xcallymotion.com/?a=blobdiff_plain;f=apidoc%2Futils%2Fsend_sample_request_utils.js;h=b7bd3615942d5114504d04d3a5cce316aa11cc97;hb=877636f71ea4e4a4bfc9f2950f78c09e94a43e10;hp=5fef426ee8a2d813fe88e1082187121d3e7d67f0;hpb=dfacf7f441a092a1e235cb444060bf10fa72e39a;p=motion2.git diff --git a/apidoc/utils/send_sample_request_utils.js b/apidoc/utils/send_sample_request_utils.js index 5fef426..b7bd361 100755 --- a/apidoc/utils/send_sample_request_utils.js +++ b/apidoc/utils/send_sample_request_utils.js @@ -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}; });