@allanfsouza/aether-sdk 2.4.9 → 2.4.11

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.
package/src/database.ts CHANGED
@@ -285,7 +285,6 @@ export class CollectionReference<T> {
285
285
  return () => { };
286
286
  }
287
287
 
288
- // URL correta de subscribe
289
288
  const url = `${this.wsUrl}/v1/db/subscribe/${this.collectionName}?token=${token}&projectId=${projectId}`;
290
289
 
291
290
  let ws: WebSocket | null = null;
@@ -296,7 +295,7 @@ export class CollectionReference<T> {
296
295
  if (!ws) return () => { };
297
296
 
298
297
  ws.onopen = () => {
299
- // Conectado
298
+ console.log(`[SDK] Realtime conectado: ${this.collectionName}`);
300
299
  };
301
300
 
302
301
  ws.onmessage = (event: any) => {
@@ -304,17 +303,32 @@ export class CollectionReference<T> {
304
303
  const raw = event.data?.toString() || event.toString();
305
304
  if (raw === "pong") return;
306
305
 
307
- const payload = JSON.parse(raw) as WebSocketMessage<T>;
308
- callback(payload.action, payload.data);
306
+ const payload = JSON.parse(raw);
307
+ console.log(`[SDK] Evento recebido:`, payload.action, payload.data?.id);
308
+
309
+ // [FIX] Mapeia 'insert' do Postgres para 'create' do SDK
310
+ let action: "create" | "update" | "delete" = payload.action;
311
+ if (payload.action === 'insert') {
312
+ action = 'create';
313
+ }
314
+
315
+ callback(action, payload.data);
309
316
  } catch (e) {
310
- // Erro silencioso de parse
317
+ console.error('[SDK] Erro ao parsear evento:', e);
311
318
  }
312
319
  };
313
320
 
321
+ ws.onerror = (err) => {
322
+ console.error('[SDK] WebSocket erro:', err);
323
+ };
324
+
325
+ ws.onclose = () => {
326
+ console.log(`[SDK] Realtime desconectado: ${this.collectionName}`);
327
+ };
328
+
314
329
  // Heartbeat
315
330
  const pingInterval = setInterval(() => {
316
- // [CORREÇÃO] Adicionada verificação explicita 'ws &&' para evitar erro 'possibly null'
317
- if (ws && ws.readyState === 1) { // 1 = OPEN
331
+ if (ws && ws.readyState === 1) {
318
332
  ws.send("ping");
319
333
  }
320
334
  }, 30000);
@@ -177,7 +177,7 @@ export function createHttpClient(
177
177
 
178
178
  // ===========================================================================
179
179
  // INTERCEPTOR DE REQUEST
180
- // Injeta token e projectId em todas as requisições
180
+ // Injeta token, projectId e API Key em todas as requisições
181
181
  // ===========================================================================
182
182
  http.interceptors.request.use((reqConfig) => {
183
183
  const token = client.getToken();
@@ -189,6 +189,11 @@ export function createHttpClient(
189
189
  reqConfig.headers["X-Project-ID"] = client.projectId;
190
190
  }
191
191
 
192
+ // [NOVO] Envia API Key para autenticação de operações de banco de dados
193
+ if (client.serviceApiKey) {
194
+ reqConfig.headers["X-API-Key"] = client.serviceApiKey;
195
+ }
196
+
192
197
  // Inicializa contador de retry
193
198
  if ((reqConfig as ExtendedAxiosRequestConfig)._retryCount === undefined) {
194
199
  (reqConfig as ExtendedAxiosRequestConfig)._retryCount = 0;
package/src/index.ts CHANGED
@@ -28,6 +28,12 @@ export type ClientConfig = {
28
28
  projectId?: string;
29
29
  apiKey?: string;
30
30
 
31
+ /**
32
+ * [NOVO] API Key de serviço para autenticação em operações de banco de dados.
33
+ * Necessário para apps client-side que usam tenant auth.
34
+ */
35
+ serviceApiKey?: string;
36
+
31
37
  /**
32
38
  * Habilita persistência automática de sessão no localStorage.
33
39
  * Padrão: true em browsers, false em Node.js/SSR.
@@ -59,6 +65,7 @@ export class PlataformaClient {
59
65
 
60
66
  public apiUrl: string;
61
67
  public projectId: string;
68
+ public serviceApiKey: string | null = null;
62
69
 
63
70
  public http: AxiosInstance;
64
71
 
@@ -76,6 +83,7 @@ export class PlataformaClient {
76
83
 
77
84
  this.apiUrl = url.replace(/\/+$/, "");
78
85
  this.projectId = project;
86
+ this.serviceApiKey = config.serviceApiKey || null;
79
87
 
80
88
  // Persistência habilitada por padrão apenas em browsers
81
89
  this._persistSession = config.persistSession ?? isBrowser();