Update Supabase clients and refactor codebase

Refactored several Supabase client functions and updated them to use generics. Also, the '@kit/supabase-config' package was removed from the project and all references were replaced accordingly. The project's dependencies were updated as well, including the Supabase package which was upgraded to the latest version.
This commit is contained in:
giancarlo
2024-04-11 12:31:08 +08:00
parent 1c344d0d7f
commit 48f1ee90c4
29 changed files with 1337 additions and 96 deletions

View File

@@ -1,5 +1,6 @@
import 'server-only';
import { Database } from '@kit/supabase/database';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { BillingGatewayService } from './billing-gateway.service';
@@ -18,7 +19,7 @@ export async function getBillingGatewayProvider(
}
async function getBillingProvider(
client: ReturnType<typeof getSupabaseServerActionClient>,
client: ReturnType<typeof getSupabaseServerActionClient<Database>>,
) {
const { data, error } = await client
.from('config')

View File

@@ -1,24 +1,18 @@
import { SupabaseClient } from '@supabase/supabase-js';
import { invariant } from '@epic-web/invariant';
import { createBrowserClient } from '@supabase/ssr';
import { Database } from '../database.types';
let client: SupabaseClient<Database>;
export function getSupabaseBrowserClient() {
/**
* @name getSupabaseBrowserClient
* @description Get a Supabase client for use in the Browser
*/
export function getSupabaseBrowserClient<GenericSchema = Database>() {
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL;
const SUPABASE_ANON_KEY = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
invariant(SUPABASE_URL, `Supabase URL was not provided`);
invariant(SUPABASE_ANON_KEY, `Supabase Anon key was not provided`);
if (client) {
return client;
}
client = createBrowserClient<Database>(SUPABASE_URL, SUPABASE_ANON_KEY);
return client;
return createBrowserClient<GenericSchema>(SUPABASE_URL, SUPABASE_ANON_KEY);
}

View File

@@ -7,15 +7,17 @@ import { createServerClient } from '@supabase/ssr';
import { Database } from '../database.types';
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
const createServerSupabaseClient = () => {
function createServerSupabaseClient() {
const keys = getSupabaseClientKeys();
return createServerClient<Database>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
};
}
export const getSupabaseServerActionClient = (params?: { admin: boolean }) => {
export function getSupabaseServerActionClient<
GenericSchema = Database,
>(params?: { admin: boolean }) {
const keys = getSupabaseClientKeys();
const admin = params?.admin ?? false;
@@ -32,7 +34,7 @@ export const getSupabaseServerActionClient = (params?: { admin: boolean }) => {
throw new Error('Supabase Service Role Key not provided');
}
return createServerClient<Database>(keys.url, serviceRoleKey, {
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {
persistSession: false,
},
@@ -41,7 +43,7 @@ export const getSupabaseServerActionClient = (params?: { admin: boolean }) => {
}
return createServerSupabaseClient();
};
}
function getCookiesStrategy() {
const cookieStore = cookies();

View File

@@ -11,11 +11,11 @@ import { getSupabaseClientKeys } from '../get-supabase-client-keys';
* @name getSupabaseServerComponentClient
* @description Get a Supabase client for use in the Server Components
*/
export const getSupabaseServerComponentClient = (
export function getSupabaseServerComponentClient<GenericSchema = Database>(
params = {
admin: false,
},
) => {
) {
const keys = getSupabaseClientKeys();
if (params.admin) {
@@ -31,7 +31,7 @@ export const getSupabaseServerComponentClient = (
throw new Error('Supabase Service Role Key not provided');
}
return createServerClient<Database>(keys.url, serviceRoleKey, {
return createServerClient<GenericSchema>(keys.url, serviceRoleKey, {
auth: {
persistSession: false,
},
@@ -39,10 +39,10 @@ export const getSupabaseServerComponentClient = (
});
}
return createServerClient<Database>(keys.url, keys.anonKey, {
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(),
});
};
}
function getCookiesStrategy() {
const cookieStore = cookies();