mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 09:51:13 -05:00
Fix Supabase client proxy
This commit is contained in:
@@ -1,118 +1,8 @@
|
||||
/**
|
||||
* Enhanced Supabase Client with Automatic Breadcrumb Tracking
|
||||
* Wraps the standard Supabase client to add automatic API call tracking
|
||||
* Central Supabase Client Export
|
||||
*
|
||||
* All application code should import from this file instead of the base client.
|
||||
* This provides a central point for potential future enhancements without breaking imports.
|
||||
*/
|
||||
|
||||
import { supabase as baseSupabase } from '@/integrations/supabase/client';
|
||||
import { breadcrumb } from './errorBreadcrumbs';
|
||||
|
||||
// Create a proxy that tracks API calls
|
||||
export const supabase = new Proxy(baseSupabase, {
|
||||
get(target, prop) {
|
||||
const value = target[prop as keyof typeof target];
|
||||
|
||||
// Track database operations
|
||||
if (prop === 'from') {
|
||||
return (table: string) => {
|
||||
const query = (target as any).from(table);
|
||||
|
||||
// Wrap query methods to track breadcrumbs
|
||||
return new Proxy(query, {
|
||||
get(queryTarget, queryProp) {
|
||||
const queryValue = queryTarget[queryProp as string];
|
||||
|
||||
if (typeof queryValue === 'function') {
|
||||
return async (...args: any[]) => {
|
||||
const method = String(queryProp).toUpperCase();
|
||||
breadcrumb.apiCall(`/table/${table}`, method);
|
||||
|
||||
try {
|
||||
const result = await queryValue.apply(queryTarget, args);
|
||||
|
||||
if (result.error) {
|
||||
breadcrumb.apiCall(`/table/${table}`, method, 400);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
breadcrumb.apiCall(`/table/${table}`, method, 500);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return queryValue;
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Track RPC calls
|
||||
if (prop === 'rpc') {
|
||||
return async (functionName: string, params?: any) => {
|
||||
breadcrumb.apiCall(`/rpc/${functionName}`, 'RPC');
|
||||
|
||||
try {
|
||||
const result = await (target as any).rpc(functionName, params);
|
||||
|
||||
if (result.error) {
|
||||
breadcrumb.apiCall(`/rpc/${functionName}`, 'RPC', 400);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
breadcrumb.apiCall(`/rpc/${functionName}`, 'RPC', 500);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Track storage operations
|
||||
if (prop === 'storage') {
|
||||
const storage = (target as any).storage;
|
||||
|
||||
return new Proxy(storage, {
|
||||
get(storageTarget, storageProp) {
|
||||
const storageValue = storageTarget[storageProp];
|
||||
|
||||
if (storageProp === 'from') {
|
||||
return (bucket: string) => {
|
||||
const bucketOps = storageValue.call(storageTarget, bucket);
|
||||
|
||||
return new Proxy(bucketOps, {
|
||||
get(bucketTarget, bucketProp) {
|
||||
const bucketValue = bucketTarget[bucketProp as string];
|
||||
|
||||
if (typeof bucketValue === 'function') {
|
||||
return async (...args: any[]) => {
|
||||
breadcrumb.apiCall(`/storage/${bucket}`, String(bucketProp).toUpperCase());
|
||||
|
||||
try {
|
||||
const result = await bucketValue.apply(bucketTarget, args);
|
||||
|
||||
if (result.error) {
|
||||
breadcrumb.apiCall(`/storage/${bucket}`, String(bucketProp).toUpperCase(), 400);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
breadcrumb.apiCall(`/storage/${bucket}`, String(bucketProp).toUpperCase(), 500);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return bucketValue;
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return storageValue;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
});
|
||||
export { supabase } from '@/integrations/supabase/client';
|
||||
|
||||
Reference in New Issue
Block a user