Skip to content

Commit

Permalink
chore: 🔧 use cache control headers directly from responses
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashRajpurohit committed Aug 3, 2024
1 parent ff93723 commit 1da173e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Hono } from 'hono';
import { prettyJSON } from 'hono/pretty-json';
import { cors } from 'hono/cors';
import { cache } from 'hono/cache'
import quotes from '../data/quotes.json';
import episodes from '../data/episodes.json';

Expand All @@ -10,7 +9,6 @@ const app = new Hono();
// Middlewares
app.use('*', prettyJSON());
app.use('*', cors());
app.use('*', cache({ cacheName: 'the-office-api', cacheControl: 's-maxage=15' }));

// Routes
app.get('/', (c) => {
Expand All @@ -24,7 +22,9 @@ app.get('/', (c) => {
// Quotes routes
app.get('/quote/random', (c) => {
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
return c.json(randomQuote);
return c.json(randomQuote, 200, {
'Cache-Control': 's-maxage=15',
});
});

app.get('/quote/:id', (c) => {
Expand Down Expand Up @@ -52,7 +52,9 @@ app.get('/quote/:id', (c) => {
);
}

return c.json(quote);
return c.json(quote, 200, {
'Cache-Control': 's-maxage=15',
});
});

// Seasons & Episodes routes
Expand Down Expand Up @@ -81,7 +83,9 @@ app.get('/season/:id', (c) => {
);
}

return c.json(seasonData);
return c.json(seasonData, 200, {
'Cache-Control': 's-maxage=15',
});
});

app.get('/season/:season_id/episode/:episode_id', (c) => {
Expand Down Expand Up @@ -112,7 +116,9 @@ app.get('/season/:season_id/episode/:episode_id', (c) => {
);
}

return c.json(episodeData);
return c.json(episodeData, 200, {
'Cache-Control': 's-maxage=15',
});
});

app.notFound((c) => c.json({ ok: false, message: 'Not Found' }, 404));
Expand Down

0 comments on commit 1da173e

Please sign in to comment.