Skip to content

Commit 590d0ad

Browse files
authored
Add new Webhook.listener() method (#55)
1 parent 9da053a commit 590d0ad

File tree

6 files changed

+458
-21
lines changed

6 files changed

+458
-21
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ const app = express() // Your express app
5757

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

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

6565
app.listen(3000) // your port
6666
```

docs/Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# Change Log
44

5+
## 3.1.0
6+
7+
- Added Webhook.listener() to replace Webhook.middleware()
8+
59
## 3.0.9
610
- A change to how typings are used, properly exports via dist/typings
711

docs/Webhook.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Top.gg Webhook
77

88
* [Webhook](#Webhook)
99
* [new Webhook(authorization)](#new_Webhook_new)
10-
* [.middleware()](#Webhook+middleware)
10+
* [.listener(fn)](#Webhook+listener)
11+
* ~~[.middleware()](#Webhook+middleware)~~
1112

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

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

30-
app.post('/dblwebhook', wh.middleware(), (req, res) => {
31-
// req.vote is your vote object e.g
32-
console.log(req.vote.user) // => 321714991050784770
33-
})
31+
app.post('/dblwebhook', wh.listener(vote => {
32+
// vote is your vote object e.g
33+
console.log(vote.user) // => 321714991050784770
34+
}))
3435

3536
app.listen(80)
3637

3738
// In this situation, your TopGG Webhook dashboard should look like
3839
// URL = http://your.server.ip:80/dblwebhook
3940
// Authorization: webhookauth123
4041
```
42+
<a name="Webhook+listener"></a>
43+
44+
### webhook.listener(fn) ⇒
45+
Listening function for handling webhook requests
46+
47+
**Kind**: instance method of [<code>Webhook</code>](#Webhook)
48+
**Returns**: An express request handler
49+
50+
| Param | Description |
51+
| --- | --- |
52+
| fn | Vote handling function, this function can also throw an error to allow for the webhook to resend from Top.gg |
53+
54+
**Example**
55+
```js
56+
app.post('/webhook', wh.listener((vote) => {
57+
console.log(vote.user) // => 395526710101278721
58+
}))
59+
```
60+
**Example**
61+
```js
62+
// Throwing an error to resend the webhook
63+
app.post('/webhook/', wh.listener((vote) => {
64+
// for example, if your bot is offline, you should probably not handle votes and try again
65+
if (bot.offline) throw new Error('Bot offline')
66+
}))
67+
```
4168
<a name="Webhook+middleware"></a>
4269

43-
### webhook.middleware()
44-
Middleware function to pass to express, sets req.vote to the payload
70+
### ~~webhook.middleware()~~
71+
***Deprecated***
72+
73+
(Use the new .listener() function) Middleware function to pass to express, sets req.vote to the payload
4574

4675
**Kind**: instance method of [<code>Webhook</code>](#Webhook)
4776
**Example**

0 commit comments

Comments
 (0)