Files
supabase/apps/studio/lib/api/apiWrapper.ts
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

40 lines
1.1 KiB
TypeScript

import { withSentry } from '@sentry/nextjs'
import { isResponseOk } from 'lib/common/fetch'
import { 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
}
}
const func = withSentry(handler as any)
// @ts-ignore
return await func(req, res)
} catch (error) {
return res.status(500).json({ error })
}
}