forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.js
More file actions
42 lines (35 loc) · 1.25 KB
/
webhooks.js
File metadata and controls
42 lines (35 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import express from 'express'
import { getWebhook } from '../../src/webhooks/lib/index.js'
import { allVersions } from '../../lib/all-versions.js'
import { defaultCacheControl } from '../cache-control.js'
const router = express.Router()
// Returns a webhook for the given category and version
//
// Example request:
//
// /api/webhooks/v1?category=check_run&version=free-pro-team%40latest
router.get('/v1', async function webhooks(req, res, next) {
if (!req.query.category) {
return res.status(400).json({ error: "Missing 'category' in query string" })
}
if (!req.query.version) {
return res.status(400).json({ error: "Missing 'version' in query string" })
}
const webhookVersion = Object.values(allVersions).find(
(version) => version.version === req.query.version
)?.version
const notFoundError = 'No webhook found for given category and version'
if (!webhookVersion) {
return res.status(404).json({ error: notFoundError })
}
const webhook = await getWebhook(webhookVersion, req.query.category)
if (webhook) {
if (process.env.NODE_ENV !== 'development') {
defaultCacheControl(res)
}
return res.status(200).send(webhook)
} else {
res.status(404).json({ error: notFoundError })
}
})
export default router