Skip to content

A Node.js library for interacting with the Dead Man's Snitch REST API

License

Notifications You must be signed in to change notification settings

pauldenver/deadmanssnitch-client

Repository files navigation

Dead Man's Snitch Client GitHub package.json version Build Status Coverage Status codecov

The deadmanssnitch-client Node.js library contains a simple and convenient HTTP client for making requests to Dead Man's Snitch check-ins and the Dead Man's Snitch REST API.

Table of contents

Installation

Using NPM:

$ npm install deadmanssnitch-client

Using Yarn:

$ yarn add deadmanssnitch-client

Usage

Initializing a client

The library exports a DeadMansSnitchClient class for interacting with Dead Man's Snitch check-ins and the Dead Man's Snitch REST API. You'll need an API key provided by Dead Man's Snitch in order to interact with the Dead Man's Snitch REST API. An API key is required for REST API calls, but it is not necessary for snitch check-ins. You can provide the API key as an apiKey option or set the API key as an environment variable using DMS_API_KEY. By default, the client will use the DMS_API_KEY environment variable if the apiKey option is not provided.

Example import methods:

const DeadMansSnitchClient = require('deadmanssnitch-client');

// Creating an API client.
const client = new DeadMansSnitchClient({
  apiKey: 'DEAD-MANS-SNITCH-API-KEY'
});
import DeadMansSnitchClient from 'deadmanssnitch-client';

// Creating an API client.
const client = new DeadMansSnitchClient({
  apiKey: 'DEAD-MANS-SNITCH-API-KEY'
});

Using environment variables in an application:

const DeadMansSnitchClient = require('deadmanssnitch-client');

// Set the API key as an environment variable.
process.env.DMS_API_KEY = 'DEAD-MANS-SNITCH-API-KEY';

const client = new DeadMansSnitchClient();

Setting environment variables before running an application:

Linux:

$ DMS_API_KEY=DEAD-MANS-SNITCH-API-KEY node app.js

Windows:

> cmd /C "set DMS_API_KEY=DEAD-MANS-SNITCH-API-KEY && node app.js"

Options

These are the available options for creating a DeadMansSnitchClient instance.

Name Default Description
apiKey undefined Dead Man's Snitch API Key
timeout 5000 Number of milliseconds before the request times out
apiBaseUrl https://api.deadmanssnitch.com Base URL for the Dead Man's Snitch REST API
checkInBaseUrl https://nosnch.in Base URL for the Dead Man's Snitch check-ins
fullResponse false Get the full response instead of just the body
maxContentLength 10000 The max size of the HTTP response content in bytes
maxBodyLength 2000 The max allowed size of the HTTP request body in bytes
apiVersion 1 The REST API Version to use

Examples

The checkIn and REST API methods return a Promise which resolves with the response data or rejects with an error.

Alternatively, the checkIn method and each of the REST API methods can also be provided a completion callback function as its last argument. When a callback function is used the corresponding method does not return a value. The arguments passed to the completion callback function are err and resp. The first argument (err) is always reserved for an exception/error and the second argument (resp) is always reserved for the data returned from the check-in or API call. If the corresponding method is completed successfully, then the first argument will be undefined. If the method fails, then the second argument will be undefined.

Create an instance:

const DeadMansSnitchClient = require('deadmanssnitch-client');

// Creating an API client.
const client = new DeadMansSnitchClient({
  apiKey: 'DEAD-MANS-SNITCH-API-KEY'
});

Check-in with a snitch:

async function performTask(token) {
  /*
   * Add task logic here.
   */

  // Check-in after a task completes.
  await client.checkIn(token);
}

Check-in with a snitch and add a message:

async function performTask(token) {
  /*
   * Add task logic here.
   */

  // Check-in after a task completes.
  await client.checkIn(token, `All's good man!`);
}

Get a list of snitches:

async function getAllSnitches() {
  try {
    return await client.getSnitches();
  } catch(err) {
    console.error(err);
  }
}

Get a certain snitch:

async function getSnitchInfo(token) {
  try {
    return await client.getSnitch(token);
  } catch(err) {
    console.error(err);
  }
}

Create a new snitch:

async function createNewSnitch() {
  try {
    return await client.createSnitch({
      name: 'Web App',
      interval: 'daily'
    });
  } catch(err) {
    console.error(err);
  }
}

Update a snitch using a callback function:

async function updateMySnitch(token, snitchInfo) {
  await client.updateSnitch(token, snitchInfo, (err, resp) => {
    if (err) {
      console.error(err);
    } else {
      console.log(resp);
    }
  });
}

Documentation

Change Log

The CHANGELOG contains descriptions of notable changes.

License

This software is licensed under the Apache 2 license.

About

A Node.js library for interacting with the Dead Man's Snitch REST API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published