@tabl.io/auth 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +51 -0
  2. package/dist/index.js +2434 -1033
  3. package/package.json +4 -2
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { JSX } from 'react/jsx-runtime';
2
2
  import { PersistOptions } from 'zustand/middleware';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { QueryClientConfig } from '@tanstack/react-query';
3
5
  import { ReactNode } from 'react';
6
+ import { RealtimePostgresChangesPayload } from '@supabase/supabase-js';
4
7
  import { StoreApi } from 'zustand';
5
8
  import { SupabaseClient } from '@supabase/supabase-js';
6
9
  import { UseBoundStore } from 'zustand';
@@ -21,6 +24,28 @@ export declare interface AuthState {
21
24
  signOut: (supabase: SupabaseClient) => Promise<void>;
22
25
  }
23
26
 
27
+ /**
28
+ * Cria um QueryClient com tratamento centralizado de token JWT expirado.
29
+ *
30
+ * Comportamento ao detectar erro de token:
31
+ * 1. Tenta renovar a sessão via `supabase.auth.refreshSession()` (uma única
32
+ * tentativa — requisições simultâneas são deduplicadas).
33
+ * 2. Sucesso → invalida as queries afetadas para refetch transparente.
34
+ * 3. Falha → chama `supabase.auth.signOut()`. O `onAuthStateChange` existente
35
+ * no `useAuthStore.init()` detecta o evento e reseta o estado, fazendo o
36
+ * App re-renderizar para a LoginPage sem nenhum código extra.
37
+ *
38
+ * @example
39
+ * // main.tsx
40
+ * import { supabase } from './api/supabase'
41
+ * import { createAuthAwareQueryClient } from '@tabl.io/auth'
42
+ *
43
+ * const queryClient = createAuthAwareQueryClient(supabase, {
44
+ * defaultOptions: { queries: { staleTime: 0, gcTime: 0 } },
45
+ * })
46
+ */
47
+ export declare function createAuthAwareQueryClient(supabase: SupabaseClient, options?: Omit<QueryClientConfig, 'queryCache'>): QueryClient;
48
+
24
49
  /**
25
50
  * Cria um cliente Supabase com as configurações padrão dos apps Tabl.
26
51
  * Cada app instancia o seu próprio cliente via esta factory.
@@ -101,6 +126,32 @@ export declare interface LoginPageProps {
101
126
  logo?: React.ReactNode;
102
127
  }
103
128
 
129
+ export declare type RealtimeEventType = 'INSERT' | 'UPDATE' | 'DELETE' | '*';
130
+
131
+ export declare type RealtimeListener<T extends Record<string, unknown> = Record<string, unknown>> = (payload: RealtimePostgresChangesPayload<T>) => void;
132
+
133
+ declare class RealtimeRegistry {
134
+ private readonly tables;
135
+ private supabase;
136
+ private authWatchActive;
137
+ /**
138
+ * Registra um listener para eventos de uma tabela.
139
+ * Canal criado no primeiro registro, destruído no último unsubscribe.
140
+ *
141
+ * IMPORTANTE: a tabela precisa estar na publication do Supabase Realtime
142
+ * (`ALTER PUBLICATION supabase_realtime ADD TABLE <tabela>`).
143
+ * Subscrever uma tabela fora da publication gera CHANNEL_ERROR em loop.
144
+ */
145
+ subscribe(supabase: SupabaseClient, table: string, event: RealtimeEventType, listener: RealtimeListener): () => void;
146
+ private watchAuth;
147
+ private reconnectAll;
148
+ private createChannel;
149
+ private dispatch;
150
+ private scheduleReconnect;
151
+ }
152
+
153
+ export declare const realtimeRegistry: RealtimeRegistry;
154
+
104
155
  /**
105
156
  * Wrapper declarativo que protege conteúdo por role.
106
157
  * Não redireciona — use `useRequireRole` para redirecionamento de rota.