feat: add workdays endpoint

Added support for our new workdays endpoint that allows you to pull the
number of working days that occur between two dates.

In addition to the new endpoint:
* chore: upgraded dependencies.
* ci: dropped Node.js 10 because it past EOL.
* ci: swapped Node.js 15 for 16 now that it's out.
* docs: added example of new endpoint.
This commit is contained in:
Josh Sherman 2021-06-10 22:56:16 -05:00
parent fa2f709f82
commit d27c12a05c
No known key found for this signature in database
GPG key ID: 55B058A80530EF22
7 changed files with 9755 additions and 3312 deletions

View file

@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['10', '12', '14', '15']
node-version: ['12', '14', '16']
steps:
- name: Checkout Code
uses: actions/checkout@v2
@ -25,7 +25,7 @@ jobs:
- name: Run Tests
run: npm run test:coverage
- name: Upload Coverage
if: ${{ matrix.node-version == '15' }}
if: ${{ matrix.node-version == '16' }}
uses: codecov/codecov-action@v1
with:
file: ./coverage/lcov.info

View file

@ -251,3 +251,15 @@ holidayApi.workday({
days: 7,
});
```
### Workdays
#### Fetch number of workdays between two dates
```javascript
holidayApi.workday({
country: 'US',
start: '2019-07-01',
end: '2019-07-10',
});
```

12910
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "holidayapi",
"version": "4.2.0",
"version": "5.0.0",
"description": "Official Node.js library for Holiday API",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@ -21,12 +21,12 @@
},
"homepage": "https://holidayapi.com",
"engines": {
"node": ">= 10.0.0"
"node": ">= 12.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/nock": "^11.1.0",
"@types/node": "^14.14.16",
"@types/node": "^15.12.2",
"@types/node-fetch": "^2.5.7",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
@ -34,9 +34,9 @@
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.3",
"jest": "^26.6.3",
"jest": "^27.0.4",
"nock": "^13.0.5",
"ts-jest": "^26.4.4",
"ts-jest": "^27.0.3",
"typescript": "^4.1.3"
},
"dependencies": {

View file

@ -19,6 +19,8 @@ import {
Responses,
WorkdayRequest,
WorkdayResponse,
WorkdaysRequest,
WorkdaysResponse,
} from './types';
export class HolidayAPI {
@ -102,4 +104,16 @@ export class HolidayAPI {
return this.request('workday', request);
}
async workdays(request: WorkdaysRequest = {}): Promise<WorkdaysResponse> {
if (!request.country) {
throw new Error('Missing country');
} else if (!request.start) {
throw new Error('Missing start date');
} else if (!request.end) {
throw new Error('Missing end date');
}
return this.request('workdays', request);
}
}

View file

@ -1,11 +1,11 @@
/**
* Copyright (c) Gravity Boulevard; LLC
* Copyright (c) Gravity Boulevard, LLC
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export type Endpoint = 'countries' | 'holidays' | 'languages' | 'workday';
export type Endpoint = 'countries' | 'holidays' | 'languages' | 'workday' | 'workdays';
type Weekday = {
name: 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday' | 'Sunday';
@ -46,6 +46,12 @@ export type WorkdayRequest = Request & {
days?: number;
};
export type WorkdaysRequest = Request & {
country?: string;
start?: string;
end?: string;
};
export type Response = {
requests: {
available: number;
@ -105,11 +111,16 @@ export type WorkdayResponse = Response & {
}
};
export type WorkdaysResponse = Response & {
workdays?: number;
};
export type Requests = (
| CountriesRequest
| HolidaysRequest
| LanguagesRequest
| WorkdayRequest
| WorkdaysRequest
);
export type Responses = (
@ -117,4 +128,5 @@ export type Responses = (
| HolidaysResponse
| LanguagesResponse
| WorkdayResponse
| WorkdaysResponse
);

View file

@ -607,5 +607,88 @@ describe('holidayapi', () => {
}
});
});
describe('/v1/workdays', () => {
const basePath = `/workdays?key=${key}`;
it('should return workdays', async () => {
const expectedResponse = {
status: 200,
requests: {
used: 1000,
available: 9000,
resets: '2019-10-01 00:00:00',
},
workdays: 7,
};
mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`)
.reply(200, expectedResponse);
expect(await holidayapi.workdays({
country: 'US',
start: '2019-07-01',
end: '2019-07-10',
})).toStrictEqual(expectedResponse);
});
it('should error when country is missing', async () => {
expect.assertions(1);
try {
await holidayapi.workdays();
} catch (err) {
expect(err.message).toMatch(/missing country/i);
}
});
it('should error when start is missing', async () => {
expect.assertions(1);
try {
await holidayapi.workdays({ country: 'US' });
} catch (err) {
expect(err.message).toMatch(/missing start date/i);
}
});
it('should error when end is missing', async () => {
expect.assertions(1);
try {
await holidayapi.workdays({ country: 'US', start: '2019-07-01' });
} catch (err) {
expect(err.message).toMatch(/missing end date/i);
}
});
it('should raise 4xx errors', async () => {
const expectedResponse = {
status: 429,
error: 'Rate limit exceeded',
};
expect.assertions(1);
mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`)
.reply(429, expectedResponse);
try {
await holidayapi.workdays({ country: 'US', start: '2019-07-01', end: '2019-07-10' });
} catch (err) {
expect(err.message).toMatch(/rate limit exceeded/i);
}
});
it('should raise 5xx errors', async () => {
expect.assertions(1);
mockRequest.get(`${basePath}&country=US&start=2019-07-01&end=2019-07-10`).reply(500);
try {
await holidayapi.workdays({ country: 'US', start: '2019-07-01', end: '2019-07-10' });
} catch (err) {
expect(err.message).toMatch(/internal server error/i);
}
});
});
});
});