@@ -29,16 +29,26 @@ type BroadCastResponse = {
2929 broadcasts : BroadcastSkinny [ ] ;
3030} ;
3131
32+ async function parseResponse ( res : Response ) {
33+ try {
34+ return await res . json ( ) as Promise < BroadCastResponse > ;
35+ } catch {
36+ return {
37+ broadcasts : [ ] ,
38+ }
39+ }
40+ }
41+
3242async function fetchBroadcastList ( ) {
3343 // 1. Fetch all broadcasts from ConvertKit
3444 // 2 pages - 100 broadcasts should be enough. This will need to be updated if we ever have more than 100 issues
3545 const responses = await Promise . all ( [
3646 fetch (
3747 `https://api.convertkit.com/v3/broadcasts?page=1&api_secret=${ env . CONVERT_KIT_SECRET } `
38- ) . then ( ( res ) => res . json ( ) as Promise < BroadCastResponse > ) ,
48+ ) . then ( parseResponse ) ,
3949 fetch (
4050 `https://api.convertkit.com/v3/broadcasts?page=2&api_secret=${ env . CONVERT_KIT_SECRET } `
41- ) . then ( ( res ) => res . json ( ) as Promise < BroadCastResponse > )
51+ ) . then ( parseResponse ) ,
4252 ] ) ;
4353
4454 const broadcasts = responses
@@ -63,29 +73,33 @@ async function fetchBroadcastList() {
6373 } ) ;
6474
6575 // Now we need to hit the ConvertKit API for every single broadcast to get info on if this broadcast was published, as well as the associated HTML for each snackpack
66- const broadcastsWithData = (
67- await Promise . all (
68- broadcasts . map ( async ( broadcast ) => {
69- const res = await fetch (
70- `https://api.convertkit.com/v3/broadcasts/${ broadcast . id } ?api_secret=${ env . CONVERT_KIT_SECRET } `
71- ) ;
72- const data = await res . json ( ) ;
73- return data . broadcast as Broadcast ;
74- } )
76+ try {
77+ const broadcastsWithData = (
78+ await Promise . all (
79+ broadcasts . map ( async ( broadcast ) => {
80+ const res = await fetch (
81+ `https://api.convertkit.com/v3/broadcasts/${ broadcast . id } ?api_secret=${ env . CONVERT_KIT_SECRET } `
82+ ) ;
83+ const data = await res . json ( ) ;
84+ return data . broadcast as Broadcast ;
85+ } )
86+ )
7587 )
76- )
77- // filter for public broadcasts
78- . filter ( ( broadcast ) => broadcast . public )
79- // trim to only the fields we need (published_at, subject)
80- . map ( ( broadcast ) => {
81- return {
82- published_at : broadcast . published_at ,
83- subject : broadcast . subject ,
84- id : broadcast . id
85- } ;
86- } ) ;
87-
88- return broadcastsWithData ;
88+ // filter for public broadcasts
89+ . filter ( ( broadcast ) => broadcast . public )
90+ // trim to only the fields we need (published_at, subject)
91+ . map ( ( broadcast ) => {
92+ return {
93+ published_at : broadcast . published_at ,
94+ subject : broadcast . subject ,
95+ id : broadcast . id
96+ } ;
97+ } ) ;
98+
99+ return broadcastsWithData ;
100+ } catch ( error ) {
101+ return [ ] ;
102+ }
89103}
90104
91105export const load : PageServerLoad = async function ( { setHeaders, params, locals } ) {
0 commit comments