diff --git a/dist/helpers/request.js b/dist/helpers/request.js index e955122..bf545ce 100644 --- a/dist/helpers/request.js +++ b/dist/helpers/request.js @@ -37,12 +37,38 @@ var Request = function () { value: function normalizeParams(params) { return _extends({ hapikey: this.apiKey }, params); } + }, { + key: 'serializeProperties', + value: function serializeProperties(_ref) { + var _ref$properties = _ref.properties, + properties = _ref$properties === undefined ? {} : _ref$properties, + _ref$property = _ref.property, + property = _ref$property === undefined ? {} : _ref$property; + + + var objParam = Object.keys(properties).length === 0 ? property : properties; + + var paramName = Object.keys(properties).length === 0 ? 'property' : 'properties'; + + return Object.keys(objParam).map(function (key) { + return paramName + '=' + encodeURIComponent(objParam[key]); + }).join('&'); + } }, { key: 'get', value: function get(endPoint) { var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; - return this.apiInstance.get('' + endPoint, { params: this.normalizeParams(params) }); + + var serializedProperties = this.serializeProperties(params); + + if (params.hasOwnProperty('properties')) delete params.properties; + + if (params.hasOwnProperty('property')) delete params.property; + + return this.apiInstance.get(endPoint + '?' + serializedProperties, { + params: this.normalizeParams(params) + }); } // TODO diff --git a/src/helpers/request.js b/src/helpers/request.js index 6484a11..39bef62 100644 --- a/src/helpers/request.js +++ b/src/helpers/request.js @@ -19,8 +19,34 @@ class Request { return { hapikey: this.apiKey, ...params } } + serializeProperties({ properties = {}, property = {} }) { + + let objParam = Object.keys(properties).length === 0 + ? property + : properties + + let paramName = Object.keys(properties).length === 0 + ? 'property' + : 'properties' + + return Object.keys(objParam).map(key => + `${paramName}=${encodeURIComponent(objParam[key])}` + ).join('&') + } + get(endPoint, params = {}) { - return this.apiInstance.get(`${endPoint}`, {params: this.normalizeParams(params)}) + + let serializedProperties = this.serializeProperties(params) + + if (params.hasOwnProperty('properties')) + delete params.properties + + if (params.hasOwnProperty('property')) + delete params.property + + return this.apiInstance.get(`${endPoint}?${serializedProperties}`, { + params: this.normalizeParams(params), + }) } // TODO