@zydon/common 2.7.28 → 2.7.30

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.
@@ -0,0 +1,3 @@
1
+ var o=(n=>(n.LOG="LOG",n.WARN="WARN",n.ERROR="ERROR",n))(o||{}),e=(n=>(n.DEVELOPMENT="development",n.HOMOLOGATION="homologation",n.PRODUCTION="production",n))(e||{});
2
+
3
+ export { o as a, e as b };
@@ -1,9 +1,10 @@
1
1
  import { a as a$1 } from '../../chunk-Q3ADZHO5.js';
2
- import '../../chunk-IHAHXNLH.js';
3
2
  import { a } from '../../chunk-ZR3AIGIM.js';
3
+ import '../../chunk-IHAHXNLH.js';
4
+ import '../../chunk-6MUF35YU.js';
4
5
  import { createContext, useState, useRef, useMemo, useEffect, useCallback, useContext } from 'react';
5
6
  import { jsx } from 'react/jsx-runtime';
6
7
 
7
- var Y=createContext(void 0),ce=({children:d,originId:D,originType:I,bearerToken:S,vapidKey:C,serviceWorkerPath:V="/",serviceWorkerFile:G="service-worker.js",pollingInterval:H=1e3,apiAdapters:n,onMessageReceived:m,onError:r})=>{let[t,p]=useState(null),[k,P]=useState(0),[x,h]=useState(!1),[y,M]=useState(!1),[v,E]=useState(!1),[W,Z]=useState({subscriptions:[]}),[A,B]=useState(!1),[R]=useState(new Set),f=useRef(new Map),z=useMemo(()=>!t||A,[t,A]);useEffect(()=>{if(!y||!v||!t||!n.fetchMessages)return;let e=setInterval(async()=>{try{let c=(await n.fetchMessages(t))?.messages?.[0];if(c?.payload){let o=c.id;if(R.has(o))return;R.add(o),P(a=>a+1),m?.(c.payload);try{await n.updateMessageStatus(o,{status:"DELIVERED"});}catch(a$1){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao marcar mensagem como entregue:",data:a$1});}finally{let a=c.topic;f.current.get(a)?.stopPollingOnFirstMessage&&(f.current.delete(a),f.current.size>0||(M(!1),E(!1)));}}}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao buscar mensagens:",data:s});}},H);return ()=>clearInterval(e)},[y,v,t,H,n,m,R]);let N=useCallback(async(e,s)=>{try{let c=e?.toJSON().keys,o={origin_type:I,origin_id:D,...s&&e?{endpoint:e.endpoint,auth:c?.auth,p256dh:c?.p256dh}:{}},a=await n.registerClient(o);return p(a.id),h(!1),s||E(!0),a.id}catch(c){return a({type:"ERROR",environment:["development","homologation"],message:"Erro ao registrar cliente:",data:c}),h(!0),r?.(c),null}},[n,D,I,r]);useEffect(()=>{let e=!0,s=null;async function c(){if(!("serviceWorker"in navigator)||!("PushManager"in window)){a({type:"WARN",environment:["development","homologation"],message:"Push notifications n\xE3o suportadas, criando cliente ativo com polling"});try{let o=await N(null,!1);e&&o&&(p(o),E(!0));}catch(o){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao registrar cliente ativo:",data:o}),r?.(o);}return}try{let o=await Notification.requestPermission(),a$2=null,g=!1;if(o==="granted")try{let L=await navigator.serviceWorker.register(`${V}${G}`);if(!e||(await navigator.serviceWorker.ready,!e))return;a$2=await L.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:a$1(C)}),s=async U=>{let j=U.data?.payload,K=U.data?.id;if(j&&(P(T=>T+1),m?.(j)),K)try{await n.updateMessageStatus(K,{status:"DELIVERED"});}catch(T){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao atualizar status da mensagem:",data:T}),h(!0),r?.(T);}},navigator.serviceWorker.addEventListener("message",s),g=!0,a({type:"LOG",environment:["development","homologation"],message:"Permiss\xE3o concedida, criando cliente passivo (aguarda webpush)"});}catch(L){a({type:"WARN",environment:["development","homologation"],message:"Falha ao configurar push subscription, criando cliente ativo:",data:L}),g=!1;}else a({type:"WARN",environment:["development","homologation"],message:"Permiss\xE3o de notifica\xE7\xE3o negada, criando cliente ativo com polling"}),g=!1;let b=await N(a$2,g);e&&b&&(p(b),g||E(!0));}catch(o){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inicializar notifica\xE7\xF5es:",data:o}),r?.(o);try{let a=await N(null,!1);e&&a&&(p(a),E(!0));}catch(a$1){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao criar cliente ativo como fallback:",data:a$1});}}}return S&&c(),()=>{e=!1,s&&"serviceWorker"in navigator&&navigator.serviceWorker.removeEventListener("message",s);}},[S,N,n,C,V,G,m,r]);let u=useCallback(async()=>{if(t){B(!0);try{let e=await n.listSubscriptions(t);Z(e);}catch(e){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao listar inscri\xE7\xF5es:",data:e}),r?.(e);}finally{B(!1);}}},[t,n,r]);useEffect(()=>{t&&u();},[t]);let F=useCallback(async(e,s)=>{if(!t){a({type:"WARN",environment:["development","homologation"],message:"Cliente n\xE3o registrado"});return}try{s&&f.current.set(e,s),await n.subscribeTopic(t,e),await u(),!y&&!v&&(M(!0),E(!0));}catch(c){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inscrever no t\xF3pico:",data:c}),r?.(c);}},[t,n,u,r,y,v]),_=useCallback(async e=>{if(!t){a({type:"WARN",environment:["development","homologation"],message:"Cliente n\xE3o registrado"});return}try{f.current.delete(e),await n.unsubscribeTopic(t,e),await u();}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao desinscrever do t\xF3pico:",data:s}),r?.(s);}},[t,n,u,r]),$=useCallback(async e=>{if(!(!t||e.length===0))try{await n.subscribeTopicsBatch(t,e),await u(),M(!0);}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inscrever em t\xF3picos:",data:s}),r?.(s);}},[t,n,u,r]),q=useCallback(async e=>{if(!(!t||e.length===0))try{await n.unsubscribeTopicsBatch(t,e),await u();}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao desinscrever de t\xF3picos:",data:s}),r?.(s);}},[t,n,u,r]),J=useCallback(()=>{P(0);},[]),ee=useMemo(()=>({clientId:t,subscribedTopics:W,isLoading:z,newMessageCount:k,isRegisterError:x,subscribeToTopic:F,unsubscribeFromTopic:_,subscribeTopicsBatch:$,unsubscribeTopicsBatch:q,resetMessageCount:J}),[t,W,z,k,x,F,_,$,q,J]);return jsx(Y.Provider,{value:ee,children:d})};function ue(){let d=useContext(Y);if(!d)throw new Error("usePushNotifications precisa estar dentro de PushNotificationProvider");return d}
8
+ var Z=createContext(void 0),le=({children:d,originId:I,originType:C,bearerToken:S,vapidKey:V,serviceWorkerPath:G="/",serviceWorkerFile:H="service-worker.js",pollingInterval:k=1e3,apiAdapters:n,onMessageReceived:m,onError:r})=>{let[t,p]=useState(null),[x,h]=useState(0),[W,M]=useState(!1),[v,b]=useState(!1),[y,O]=useState(!1),[N,ee]=useState(!1),[A,te]=useState({subscriptions:[]}),[B,z]=useState(!1),[R]=useState(new Set),f=useRef(new Map),F=useMemo(()=>!t||B,[t,B]);useEffect(()=>{if(!v||!y||!t||!n.fetchMessages)return;let e=setInterval(async()=>{try{let c=(await n.fetchMessages(t))?.messages?.[0];if(c?.payload){let o=c.id;if(R.has(o))return;R.add(o),h(a=>a+1),m?.(c.payload);try{await n.updateMessageStatus(o,{status:"DELIVERED"});}catch(a$1){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao marcar mensagem como entregue:",data:a$1});}finally{let a=c.topic;f.current.get(a)?.stopPollingOnFirstMessage&&(f.current.delete(a),f.current.size>0||(b(!1),O(!1)));}}}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao buscar mensagens:",data:s});}},k);return ()=>clearInterval(e)},[v,y,t,k,n,m,R]);let P=useCallback(async(e,s)=>{try{let c=e?.toJSON().keys,o={origin_type:C,origin_id:I,...s&&e?{endpoint:e.endpoint,auth:c?.auth,p256dh:c?.p256dh}:{}},a=await n.registerClient(o);return p(a.id),M(!1),ee(s),s||O(!0),a.id}catch(c){return a({type:"ERROR",environment:["development","homologation"],message:"Erro ao registrar cliente:",data:c}),M(!0),r?.(c),null}},[n,I,C,r]);useEffect(()=>{let e=!0,s=null;async function c(){if(!("serviceWorker"in navigator)||!("PushManager"in window)){a({type:"WARN",environment:["development","homologation"],message:"Push notifications n\xE3o suportadas, criando cliente ativo com polling"});try{let o=await P(null,!1);e&&o&&(p(o),O(!0));}catch(o){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao registrar cliente ativo:",data:o}),r?.(o);}return}try{let o=await Notification.requestPermission(),a$2=null,g=!1;if(o==="granted")try{let w=await navigator.serviceWorker.register(`${G}${H}`);if(!e||(await navigator.serviceWorker.ready,!e))return;a$2=await w.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:a$1(V)}),s=async j=>{let K=j.data?.payload,Q=j.data?.id;if(K&&(h(T=>T+1),m?.(K)),Q)try{await n.updateMessageStatus(Q,{status:"DELIVERED"});}catch(T){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao atualizar status da mensagem:",data:T}),M(!0),r?.(T);}},navigator.serviceWorker.addEventListener("message",s),g=!0,a({type:"LOG",environment:["development","homologation"],message:"Permiss\xE3o concedida, criando cliente passivo (aguarda webpush)"});}catch(w){a({type:"WARN",environment:["development","homologation"],message:"Falha ao configurar push subscription, criando cliente ativo:",data:w}),g=!1;}else a({type:"WARN",environment:["development","homologation"],message:"Permiss\xE3o de notifica\xE7\xE3o negada, criando cliente ativo com polling"}),g=!1;let L=await P(a$2,g);e&&L&&(p(L),g||O(!0));}catch(o){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inicializar notifica\xE7\xF5es:",data:o}),r?.(o);try{let a=await P(null,!1);e&&a&&(p(a),O(!0));}catch(a$1){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao criar cliente ativo como fallback:",data:a$1});}}}return S&&c(),()=>{e=!1,s&&"serviceWorker"in navigator&&navigator.serviceWorker.removeEventListener("message",s);}},[S,P,n,V,G,H,m,r]);let u=useCallback(async()=>{if(t){z(!0);try{let e=await n.listSubscriptions(t);te(e);}catch(e){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao listar inscri\xE7\xF5es:",data:e}),r?.(e);}finally{z(!1);}}},[t,n,r]);useEffect(()=>{t&&u();},[t]);let _=useCallback(async(e,s)=>{if(!t){a({type:"WARN",environment:["development","homologation"],message:"Cliente n\xE3o registrado"});return}try{s&&f.current.set(e,s),await n.subscribeTopic(t,e),await u(),!N&&(!v||!y)&&(b(!0),O(!0));}catch(c){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inscrever no t\xF3pico:",data:c}),r?.(c);}},[t,n,u,r,v,y,N]),$=useCallback(async e=>{if(!t){a({type:"WARN",environment:["development","homologation"],message:"Cliente n\xE3o registrado"});return}try{f.current.delete(e),await n.unsubscribeTopic(t,e),await u();}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao desinscrever do t\xF3pico:",data:s}),r?.(s);}},[t,n,u,r]),q=useCallback(async e=>{if(!(!t||e.length===0))try{await n.subscribeTopicsBatch(t,e),await u(),N||(b(!0),O(!0));}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao inscrever em t\xF3picos:",data:s}),r?.(s);}},[t,n,u,r,N]),J=useCallback(async e=>{if(!(!t||e.length===0))try{await n.unsubscribeTopicsBatch(t,e),await u();}catch(s){a({type:"ERROR",environment:["development","homologation"],message:"Erro ao desinscrever de t\xF3picos:",data:s}),r?.(s);}},[t,n,u,r]),U=useCallback(()=>{h(0);},[]),se=useMemo(()=>({clientId:t,subscribedTopics:A,isLoading:F,newMessageCount:x,isRegisterError:W,subscribeToTopic:_,unsubscribeFromTopic:$,subscribeTopicsBatch:q,unsubscribeTopicsBatch:J,resetMessageCount:U}),[t,A,F,x,W,_,$,q,J,U]);return jsx(Z.Provider,{value:se,children:d})};function Oe(){let d=useContext(Z);if(!d)throw new Error("usePushNotifications precisa estar dentro de PushNotificationProvider");return d}
8
9
 
