Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Fixed #9
Browse files Browse the repository at this point in the history
  • Loading branch information
hmschreiner committed Jun 16, 2017
1 parent ace14be commit ecbb192
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
28 changes: 27 additions & 1 deletion dist/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion src/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ecbb192

Please sign in to comment.