Skip to content

Commit

Permalink
Version bump and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed Dec 14, 2022
1 parent 1acbd37 commit 36a2eb2
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 55 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ Industry-leading Holiday and Event API for JavaScript/TypeScript. Over 5,000 hol
# Authentication
Access to the Holiday and Event API requires an API Key. You can get for one for FREE [here](https://apilayer.com/marketplace/checkiday-api#pricing), no credit card required! Note that free plans are limited. To access more data and have more requests, a paid plan is required.

# Installation

```terminal
$ npm install --save holiday-event-api
```

# Example Usage

## Authentication
Simply construct an instance of `HolidayApi` and pass your API Key like this:
Simply construct an instance of `Holidays` and pass your API Key like this:
```ts
import { HolidayApi } from 'holiday-event-api';
import { Holidays } from 'holiday-event-api';

// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
const api = new HolidayApi({ apiKey: '<Your API Key Here>' });
const api = new Holidays({ apiKey: '<Your API Key Here>' });
```

## Example Project
Expand Down
10 changes: 5 additions & 5 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HolidayApi } from 'holiday-event-api';
import { Holidays } from 'holiday-event-api';

// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
const holidayApi = new HolidayApi({ apiKey: '<Your API Key Here>' });
const holidays = new Holidays({ apiKey: '<Your API Key Here>' });

