From cb1e4521e18e3d801333ecab7fa2889923275d83 Mon Sep 17 00:00:00 2001 From: thanatos666 Date: Wed, 22 Jul 2020 13:02:59 +0000 Subject: [PATCH] Added shorthand function for pings. --- lib/healthchecks_ping_client.js | 29 +++++++++++++- lib/index.js | 8 +++- package.json | 2 +- test/healthchecks_ping_client.test.js | 57 +++++++++++++++++++++++---- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/lib/healthchecks_ping_client.js b/lib/healthchecks_ping_client.js index 386b556..359a679 100644 --- a/lib/healthchecks_ping_client.js +++ b/lib/healthchecks_ping_client.js @@ -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, +}; diff --git a/lib/index.js b/lib/index.js index 7ff4616..e42331d 100644 --- a/lib/index.js +++ b/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, }; diff --git a/package.json b/package.json index 1727bd0..07dfa94 100644 --- a/package.json +++ b/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", diff --git a/test/healthchecks_ping_client.test.js b/test/healthchecks_ping_client.test.js index a911cf0..ebd585b 100644 --- a/test/healthchecks_ping_client.test.js +++ b/test/healthchecks_ping_client.test.js @@ -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); @@ -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); @@ -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); }); @@ -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(); }); @@ -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(); }); @@ -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(); }); @@ -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(); });