Skip to content

Instantly share code, notes, and snippets.

@KolbySisk
Last active August 29, 2022 15:00
Show Gist options
  • Save KolbySisk/6dd29d88ae4c3f46fe997234f1e58867 to your computer and use it in GitHub Desktop.
Save KolbySisk/6dd29d88ae4c3f46fe997234f1e58867 to your computer and use it in GitHub Desktop.
import type { NextApiRequest, NextApiResponse } from 'next';
type User = { userId: string };
const allowedMethods = ['GET'];
const handler = (req: NextApiRequest, res: NextApiResponse<User>) => {
try {
if (!allowedMethods.includes(req.method!) || req.method == 'OPTIONS') {
return res.status(405).send({ message: 'Method not allowed.' });
}
return res.status(200).json({ userId: 'abc123' });
} catch (error) {
// Catch and log errors - return a 500 with a message
console.error(error);
Sentry.captureException(error);
res.status(500).send({ message: 'Server error!' });
}
};
export default handler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment