Refactor Supabase client and service role key usage

The refactoring includes the moving of the `getSupabaseClientKeys()` and `getServiceRoleKey()` function calls to the module scope and the addition of a `warnServiceRoleKeyUsage()` function, used to display a warning when the Supabase Service Role is accessed. The change aims to improve code readability and maintainability.
This commit is contained in:
giancarlo
2024-04-13 20:03:08 +08:00
parent 4b9c023700
commit f0bc6959e1
4 changed files with 32 additions and 14 deletions

View File

@@ -1,3 +1,5 @@
import 'server-only';
import { z } from 'zod';
/**
@@ -7,12 +9,16 @@ import { z } from 'zod';
*/
export function getServiceRoleKey() {
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
return z.string().min(1).parse(serviceRoleKey);
}
/**
* Displays a warning message if the Supabase Service Role is being used.
*/
export function warnServiceRoleKeyUsage() {
if (process.env.NODE_ENV !== 'production') {
console.warn(
`[Dev Only] This is a simple warning to let you know you are using the Supabase Service Role. Make sure it's the right call.`,
);
}
return z.string().min(1).parse(serviceRoleKey);
}