Skip to content

Commit

Permalink
Added shorthand function for pings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldenver committed Jul 22, 2020
1 parent 74d396d commit cb1e452
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
29 changes: 28 additions & 1 deletion lib/healthchecks_ping_client.js
Expand Up @@ -205,4 +205,31 @@ class HealthChecksPingClient {
}
}

module.exports = HealthChecksPingClient;
/**
* A shorthand version for performing a Healthchecks.io ping
*
* @param {String} uuid Healthchecks.io UUID.
* @param {String} action Ping action ('success' or 'fail').
* @param {String} payload Payload string.
* @returns {String} The response string.
*/
const ping = async (uuid, action, payload) => {
// Set the action.
action = action || 'success';

// Create the ping client.
const pingClient = new HealthChecksPingClient({ uuid });

if (action === 'fail') {
// Perform the failure ping.
return await pingClient.fail(payload);
} else {
// Default to the success ping for everything else.
return await pingClient.success(payload);
}
};

module.exports = {
HealthChecksPingClient,
ping,
};
8 changes: 6 additions & 2 deletions lib/index.js
@@ -1,4 +1,8 @@
const { HealthChecksPingClient, ping } = require('./healthchecks_ping_client');
const HealthChecksApiClient = require('./healthchecks_api_client');

module.exports = {
HealthChecksPingClient: require('./healthchecks_ping_client'),
HealthChecksApiClient: require('./healthchecks_api_client'),
ping,
HealthChecksPingClient,
HealthChecksApiClient,
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "healthchecks-io-client",
"description": "A library for interacting with the Healthchecks.io API",
"version": "1.0.1",
"version": "1.0.2",
"main": "./lib/index.js",
"keywords": [
"healthchecks.io",
Expand Down
57 changes: 50 additions & 7 deletions test/healthchecks_ping_client.test.js
Expand Up @@ -2,7 +2,7 @@ const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const rewire = require('rewire');
const nock = require('nock');
const { HealthChecksPingClient } = require('../lib');
const { HealthChecksPingClient, ping } = require('../lib');

// Use 'chai-as-promised'.
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('Healthchecks.io Ping Client Tests', () => {
err.config = {};
err.request = {};
err.isAxiosError = true;
err.toJSON = () => {};
err.toJSON = () => { };

// Create the error.
const handledErr = client._handleError(err);
Expand Down Expand Up @@ -180,10 +180,10 @@ describe('Healthchecks.io Ping Client Tests', () => {

it(`should return an unmodified error`, () => {
const err = new Error('Oops, something happened');

// Create the error.
const handledErr = client._handleError(err);

expect(handledErr).to.be.an.instanceof(Error);
expect(handledErr).to.eql(err);
});
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('Healthchecks.io Ping Client Tests', () => {

expect(resp).to.be.a('string');
expect(resp).to.eql('OK');

// Remove the mocks.
nock.cleanAll();
});
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('Healthchecks.io Ping Client Tests', () => {

expect(resp).to.be.a('string');
expect(resp).to.eql('OK');

// Remove the mocks.
nock.cleanAll();
});
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('Healthchecks.io Ping Client Tests', () => {

expect(resp).to.be.a('string');
expect(resp).to.eql('OK');

// Remove the mocks.
nock.cleanAll();
});
Expand All @@ -359,6 +359,49 @@ describe('Healthchecks.io Ping Client Tests', () => {

await expect(client.start()).to.be.rejectedWith(Error);

// Remove the mocks.
nock.cleanAll();
});
});
});

describe('Healthchecks.io Ping Tests', () => {
// Mock UUID.
const uuid = '3c1169a0-7b50-11ea-873d-3c970e75c219';

context('#ping()', () => {
it('should perform a success ping', async () => {
// Mock the API request.
nock(baseUrl)
.get(`/${uuid}`)
.reply(200, 'OK');

await expect(ping(uuid, 'success')).to.be.fulfilled;

// Remove the mocks.
nock.cleanAll();
});

it('should perform a success ping by default', async () => {
// Mock the API request.
nock(baseUrl)
.get(`/${uuid}`)
.reply(200, 'OK');

await expect(ping(uuid)).to.be.fulfilled;

// Remove the mocks.
nock.cleanAll();
});

it('should perform a fail ping', async () => {
// Mock the API request.
nock(baseUrl)
.get(`/${uuid}/fail`)
.reply(200, 'OK');

await expect(ping(uuid, 'fail')).to.be.fulfilled;

// Remove the mocks.
nock.cleanAll();
});
Expand Down

0 comments on commit cb1e452

Please sign in to comment.