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

autohost-webhook

v0.1.1

Published

A webhook resource for autohost

Downloads

26

Readme

Autohost Webhook

Webhooks for all.

Installation & Initialization

Installation and setup are straight-forward.

Example - includes installation of prerequisites

npm install autohost hyped fount postal -S

Using default NeDB storage for hook information

var autohost = require( "autohost" );
var host;
var hyped = require( "hyped" )();
var fount = require( "fount" );
var postal = require( "postal" );
var channel = postal.channel( "eventChannel" );

fount.register( "webHookEvents", channel );

host = hyped.createHost( autohost, {
	port: config.nonstop.host.port,
	modules: [
		"autohost-webhook"
	],
	fount: fount
}, function() {
	host.start();
} );

Using a custom storage provider for hook information

See Storage API to understand how to create your own compatible storage layer.

var autohost = require( "autohost" );
var host;
var hyped = require( "hyped" )();
var fount = require( "fount" );
var postal = require( "postal" );
var channel = postal.channel( "eventChannel" );
var hooks = require( "./myHookStorage" );

fount.register( "hooks", hooks );
fount.register( "webHookEvents", channel );

host = hyped.createHost( autohost, {
	port: config.nonstop.host.port,
	modules: [
		"autohost-webhook"
	],
	fount: fount
}, function() {
	host.start();
} );

Use

Sending Messages

To call webhooks, simply send a message to the channel registered as "webHookEvents" earlier.

// this will send the message to all registered webhooks with a subscription matching "some.event" or that have not provided an `events` filter
channel.publish( "some.event", { ... } );

HTTP API

About Headers

When creating a web hook, please note that the headers section is the only way to support/provide authentication when calling an endpoint. It is recommended that even with HTTPS in place, your users don't provide anything other than tokens with least privilege.

About Events

Without providing a list of events that your webhook is interested in, this module will call your webhook for every event. Events support AMQP style pattern matching so if you wanted to know about every event related to a user, you could provide "user.#" to see all events under that namespace.

PUT vs. PATCH

Both are supported. PUT requires you to provide all the hook data while PATCH supports only changing the property you need to change.

Self

Request GET /api/hook/:id

Response

{
	"id": "",
	"url": "http://the.full.url/to/call",
	"method": "POST",
	"headers": {
	},
	"events": []
}

List

Request GET /api/hook/

Response

{
	"hooks": [
		{
			"id": "",
			"url": "http://the.full.url/to/call",
			"method": "POST",
			"headers": {
				"Authorization": "Bearer a.token"
			},
			"events": []
		},
		{
			"id": "",
			"url": "http://the.full.url/to/call",
			"method": "POST",
			"headers": {
				"Authorization": "Bearer a.token"
			},
			"events": []
		}
	]
}

Add

Request POST /api/hook/:id

{
	"url": "http://the.full.url/to/call",
	"method": "POST",
	"headers": {
		"Authorization": "Bearer a.token"
	},
	"events": []
}

Response

{
	"id": "webhook.identifier",
	"message": "Webhook webhook.identifier added successfully"
}

### Update
_Request_
`PATCH /api/hook/:id`
```json
[
	{ "op": "add", "field": "events", "value": "user.#" },
	{ "op": "remove", "field": "headers" },
	{ "op": "change", "field": "method", "value": "PUT" },
]

Response

{
	"id": "webhook.identifier",
	"message": "Webhook webhook.identifier updated successfully"
}

### Replace
_Request_
`PUT /api/hook/:id`
```json
{
	"url": "http://the.full.url/to/call",
	"method": "POST",
	"headers": {
		"Authorization": "Bearer new.token"
	},
	"events": [ "stuff" ]
}

Response

{
	"id": "webhook.identifier",
	"message": "Webhook webhook.identifier replaced successfully"
}


### Remove
_Request_
`DELETE /api/hook/:id`

_Response_
```json
{
	"id": "webhook.identifier",
	"message": "Webhook webhook.identifier removed successfully"
}

## Hook Storage API
Each call should return a promise. The following calls must be supplied:

 * add( id, hook )
 * checkId( id )
 * getById( id )
 * getList()
 * remove( id )
 * update( id, hook )

The structure of the hook object follows this format:

```javascript
{
	id: '', 		// required
	url: '',		// required
	method: '',		// required
	headers: {},	// optional, content-type will always be "application/json"
	events: [],		// optional, list of events to subscribe to
}

Note: the id is auto-prefixed based on the authenticated user. The delimiter used to separate the user identifier from the hook id is "::".