Files
supabase/apps/studio/lib/api/apiWrapper.ts
Ivan Vasilov 92955d5555 chore: Bump sentry to v8+ (#29896)
* Bump sentry to v8.

* Fix all obsolete APIs.

* Fix the sentry configs.

* Add pages for testing sentry.

* Add missing imports to sentry.config file.

* Add base path to the api path.

* Remove extra files.
2024-10-17 11:39:06 +02:00

37 lines
1.0 KiB
TypeScript

import { isResponseOk } from 'lib/common/fetch'
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
import { IS_PLATFORM } from '../constants'
import { apiAuthenticate } from './apiAuthenticate'
// Purpose of this apiWrapper is to function like a global catchall for ANY errors
// It's a safety net as the API service should never drop, nor fail
export default async function apiWrapper(
req: NextApiRequest,
res: NextApiResponse,
handler: NextApiHandler,
options?: { withAuth: boolean }
) {
try {
const { withAuth } = options || {}
if (IS_PLATFORM && withAuth) {
const response = await apiAuthenticate(req, res)
if (!isResponseOk(response)) {
return res.status(401).json({
error: {
message: `Unauthorized: ${response.error.message}`,
},
})
} else {
// Attach user information to request parameters
;(req as any).user = response
}
}
return handler(req, res)
} catch (error) {
return res.status(500).json({ error })
}
}