From c1b0ad6ec541bf0c51bbac2743604969959ef56f Mon Sep 17 00:00:00 2001 From: Adw8 Date: Thu, 30 Jan 2025 10:16:05 +0530 Subject: [PATCH] Add API for getting latest quote --- server.ts | 18 ++++++++++++++++-- src/app/api/quotes/route.ts | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/app/api/quotes/route.ts diff --git a/server.ts b/server.ts index fceaefe..16d841e 100644 --- a/server.ts +++ b/server.ts @@ -10,6 +10,22 @@ const port = parseInt(process.env.PORT || '3000', 10); const app = next({ dev: process.env.NODE_ENV !== 'production' }); const handle = app.getRequestHandler(); +declare global { + namespace NodeJS { + interface Global { + quoteService: { + getLatestQuote: typeof getLatestQuote; + } + } + } +} + +// TODO: Look for a better way to use quoteService +// Initialize the global quote service +(global as any).quoteService = { + getLatestQuote +}; + app.prepare().then(() => { const server = createServer(async (req, res) => { const parsedUrl = parse(req.url!, true); @@ -27,5 +43,3 @@ app.prepare().then(() => { fetchAndCacheQuotes(); // Initial store setInterval(fetchAndCacheQuotes, 5 * 60 * 1000); // Update cache every 5 minutes }); - -export { getLatestQuote }; diff --git a/src/app/api/quotes/route.ts b/src/app/api/quotes/route.ts new file mode 100644 index 0000000..ee8f78d --- /dev/null +++ b/src/app/api/quotes/route.ts @@ -0,0 +1,6 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export async function GET(req: NextRequest) { + const quote = (global as any).quoteService.getLatestQuote(); + return NextResponse.json(quote); +}