Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.0.1 #12

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## Release 1.0.1 (2021-10-29)

### Added

- Slack webhook adapter

### Updated

- Moved events from `event.ts` into their own classes
- Types folder is now located in the root directory
- Refactored process for dispatching events
- Logging is now done through a helper

## Release 1.0.0 (2021-10-08)

- Initial release
1,945 changes: 997 additions & 948 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stripe-webhooks",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -19,15 +19,18 @@
},
"homepage": "https://github.com/boone-software/stripe-webhooks#readme",
"dependencies": {
"chalk": "^4.1.2",
"date-fns": "^2.25.0",
"date-fns-tz": "^1.1.6",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"lodash": "^4.17.21",
"pm2": "^5.1.2"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jest": "^27.0.2",
"@types/lodash": "^4.14.176",
"@types/node": "^16.10.3",
"@types/supertest": "^2.0.11",
"jest": "^27.2.5",
Expand Down
9 changes: 8 additions & 1 deletion src/adapters/discord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { GenericMessage, FormattedMessage } from './../types/messages'
import { GenericMessage, FormattedMessage } from '../../types/messages'
import Dispatcher from '../dispatcher'

class DiscordAdapter extends Dispatcher {
/**
* Events that this dispatcher supports. If empty, all events are supported.
*
* @var {string[]}
*/
public readonly events: string[] = []

/**
* Constructor.
*/
Expand Down
39 changes: 39 additions & 0 deletions src/adapters/slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GenericMessage, FormattedMessage } from '../../types/messages'
import Dispatcher from '../dispatcher'

class SlackAdapter extends Dispatcher {
/**
* Events that this dispatcher supports. If empty, all events are supported.
*
* @var {string[]}
*/
public readonly events: string[] = []

/**
* Constructor.
*/
constructor() {
super()

this.endpoint = process.env.SLACK_WEBHOOK
this.name = 'Slack'

this.headers = {
'Content-Type': 'application/json',
}
}

/**
* Format generic message to target platform.
*
* @param {GenericMessage} data Generic message to format.
* @return {FormattedMessage}
*/
public format(data: GenericMessage): FormattedMessage {
return {
text: data.message,
}
}
}

export default SlackAdapter
66 changes: 20 additions & 46 deletions src/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import https from 'https'

import IDispatcher from './interfaces/dispatcher'
import { Response } from './types/events'
import { FormattedMessage } from './types/messages'
import { Response } from '../types/events'
import { FormattedMessage } from '../types/messages'

import Logger from './util/logger'

class Dispatcher implements IDispatcher {
/**
Expand All @@ -27,67 +29,39 @@ class Dispatcher implements IDispatcher {
public name: string = 'Dispatcher'

/**
* The type of event.
* The path to the endpoint.
*
* @var {string}
*/
private type: string
public path: string = '/'

/**
* Log output to the console.
* The type of event.
*
* @param {string} type Type of message.
* @param {string} message Message to output to console.
* @param {string} name (Optional) Adapter name.
* @return {void}
* @var {string}
*/
private logger(type: string, message: string, name?: string): void {
const adapterName = name || this.name

switch (type) {
case 'action':
console.debug(`⚙️\t[${adapterName}] ${message.trim()}`)
break

case 'error':
console.error(`😱\t[${adapterName}] ${message.trim()}`)
break

case 'info':
console.log(`💬\t[${adapterName}] ${message.trim()}`)
break

case 'warn':
console.warn(`⚠️\t[${adapterName}] ${message.trim()}`)
break

case 'debug':
// Fall through to default

default:
if (process.env.DEBUG) {
console.debug(`🐛\t[${adapterName}] ${message.trim()}`)
}
break
}
}
private type: string

/**
* Format the Stripe webhook event and then send the formatted message to the
* specified webhook.
*
* @param {FormattedMessage} message Stripe webhook event.
* @return {Promise<Response></Response>}
* @return {Promise<Response>}
*/
public async send(message: FormattedMessage): Promise<Response> {
if (!this.endpoint) {
throw new Error(`No endpoint specified for adapter "${this.name}"".`)
}

this.logger('info', 'Formatted message for adapter.')
this.logger('action', 'Let\'s send this message!')
Logger.info('Formatted message for adapter.', this.name)
Logger.action('Let\'s send this message!', this.name)

const headers: { [x: string]: any } = {'Content-Type': 'application/json', ...this.headers}
const headers: { [x: string]: any } = {
'Content-Type': 'application/json',
path: this.path,
...this.headers
}

const options = {
headers,
Expand All @@ -99,10 +73,10 @@ class Dispatcher implements IDispatcher {
const request = https.request(this.endpoint, options, (response) => {
let body: string = ''

this.logger('info', `Webhook endpoint response: ${response.statusCode}`)
Logger.info(`Webhook endpoint response: ${response.statusCode}`, this.name)

response.on('data', (chunk) => {
this.logger('info', `Response from adapter: ${chunk}`)
Logger.info(`Response from adapter: ${chunk}`, this.name)
body += chunk.toString()
})

Expand All @@ -120,7 +94,7 @@ class Dispatcher implements IDispatcher {
})

request.on('error', (error) => {
this.logger('error', `Error sending message to adapter: ${error.message}`)
Logger.error(`Error sending message to adapter: ${error.message}`, this.name)
reject(error)
})

Expand Down
Loading