Skip to content

refactor: move supabaseAdmin instantiation outside request handler and improve error handling #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions apps/deploy-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const app = new Hono()

app.use('*', cors())

// Global instance for suppabase admin (does not depend on the user)
const supabaseAdmin = createClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
)

app.post(
'/',
zValidator(
Expand All @@ -49,11 +55,7 @@ app.post(
throw new HTTPException(401, { message: 'Unauthorized' })
}

const supabaseAdmin = createClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!
)

// Only the user's customer is created by Request
const supabase = createClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!
Expand Down Expand Up @@ -81,11 +83,11 @@ app.post(
} catch (error: unknown) {
console.error(error)
if (error instanceof DeployError) {
throw new HTTPException(500, { message: error.message })
throw new HTTPException(500, { message: (error as Error).message })
}
if (error instanceof IntegrationRevokedError) {
await revokeIntegration(ctx, { integrationId })
throw new HTTPException(406, { message: error.message })
// It is not necessary to wait for the revocation to respond to respond void revokeIntegration(ctx, { integrationId })
throw new HTTPException(406, { message: (error as Error).message })
}
throw new HTTPException(500, { message: 'Internal server error' })
}
Expand Down