From b6ad36a075238ff4aec0f727ac2d0d9b9d796980 Mon Sep 17 00:00:00 2001 From: Yaw Etse Date: Sat, 23 Jun 2018 17:49:02 -0400 Subject: [PATCH] allow for the key to be passed as a parameter this will also use the property `key` on the prototype if it's defined. --- index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 12c43a2..819cc92 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; -var https = require('https'); +const https = require('https'); +const qs = require('querystring'); var HolidayAPI = function (key) { if ('undefined' !== typeof key) { @@ -11,16 +12,13 @@ var HolidayAPI = function (key) { HolidayAPI.prototype.v1 = {}; HolidayAPI.prototype.v1.holidays = function (parameters, callback) { - var url = 'https://holidayapi.com/v1/holidays'; - var querystring = '?key=' + HolidayAPI.prototype.key; - - if ('object' === typeof parameters) { - for (var parameter in parameters) { - querystring += '&' + parameter + '=' + parameters[parameter]; - } - } - - url += querystring; + const querystringObject = Object.assign( + {}, + {key: HolidayAPI.prototype.key}, + parameters, + ) + const querystring = qs.stringify(querystringObject); + const url = `https://holidayapi.com/v1/holidays?${querystring}`; https.get(url, function (res) { res.on('data', function (data) {