Skip to content

Commit afc0c4d

Browse files
heiskrCopilot
andauthored
Convert webhooks directory JS files to TypeScript (#55608)
Co-authored-by: Copilot <[email protected]>
1 parent 52d6c14 commit afc0c4d

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,44 @@ export const WEBHOOK_DATA_DIR = 'src/webhooks/data'
77
export const WEBHOOK_SCHEMA_FILENAME = 'schema.json'
88

99
// cache for webhook data per version
10-
const webhooksCache = new Map()
10+
const webhooksCache = new Map<string, Promise<Record<string, any>>>()
1111
// cache for webhook data for when you first visit the webhooks page where we
1212
// show all webhooks for the current version but only 1 action type per webhook
1313
// and also no nested parameters
14-
const initialWebhooksCache = new Map()
14+
const initialWebhooksCache = new Map<string, InitialWebhook[]>()
15+
16+
interface InitialWebhook {
17+
name: string
18+
actionTypes: string[]
19+
data: {
20+
bodyParameters?: Array<{
21+
childParamsGroups?: any[]
22+
[key: string]: any
23+
}>
24+
[key: string]: any
25+
}
26+
}
1527

1628
// return the webhoook data as described for `initialWebhooksCache` for the given
1729
// version
18-
export async function getInitialPageWebhooks(version) {
30+
export async function getInitialPageWebhooks(version: string): Promise<InitialWebhook[]> {
1931
if (initialWebhooksCache.has(version)) {
20-
return initialWebhooksCache.get(version)
32+
return initialWebhooksCache.get(version) || []
2133
}
2234
const allWebhooks = await getWebhooks(version)
23-
const initialWebhooks = []
35+
const initialWebhooks: InitialWebhook[] = []
2436

2537
// The webhooks page shows all webhooks but for each webhook only a single
2638
// webhook action type at a time. We pick the first webhook type from each
2739
// webhook's set of action types to show.
2840
for (const [key, webhook] of Object.entries(allWebhooks)) {
2941
const actionTypes = Object.keys(webhook)
30-
const defaultAction = actionTypes ? actionTypes[0] : null
42+
const defaultAction = actionTypes.length > 0 ? actionTypes[0] : ''
3143

32-
const initialWebhook = {
44+
const initialWebhook: InitialWebhook = {
3345
name: key,
3446
actionTypes,
35-
data: webhook[defaultAction],
47+
data: defaultAction ? webhook[defaultAction] : {},
3648
}
3749

3850
// remove all nested params for the initial webhooks page, we'll load
@@ -54,13 +66,16 @@ export async function getInitialPageWebhooks(version) {
5466
// returns the webhook data for the given version and webhook category (e.g.
5567
// `check_run`) -- this includes all the data per webhook action type and all
5668
// nested parameters
57-
export async function getWebhook(version, webhookCategory) {
69+
export async function getWebhook(
70+
version: string,
71+
webhookCategory: string,
72+
): Promise<Record<string, any> | undefined> {
5873
const webhooks = await getWebhooks(version)
5974
return webhooks[webhookCategory]
6075
}
6176

6277
// returns all the webhook data for the given version
63-
export async function getWebhooks(version) {
78+
export async function getWebhooks(version: string): Promise<Record<string, any>> {
6479
const openApiVersion = getOpenApiVersion(version)
6580
if (!webhooksCache.has(openApiVersion)) {
6681
// The `readCompressedJsonFileFallback()` function
@@ -73,5 +88,5 @@ export async function getWebhooks(version) {
7388
)
7489
}
7590

76-
return webhooksCache.get(openApiVersion)
91+
return webhooksCache.get(openApiVersion) || Promise.resolve({})
7792
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const router = express.Router()
1010
// Example request:
1111
//
1212
// /api/webhooks/v1?category=check_run&version=free-pro-team%40latest
13-
router.get('/v1', async function webhooks(req, res, next) {
13+
router.get('/v1', async function webhooks(req, res) {
1414
if (!req.query.category) {
1515
return res.status(400).json({ error: "Missing 'category' in query string" })
1616
}
@@ -27,7 +27,7 @@ router.get('/v1', async function webhooks(req, res, next) {
2727
return res.status(404).json({ error: notFoundError })
2828
}
2929

30-
const webhook = await getWebhook(webhookVersion, req.query.category)
30+
const webhook = await getWebhook(webhookVersion, req.query.category as string)
3131

3232
if (webhook) {
3333
if (process.env.NODE_ENV !== 'development') {

0 commit comments

Comments
 (0)