Refactor & clean up code.

This commit is contained in:
2025-07-17 15:20:59 -05:00
parent dabc248010
commit fefe7e8717
31 changed files with 473 additions and 295 deletions

View File

@@ -1,12 +1,10 @@
'use client';
import { createBrowserClient } from '@supabase/ssr';
import type { Database, SupabaseClient } from '@/utils/supabase';
import { useMemo } from 'react';
import type { Database, SBClientWithDatabase } from '@/utils/supabase';
let client: SupabaseClient | undefined;
let client: SBClientWithDatabase | undefined;
const getSupbaseClient = (): SupabaseClient | undefined => {
const getSupbaseClient = (): SBClientWithDatabase | undefined => {
if (client) return client;
client = createBrowserClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
@@ -15,8 +13,6 @@ const getSupbaseClient = (): SupabaseClient | undefined => {
return client;
};
const useSupabaseClient = () => {
return useMemo(getSupbaseClient, []);
};
const SupabaseClient = () => getSupbaseClient();
export { useSupabaseClient };
export { SupabaseClient };

View File

@@ -1,5 +1,5 @@
export { useSupabaseClient } from './client';
export { updateSession } from './middleware';
export { SupabaseClient } from './client';
export { SupabaseServer } from './server';
export { updateSession } from './middleware';
export type { Database } from './database.types';
export type * from './types';

View File

@@ -1,11 +1,10 @@
'use server';
import 'server-only';
import { createServerClient } from '@supabase/ssr';
import type { Database } from '@/utils/supabase';
import type { Database, SBClientWithDatabase } from '@/utils/supabase';
import { cookies } from 'next/headers';
export const SupabaseServer = async () => {
const SupabaseServer = async () => {
const cookieStore = await cookies();
return createServerClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
@@ -26,5 +25,7 @@ export const SupabaseServer = async () => {
},
},
},
);
) as SBClientWithDatabase;
};
export { SupabaseServer };

View File

@@ -1,7 +1,7 @@
import type { Database } from '@/utils/supabase/database.types';
import type { SupabaseClient as SBClient } from '@supabase/supabase-js'
export type SupabaseClient = SBClient<Database>;
export type SBClientWithDatabase = SBClient<Database>;
export type { User } from '@supabase/supabase-js';
@@ -10,6 +10,20 @@ export type Result<T> = {
error: { message: string } | null;
};
export type UserRecord = {
id: string,
updated_at: string | null,
email: string | null,
full_name: string | null,
avatar_url: string | null,
provider: string | null,
status: {
status: string,
created_at: string,
updated_by: Profile | null,
}
};
export type AsyncReturnType<T extends (...args: any) => Promise<any>> =
T extends (...args: any) => Promise<infer R> ? R : never;