UNPKG

novagate

Version:

Novagate is a flexible and fast API key middleware system for Node.js apps. It provides authentication, usage limits, burst protection, suspension, logging, and analytics.

25 lines (17 loc) 590 B
function CreateDailyUsageTracker(options) { const { storage } = options; return async function limit(req, res, next) { const dbKey = req.auth_key; let data = storage.getData(dbKey); // user object we will now if (data?.presentUsage >= data?.dailyUsage) { res.statusCode = 429; res.setHeader("Content-Type", "application/json"); return res.end(JSON.stringify({ error: "Daily API limit exceeded" })); } await storage.incrementUsage(dbKey); next(); }; } export { CreateDailyUsageTracker };