Skip to content

Commit

Permalink
Merge pull request #3275 from zeroCoder1/master
Browse files Browse the repository at this point in the history
Enhance Batch Processing by Reusing Existing Event Handling Logic
  • Loading branch information
mikecao authored Feb 28, 2025
2 parents 75009f7 + 796f6d4 commit f166a01
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/pages/api/batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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,
status: (code) => {
res.status(code);
return mockRes;
},
json: (data) => res.json(data),
setHeader: (key, value) => res.setHeader(key, value),
end: () => {},
};

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

0 comments on commit f166a01

Please sign in to comment.