(async () => {
try {
// Get Events for a given Date
const result = await holidayApi.getEvents({
const result = await holidays.getEvents({
// These parameters are the defaults but can be specified:
// date: 'today',
// timezone: 'America/Chicago',
Expand All @@ -17,7 +17,7 @@ const holidayApi = new HolidayApi({ apiKey: '<Your API Key Here>' });
console.log(`Rate limits remaining: ${result.rateLimit.remainingMonth}/${result.rateLimit.limitMonth} (month).`);

// Get Event Information
const eventInfo = await holidayApi.getEventInfo({
const eventInfo = await holidays.getEventInfo({
id: event.id,
// These parameters can be specified to calculate the range of eventInfo.Event.Occurrences
//start: 2020,
Expand All @@ -28,7 +28,7 @@ const holidayApi = new HolidayApi({ apiKey: '<Your API Key Here>' });

// Search for Events
const query = 'pizza day';
const search = await holidayApi.search({
const search = await holidays.search({
query: query,
// These parameters are the defaults but can be specified:
// adult: false,
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "tsc"
},
"dependencies": {
"holiday-event-api": "^0.0.3"
"holiday-event-api": "^1.0.0"
},
"devDependencies": {
"typescript": "^4.7.4"
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "holiday-event-api",
"version": "0.0.3",
"version": "1.0.0",
"description": "Industry-leading holiday and event API for Node.js.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/holidayapi.ts → src/holidays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
GetEventInfoResponse,
GetEventsRequest,
GetEventsResponse,
HolidayApiConfig,
HolidaysConfig,
SearchRequest,
SearchResponse,
} from './types';

export class HolidayApi {
export class Holidays {
private apiKey: string;
private version: string = '0.0.3';
private version: string = '1.0.0';
private baseUrl: string = 'https://api.apilayer.com/checkiday/';
private userAgent: string = 'HolidayApiJs/' + this.version;

constructor(config: HolidayApiConfig) {
constructor(config: HolidaysConfig) {
if (!config?.apiKey || config.apiKey.length == 0) {
throw new Error('Please provide a valid API key. Get one at https://apilayer.com/marketplace/checkiday-api#pricing.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './holidayapi';
export * from './holidays';
export * from './types';
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Data needed to create a new API client.
*/
export type HolidayApiConfig = {
export type HolidaysConfig = {
/**
* Your API Key.
* Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
Expand Down
50 changes: 25 additions & 25 deletions test/holidayapi.test.ts → test/holidays.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as nock from 'nock';
import { HolidayApi } from '../src/holidayapi';
import { Holidays } from '../src/holidays';
import { RateLimit } from '../src/types';

const PACKAGE_VERSION = require('../package.json').version;
Expand All @@ -11,16 +11,16 @@ afterEach(() => {

describe('constructor tests', () => {
test('works with a proper-looking configuration', () => {
expect(new HolidayApi({ apiKey: 'abc123' })).toBeInstanceOf(HolidayApi);
expect(new Holidays({ apiKey: 'abc123' })).toBeInstanceOf(Holidays);
});

test('requires a configuration', () => {
expect(() => new HolidayApi(null as any)).toThrowError('Please provide a valid API key.');
expect(() => new Holidays(null as any)).toThrowError('Please provide a valid API key.');
});

test('requires a proper-looking API key', () => {
expect(() => new HolidayApi({} as any)).toThrowError('Please provide a valid API key.');
expect(() => new HolidayApi({apiKey: ''})).toThrowError('Please provide a valid API key.');
expect(() => new Holidays({} as any)).toThrowError('Please provide a valid API key.');
expect(() => new Holidays({apiKey: ''})).toThrowError('Please provide a valid API key.');
});
});

Expand All @@ -33,7 +33,7 @@ describe('common functionality tests', () => {
}).get('/events')
.reply(200, {});

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
await api.getEvents();
});

Expand All @@ -45,7 +45,7 @@ describe('common functionality tests', () => {
}).get('/events')
.reply(200, {});

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
await api.getEvents();
});

Expand All @@ -54,7 +54,7 @@ describe('common functionality tests', () => {
.get('/events')
.reply(401, { error: 'MyError!' });

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEvents()).rejects.toThrowError('MyError!');
});

Expand All @@ -63,7 +63,7 @@ describe('common functionality tests', () => {
.get('/events')
.reply(500);

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEvents()).rejects.toThrowError('Internal Server Error');
});

Expand All @@ -72,7 +72,7 @@ describe('common functionality tests', () => {
.get('/events')
.reply(599);

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEvents()).rejects.toThrowError('599');
});

Expand All @@ -81,7 +81,7 @@ describe('common functionality tests', () => {
.get('/events')
.reply(200, '');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEvents()).rejects.toThrowError('Unable to parse response.');
});

Expand All @@ -94,7 +94,7 @@ describe('common functionality tests', () => {
.get('/redirected')
.reply(200, {});

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
await api.getEvents();
});

Expand All @@ -106,7 +106,7 @@ describe('common functionality tests', () => {
'x-ratelimit-remaining-month': '88',
});

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.getEvents();
expect(response.rateLimit).toEqual<RateLimit>({
limitMonth: 100,
Expand All @@ -121,7 +121,7 @@ describe('getEvents', () => {
.get('/events')
.replyWithFile(200, 'test/responses/getEvents-default.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.getEvents();
expect(response.adult).toBe(false);
expect(response.timezone).toBe('America/Chicago');
Expand All @@ -145,7 +145,7 @@ describe('getEvents', () => {
})
.replyWithFile(200, 'test/responses/getEvents-parameters.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.getEvents({
adult: true,
timezone: 'America/New_York',
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('getEventInfo', () => {
})
.replyWithFile(200, 'test/responses/getEventInfo.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.getEventInfo({
id: 'f90b893ea04939d7456f30c54f68d7b4',
});
Expand All @@ -191,7 +191,7 @@ describe('getEventInfo', () => {
})
.replyWithFile(200, 'test/responses/getEventInfo-parameters.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.getEventInfo({
id: 'f90b893ea04939d7456f30c54f68d7b4',
start: 2002,
Expand All @@ -212,17 +212,17 @@ describe('getEventInfo', () => {
})
.reply(404, { error: 'Event not found.' });

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEventInfo({ id: 'hi' })).rejects.toThrowError('Event not found.');
});

test('missing id', async () => {
const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEventInfo({} as any)).rejects.toThrowError('Event id is required.');
});

test('missing parameters', async () => {
const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.getEventInfo(undefined as any)).rejects.toThrowError('Event id is required.');
});
});
Expand All @@ -236,7 +236,7 @@ describe('search', () => {
})
.replyWithFile(200, 'test/responses/search-default.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.search({
query: 'zucchini',
});
Expand All @@ -259,7 +259,7 @@ describe('search', () => {
})
.replyWithFile(200, 'test/responses/search-parameters.json');

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
const response = await api.search({
adult: true,
query: 'porch day',
Expand All @@ -282,7 +282,7 @@ describe('search', () => {
})
.reply(400, { error: 'Please enter a longer search term.' });

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.search({ query: 'a' })).rejects.toThrowError('Please enter a longer search term.');
});

Expand All @@ -294,12 +294,12 @@ describe('search', () => {
})
.reply(400, { error: 'Too many results returned. Please refine your query.' });

const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.search({ query: 'day' })).rejects.toThrowError('Too many results returned. Please refine your query.');
});

test('missing parameters', async () => {
const api = new HolidayApi({ apiKey: 'abc123' });
const api = new Holidays({ apiKey: 'abc123' });
expect(api.search(undefined as any)).rejects.toThrowError('Search query is required.');
});
});

0 comments on commit 36a2eb2

Please sign in to comment.