1.0.3 • Published 2 years ago

toggl-webhook-express v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

toggl-webhook-express

Install

npm install --save toggl-webhook

Usage

import express from 'express';
import { webhookHandler } from 'toggl-webhook-express';

const app = express();
const port = 3001;

app.use(express.json());

const webhook = webhookHandler({
  secretProvider: 'shhhh' // same secret as used in your webhook subscription
});

app.post('/hook/', webhook, (req, res) => {
  const { body } = req;

  console.log('valid webhook event recieved', {date: new Date(), body});
  res.json({status: 'OK'});
});

Provide a secret based on incoming request

const webhook = webhookHandler({
  secretProvider: async (req: Request): Promise<string | null> => {
    // return secret based on request params (eg. based on userId)
  }
});

Disable autoValidate

By default all incoming ping requests that have a validation_code set in the body are validated by default. All other ping requests are just logged. To disable this feature set autoValidate to false.

const webhook = webhookHandler({
  autoValidate: false
});

Provide a custom logger

import { Logger } from 'toggl-webhook-express';

class MyLogger implements Logger {
  info(msg: string, tag: string, meta: unknown) {
    console.info(tag, msg, meta);
  }
  warning(msg: string, tag: string, meta: unknown) {
    console.warn(tag, msg, meta);
  }
}

const webhook = webhookHandler({
  logger: new MyLogger()
});

Create a subscription

toggl-webhook provides a node.js API wrapper for the toggl webhook api.