Skip to content

Commit

Permalink
Add new Webhook.listener() method (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbberry authored Apr 13, 2021
1 parent 9da053a commit 590d0ad
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 21 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ const app = express() // Your express app

const webhook = new Topgg.Webhook('topggauth123') // add your top.gg webhook authorization (not bot token)

app.post('/dblwebhook', webhook.middleware(), (req, res) => {
// req.vote is your vote object e.g
console.log(req.vote.user) // 221221226561929217
}) // attach the middleware
app.post('/dblwebhook', webhook.listener(vote => {
// vote is your vote object
console.log(vote.user) // 221221226561929217
})) // attach the middleware

app.listen(3000) // your port
```
Expand Down
4 changes: 4 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Change Log

## 3.1.0

- Added Webhook.listener() to replace Webhook.middleware()

## 3.0.9
- A change to how typings are used, properly exports via dist/typings

Expand Down
43 changes: 36 additions & 7 deletions docs/Webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Top.gg Webhook

* [Webhook](#Webhook)
* [new Webhook(authorization)](#new_Webhook_new)
* [.middleware()](#Webhook+middleware)
* [.listener(fn)](#Webhook+listener)
* ~~[.middleware()](#Webhook+middleware)~~

<a name="new_Webhook_new"></a>

Expand All @@ -27,21 +28,49 @@ const { Webhook } = require(`@top-gg/sdk`)
const app = express()
const wh = new Webhook('webhookauth123')

app.post('/dblwebhook', wh.middleware(), (req, res) => {
// req.vote is your vote object e.g
console.log(req.vote.user) // => 321714991050784770
})
app.post('/dblwebhook', wh.listener(vote => {
// vote is your vote object e.g
console.log(vote.user) // => 321714991050784770
}))

app.listen(80)

// In this situation, your TopGG Webhook dashboard should look like
// URL = http://your.server.ip:80/dblwebhook
// Authorization: webhookauth123
```
<a name="Webhook+listener"></a>

### webhook.listener(fn) ⇒
Listening function for handling webhook requests

**Kind**: instance method of [<code>Webhook</code>](#Webhook)
**Returns**: An express request handler

| Param | Description |
| --- | --- |
| fn | Vote handling function, this function can also throw an error to allow for the webhook to resend from Top.gg |

**Example**
```js
app.post('/webhook', wh.listener((vote) => {
console.log(vote.user) // => 395526710101278721
}))
```
**Example**
```js
// Throwing an error to resend the webhook
app.post('/webhook/', wh.listener((vote) => {
// for example, if your bot is offline, you should probably not handle votes and try again
if (bot.offline) throw new Error('Bot offline')
}))
```
<a name="Webhook+middleware"></a>

### webhook.middleware()
Middleware function to pass to express, sets req.vote to the payload
### ~~webhook.middleware()~~
***Deprecated***

(Use the new .listener() function) Middleware function to pass to express, sets req.vote to the payload

**Kind**: instance method of [<code>Webhook</code>](#Webhook)
**Example**
Expand Down
Loading

0 comments on commit 590d0ad

Please sign in to comment.