├── .eslintrc.json ├── .github └── workflows │ └── test.yml ├── .gitignore ├── .npmignore ├── .nvmrc ├── LICENSE ├── README.md ├── codecov.yml ├── dist ├── holidayapi.d.ts ├── holidayapi.js ├── index.d.ts ├── index.js ├── types.d.ts └── types.js ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── holidayapi.ts ├── index.ts └── types.ts ├── tests └── holidayapi.test.ts └── tsconfig.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "jest/globals": true, 5 | "node": true 6 | }, 7 | "extends": [ 8 | "airbnb-base", 9 | "plugin:import/typescript" 10 | ], 11 | "globals": { 12 | "Atomics": "readonly", 13 | "SharedArrayBuffer": "readonly" 14 | }, 15 | "parser": "@typescript-eslint/parser", 16 | "parserOptions": { 17 | "ecmaVersion": 2018, 18 | "sourceType": "module" 19 | }, 20 | "plugins": [ 21 | "@typescript-eslint", 22 | "jest" 23 | ], 24 | "rules": { 25 | "@typescript-eslint/no-unused-vars": [ 26 | "error", 27 | { 28 | "vars": "all", 29 | "args": "after-used", 30 | "ignoreRestSiblings": false 31 | } 32 | ], 33 | "import/extensions": [ 34 | "error", 35 | "ignorePackages", 36 | { 37 | "ts": "never" 38 | } 39 | ], 40 | "import/no-extraneous-dependencies": [ 41 | "error", 42 | { 43 | "devDependencies": [ 44 | "**/*.test.ts" 45 | ] 46 | } 47 | ], 48 | "import/prefer-default-export": "off", 49 | "lines-between-class-members": [ 50 | "error", 51 | "always", 52 | { 53 | "exceptAfterSingleLine": true 54 | } 55 | ], 56 | "no-unused-vars": "off" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | name: Test Node.js ${{ matrix.node-version }} 6 | runs-on: ubuntu-latest 7 | strategy: 8 | matrix: 9 | node-version: ['18', '20', '22'] 10 | steps: 11 | - name: Checkout Code 12 | uses: actions/checkout@v2 13 | - name: Setup Node.js 14 | uses: actions/setup-node@v1 15 | with: 16 | node-version: ${{ matrix.node-version }} 17 | - name: Node.js Version 18 | run: node --version 19 | - name: NPM Version 20 | run: npm --version 21 | - name: Install Dependencies 22 | run: npm install 23 | - name: Run Linter 24 | run: npm run lint 25 | - name: Run Tests 26 | run: npm run test:coverage 27 | - name: Upload Coverage 28 | if: ${{ matrix.node-version == '20' }} 29 | uses: codecov/codecov-action@v1 30 | with: 31 | file: ./coverage/lcov.info 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log* 3 | coverage/ 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | tsconfig.json 2 | src 3 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v20 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 Gravity Boulevard, LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Holiday API Node.js Library 2 | 3 | [![License](https://img.shields.io/npm/l/holidayapi-node?style=for-the-badge)](https://github.com/holidayapi/holidayapi-node/blob/master/LICENSE) 4 | ![Node Version](https://img.shields.io/node/v/holidayapi?style=for-the-badge) 5 | [![Code Coverage](https://img.shields.io/codecov/c/github/holidayapi/holidayapi-node?style=for-the-badge)](https://codecov.io/gh/holidayapi/holidayapi-node) 6 | 7 | Official Node.js library for [Holiday API](https://holidayapi.com) providing 8 | quick and easy access to holiday information from applications written in 9 | server-side JavaScript. 10 | 11 | ## Documentation 12 | 13 | Full documentation of the Holiday API endpoints is available 14 | [here](https://holidayapi.com/docs). 15 | 16 | ## Installation 17 | 18 | ```shell 19 | # NPM 20 | npm install --save holidayapi 21 | 22 | # Yarn 23 | yarn add holidayapi 24 | ``` 25 | 26 | ## Usage 27 | 28 | ```javascript 29 | import { HolidayAPI } from 'holidayapi'; 30 | 31 | const key = 'Insert your API key here'; 32 | const holidayApi = new HolidayAPI({ key }); 33 | 34 | // Fetch supported countries and subdivisions 35 | holidayApi.countries() 36 | .then((countries) => { console.log(countries); }) 37 | .catch((err) => { console.error(err); }); 38 | 39 | // Fetch supported languages 40 | holidayApi.languages() 41 | .then((languages) => { console.log(languages); }) 42 | .catch((err) => { console.error(err); }); 43 | 44 | // Fetch holidays with minimum parameters 45 | holidayApi.holidays({ country: 'US', year: 2019 }) 46 | .then((holidays) => { console.log(holidays); }) 47 | .catch((err) => { console.error(err); }); 48 | 49 | // Async? Await? No problem! 50 | (async () => { 51 | // Fetch supported countries and subdivisions 52 | const countries = await holidayApi.countries(); 53 | 54 | // Fetch supported languages 55 | const languages = await holidayApi.languages(); 56 | 57 | // Fetch holidays with minimum parameters 58 | const holidays = await holidayApi.holidays({ 59 | country: 'US', 60 | year: 2019, 61 | }); 62 | })(); 63 | ``` 64 | 65 | ## Examples 66 | 67 | ### Countries 68 | 69 | #### Fetch all supported countries 70 | 71 | ```javascript 72 | holidayApi.countries(); 73 | ``` 74 | 75 | #### Fetch only countries with public holidays 76 | 77 | ```javascript 78 | holidayApi.countries({ 79 | public: true, 80 | }); 81 | ``` 82 | 83 | #### Fetch a supported country by code 84 | 85 | ```javascript 86 | holidayApi.countries({ 87 | country: 'NO', 88 | }); 89 | ``` 90 | 91 | #### Search for countries by code or name 92 | 93 | ```javascript 94 | holidayApi.countries({ 95 | search: 'united', 96 | }); 97 | ``` 98 | 99 | ### Languages 100 | 101 | #### Fetch all supported languages 102 | 103 | ```javascript 104 | holidayApi.languages(); 105 | ``` 106 | 107 | #### Fetch a supported language by code 108 | 109 | ```javascript 110 | holidayApi.language({ 111 | language: 'es', 112 | }); 113 | ``` 114 | 115 | #### Search for languages by code or name 116 | 117 | ```javascript 118 | holidayApi.language({ 119 | search: 'Chinese', 120 | }); 121 | ``` 122 | 123 | ### Holidays 124 | 125 | #### Fetch holidays for a specific year 126 | 127 | ```javascript 128 | holidayApi.holidays({ 129 | country: 'US', 130 | year: 2019, 131 | }); 132 | ``` 133 | 134 | #### Fetch holidays for a specific month 135 | 136 | ```javascript 137 | holidayApi.holidays({ 138 | country: 'US', 139 | year: 2019, 140 | month: 7, 141 | }); 142 | ``` 143 | 144 | #### Fetch holidays for a specific day 145 | 146 | ```javascript 147 | holidayApi.holidays({ 148 | country: 'US', 149 | year: 2019, 150 | month: 7, 151 | day: 4, 152 | }); 153 | ``` 154 | 155 | #### Fetch upcoming holidays based on a specific date 156 | 157 | ```javascript 158 | holidayApi.holidays({ 159 | country: 'US', 160 | year: 2019, 161 | month: 7, 162 | day: 4, 163 | upcoming: true, 164 | }); 165 | ``` 166 | 167 | #### Fetch previous holidays based on a specific date 168 | 169 | ```javascript 170 | holidayApi.holidays({ 171 | country: 'US', 172 | year: 2019, 173 | month: 7, 174 | day: 4, 175 | previous: true, 176 | }); 177 | ``` 178 | 179 | #### Fetch only public holidays 180 | 181 | ```javascript 182 | holidayApi.holidays({ 183 | country: 'US', 184 | year: 2019, 185 | public: true, 186 | }); 187 | ``` 188 | 189 | #### Fetch holidays for a specific subdivision 190 | 191 | ```javascript 192 | holidayApi.holidays({ 193 | country: 'GB-ENG', 194 | year: 2019, 195 | }); 196 | ``` 197 | 198 | #### Include subdivision holidays with countrywide holidays 199 | 200 | ```javascript 201 | holidayApi.holidays({ 202 | country: 'US', 203 | year: 2019, 204 | subdivisions: true, 205 | }); 206 | ``` 207 | 208 | #### Search for a holiday by name 209 | 210 | ```javascript 211 | holidayApi.holidays({ 212 | country: 'US', 213 | year: 2019, 214 | search: 'New Year', 215 | }); 216 | ``` 217 | 218 | #### Translate holidays to another language 219 | 220 | ```javascript 221 | holidayApi.holidays({ 222 | country: 'US', 223 | year: 2019, 224 | language: 'zh', // Chinese (Simplified) 225 | }); 226 | ``` 227 | 228 | #### Fetch holidays for multiple countries 229 | 230 | ```javascript 231 | holidayApi.holidays({ 232 | country: 'US,GB,NZ', 233 | year: 2019, 234 | }); 235 | 236 | holidayApi.holidays({ 237 | country: ['US', 'GB', 'NZ'], 238 | year: 2019, 239 | }); 240 | ``` 241 | 242 | ### Workday 243 | 244 | #### Fetch workday 7 business days after a date 245 | 246 | ```javascript 247 | holidayApi.workday({ 248 | country: 'US', 249 | start: '2019-07-01', 250 | days: 7, 251 | }); 252 | ``` 253 | 254 | ### Workdays 255 | 256 | #### Fetch number of workdays between two dates 257 | 258 | ```javascript 259 | holidayApi.workday({ 260 | country: 'US', 261 | start: '2019-07-01', 262 | end: '2019-07-10', 263 | }); 264 | ``` 265 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | require_ci_to_pass: yes 3 | 4 | coverage: 5 | precision: 2 6 | round: down 7 | range: "70...100" 8 | 9 | parsers: 10 | gcov: 11 | branch_detection: 12 | conditional: yes 13 | loop: yes 14 | method: no 15 | macro: no 16 | 17 | comment: 18 | layout: "reach,diff,flags,tree" 19 | behavior: default 20 | require_changes: no 21 | -------------------------------------------------------------------------------- /dist/holidayapi.d.ts: -------------------------------------------------------------------------------- 1 | import { CountriesRequest, CountriesResponse, HolidaysRequest, HolidaysResponse, LanguagesRequest, LanguagesResponse, WorkdayRequest, WorkdayResponse, WorkdaysRequest, WorkdaysResponse } from './types'; 2 | export declare class HolidayAPI { 3 | baseUrl: string; 4 | key: string; 5 | constructor({ key, version }: { 6 | key?: string; 7 | version?: number; 8 | }); 9 | private createUrl; 10 | private request; 11 | countries(request?: CountriesRequest): Promise; 12 | holidays(request?: HolidaysRequest): Promise; 13 | languages(request?: LanguagesRequest): Promise; 14 | workday(request?: WorkdayRequest): Promise; 15 | workdays(request?: WorkdaysRequest): Promise; 16 | } 17 | -------------------------------------------------------------------------------- /dist/holidayapi.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __assign = (this && this.__assign) || function () { 3 | __assign = Object.assign || function(t) { 4 | for (var s, i = 1, n = arguments.length; i < n; i++) { 5 | s = arguments[i]; 6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) 7 | t[p] = s[p]; 8 | } 9 | return t; 10 | }; 11 | return __assign.apply(this, arguments); 12 | }; 13 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 14 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 15 | return new (P || (P = Promise))(function (resolve, reject) { 16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } 17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } 18 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } 19 | step((generator = generator.apply(thisArg, _arguments || [])).next()); 20 | }); 21 | }; 22 | var __generator = (this && this.__generator) || function (thisArg, body) { 23 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; 24 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; 25 | function verb(n) { return function (v) { return step([n, v]); }; } 26 | function step(op) { 27 | if (f) throw new TypeError("Generator is already executing."); 28 | while (g && (g = 0, op[0] && (_ = 0)), _) try { 29 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; 30 | if (y = 0, t) op = [op[0] & 2, t.value]; 31 | switch (op[0]) { 32 | case 0: case 1: t = op; break; 33 | case 4: _.label++; return { value: op[1], done: false }; 34 | case 5: _.label++; y = op[1]; op = [0]; continue; 35 | case 7: op = _.ops.pop(); _.trys.pop(); continue; 36 | default: 37 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } 38 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } 39 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } 40 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } 41 | if (t[2]) _.ops.pop(); 42 | _.trys.pop(); continue; 43 | } 44 | op = body.call(thisArg, _); 45 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } 46 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; 47 | } 48 | }; 49 | Object.defineProperty(exports, "__esModule", { value: true }); 50 | exports.HolidayAPI = void 0; 51 | var node_fetch_1 = require("node-fetch"); 52 | var HolidayAPI = (function () { 53 | function HolidayAPI(_a) { 54 | var key = _a.key, _b = _a.version, version = _b === void 0 ? 1 : _b; 55 | var getYours = 'get yours at HolidayAPI.com'; 56 | var uuidRegExp = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/; 57 | if (!key) { 58 | throw new Error("Missing API key, ".concat(getYours)); 59 | } 60 | if (!uuidRegExp.test(key)) { 61 | throw new Error("Invalid API key, ".concat(getYours)); 62 | } 63 | if (version !== 1) { 64 | throw new Error('Invalid version number, expected "1"'); 65 | } 66 | this.baseUrl = "https://holidayapi.com/v".concat(version, "/"); 67 | this.key = key; 68 | } 69 | HolidayAPI.prototype.createUrl = function (endpoint, request) { 70 | var parameters = __assign({ key: this.key }, request); 71 | var url = new URL(endpoint, this.baseUrl); 72 | url.search = new URLSearchParams(parameters).toString(); 73 | return url.toString(); 74 | }; 75 | HolidayAPI.prototype.request = function (endpoint, request) { 76 | return __awaiter(this, void 0, void 0, function () { 77 | var response, payload, err_1; 78 | return __generator(this, function (_a) { 79 | switch (_a.label) { 80 | case 0: return [4, (0, node_fetch_1.default)(this.createUrl(endpoint, request))]; 81 | case 1: 82 | response = _a.sent(); 83 | _a.label = 2; 84 | case 2: 85 | _a.trys.push([2, 4, , 5]); 86 | return [4, response.json()]; 87 | case 3: 88 | payload = _a.sent(); 89 | return [3, 5]; 90 | case 4: 91 | err_1 = _a.sent(); 92 | payload = {}; 93 | return [3, 5]; 94 | case 5: 95 | if (!response.ok) { 96 | throw new Error(payload.error || response.statusText); 97 | } 98 | return [2, payload]; 99 | } 100 | }); 101 | }); 102 | }; 103 | HolidayAPI.prototype.countries = function (request) { 104 | return __awaiter(this, void 0, void 0, function () { 105 | return __generator(this, function (_a) { 106 | return [2, this.request('countries', request)]; 107 | }); 108 | }); 109 | }; 110 | HolidayAPI.prototype.holidays = function (request) { 111 | return __awaiter(this, void 0, void 0, function () { 112 | return __generator(this, function (_a) { 113 | return [2, this.request('holidays', request)]; 114 | }); 115 | }); 116 | }; 117 | HolidayAPI.prototype.languages = function (request) { 118 | return __awaiter(this, void 0, void 0, function () { 119 | return __generator(this, function (_a) { 120 | return [2, this.request('languages', request)]; 121 | }); 122 | }); 123 | }; 124 | HolidayAPI.prototype.workday = function (request) { 125 | return __awaiter(this, void 0, void 0, function () { 126 | return __generator(this, function (_a) { 127 | return [2, this.request('workday', request)]; 128 | }); 129 | }); 130 | }; 131 | HolidayAPI.prototype.workdays = function (request) { 132 | return __awaiter(this, void 0, void 0, function () { 133 | return __generator(this, function (_a) { 134 | return [2, this.request('workdays', request)]; 135 | }); 136 | }); 137 | }; 138 | return HolidayAPI; 139 | }()); 140 | exports.HolidayAPI = HolidayAPI; 141 | -------------------------------------------------------------------------------- /dist/index.d.ts: -------------------------------------------------------------------------------- 1 | export * from './types'; 2 | export * from './holidayapi'; 3 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 3 | if (k2 === undefined) k2 = k; 4 | var desc = Object.getOwnPropertyDescriptor(m, k); 5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { 6 | desc = { enumerable: true, get: function() { return m[k]; } }; 7 | } 8 | Object.defineProperty(o, k2, desc); 9 | }) : (function(o, m, k, k2) { 10 | if (k2 === undefined) k2 = k; 11 | o[k2] = m[k]; 12 | })); 13 | var __exportStar = (this && this.__exportStar) || function(m, exports) { 14 | for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); 15 | }; 16 | Object.defineProperty(exports, "__esModule", { value: true }); 17 | __exportStar(require("./types"), exports); 18 | __exportStar(require("./holidayapi"), exports); 19 | -------------------------------------------------------------------------------- /dist/types.d.ts: -------------------------------------------------------------------------------- 1 | export type Endpoint = 'countries' | 'holidays' | 'languages' | 'workday' | 'workdays'; 2 | export type Weekday = { 3 | name: 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday'; 4 | numeric: 1 | 2 | 3 | 4 | 5 | 6 | 7; 5 | }; 6 | type Request = { 7 | format?: 'csv' | 'json' | 'php' | 'tsv' | 'xml' | 'yaml'; 8 | key?: string; 9 | pretty?: boolean; 10 | search?: string; 11 | }; 12 | export type CountriesRequest = Request & { 13 | country?: string; 14 | public?: boolean; 15 | }; 16 | export type HolidaysRequest = Request & { 17 | country?: string; 18 | day?: number; 19 | language?: string; 20 | month?: number; 21 | previous?: boolean; 22 | public?: boolean; 23 | subdivisions?: boolean; 24 | upcoming?: boolean; 25 | year?: number; 26 | }; 27 | export type LanguagesRequest = Request & { 28 | language?: string; 29 | }; 30 | export type WorkdayRequest = Request & { 31 | country?: string; 32 | start?: string; 33 | days?: number; 34 | }; 35 | export type WorkdaysRequest = Request & { 36 | country?: string; 37 | start?: string; 38 | end?: string; 39 | }; 40 | export type Response = { 41 | requests: { 42 | available: number; 43 | resets: string; 44 | used: number; 45 | }; 46 | status: number; 47 | error?: string; 48 | }; 49 | export type CountriesResponse = Response & { 50 | countries?: { 51 | code: string; 52 | codes: { 53 | 'alpha-2': string; 54 | 'alpha-3': string; 55 | numeric: string; 56 | }; 57 | flag: string; 58 | languages: string[]; 59 | name: string; 60 | subdivisions: { 61 | code: string; 62 | languages: string[]; 63 | name: string; 64 | }[]; 65 | weekday: { 66 | date: Weekday; 67 | observed: Weekday; 68 | }; 69 | }[]; 70 | }; 71 | export type HolidaysResponse = Response & { 72 | holidays?: { 73 | country: string; 74 | date: string; 75 | name: string; 76 | observed: string; 77 | public: boolean; 78 | uuid: string; 79 | subdivisions?: string[]; 80 | }[]; 81 | }; 82 | export type LanguagesResponse = Response & { 83 | languages?: { 84 | code: string; 85 | name: string; 86 | }[]; 87 | }; 88 | export type WorkdayResponse = Response & { 89 | workday?: { 90 | date: string; 91 | weekday: Weekday; 92 | }; 93 | }; 94 | export type WorkdaysResponse = Response & { 95 | workdays?: number; 96 | }; 97 | export type Requests = (CountriesRequest | HolidaysRequest | LanguagesRequest | WorkdayRequest | WorkdaysRequest); 98 | export type Responses = (CountriesResponse | HolidaysResponse | LanguagesResponse | WorkdayResponse | WorkdaysResponse); 99 | export {}; 100 | -------------------------------------------------------------------------------- /dist/types.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | testEnvironment: 'node', 4 | coverageReporters: ['lcov'], 5 | }; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "holidayapi", 3 | "version": "7.1.0", 4 | "description": "Official Node.js library for Holiday API", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "repository": { 8 | "type": "git", 9 | "url": "git+https://github.com/holidayapi/holidayapi-node.git" 10 | }, 11 | "keywords": [ 12 | "calendar", 13 | "holiday", 14 | "holidays", 15 | "holidayapi" 16 | ], 17 | "author": "Josh Sherman (https://holidayapi.com)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/holidayapi/holidayapi-node/issues" 21 | }, 22 | "homepage": "https://holidayapi.com", 23 | "engines": { 24 | "node": ">= 18.0.0" 25 | }, 26 | "devDependencies": { 27 | "@types/jest": "^29.5.6", 28 | "@types/nock": "^11.1.0", 29 | "@types/node": "^22.2.0", 30 | "@types/node-fetch": "^2.6.7", 31 | "@typescript-eslint/eslint-plugin": "^6.9.0", 32 | "@typescript-eslint/parser": "^6.9.0", 33 | "eslint": "^8.12.0", 34 | "eslint-config-airbnb-base": "^15.0.0", 35 | "eslint-plugin-import": "^2.26.0", 36 | "eslint-plugin-jest": "^27.4.3", 37 | "jest": "^29.7.0", 38 | "nock": "^13.2.4", 39 | "ts-jest": "^29.1.1", 40 | "typescript": "^5.2.2" 41 | }, 42 | "dependencies": { 43 | "node-fetch": "^2.7.0" 44 | }, 45 | "scripts": { 46 | "build": "tsc", 47 | "lint": "eslint --ext .js,.ts src tests", 48 | "test": "jest", 49 | "test:coverage": "jest --coverage" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/holidayapi.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Gravity Boulevard, LLC 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | */ 7 | 8 | import fetch from 'node-fetch'; 9 | import { 10 | CountriesRequest, 11 | CountriesResponse, 12 | Endpoint, 13 | HolidaysRequest, 14 | HolidaysResponse, 15 | LanguagesRequest, 16 | LanguagesResponse, 17 | Requests, 18 | Responses, 19 | WorkdayRequest, 20 | WorkdayResponse, 21 | WorkdaysRequest, 22 | WorkdaysResponse, 23 | } from './types'; 24 | 25 | export class HolidayAPI { 26 | baseUrl: string; 27 | key: string; 28 | 29 | constructor({ key, version = 1 }: { key?: string, version?: number }) { 30 | const getYours = 'get yours at HolidayAPI.com'; 31 | const uuidRegExp = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/; 32 | 33 | if (!key) { 34 | throw new Error(`Missing API key, ${getYours}`); 35 | } 36 | 37 | if (!uuidRegExp.test(key)) { 38 | throw new Error(`Invalid API key, ${getYours}`); 39 | } 40 | 41 | if (version !== 1) { 42 | throw new Error('Invalid version number, expected "1"'); 43 | } 44 | 45 | this.baseUrl = `https://holidayapi.com/v${version}/`; 46 | this.key = key; 47 | } 48 | 49 | private createUrl(endpoint: Endpoint, request?: Requests): string { 50 | const parameters = { key: this.key, ...request } as any; 51 | const url = new URL(endpoint, this.baseUrl); 52 | url.search = new URLSearchParams(parameters).toString(); 53 | return url.toString(); 54 | } 55 | 56 | private async request(endpoint: Endpoint, request?: Requests): Promise { 57 | const response = await fetch(this.createUrl(endpoint, request)); 58 | let payload: any; 59 | 60 | try { 61 | payload = await response.json(); 62 | } catch (err) { 63 | payload = {}; 64 | } 65 | 66 | if (!response.ok) { 67 | throw new Error(payload.error || response.statusText); 68 | } 69 | 70 | return payload; 71 | } 72 | 73 | async countries(request?: CountriesRequest): Promise { 74 | return this.request('countries', request); 75 | } 76 | 77 | async holidays(request?: HolidaysRequest): Promise { 78 | return this.request('holidays', request); 79 | } 80 | 81 | async languages(request?: LanguagesRequest): Promise { 82 | return this.request('languages', request); 83 | } 84 | 85 | async workday(request?: WorkdayRequest): Promise { 86 | return this.request('workday', request); 87 | } 88 | 89 | async workdays(request?: WorkdaysRequest): Promise { 90 | return this.request('workdays', request); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Gravity Boulevard, LLC 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | */ 7 | 8 | export * from './types'; 9 | export * from './holidayapi'; 10 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Gravity Boulevard, LLC 3 | * 4 | * This source code is licensed under the MIT license found in the 5 | * LICENSE file in the root directory of this source tree. 6 | */ 7 | 8 | export type Endpoint = 'countries' | 'holidays' | 'languages' | 'workday' | 'workdays'; 9 | 10 | export type Weekday = { 11 | name: 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday'; 12 | numeric: 1 | 2 | 3 | 4 | 5 | 6 | 7; 13 | }; 14 | 15 | type Request = { 16 | format?: 'csv' | 'json' | 'php' | 'tsv' | 'xml' | 'yaml'; 17 | key?: string; 18 | pretty?: boolean; 19 | search?: string; 20 | }; 21 | 22 | export type CountriesRequest = Request & { 23 | country?: string; 24 | public?: boolean; 25 | }; 26 | 27 | export type HolidaysRequest = Request & { 28 | country?: string; 29 | day?: number; 30 | language?: string; 31 | month?: number; 32 | previous?: boolean; 33 | public?: boolean; 34 | subdivisions?: boolean; 35 | upcoming?: boolean; 36 | year?: number; 37 | }; 38 | 39 | export type LanguagesRequest = Request & { 40 | language?: string; 41 | }; 42 | 43 | export type WorkdayRequest = Request & { 44 | country?: string; 45 | start?: string; 46 | days?: number; 47 | }; 48 | 49 | export type WorkdaysRequest = Request & { 50 | country?: string; 51 | start?: string; 52 | end?: string; 53 | }; 54 | 55 | export type Response = { 56 | requests: { 57 | available: number; 58 | resets: string; 59 | used: number; 60 | }; 61 | status: number; 62 | error?: string; 63 | }; 64 | 65 | export type CountriesResponse = Response & { 66 | countries?: { 67 | code: string; 68 | codes: { 69 | 'alpha-2': string; 70 | 'alpha-3': string; 71 | numeric: string; 72 | }; 73 | flag: string; 74 | languages: string[]; 75 | name: string; 76 | subdivisions: { 77 | code: string; 78 | languages: string[]; 79 | name: string; 80 | }[]; 81 | weekday: { 82 | date: Weekday; 83 | observed: Weekday; 84 | }; 85 | }[]; 86 | }; 87 | 88 | export type HolidaysResponse = Response & { 89 | holidays?: { 90 | country: string; 91 | date: string; 92 | name: string; 93 | observed: string; 94 | public: boolean; 95 | uuid: string; 96 | subdivisions?: string[]; 97 | }[]; 98 | }; 99 | 100 | export type LanguagesResponse = Response & { 101 | languages?: { 102 | code: string; 103 | name: string; 104 | }[]; 105 | }; 106 | 107 | export type WorkdayResponse = Response & { 108 | workday?: { 109 | date: string; 110 | weekday: Weekday; 111 | } 112 | }; 113 | 114 | export type WorkdaysResponse = Response & { 115 | workdays?: number; 116 | }; 117 | 118 | export type Requests = ( 119 | | CountriesRequest 120 | | HolidaysRequest 121 | | LanguagesRequest 122 | | WorkdayRequest 123 | | WorkdaysRequest 124 | ); 125 | 126 | export type Responses = ( 127 | | CountriesResponse 128 | | HolidaysResponse 129 | | LanguagesResponse 130 | | WorkdayResponse 131 | | WorkdaysResponse 132 | ); 133 | -------------------------------------------------------------------------------- /tests/holidayapi.test.ts: -------------------------------------------------------------------------------- 1 | import * as nock from 'nock'; 2 | import { HolidayAPI } from '../src/holidayapi'; 3 | 4 | const baseUrl = 'https://holidayapi.com/v1/'; 5 | const key = 'b58e6dec-8a47-459f-a3c1-eaa26eb4dd30'; 6 | 7 | describe('holidayapi', () => { 8 | describe('instantiation', () => { 9 | it('should error when key is missing', () => { 10 | expect.assertions(1); 11 | expect(() => { 12 | const holidayapi = new HolidayAPI({}); 13 | expect(holidayapi.key).toBeUndefined(); 14 | }).toThrowError(/missing api key/i); 15 | }); 16 | 17 | it('should error when key is invalid format', () => { 18 | expect.assertions(1); 19 | expect(() => { 20 | const holidayapi = new HolidayAPI({ 21 | key: 'zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz', 22 | }); 23 | expect(holidayapi.key).toBeUndefined(); 24 | }).toThrowError(/invalid api key/i); 25 | }); 26 | 27 | it('should error when version is too low', () => { 28 | expect.assertions(1); 29 | expect(() => { 30 | const holidayapi = new HolidayAPI({ key, version: 0 }); 31 | expect(holidayapi.baseUrl).toBeUndefined(); 32 | }).toThrowError(/invalid version/i); 33 | }); 34 | 35 | it('should error when version is too high', () => { 36 | expect.assertions(1); 37 | expect(() => { 38 | const holidayapi = new HolidayAPI({ key, version: 2 }); 39 | expect(holidayapi.baseUrl).toBeUndefined(); 40 | }).toThrowError(/invalid version/i); 41 | }); 42 | 43 | it('should assign class members', () => { 44 | const holidayapi = new HolidayAPI({ key }); 45 | expect(holidayapi.baseUrl).toBe(baseUrl); 46 | expect(holidayapi.key).toBe(key); 47 | }); 48 | }); 49 | 50 | describe('v1 requests', () => { 51 | const holidayapi = new HolidayAPI({ key }); 52 | const mockRequest = nock(baseUrl); 53 | 54 | describe('/v1/countries', () => { 55 | const basePath = `/countries?key=${key}`; 56 | 57 | it('should return countries', async () => { 58 | const expectedResponse = { 59 | status: 200, 60 | requests: { 61 | used: 1000, 62 | available: 9000, 63 | resets: '2019-10-01 00:00:00', 64 | }, 65 | countries: [ 66 | { 67 | code: 'ST', 68 | name: 'Sao Tome and Principe', 69 | languages: ['pt'], 70 | codes: { 71 | 'alpha-2': 'ST', 72 | 'alpha-3': 'STP', 73 | numeric: 678, 74 | }, 75 | flag: 'https://www.countryflags.io/ST/flat/64.png', 76 | subdivisions: [ 77 | { 78 | code: 'ST-P', 79 | name: 'Príncipe', 80 | languages: ['pt'], 81 | }, 82 | { 83 | code: 'ST-S', 84 | name: 'São Tomé', 85 | languages: ['pt'], 86 | }, 87 | ], 88 | }, 89 | ], 90 | }; 91 | 92 | mockRequest.get(basePath).reply(200, expectedResponse); 93 | expect(await holidayapi.countries()).toStrictEqual(expectedResponse); 94 | }); 95 | 96 | it('should raise 4xx errors', async () => { 97 | const expectedResponse = { 98 | status: 429, 99 | error: 'Rate limit exceeded', 100 | }; 101 | 102 | expect.assertions(1); 103 | mockRequest.get(basePath).reply(429, expectedResponse); 104 | 105 | try { 106 | await holidayapi.countries(); 107 | } catch (err) { 108 | expect(err.message).toMatch(/rate limit exceeded/i); 109 | } 110 | }); 111 | 112 | it('should raise 5xx errors', async () => { 113 | expect.assertions(1); 114 | mockRequest.get(basePath).reply(500); 115 | 116 | try { 117 | await holidayapi.countries(); 118 | } catch (err) { 119 | expect(err.message).toMatch(/internal server error/i); 120 | } 121 | }); 122 | }); 123 | 124 | describe('/v1/holidays', () => { 125 | const basePath = `/holidays?key=${key}`; 126 | 127 | it('should return holidays', async () => { 128 | const expectedResponse = { 129 | status: 200, 130 | requests: { 131 | used: 1000, 132 | available: 9000, 133 | resets: '2019-10-01 00:00:00', 134 | }, 135 | holidays: [ 136 | { 137 | name: 'Independence Day', 138 | date: '2015-07-04', 139 | observed: '2015-07-03', 140 | public: true, 141 | country: 'US', 142 | uuid: '88268759-9b90-468c-804f-b729b8418e7c', 143 | weekday: { 144 | date: { 145 | name: 'Saturday', 146 | numeric: '6', 147 | }, 148 | observed: { 149 | name: 'Friday', 150 | numeric: '5', 151 | }, 152 | }, 153 | }, 154 | ], 155 | }; 156 | 157 | mockRequest.get(`${basePath}&country=US&year=2019&month=7&day=4`) 158 | .reply(200, expectedResponse); 159 | 160 | expect(await holidayapi.holidays({ 161 | country: 'US', 162 | year: 2019, 163 | month: 7, 164 | day: 4, 165 | })).toStrictEqual(expectedResponse); 166 | }); 167 | 168 | it('should raise 4xx errors', async () => { 169 | const expectedResponse = { 170 | status: 429, 171 | error: 'Rate limit exceeded', 172 | }; 173 | 174 | expect.assertions(1); 175 | mockRequest.get(`${basePath}&country=US&year=2019`) 176 | .reply(429, expectedResponse); 177 | 178 | try { 179 | await holidayapi.holidays({ country: 'US', year: 2019 }); 180 | } catch (err) { 181 | expect(err.message).toMatch(/rate limit exceeded/i); 182 | } 183 | }); 184 | 185 | it('should raise 5xx errors', async () => { 186 | expect.assertions(1); 187 | mockRequest.get(`${basePath}&country=US&year=2019`).reply(500); 188 | 189 | try { 190 | await holidayapi.holidays({ country: 'US', year: 2019 }); 191 | } catch (err) { 192 | expect(err.message).toMatch(/internal server error/i); 193 | } 194 | }); 195 | }); 196 | 197 | describe('/v1/languages', () => { 198 | const basePath = `/languages?key=${key}`; 199 | 200 | it('should return languages', async () => { 201 | const expectedResponse = { 202 | status: 200, 203 | requests: { 204 | used: 1000, 205 | available: 9000, 206 | resets: '2019-10-01 00:00:00', 207 | }, 208 | languages: [ 209 | { 210 | code: 'ar', 211 | name: 'Arabic', 212 | }, 213 | { 214 | code: 'en', 215 | name: 'English', 216 | }, 217 | { 218 | code: 'es', 219 | name: 'Spanish, Castilian', 220 | }, 221 | { 222 | code: 'hi', 223 | name: 'Hindi', 224 | }, 225 | { 226 | code: 'zh', 227 | name: 'Chinese (Simplified)', 228 | }, 229 | ], 230 | }; 231 | 232 | mockRequest.get(basePath).reply(200, expectedResponse); 233 | expect(await holidayapi.languages()).toStrictEqual(expectedResponse); 234 | }); 235 | 236 | it('should raise 4xx errors', async () => { 237 | const expectedResponse = { 238 | status: 429, 239 | error: 'Rate limit exceeded', 240 | }; 241 | 242 | expect.assertions(1); 243 | mockRequest.get(basePath).reply(429, expectedResponse); 244 | 245 | try { 246 | await holidayapi.languages(); 247 | } catch (err) { 248 | expect(err.message).toMatch(/rate limit exceeded/i); 249 | } 250 | }); 251 | 252 | it('should raise 5xx errors', async () => { 253 | expect.assertions(1); 254 | mockRequest.get(basePath).times(2).reply(500); 255 | 256 | try { 257 | await holidayapi.languages(); 258 | } catch (err) { 259 | expect(err.message).toMatch(/internal server error/i); 260 | } 261 | }); 262 | }); 263 | 264 | describe('/v1/workday', () => { 265 | const basePath = `/workday?key=${key}`; 266 | 267 | it('should return workday', async () => { 268 | const expectedResponse = { 269 | status: 200, 270 | requests: { 271 | used: 1000, 272 | available: 9000, 273 | resets: '2019-10-01 00:00:00', 274 | }, 275 | workday: [ 276 | { 277 | date: '2019-07-16', 278 | weekday: { 279 | name: 'Tuesday', 280 | numeric: '2', 281 | }, 282 | }, 283 | ], 284 | }; 285 | 286 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&days=10`) 287 | .reply(200, expectedResponse); 288 | 289 | expect(await holidayapi.workday({ 290 | country: 'US', 291 | start: '2019-07-01', 292 | days: 10, 293 | })).toStrictEqual(expectedResponse); 294 | }); 295 | 296 | it('should raise 4xx errors', async () => { 297 | const expectedResponse = { 298 | status: 429, 299 | error: 'Rate limit exceeded', 300 | }; 301 | 302 | expect.assertions(1); 303 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&days=10`) 304 | .reply(429, expectedResponse); 305 | 306 | try { 307 | await holidayapi.workday({ country: 'US', start: '2019-07-01', days: 10 }); 308 | } catch (err) { 309 | expect(err.message).toMatch(/rate limit exceeded/i); 310 | } 311 | }); 312 | 313 | it('should raise 5xx errors', async () => { 314 | expect.assertions(1); 315 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&days=10`).reply(500); 316 | 317 | try { 318 | await holidayapi.workday({ country: 'US', start: '2019-07-01', days: 10 }); 319 | } catch (err) { 320 | expect(err.message).toMatch(/internal server error/i); 321 | } 322 | }); 323 | }); 324 | 325 | describe('/v1/workdays', () => { 326 | const basePath = `/workdays?key=${key}`; 327 | 328 | it('should return workdays', async () => { 329 | const expectedResponse = { 330 | status: 200, 331 | requests: { 332 | used: 1000, 333 | available: 9000, 334 | resets: '2019-10-01 00:00:00', 335 | }, 336 | workdays: 7, 337 | }; 338 | 339 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`) 340 | .reply(200, expectedResponse); 341 | 342 | expect(await holidayapi.workdays({ 343 | country: 'US', 344 | start: '2019-07-01', 345 | end: '2019-07-10', 346 | })).toStrictEqual(expectedResponse); 347 | }); 348 | 349 | it('should raise 4xx errors', async () => { 350 | const expectedResponse = { 351 | status: 429, 352 | error: 'Rate limit exceeded', 353 | }; 354 | 355 | expect.assertions(1); 356 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`) 357 | .reply(429, expectedResponse); 358 | 359 | try { 360 | await holidayapi.workdays({ country: 'US', start: '2019-07-01', end: '2019-07-10' }); 361 | } catch (err) { 362 | expect(err.message).toMatch(/rate limit exceeded/i); 363 | } 364 | }); 365 | 366 | it('should raise 5xx errors', async () => { 367 | expect.assertions(1); 368 | mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`).reply(500); 369 | 370 | try { 371 | await holidayapi.workdays({ country: 'US', start: '2019-07-01', end: '2019-07-10' }); 372 | } catch (err) { 373 | expect(err.message).toMatch(/internal server error/i); 374 | } 375 | }); 376 | }); 377 | }); 378 | }); 379 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": true, 6 | "outDir": "./dist", 7 | "strict": true, 8 | "removeComments": true, 9 | "noUnusedLocals": true, 10 | "noUnusedParameters": true, 11 | "useUnknownInCatchVariables": false 12 | }, 13 | "include": [ 14 | "src/**/*" 15 | ] 16 | } 17 | --------------------------------------------------------------------------------