UNPKG

@digicms/cms

Version:

An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite

34 lines (25 loc) 750 B
'use strict'; const createMiddleware = ({ sendEvent }) => { const _state = { currentDay: null, counter: 0, }; return async (ctx, next) => { const { url, method } = ctx.request; if (!url.includes('.') && ['GET', 'PUT', 'POST', 'DELETE'].includes(method)) { const dayOfMonth = new Date().getDate(); if (dayOfMonth !== _state.currentDay) { _state.currentDay = dayOfMonth; _state.counter = 0; } // Send max. 1000 events per day. if (_state.counter < 1000) { sendEvent('didReceiveRequest', { eventProperties: { url: ctx.request.url } }); // Increase counter. _state.counter += 1; } } await next(); }; }; module.exports = createMiddleware;