@@ -5,77 +5,84 @@ import { env } from "hono/adapter";
55
66// Define the app context type to include environment variables
77interface AppEnv {
8- NTHUMODS_AUTH_URL : string ;
8+ NTHUMODS_AUTH_URL : string ;
99}
1010
11- const app = new Hono < { Bindings : AppEnv } > ( )
12- . get (
13- "/ical/:userId" ,
14- zValidator (
15- "query" ,
16- z . object ( {
17- key : z . string ( ) . optional ( ) ,
18- type : z . enum ( [ "basic" , "full" ] ) . default ( "basic" ) ,
19- } ) ,
20- ) ,
21- zValidator (
22- "param" ,
23- z . object ( {
24- userId : z . string ( ) ,
25- } ) ,
26- ) ,
27- async ( c ) => {
28- try {
29- const { userId } = c . req . valid ( "param" ) ;
30- const { key, type } = c . req . valid ( "query" ) ;
11+ const app = new Hono < { Bindings : AppEnv } > ( ) . get (
12+ "/ical/:userId" ,
13+ zValidator (
14+ "query" ,
15+ z . object ( {
16+ key : z . string ( ) . optional ( ) ,
17+ type : z . enum ( [ "basic" , "full" ] ) . default ( "basic" ) ,
18+ } ) ,
19+ ) ,
20+ zValidator (
21+ "param" ,
22+ z . object ( {
23+ userId : z . string ( ) ,
24+ } ) ,
25+ ) ,
26+ async ( c ) => {
27+ try {
28+ const { userId } = c . req . valid ( "param" ) ;
29+ const { key, type } = c . req . valid ( "query" ) ;
3130
32- if ( ! key ) {
33- return c . json ( { error : "Missing API key" } , 400 ) ;
34- }
31+ if ( ! key ) {
32+ return c . json ( { error : "Missing API key" } , 400 ) ;
33+ }
3534
36- // Get the secure API URL from environment variable
37- const { NTHUMODS_AUTH_URL : secureApiUrl } = env < { NTHUMODS_AUTH_URL : string } > ( c ) ;
35+ // Get the secure API URL from environment variable
36+ const { NTHUMODS_AUTH_URL : secureApiUrl } = env < {
37+ NTHUMODS_AUTH_URL : string ;
38+ } > ( c ) ;
3839
39- // Construct the URL for the secure API request
40- const url = new URL ( `${ secureApiUrl } /calendar/ics/${ userId } ` ) ;
41- url . searchParams . set ( "key" , key ) ;
42- url . searchParams . set ( "type" , type ) ;
40+ // Construct the URL for the secure API request
41+ const url = new URL ( `${ secureApiUrl } /calendar/ics/${ userId } ` ) ;
42+ url . searchParams . set ( "key" , key ) ;
43+ url . searchParams . set ( "type" , type ) ;
4344
44- // Forward the request to the secure API
45- const response = await fetch ( url . toString ( ) , {
46- headers : {
47- // Forward any necessary headers
48- " Accept" : "text/calendar" ,
49- } ,
50- } ) ;
45+ // Forward the request to the secure API
46+ const response = await fetch ( url . toString ( ) , {
47+ headers : {
48+ // Forward any necessary headers
49+ Accept : "text/calendar" ,
50+ } ,
51+ } ) ;
5152
52- // Check if the request was successful
53- if ( ! response . ok ) {
54- const errorText = await response . text ( ) ;
55- console . error ( `Error fetching calendar: ${ response . status } ${ errorText } ` ) ;
56- return c . json (
57- { error : `Failed to fetch calendar: ${ response . statusText } ` , status : response . status }
58- ) ;
59- }
53+ // Check if the request was successful
54+ if ( ! response . ok ) {
55+ const errorText = await response . text ( ) ;
56+ console . error (
57+ `Error fetching calendar: ${ response . status } ${ errorText } ` ,
58+ ) ;
59+ return c . json ( {
60+ error : `Failed to fetch calendar: ${ response . statusText } ` ,
61+ status : response . status ,
62+ } ) ;
63+ }
6064
61- // Get the calendar data
62- const calendarData = await response . text ( ) ;
65+ // Get the calendar data
66+ const calendarData = await response . text ( ) ;
6367
64- // Set the appropriate headers for the iCalendar file
65- c . header ( "Content-Type" , "text/calendar" ) ;
66- c . header ( "Content-Disposition" , `attachment; filename=${ userId } _calendar.ics` ) ;
67- c . header ( "Cache-Control" , "private, max-age=3600" ) ; // Cache for 1 hour
68+ // Set the appropriate headers for the iCalendar file
69+ c . header ( "Content-Type" , "text/calendar" ) ;
70+ c . header (
71+ "Content-Disposition" ,
72+ `attachment; filename=${ userId } _calendar.ics` ,
73+ ) ;
74+ c . header ( "Cache-Control" , "private, max-age=3600" ) ; // Cache for 1 hour
6875
69- // Return the calendar data
70- return c . body ( calendarData ) ;
71- } catch ( error ) {
72- console . error ( "Error proxying calendar request:" , error ) ;
73- return c . json (
74- { error : "Internal server error while fetching calendar" } ,
75- 500
76- ) ;
77- }
78- }
79- ) ;
76+ // Return the calendar data
77+ return c . body ( calendarData ) ;
78+ } catch ( error ) {
79+ console . error ( "Error proxying calendar request:" , error ) ;
80+ return c . json (
81+ { error : "Internal server error while fetching calendar" } ,
82+ 500 ,
83+ ) ;
84+ }
85+ } ,
86+ ) ;
8087
8188export default app ;
0 commit comments