@@ -79,7 +79,11 @@ const getEventJsonSchema = zodToJsonSchema(getEventSchema);
7979
8080const getEventsSchema = z . array ( getEventSchema ) ;
8181export type EventsGetResponse = z . infer < typeof getEventsSchema > ;
82- type EventsGetQueryParams = { upcomingOnly ?: boolean } ;
82+ type EventsGetQueryParams = {
83+ upcomingOnly ?: boolean ;
84+ host ?: string ;
85+ ts ?: number ;
86+ } ;
8387
8488const eventsPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
8589 fastify . post < { Body : EventPostRequest } > (
@@ -290,29 +294,50 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
290294 type : "object" ,
291295 properties : {
292296 upcomingOnly : { type : "boolean" } ,
297+ host : { type : "string" } ,
298+ ts : { type : "number" } ,
293299 } ,
294300 } ,
295301 response : { 200 : getEventsSchema } ,
296302 } ,
297303 } ,
298304 async ( request : FastifyRequest < EventsGetRequest > , reply ) => {
299305 const upcomingOnly = request . query ?. upcomingOnly || false ;
306+ const host = request . query ?. host ;
307+ const ts = request . query ?. ts ; // we only use this to disable cache control
300308 const cachedResponse = fastify . nodeCache . get (
301- `events-upcoming_only=${ upcomingOnly } ` ,
309+ `events-upcoming_only=${ upcomingOnly } |host= ${ host } ` ,
302310 ) ;
303311 if ( cachedResponse ) {
304312 return reply
305313 . header (
306314 "cache-control" ,
307- "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
315+ ts
316+ ? "no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate"
317+ : "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
308318 )
309- . header ( "acm-cache-status" , "hit" )
319+ . header ( "x- acm-cache-status" , "hit" )
310320 . send ( cachedResponse ) ;
311321 }
312322 try {
313- const response = await fastify . dynamoClient . send (
314- new ScanCommand ( { TableName : genericConfig . EventsDynamoTableName } ) ,
315- ) ;
323+ let command ;
324+ if ( host ) {
325+ command = new QueryCommand ( {
326+ TableName : genericConfig . EventsDynamoTableName ,
327+ ExpressionAttributeValues : {
328+ ":host" : {
329+ S : host ,
330+ } ,
331+ } ,
332+ KeyConditionExpression : "host = :host" ,
333+ IndexName : "HostIndex" ,
334+ } ) ;
335+ } else {
336+ command = new ScanCommand ( {
337+ TableName : genericConfig . EventsDynamoTableName ,
338+ } ) ;
339+ }
340+ const response = await fastify . dynamoClient . send ( command ) ;
316341 const items = response . Items ?. map ( ( item ) => unmarshall ( item ) ) ;
317342 const currentTimeChicago = moment ( ) . tz ( "America/Chicago" ) ;
318343 let parsedItems = getEventsSchema . parse ( items ) ;
@@ -354,9 +379,11 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
354379 reply
355380 . header (
356381 "cache-control" ,
357- "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
382+ ts
383+ ? "no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate"
384+ : "public, max-age=7200, stale-while-revalidate=900, stale-if-error=86400" ,
358385 )
359- . header ( "acm-cache-status" , "miss" )
386+ . header ( "x- acm-cache-status" , "miss" )
360387 . send ( parsedItems ) ;
361388 } catch ( e : unknown ) {
362389 if ( e instanceof Error ) {
0 commit comments