Add API for getting latest quote

This commit is contained in:
Adw8 2025-01-30 10:16:05 +05:30
parent 8ed2c29c44
commit c1b0ad6ec5
2 changed files with 22 additions and 2 deletions

View File

@ -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 };

View File

@ -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);
}