npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeorm-webhook-extensions

v0.0.3

Published

Simply add Webhooks to your TypeORM models

Downloads

6

Readme

TypeORM Webhook Extensions

Simply add webhooks to your TypeORM entities. Send events to a webhook-configured endpoint(s) after a TypeORM entity is loaded, removed, updated, or inserted.

Usage

Installation

npm install --save typeorm-webhook-extensions

or

yarn add typeorm-webhook-extensions

Decorate TypeORM Models with @Webhook

import { Entity } from 'typeorm';
import webhookExtension, { Webhook } from 'typeorm-webhook-extensions';

@Entity('users')
@Webhook({
  sendOnInsert: true,
  sendOnLoad: true,
  sendOnRemove: true,
  sendOnUpdate: true,
  onError: (error) => { console.log(error); },
  onSuccess: (endpoint, status, payload) => { console.log(endpoint, status, payload )},
})
class User {
  @PrimaryGeneratedColumn()
  public id: number = 0;

  @Column()
  public username: string = '';

  @Column()
  public password: string = '';
}

The @Webhook decorator takes arguments that match the WebhookOptions interface:

interface WebhookOptions {
  sendOnLoad?: boolean | WebhookFormatter;
  sendOnUpdate?: boolean | WebhookFormatter;
  sendOnRemove?: boolean | WebhookFormatter;
  sendOnInsert?: boolean | WebhookFormatter;
  webhookEndpoint?: string | string | WebhookEndpointBuilder;
  okStatuses?: number[];
  onError?: WebhookErrorHandler;
  onSuccess?: WebhookActionLogger;
};

WebhookEndpointBuilder is an asynchronous function that returns Promise<string | string[]>. If you need to send events to different webhook URLs based on information in the entity, this is approximately how you can do so:

const getWebhookEndpoints = async (entity: any) => {
  const companyEndpoints = await getConnection().query(`SELECT webhook_endpoint FROM companies WHERE owner_id=${entity.id};`);
  return companyEndpoints.map(company => company.webhook_endpoint);
}

If true is passed to sendOnLoad, sendOnUpdate, sendOnRemove, or sendOnInsert, a payload containing the non-function properties from your entity along with webhookType will be sent to your specified webhook. Optionally, you may pass a WebhookFormatter function in for any of these options. The WebhookFormatter receives the instance of the entity as its sole argument. For example, this would exclude the (hopefully hashed) password from being sent to the webhook endpoint:

...
@Webhook({
  sendOnLoad: (entity => ({
    event_type: 'load',
    resource: 'user',
    username: entity.username,
  })),
})

Change Global Options

Some options can be configured globally for all decorated TypeORM entities. Here is an example of how you can change these options:

// Import as above
webhookExtension.setOptions(<TypeORMWebhookExtensionsOptions> {
  okStatuses: [200, 201], // HTTP status codes that signify the webhook server successfully received the event
  webhookEndpoint: 'https://localhost/api/webhook', // The URL to send webhook events to
  onError: (error) => { console.log(error); }, // A callback to run in the event that the webhook event does not successfully send
  onSuccess: (endpoint, status, payload) => { console.log(endpoint, status, payload )}, // A callback to run in the event that the webhook event successfully sends
});

TypeScript Configuration

{
  "target": "es2015", // at least
  "experimentalDecorators": true
}

Note

Be sure to set up a webhookEndpoint, either on the default export singleton or in the webhook decorator's options.

Contribution

Feel free to contribute by forking this repository, making, testing, and building your changes, then opening a pull request. Please try to maintain a uniform code style.