mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 16:45:35 +01:00
Added batching
This commit is contained in:
parent
e34547cf2d
commit
de8f5c4413
1 changed files with 35 additions and 0 deletions
35
src/pages/api/batch.ts
Normal file
35
src/pages/api/batch.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import sendHandler from './send';
|
||||||
|
|
||||||
|
export default async function handler(req, res) {
|
||||||
|
if (req.method !== 'POST') {
|
||||||
|
res.setHeader('Allow', ['POST']);
|
||||||
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = req.body;
|
||||||
|
|
||||||
|
if (!Array.isArray(events)) {
|
||||||
|
return res.status(400).json({ error: 'Invalid payload, expected an array.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (const event of events) {
|
||||||
|
const mockReq = {
|
||||||
|
...req,
|
||||||
|
body: event,
|
||||||
|
headers: { ...req.headers, origin: req.headers.origin || 'http://localhost:3000' },
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockRes = {
|
||||||
|
...res,
|
||||||
|
end: () => {}, // Prevent premature response closure
|
||||||
|
};
|
||||||
|
|
||||||
|
await sendHandler(mockReq, mockRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({ success: true, message: `${events.length} events processed.` });
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json({ error: 'Internal Server Error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue