This repository has been archived on 2025-06-18. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
node-holidayapi/index.js
Yaw Etse b6ad36a075 allow for the key to be passed as a parameter
this will also use the property `key` on the prototype if it's defined.
2019-06-01 13:54:16 -05:00

49 lines
1 KiB
JavaScript

'use strict';
const https = require('https');
const qs = require('querystring');
var HolidayAPI = function (key) {
if ('undefined' !== typeof key) {
HolidayAPI.prototype.key = key;
}
};
HolidayAPI.prototype.v1 = {};
HolidayAPI.prototype.v1.holidays = function (parameters, callback) {
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) {
try {
data = JSON.parse(data);
} catch (e) {
data = {};
}
var error = null;
if (res.statusCode !== 200) {
if ('undefined' === typeof data['error']) {
error = 'Unknown error.';
} else {
error = data.error;
}
}
return callback(error, data);
});
}).on('error', function (e) {
callback(e.message);
});
};
module.exports = HolidayAPI;