9
- export { ce as PushNotificationProvider, ue as usePushNotifications };
10
+ export { le as PushNotificationProvider, Oe as usePushNotifications };
@@ -0,0 +1,19 @@
1
+ declare enum ConsoleType {
2
+ LOG = "LOG",
3
+ WARN = "WARN",
4
+ ERROR = "ERROR"
5
+ }
6
+ declare enum Environment {
7
+ DEVELOPMENT = "development",
8
+ HOMOLOGATION = "homologation",
9
+ PRODUCTION = "production"
10
+ }
11
+ interface ConsoleLogProps {
12
+ type: ConsoleType;
13
+ environment: Environment | Environment[];
14
+ message: string;
15
+ data?: unknown;
16
+ args?: unknown[];
17
+ }
18
+
19
+ export { ConsoleLogProps, ConsoleType, Environment };
@@ -0,0 +1 @@
1
+ export { a as ConsoleType, b as Environment } from '../chunk-6MUF35YU.js';
@@ -1,20 +1,4 @@
1
- declare enum ConsoleType {
2
- LOG = "LOG",
3
- WARN = "WARN",
4
- ERROR = "ERROR"
5
- }
6
- declare enum Environment {
7
- DEVELOPMENT = "development",
8
- HOMOLOGATION = "homologation",
9
- PRODUCTION = "production"
10
- }
11
- interface ConsoleLogProps {
12
- type: ConsoleType;
13
- environment: Environment | Environment[];
14
- message: string;
15
- data?: unknown;
16
- args?: unknown[];
17
- }
1
+ import { ConsoleLogProps } from '../types/consoleLog.js';
18
2
 
19
3
  declare const consoleLog: ({ type, environment, message, data, args, }: ConsoleLogProps) => void;
20
4
 
@@ -1 +1,2 @@
1
1
  export { a as consoleLog } from '../chunk-ZR3AIGIM.js';
2
+ import '../chunk-6MUF35YU.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zydon/common",
3
- "version": "2.7.28",
3
+ "version": "2.7.30",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",