@@ -7,32 +7,44 @@ export const WEBHOOK_DATA_DIR = 'src/webhooks/data'
77export 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}
0 commit comments