@notifizz/vanilla 1.1.0 → 1.1.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.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # @notifizz/vanilla
2
+
3
+ Embed the Notifizz notification center and control it from vanilla JavaScript or TypeScript (no framework). Use `createNotifizz` to get an API that lets you mount the widget, subscribe to state, attach a custom bell button, and open/close the panel.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @notifizz/vanilla
9
+ # or
10
+ yarn add @notifizz/vanilla
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```javascript
16
+ import { createNotifizz } from '@notifizz/vanilla';
17
+
18
+ const nz = createNotifizz({
19
+ apiKey: 'YOUR_FRONT_API_KEY',
20
+ token: 'USER_FIREBASE_TOKEN_OR_BACKEND_TOKEN',
21
+ authType: 'backendToken',
22
+ userEmail: 'user@example.com',
23
+ userId: '12345',
24
+ position: 'top-right',
25
+ });
26
+
27
+ // Mount the notification center (creates a div or uses the one you pass)
28
+ nz.mount();
29
+
30
+ // Optional: use your own bell button
31
+ const myButton = document.getElementById('my-bell');
32
+ nz.setBellElement(myButton);
33
+
34
+ // React to state changes (e.g. update a custom badge)
35
+ nz.onBellUpdate((ctx) => {
36
+ console.log('Unread:', ctx.unreadCount);
37
+ myButton.setAttribute('data-unread', String(ctx.unreadCount));
38
+ });
39
+
40
+ // Programmatic control
41
+ nz.open();
42
+ nz.close();
43
+ nz.toggle();
44
+ ```
45
+
46
+ Use `authType: 'backendToken'` when using a backend-generated token; then provide `userEmail` and `userId`. Use `authType: 'firebase'` for Firebase ID tokens.
47
+
48
+ ## Options
49
+
50
+ | Option | Type | Required | Description |
51
+ |--------|------|----------|-------------|
52
+ | `apiKey` | `string` | Yes | Your Notifizz front API key. |
53
+ | `token` | `string` | Yes | Auth token (Firebase ID token or backend token). |
54
+ | `authType` | `'firebase'` \| `'backendToken'` | Yes | How the user is authenticated. |
55
+ | `position` | `'top-right'` \| `'top-left'` \| `'bottom-right'` \| `'bottom-left'` | No | Bell position. |
56
+ | `notificationCenterStyles` | `{ marginTop?: string }` | No | Styles for the notification center. |
57
+ | `bellStyles` | `{ marginRight?: string; marginLeft?: string }` | No | Styles for the bell. |
58
+ | `userEmail` | `string` | If `authType === 'backendToken'` | User email. |
59
+ | `userId` | `string` | If `authType === 'backendToken'` | User ID. |
60
+ | `serverUrl` | `string` | No | Override widget server URL. |
61
+ | `apiUrl` | `string` | No | Override API base URL. |
62
+ | `widgetPath` | `string` | No | Override widget script path. |
63
+ | `mountId` | `string` | No | ID of the mount element (default: `"notifizz-notifications"`). |
64
+
65
+ ## Returned API
66
+
67
+ | Method | Description |
68
+ |--------|-------------|
69
+ | `mount(element?)` | Mount the notification center. Pass an `HTMLElement` to use it, or omit to create a div with `mountId`. Returns the mount element. |
70
+ | `getState()` | Current state: `{ isReady, isOpen, unreadCount, lastUpdated }`. |
71
+ | `onReady(cb)` | Register a callback when the widget is ready. Returns an unsubscribe function. |
72
+ | `onStateChange(cb)` | Register a callback when state changes. Returns an unsubscribe function. |
73
+ | `onBellUpdate(cb)` | Register a callback with bell context (`unreadCount`, `toggle`, `open`, `close`, etc.); called on state changes. Returns an unsubscribe function. |
74
+ | `setBellElement(el)` | Attach a custom bell element; clicks will call `toggle`. Pass `null` to detach. |
75
+ | `toggle()` | Toggle the notification panel. |
76
+ | `open()` | Open the notification panel. |
77
+ | `close()` | Close the notification panel. |
78
+ | `destroy()` | Remove the mount element and all event listeners. |
79
+
80
+ When you are done (e.g. on route change or teardown), call `nz.destroy()` to clean up.
package/dist/index.cjs CHANGED
@@ -1,23 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function L(e,d){const n=document.head;if(document.querySelector('script[data-widget="notifizz"]')){d();return}const o=document.createElement("script");window.notifizzCallback=d,o.type="module",o.dataset.widget="notifizz",o.textContent=`(function (n, o, t, i, f) {
2
- (n._notifizz = n._notifizz || {}),
3
- (n._notifizz._queue = n._notifizz._queue || []),
4
- (n.notifizz =
5
- n.notifizz ||
6
- function () {
7
- let args = [];
8
- for (i = 0; i < arguments.length; i++) args.push(arguments[i]);
9
- n._notifizz._queue.push(args);
10
- }),
11
- (n._notifizz._notifizzSettings = {
12
- apiKey: "${e.apiKey}",
13
- loaderVersion: "1",
14
- server: t,
15
- apiUrl: "${e.apiUrl}"
16
- }),
17
- (f = o.getElementsByTagName("head")[0]);
18
- let r = o.createElement("script");
19
- r.type = 'module';
20
- r.onload = n.notifizzCallback;
21
- (r.async = !0), (r.src = t + i), f.appendChild(r);
22
- })(window, document, "${e.serverUrl}", "${e.widgetPath}")`,n.appendChild(o)}function b(e){if(e.authType==="firebase"){window.notifizz("authenticateWithFirebase",e.token,{},()=>{});return}if(e.authType==="backendToken"){if(!e.userEmail||!e.userId)throw new Error("Missing required options: userEmail and userId are required for backendToken");window.notifizz("authenticateWithBackendToken",e.token,{user:{email:e.userEmail,id:e.userId}},()=>{})}}function k(e){["top-right","top-left","bottom-right","bottom-left"].includes(e)&&window.notifizz("setPosition",e)}function R(e){window.notifizz("setNotificationCenterStyles",e)}function I(e){window.notifizz("setBellStyles",e)}const B="https://widget.notifizz.com",O="https://eu.api.notifizz.com/v1",x="/v1/widget.js",q="notifizz-notifications";function A(e){if(!e.apiKey||!e.authType)throw new Error("Missing required options: apiKey and authType are required");const d=e.mountId??q;let n={isReady:!1,isOpen:!1,unreadCount:0,lastUpdated:0};const l=[],o=[],u=[];let a=null,s=null,w=!1,m=!1,c=!1;const z=(t,...i)=>{typeof window<"u"&&window.notifizz&&window.notifizz(t,...i)},p=()=>z("toggle"),g=()=>z("open"),E=()=>z("close"),y=()=>{p()},C=()=>({unreadCount:n.unreadCount,isOpen:n.isOpen,isReady:n.isReady,toggle:p,open:g,close:E}),v=()=>{const t=C();u.forEach(i=>i(t)),a&&a.setAttribute("data-unread",String(n.unreadCount))},U=()=>{b({apiKey:e.apiKey,token:e.token,authType:e.authType,userEmail:e.userEmail,userId:e.userId}),e.position&&k(e.position),e.notificationCenterStyles&&R(e.notificationCenterStyles),e.bellStyles&&I(e.bellStyles)},h=()=>{m||(m=!0,U(),n={...n,isReady:!0,lastUpdated:Date.now()},l.forEach(t=>t()),v())},S=t=>{const r=t.detail;r&&typeof r=="object"&&(n={...n,isReady:r.isReady??n.isReady,isOpen:r.isOpen??n.isOpen,unreadCount:typeof r.unreadCount=="number"?r.unreadCount:n.unreadCount,lastUpdated:r.lastUpdated??n.lastUpdated,hasError:r.hasError,errorCode:r.errorCode},o.forEach(_=>_(n)),v())},T=()=>{if(w||c)return;w=!0,window.addEventListener("notifizz:ready",h),window.addEventListener("notifizz:state",S);const t=e.serverUrl??B,i=e.apiUrl??O,r=e.widgetPath??x;L({apiKey:e.apiKey,serverUrl:t,apiUrl:i,widgetPath:r},()=>{typeof window.notifizz=="function"&&window.notifizz("getState",f=>{f&&typeof f=="object"&&(n={...n,...f,lastUpdated:f.lastUpdated??n.lastUpdated}),f?.isReady&&h()}),document.getElementById(d)&&U()})};return{mount(t){let i;if(t)i=t,i.id||(i.id=d);else{const r=document.getElementById(d);if(r)return s=r,T(),r;i=document.createElement("div"),i.id=d,document.body.appendChild(i)}return s=i,T(),i},getState(){return{...n}},onReady(t){return l.push(t),n.isReady&&t(),()=>{const i=l.indexOf(t);i!==-1&&l.splice(i,1)}},onStateChange(t){return o.push(t),()=>{const i=o.indexOf(t);i!==-1&&o.splice(i,1)}},onBellUpdate(t){return u.push(t),t(C()),()=>{const i=u.indexOf(t);i!==-1&&u.splice(i,1)}},open:g,close:E,toggle:p,setBellElement(t){a&&a.removeEventListener("click",y),a=t,a&&(a.addEventListener("click",y),a.setAttribute("data-unread",String(n.unreadCount)))},destroy(){c||(c=!0,window.removeEventListener("notifizz:ready",h),window.removeEventListener("notifizz:state",S),a&&(a.removeEventListener("click",y),a=null),l.length=0,o.length=0,u.length=0,s&&s.parentNode&&(s.parentNode.removeChild(s),s=null))}}}exports.createNotifizz=A;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function I(e,l){if(document.querySelector('script[data-widget="notifizz"]')){l();return}window._notifizz=window._notifizz||{},window._notifizz._queue=window._notifizz._queue||[],window.notifizz=window.notifizz||function(){const u=[];for(let s=0;s<arguments.length;s++)u.push(arguments[s]);window._notifizz._queue.push(u)},window._notifizz._notifizzSettings={apiKey:e.apiKey,loaderVersion:"1",server:e.serverUrl,apiUrl:e.apiUrl};const o=document.createElement("script");o.type="module",o.async=!0,o.dataset.widget="notifizz",o.src=e.serverUrl+e.widgetPath,o.onload=l,document.head.appendChild(o)}function b(e){if(e.authType==="none"){if(!e.userEmail||!e.userId)throw new Error('userEmail and userId are required for authType "none"');window.notifizz("authenticateWithNone",{user:{email:e.userEmail,id:e.userId}});return}if(e.authType==="firebase"){window.notifizz("authenticateWithFirebase",e.token,{},()=>{});return}if(e.authType==="backendToken"){if(!e.userEmail||!e.userId)throw new Error("Missing required options: userEmail and userId are required for backendToken");window.notifizz("authenticateWithBackendToken",e.token,{user:{email:e.userEmail,id:e.userId}},()=>{})}}function R(e){["top-right","top-left","bottom-right","bottom-left"].includes(e)&&window.notifizz("setPosition",e)}function B(e){window.notifizz("setNotificationCenterStyles",e)}function k(e){window.notifizz("setBellStyles",e)}const O="https://widget.notifizz.com",D="https://eu.api.notifizz.com/v1",q="/v1/widget.js",x="notifizz-notifications",A=".notifizz-custom-bell #notifizz-isolated-css > div > a { display: none !important; }";let T=!1;function K(){if(T||typeof document>"u")return;const e=document.createElement("style");e.textContent=A,document.head.appendChild(e),T=!0}function N(e){if(!e.apiKey||!e.authType)throw new Error("Missing required options: apiKey and authType are required");const l=e.mountId??x;let n={isReady:!1,isOpen:!1,unreadCount:0,lastUpdated:0};const o=[],u=[],s=[];let a=null,d=null,m=!1,h=!1,c=!1;const z=(t,...i)=>{typeof window<"u"&&window.notifizz&&window.notifizz(t,...i)},y=()=>z("toggle"),E=()=>z("open"),g=()=>z("close"),w=()=>{y()},C=()=>({unreadCount:n.unreadCount,isOpen:n.isOpen,isReady:n.isReady,toggle:y,open:E,close:g}),v=()=>{const t=C();s.forEach(i=>i(t)),a&&a.setAttribute("data-unread",String(n.unreadCount))},U=()=>{b({apiKey:e.apiKey,token:e.token,authType:e.authType,userEmail:e.userEmail,userId:e.userId}),e.position&&R(e.position),e.notificationCenterStyles&&B(e.notificationCenterStyles),e.bellStyles&&k(e.bellStyles)},p=()=>{h||(h=!0,U(),n={...n,isReady:!0,lastUpdated:Date.now()},o.forEach(t=>t()),v())},S=t=>{const r=t.detail;r&&typeof r=="object"&&(n={...n,isReady:r.isReady??n.isReady,isOpen:r.isOpen??n.isOpen,unreadCount:typeof r.unreadCount=="number"?r.unreadCount:n.unreadCount,lastUpdated:r.lastUpdated??n.lastUpdated,hasError:r.hasError,errorCode:r.errorCode},u.forEach(_=>_(n)),v())},L=()=>{if(m||c)return;m=!0,window.addEventListener("notifizz:ready",p),window.addEventListener("notifizz:state",S);const t=e.serverUrl??O,i=e.apiUrl??D,r=e.widgetPath??q;I({apiKey:e.apiKey,serverUrl:t,apiUrl:i,widgetPath:r},()=>{typeof window.notifizz=="function"&&window.notifizz("getState",f=>{f&&typeof f=="object"&&(n={...n,...f,lastUpdated:f.lastUpdated??n.lastUpdated}),f?.isReady&&p()}),document.getElementById(l)&&U()})};return{mount(t){let i;if(t)i=t,i.id||(i.id=l);else{const r=document.getElementById(l);if(r)return d=r,L(),r;i=document.createElement("div"),i.id=l,document.body.appendChild(i)}return d=i,L(),i},getState(){return{...n}},onReady(t){return o.push(t),n.isReady&&t(),()=>{const i=o.indexOf(t);i!==-1&&o.splice(i,1)}},onStateChange(t){return u.push(t),()=>{const i=u.indexOf(t);i!==-1&&u.splice(i,1)}},onBellUpdate(t){return s.push(t),t(C()),()=>{const i=s.indexOf(t);i!==-1&&s.splice(i,1)}},open:E,close:g,toggle:y,setBellElement(t){a&&a.removeEventListener("click",w),a=t,a?(a.addEventListener("click",w),a.setAttribute("data-unread",String(n.unreadCount)),d&&(d.classList.add("notifizz-custom-bell"),K())):d&&d.classList.remove("notifizz-custom-bell")},destroy(){c||(c=!0,window.removeEventListener("notifizz:ready",p),window.removeEventListener("notifizz:state",S),a&&(a.removeEventListener("click",w),a=null),o.length=0,u.length=0,s.length=0,d&&d.parentNode&&(d.parentNode.removeChild(d),d=null))}}}exports.createNotifizz=N;
23
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../notifizz-shared/src/scriptLoader.ts","../src/createNotifizz.ts"],"sourcesContent":["export interface NotifizzConfig {\n apiKey: string;\n apiUrl: string;\n serverUrl: string;\n widgetPath: string;\n}\n\nexport function importScriptInBrowser(\n config: NotifizzConfig,\n callback: () => void\n): void {\n const head = document.head;\n const existingScript = document.querySelector(\n 'script[data-widget=\"notifizz\"]'\n );\n if (existingScript) {\n callback();\n return;\n }\n const script = document.createElement('script');\n (window as Window & { notifizzCallback?: () => void }).notifizzCallback =\n callback;\n script.type = 'module';\n script.dataset['widget'] = 'notifizz';\n script.textContent = `(function (n, o, t, i, f) {\n (n._notifizz = n._notifizz || {}),\n (n._notifizz._queue = n._notifizz._queue || []),\n (n.notifizz =\n n.notifizz ||\n function () {\n let args = [];\n for (i = 0; i < arguments.length; i++) args.push(arguments[i]);\n n._notifizz._queue.push(args);\n }),\n (n._notifizz._notifizzSettings = {\n apiKey: \"${config.apiKey}\",\n loaderVersion: \"1\",\n server: t,\n apiUrl: \"${config.apiUrl}\"\n }),\n (f = o.getElementsByTagName(\"head\")[0]);\n let r = o.createElement(\"script\");\n r.type = 'module';\n r.onload = n.notifizzCallback;\n (r.async = !0), (r.src = t + i), f.appendChild(r);\n })(window, document, \"${config.serverUrl}\", \"${config.widgetPath}\")`;\n head.appendChild(script);\n}\n\nexport interface AuthOptions {\n apiKey: string;\n token: string;\n authType: 'firebase' | 'backendToken';\n userEmail?: string;\n userId?: string;\n}\n\nexport function authenticate(options: AuthOptions): void {\n if (options.authType === 'firebase') {\n window.notifizz('authenticateWithFirebase', options.token, {}, () => {});\n return;\n }\n if (options.authType === 'backendToken') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'Missing required options: userEmail and userId are required for backendToken'\n );\n }\n window.notifizz(\n 'authenticateWithBackendToken',\n options.token,\n { user: { email: options.userEmail, id: options.userId } },\n () => {}\n );\n }\n}\n\nexport function applyPosition(\n position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'\n): void {\n const valid = ['top-right', 'top-left', 'bottom-right', 'bottom-left'];\n if (valid.includes(position)) {\n window.notifizz('setPosition', position);\n }\n}\n\nexport function applyNotificationCenterStyles(styles: {\n marginTop?: string;\n}): void {\n window.notifizz('setNotificationCenterStyles', styles);\n}\n\nexport function applyBellStyles(styles: {\n marginRight?: string;\n marginLeft?: string;\n}): void {\n window.notifizz('setBellStyles', styles);\n}\n","import type { NotifizzState, NotifizzBellContext } from '../../notifizz-shared/src/types';\nimport type { NotifizzOptions, NotifizzVanillaApi } from './types';\nimport {\n importScriptInBrowser,\n authenticate,\n applyPosition,\n applyNotificationCenterStyles,\n applyBellStyles,\n} from '../../notifizz-shared/src/scriptLoader';\n\nconst DEFAULT_SERVER_URL = 'https://widget.notifizz.com';\nconst DEFAULT_API_URL = 'https://eu.api.notifizz.com/v1';\nconst DEFAULT_WIDGET_PATH = '/v1/widget.js';\nconst DEFAULT_MOUNT_ID = 'notifizz-notifications';\n\nexport function createNotifizz(options: NotifizzOptions): NotifizzVanillaApi {\n if (!options.apiKey || !options.authType) {\n throw new Error(\n 'Missing required options: apiKey and authType are required'\n );\n }\n\n const mountId = options.mountId ?? DEFAULT_MOUNT_ID;\n let state: NotifizzState = {\n isReady: false,\n isOpen: false,\n unreadCount: 0,\n lastUpdated: 0,\n };\n const readyListeners: (() => void)[] = [];\n const stateChangeListeners: ((s: NotifizzState) => void)[] = [];\n const bellUpdateListeners: ((ctx: NotifizzBellContext) => void)[] = [];\n let bellElement: HTMLElement | null = null;\n let mountElement: HTMLElement | null = null;\n let initDone = false;\n let readyFired = false;\n let destroyed = false;\n\n const send = (command: string, ...args: unknown[]): void => {\n if (typeof window !== 'undefined' && window.notifizz) {\n window.notifizz(command, ...args);\n }\n };\n\n const toggle = (): void => send('toggle');\n const open = (): void => send('open');\n const close = (): void => send('close');\n\n const onBellClick = (): void => {\n toggle();\n };\n\n const buildBellContext = (): NotifizzBellContext => ({\n unreadCount: state.unreadCount,\n isOpen: state.isOpen,\n isReady: state.isReady,\n toggle,\n open,\n close,\n });\n\n const notifyBellListeners = (): void => {\n const ctx = buildBellContext();\n bellUpdateListeners.forEach((cb) => cb(ctx));\n if (bellElement) {\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n };\n\n const applyOptions = (): void => {\n authenticate({\n apiKey: options.apiKey,\n token: options.token,\n authType: options.authType,\n userEmail: options.userEmail,\n userId: options.userId,\n });\n if (options.position) applyPosition(options.position);\n if (options.notificationCenterStyles) {\n applyNotificationCenterStyles(options.notificationCenterStyles);\n }\n if (options.bellStyles) applyBellStyles(options.bellStyles);\n };\n\n const handleReady = (): void => {\n if (readyFired) return;\n readyFired = true;\n applyOptions();\n state = { ...state, isReady: true, lastUpdated: Date.now() };\n readyListeners.forEach((cb) => cb());\n notifyBellListeners();\n };\n\n const handleState = (e: Event): void => {\n const ev = e as CustomEvent<NotifizzState>;\n const detail = ev.detail;\n if (detail && typeof detail === 'object') {\n state = {\n ...state,\n isReady: detail.isReady ?? state.isReady,\n isOpen: detail.isOpen ?? state.isOpen,\n unreadCount:\n typeof detail.unreadCount === 'number'\n ? detail.unreadCount\n : state.unreadCount,\n lastUpdated: detail.lastUpdated ?? state.lastUpdated,\n hasError: detail.hasError,\n errorCode: detail.errorCode,\n };\n stateChangeListeners.forEach((cb) => cb(state));\n notifyBellListeners();\n }\n };\n\n const init = (): void => {\n if (initDone || destroyed) return;\n initDone = true;\n window.addEventListener('notifizz:ready', handleReady);\n window.addEventListener('notifizz:state', handleState);\n\n const serverUrl = options.serverUrl ?? DEFAULT_SERVER_URL;\n const apiUrl = options.apiUrl ?? DEFAULT_API_URL;\n const widgetPath = options.widgetPath ?? DEFAULT_WIDGET_PATH;\n\n importScriptInBrowser(\n { apiKey: options.apiKey, serverUrl, apiUrl, widgetPath },\n () => {\n if (typeof window.notifizz === 'function') {\n window.notifizz('getState', (current: NotifizzState) => {\n if (current && typeof current === 'object') {\n state = {\n ...state,\n ...current,\n lastUpdated: current.lastUpdated ?? state.lastUpdated,\n };\n }\n if (current?.isReady) handleReady();\n });\n }\n const el = document.getElementById(mountId);\n if (el) applyOptions();\n }\n );\n };\n\n return {\n mount(element?: HTMLElement): HTMLElement {\n let el: HTMLElement;\n if (element) {\n el = element;\n if (!el.id) el.id = mountId;\n } else {\n const existing = document.getElementById(mountId);\n if (existing) {\n mountElement = existing;\n init();\n return existing;\n }\n el = document.createElement('div');\n el.id = mountId;\n document.body.appendChild(el);\n }\n mountElement = el;\n init();\n return el;\n },\n\n getState(): NotifizzState {\n return { ...state };\n },\n\n onReady(cb: () => void): () => void {\n readyListeners.push(cb);\n if (state.isReady) cb();\n return () => {\n const i = readyListeners.indexOf(cb);\n if (i !== -1) readyListeners.splice(i, 1);\n };\n },\n\n onStateChange(cb: (s: NotifizzState) => void): () => void {\n stateChangeListeners.push(cb);\n return () => {\n const i = stateChangeListeners.indexOf(cb);\n if (i !== -1) stateChangeListeners.splice(i, 1);\n };\n },\n\n onBellUpdate(cb: (ctx: NotifizzBellContext) => void): () => void {\n bellUpdateListeners.push(cb);\n cb(buildBellContext());\n return () => {\n const i = bellUpdateListeners.indexOf(cb);\n if (i !== -1) bellUpdateListeners.splice(i, 1);\n };\n },\n\n open,\n close,\n toggle,\n\n setBellElement(el: HTMLElement | null): void {\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n }\n bellElement = el;\n if (bellElement) {\n bellElement.addEventListener('click', onBellClick);\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n },\n\n destroy(): void {\n if (destroyed) return;\n destroyed = true;\n window.removeEventListener('notifizz:ready', handleReady);\n window.removeEventListener('notifizz:state', handleState);\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n bellElement = null;\n }\n readyListeners.length = 0;\n stateChangeListeners.length = 0;\n bellUpdateListeners.length = 0;\n if (mountElement && mountElement.parentNode) {\n mountElement.parentNode.removeChild(mountElement);\n mountElement = null;\n }\n },\n };\n}\n"],"names":["importScriptInBrowser","config","callback","head","script","authenticate","options","applyPosition","position","applyNotificationCenterStyles","styles","applyBellStyles","DEFAULT_SERVER_URL","DEFAULT_API_URL","DEFAULT_WIDGET_PATH","DEFAULT_MOUNT_ID","createNotifizz","mountId","state","readyListeners","stateChangeListeners","bellUpdateListeners","bellElement","mountElement","initDone","readyFired","destroyed","send","command","args","toggle","open","close","onBellClick","buildBellContext","notifyBellListeners","ctx","cb","applyOptions","handleReady","handleState","e","detail","init","serverUrl","apiUrl","widgetPath","current","element","el","existing"],"mappings":"gFAOO,SAASA,EACdC,EACAC,EACM,CACN,MAAMC,EAAO,SAAS,KAItB,GAHuB,SAAS,cAC9B,gCAAA,EAEkB,CAClBD,EAAA,EACA,MACF,CACA,MAAME,EAAS,SAAS,cAAc,QAAQ,EAC7C,OAAsD,iBACrDF,EACFE,EAAO,KAAO,SACdA,EAAO,QAAQ,OAAY,WAC3BA,EAAO,YAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAWJH,EAAO,MAAM;AAAA;AAAA;AAAA,mBAGbA,EAAO,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAONA,EAAO,SAAS,OAAOA,EAAO,UAAU,KAChEE,EAAK,YAAYC,CAAM,CACzB,CAUO,SAASC,EAAaC,EAA4B,CACvD,GAAIA,EAAQ,WAAa,WAAY,CACnC,OAAO,SAAS,2BAA4BA,EAAQ,MAAO,CAAA,EAAI,IAAM,CAAC,CAAC,EACvE,MACF,CACA,GAAIA,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EACd,IAAM,CACX,MAAM,EAAIb,EAAoB,QAAQgB,CAAE,EACpC,IAAM,IAAIhB,EAAoB,OAAO,EAAG,CAAC,CAC/C,CACF,EAEA,KAAAU,EACA,MAAAC,EACA,OAAAF,EAEA,eAAemB,EAA8B,CACvC3B,GACFA,EAAY,oBAAoB,QAASW,CAAW,EAEtDX,EAAc2B,EACV3B,IACFA,EAAY,iBAAiB,QAASW,CAAW,EACjDX,EAAY,aAAa,cAAe,OAAOJ,EAAM,WAAW,CAAC,EAErE,EAEA,SAAgB,CACVQ,IACJA,EAAY,GACZ,OAAO,oBAAoB,iBAAkBa,CAAW,EACxD,OAAO,oBAAoB,iBAAkBC,CAAW,EACpDlB,IACFA,EAAY,oBAAoB,QAASW,CAAW,EACpDX,EAAc,MAEhBH,EAAe,OAAS,EACxBC,EAAqB,OAAS,EAC9BC,EAAoB,OAAS,EACzBE,GAAgBA,EAAa,aAC/BA,EAAa,WAAW,YAAYA,CAAY,EAChDA,EAAe,MAEnB,CAAA,CAEJ"}
1
+ {"version":3,"file":"index.cjs","sources":["../../notifizz-shared/src/scriptLoader.ts","../src/createNotifizz.ts"],"sourcesContent":["/// <reference path=\"./global.d.ts\" />\n\nexport interface NotifizzConfig {\n apiKey: string;\n apiUrl: string;\n serverUrl: string;\n widgetPath: string;\n}\n\nexport function importScriptInBrowser(\n config: NotifizzConfig,\n callback: () => void\n): void {\n const existingScript = document.querySelector(\n 'script[data-widget=\"notifizz\"]'\n );\n if (existingScript) {\n callback();\n return;\n }\n\n window._notifizz = window._notifizz || {};\n window._notifizz._queue = window._notifizz._queue || [];\n window.notifizz =\n window.notifizz ||\n function () {\n const args: unknown[] = [];\n for (let i = 0; i < arguments.length; i++) args.push(arguments[i]);\n window._notifizz!._queue!.push(args);\n };\n window._notifizz._notifizzSettings = {\n apiKey: config.apiKey,\n loaderVersion: '1',\n server: config.serverUrl,\n apiUrl: config.apiUrl,\n };\n\n const script = document.createElement('script');\n script.type = 'module';\n script.async = true;\n script.dataset.widget = 'notifizz';\n script.src = config.serverUrl + config.widgetPath;\n script.onload = callback;\n document.head.appendChild(script);\n}\n\nexport interface AuthOptions {\n apiKey: string;\n token?: string;\n authType: 'firebase' | 'backendToken' | 'none';\n userEmail?: string;\n userId?: string;\n}\n\nexport function authenticate(options: AuthOptions): void {\n if (options.authType === 'none') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'userEmail and userId are required for authType \"none\"'\n );\n }\n window.notifizz('authenticateWithNone', {\n user: { email: options.userEmail, id: options.userId },\n });\n return;\n }\n if (options.authType === 'firebase') {\n window.notifizz('authenticateWithFirebase', options.token, {}, () => {});\n return;\n }\n if (options.authType === 'backendToken') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'Missing required options: userEmail and userId are required for backendToken'\n );\n }\n window.notifizz(\n 'authenticateWithBackendToken',\n options.token,\n { user: { email: options.userEmail, id: options.userId } },\n () => {}\n );\n }\n}\n\nexport function applyPosition(\n position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'\n): void {\n const valid = ['top-right', 'top-left', 'bottom-right', 'bottom-left'];\n if (valid.includes(position)) {\n window.notifizz('setPosition', position);\n }\n}\n\nexport function applyNotificationCenterStyles(styles: {\n marginTop?: string;\n}): void {\n window.notifizz('setNotificationCenterStyles', styles);\n}\n\nexport function applyBellStyles(styles: {\n marginRight?: string;\n marginLeft?: string;\n}): void {\n window.notifizz('setBellStyles', styles);\n}\n","import type { NotifizzState, NotifizzBellContext } from '../../notifizz-shared/src/types';\nimport type { NotifizzOptions, NotifizzVanillaApi } from './types';\nimport {\n importScriptInBrowser,\n authenticate,\n applyPosition,\n applyNotificationCenterStyles,\n applyBellStyles,\n} from '../../notifizz-shared/src/scriptLoader';\n\nconst DEFAULT_SERVER_URL = 'https://widget.notifizz.com';\nconst DEFAULT_API_URL = 'https://eu.api.notifizz.com/v1';\nconst DEFAULT_WIDGET_PATH = '/v1/widget.js';\nconst DEFAULT_MOUNT_ID = 'notifizz-notifications';\nconst HIDE_WIDGET_BELL_CSS =\n '.notifizz-custom-bell #notifizz-isolated-css > div > a { display: none !important; }';\n\nlet customBellCssInjected = false;\nfunction injectCustomBellCss(): void {\n if (customBellCssInjected || typeof document === 'undefined') return;\n const style = document.createElement('style');\n style.textContent = HIDE_WIDGET_BELL_CSS;\n document.head.appendChild(style);\n customBellCssInjected = true;\n}\n\nexport function createNotifizz(options: NotifizzOptions): NotifizzVanillaApi {\n if (!options.apiKey || !options.authType) {\n throw new Error(\n 'Missing required options: apiKey and authType are required'\n );\n }\n\n const mountId = options.mountId ?? DEFAULT_MOUNT_ID;\n let state: NotifizzState = {\n isReady: false,\n isOpen: false,\n unreadCount: 0,\n lastUpdated: 0,\n };\n const readyListeners: (() => void)[] = [];\n const stateChangeListeners: ((s: NotifizzState) => void)[] = [];\n const bellUpdateListeners: ((ctx: NotifizzBellContext) => void)[] = [];\n let bellElement: HTMLElement | null = null;\n let mountElement: HTMLElement | null = null;\n let initDone = false;\n let readyFired = false;\n let destroyed = false;\n\n const send = (command: string, ...args: unknown[]): void => {\n if (typeof window !== 'undefined' && window.notifizz) {\n window.notifizz(command, ...args);\n }\n };\n\n const toggle = (): void => send('toggle');\n const open = (): void => send('open');\n const close = (): void => send('close');\n\n const onBellClick = (): void => {\n toggle();\n };\n\n const buildBellContext = (): NotifizzBellContext => ({\n unreadCount: state.unreadCount,\n isOpen: state.isOpen,\n isReady: state.isReady,\n toggle,\n open,\n close,\n });\n\n const notifyBellListeners = (): void => {\n const ctx = buildBellContext();\n bellUpdateListeners.forEach((cb) => cb(ctx));\n if (bellElement) {\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n };\n\n const applyOptions = (): void => {\n authenticate({\n apiKey: options.apiKey,\n token: options.token,\n authType: options.authType,\n userEmail: options.userEmail,\n userId: options.userId,\n });\n if (options.position) applyPosition(options.position);\n if (options.notificationCenterStyles) {\n applyNotificationCenterStyles(options.notificationCenterStyles);\n }\n if (options.bellStyles) applyBellStyles(options.bellStyles);\n };\n\n const handleReady = (): void => {\n if (readyFired) return;\n readyFired = true;\n applyOptions();\n state = { ...state, isReady: true, lastUpdated: Date.now() };\n readyListeners.forEach((cb) => cb());\n notifyBellListeners();\n };\n\n const handleState = (e: Event): void => {\n const ev = e as CustomEvent<NotifizzState>;\n const detail = ev.detail;\n if (detail && typeof detail === 'object') {\n state = {\n ...state,\n isReady: detail.isReady ?? state.isReady,\n isOpen: detail.isOpen ?? state.isOpen,\n unreadCount:\n typeof detail.unreadCount === 'number'\n ? detail.unreadCount\n : state.unreadCount,\n lastUpdated: detail.lastUpdated ?? state.lastUpdated,\n hasError: detail.hasError,\n errorCode: detail.errorCode,\n };\n stateChangeListeners.forEach((cb) => cb(state));\n notifyBellListeners();\n }\n };\n\n const init = (): void => {\n if (initDone || destroyed) return;\n initDone = true;\n window.addEventListener('notifizz:ready', handleReady);\n window.addEventListener('notifizz:state', handleState);\n\n const serverUrl = options.serverUrl ?? DEFAULT_SERVER_URL;\n const apiUrl = options.apiUrl ?? DEFAULT_API_URL;\n const widgetPath = options.widgetPath ?? DEFAULT_WIDGET_PATH;\n\n importScriptInBrowser(\n { apiKey: options.apiKey, serverUrl, apiUrl, widgetPath },\n () => {\n if (typeof window.notifizz === 'function') {\n window.notifizz('getState', (current: NotifizzState) => {\n if (current && typeof current === 'object') {\n state = {\n ...state,\n ...current,\n lastUpdated: current.lastUpdated ?? state.lastUpdated,\n };\n }\n if (current?.isReady) handleReady();\n });\n }\n const el = document.getElementById(mountId);\n if (el) applyOptions();\n }\n );\n };\n\n return {\n mount(element?: HTMLElement): HTMLElement {\n let el: HTMLElement;\n if (element) {\n el = element;\n if (!el.id) el.id = mountId;\n } else {\n const existing = document.getElementById(mountId);\n if (existing) {\n mountElement = existing;\n init();\n return existing;\n }\n el = document.createElement('div');\n el.id = mountId;\n document.body.appendChild(el);\n }\n mountElement = el;\n init();\n return el;\n },\n\n getState(): NotifizzState {\n return { ...state };\n },\n\n onReady(cb: () => void): () => void {\n readyListeners.push(cb);\n if (state.isReady) cb();\n return () => {\n const i = readyListeners.indexOf(cb);\n if (i !== -1) readyListeners.splice(i, 1);\n };\n },\n\n onStateChange(cb: (s: NotifizzState) => void): () => void {\n stateChangeListeners.push(cb);\n return () => {\n const i = stateChangeListeners.indexOf(cb);\n if (i !== -1) stateChangeListeners.splice(i, 1);\n };\n },\n\n onBellUpdate(cb: (ctx: NotifizzBellContext) => void): () => void {\n bellUpdateListeners.push(cb);\n cb(buildBellContext());\n return () => {\n const i = bellUpdateListeners.indexOf(cb);\n if (i !== -1) bellUpdateListeners.splice(i, 1);\n };\n },\n\n open,\n close,\n toggle,\n\n setBellElement(el: HTMLElement | null): void {\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n }\n bellElement = el;\n if (bellElement) {\n bellElement.addEventListener('click', onBellClick);\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n if (mountElement) {\n mountElement.classList.add('notifizz-custom-bell');\n injectCustomBellCss();\n }\n } else if (mountElement) {\n mountElement.classList.remove('notifizz-custom-bell');\n }\n },\n\n destroy(): void {\n if (destroyed) return;\n destroyed = true;\n window.removeEventListener('notifizz:ready', handleReady);\n window.removeEventListener('notifizz:state', handleState);\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n bellElement = null;\n }\n readyListeners.length = 0;\n stateChangeListeners.length = 0;\n bellUpdateListeners.length = 0;\n if (mountElement && mountElement.parentNode) {\n mountElement.parentNode.removeChild(mountElement);\n mountElement = null;\n }\n },\n };\n}\n"],"names":["importScriptInBrowser","config","callback","args","i","script","authenticate","options","applyPosition","position","applyNotificationCenterStyles","styles","applyBellStyles","DEFAULT_SERVER_URL","DEFAULT_API_URL","DEFAULT_WIDGET_PATH","DEFAULT_MOUNT_ID","HIDE_WIDGET_BELL_CSS","customBellCssInjected","injectCustomBellCss","style","createNotifizz","mountId","state","readyListeners","stateChangeListeners","bellUpdateListeners","bellElement","mountElement","initDone","readyFired","destroyed","send","command","toggle","open","close","onBellClick","buildBellContext","notifyBellListeners","ctx","cb","applyOptions","handleReady","handleState","e","detail","init","serverUrl","apiUrl","widgetPath","current","element","el","existing"],"mappings":"gFASO,SAASA,EACdC,EACAC,EACM,CAIN,GAHuB,SAAS,cAC9B,gCAAA,EAEkB,CAClBA,EAAA,EACA,MACF,CAEA,OAAO,UAAY,OAAO,WAAa,CAAA,EACvC,OAAO,UAAU,OAAS,OAAO,UAAU,QAAU,CAAA,EACrD,OAAO,SACL,OAAO,UACP,UAAY,CACV,MAAMC,EAAkB,CAAA,EACxB,QAASC,EAAI,EAAGA,EAAI,UAAU,OAAQA,IAAKD,EAAK,KAAK,UAAUC,CAAC,CAAC,EACjE,OAAO,UAAW,OAAQ,KAAKD,CAAI,CACrC,EACF,OAAO,UAAU,kBAAoB,CACnC,OAAQF,EAAO,OACf,cAAe,IACf,OAAQA,EAAO,UACf,OAAQA,EAAO,MAAA,EAGjB,MAAMI,EAAS,SAAS,cAAc,QAAQ,EAC9CA,EAAO,KAAO,SACdA,EAAO,MAAQ,GACfA,EAAO,QAAQ,OAAS,WACxBA,EAAO,IAAMJ,EAAO,UAAYA,EAAO,WACvCI,EAAO,OAASH,EAChB,SAAS,KAAK,YAAYG,CAAM,CAClC,CAUO,SAASC,EAAaC,EAA4B,CACvD,GAAIA,EAAQ,WAAa,OAAQ,CAC/B,GAAI,CAACA,EAAQ,WAAa,CAACA,EAAQ,OACjC,MAAM,IAAI,MACR,uDAAA,EAGJ,OAAO,SAAS,uBAAwB,CACtC,KAAM,CAAE,MAAOA,EAAQ,UAAW,GAAIA,EAAQ,MAAA,CAAO,CACtD,EACD,MACF,CACA,GAAIA,EAAQ,WAAa,WAAY,CACnC,OAAO,SAAS,2BAA4BA,EAAQ,MAAO,CAAA,EAAI,IAAM,CAAC,CAAC,EACvE,MACF,CACA,GAAIA,EAAQ,WAAa,eAAgB,CACvC,GAAI,CAACA,EAAQ,WAAa,CAACA,EAAQ,OACjC,MAAM,IAAI,MACR,8EAAA,EAGJ,OAAO,S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O,EAAG,CAAC,CAChD,CACF,EAEA,aAAagB,EAAoD,CAC/D,OAAAf,EAAoB,KAAKe,CAAE,EAC3BA,EAAGH,GAAkB,EACd,IAAM,CACX,MAAM,EAAIZ,EAAoB,QAAQe,CAAE,EACpC,IAAM,IAAIf,EAAoB,OAAO,EAAG,CAAC,CAC/C,CACF,EAEA,KAAAS,EACA,MAAAC,EACA,OAAAF,EAEA,eAAemB,EAA8B,CACvC1B,GACFA,EAAY,oBAAoB,QAASU,CAAW,EAEtDV,EAAc0B,EACV1B,GACFA,EAAY,iBAAiB,QAASU,CAAW,EACjDV,EAAY,aAAa,cAAe,OAAOJ,EAAM,WAAW,CAAC,EAC7DK,IACFA,EAAa,UAAU,IAAI,sBAAsB,EACjDT,EAAA,IAEOS,GACTA,EAAa,UAAU,OAAO,sBAAsB,CAExD,EAEA,SAAgB,CACVG,IACJA,EAAY,GACZ,OAAO,oBAAoB,iBAAkBY,CAAW,EACxD,OAAO,oBAAoB,iBAAkBC,CAAW,EACpDjB,IACFA,EAAY,oBAAoB,QAASU,CAAW,EACpDV,EAAc,MAEhBH,EAAe,OAAS,EACxBC,EAAqB,OAAS,EAC9BC,EAAoB,OAAS,EACzBE,GAAgBA,EAAa,aAC/BA,EAAa,WAAW,YAAYA,CAAY,EAChDA,EAAe,MAEnB,CAAA,CAEJ"}
package/dist/index.d.ts CHANGED
@@ -8,8 +8,8 @@ export { NotifizzBellContext }
8
8
 
9
9
  export declare interface NotifizzOptions {
10
10
  apiKey: string;
11
- token: string;
12
- authType: 'firebase' | 'backendToken';
11
+ token?: string;
12
+ authType: 'firebase' | 'backendToken' | 'none';
13
13
  userEmail?: string;
14
14
  userId?: string;
15
15
  position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
package/dist/index.js CHANGED
@@ -1,36 +1,34 @@
1
- function T(e, d) {
2
- const n = document.head;
1
+ function T(e, l) {
3
2
  if (document.querySelector(
4
3
  'script[data-widget="notifizz"]'
5
4
  )) {
6
- d();
5
+ l();
7
6
  return;
8
7
  }
8
+ window._notifizz = window._notifizz || {}, window._notifizz._queue = window._notifizz._queue || [], window.notifizz = window.notifizz || function() {
9
+ const u = [];
10
+ for (let s = 0; s < arguments.length; s++) u.push(arguments[s]);
11
+ window._notifizz._queue.push(u);
12
+ }, window._notifizz._notifizzSettings = {
13
+ apiKey: e.apiKey,
14
+ loaderVersion: "1",
15
+ server: e.serverUrl,
16
+ apiUrl: e.apiUrl
17
+ };
9
18
  const o = document.createElement("script");
10
- window.notifizzCallback = d, o.type = "module", o.dataset.widget = "notifizz", o.textContent = `(function (n, o, t, i, f) {
11
- (n._notifizz = n._notifizz || {}),
12
- (n._notifizz._queue = n._notifizz._queue || []),
13
- (n.notifizz =
14
- n.notifizz ||
15
- function () {
16
- let args = [];
17
- for (i = 0; i < arguments.length; i++) args.push(arguments[i]);
18
- n._notifizz._queue.push(args);
19
- }),
20
- (n._notifizz._notifizzSettings = {
21
- apiKey: "${e.apiKey}",
22
- loaderVersion: "1",
23
- server: t,
24
- apiUrl: "${e.apiUrl}"
25
- }),
26
- (f = o.getElementsByTagName("head")[0]);
27
- let r = o.createElement("script");
28
- r.type = 'module';
29
- r.onload = n.notifizzCallback;
30
- (r.async = !0), (r.src = t + i), f.appendChild(r);
31
- })(window, document, "${e.serverUrl}", "${e.widgetPath}")`, n.appendChild(o);
19
+ o.type = "module", o.async = !0, o.dataset.widget = "notifizz", o.src = e.serverUrl + e.widgetPath, o.onload = l, document.head.appendChild(o);
32
20
  }
33
- function k(e) {
21
+ function R(e) {
22
+ if (e.authType === "none") {
23
+ if (!e.userEmail || !e.userId)
24
+ throw new Error(
25
+ 'userEmail and userId are required for authType "none"'
26
+ );
27
+ window.notifizz("authenticateWithNone", {
28
+ user: { email: e.userEmail, id: e.userId }
29
+ });
30
+ return;
31
+ }
34
32
  if (e.authType === "firebase") {
35
33
  window.notifizz("authenticateWithFirebase", e.token, {}, () => {
36
34
  });
@@ -50,54 +48,60 @@ function k(e) {
50
48
  );
51
49
  }
52
50
  }
53
- function R(e) {
51
+ function b(e) {
54
52
  ["top-right", "top-left", "bottom-right", "bottom-left"].includes(e) && window.notifizz("setPosition", e);
55
53
  }
56
- function b(e) {
54
+ function B(e) {
57
55
  window.notifizz("setNotificationCenterStyles", e);
58
56
  }
59
- function I(e) {
57
+ function k(e) {
60
58
  window.notifizz("setBellStyles", e);
61
59
  }
62
- const B = "https://widget.notifizz.com", O = "https://eu.api.notifizz.com/v1", x = "/v1/widget.js", q = "notifizz-notifications";
63
- function A(e) {
60
+ const O = "https://widget.notifizz.com", x = "https://eu.api.notifizz.com/v1", D = "/v1/widget.js", q = "notifizz-notifications", A = ".notifizz-custom-bell #notifizz-isolated-css > div > a { display: none !important; }";
61
+ let I = !1;
62
+ function K() {
63
+ if (I || typeof document > "u") return;
64
+ const e = document.createElement("style");
65
+ e.textContent = A, document.head.appendChild(e), I = !0;
66
+ }
67
+ function N(e) {
64
68
  if (!e.apiKey || !e.authType)
65
69
  throw new Error(
66
70
  "Missing required options: apiKey and authType are required"
67
71
  );
68
- const d = e.mountId ?? q;
72
+ const l = e.mountId ?? q;
69
73
  let n = {
70
74
  isReady: !1,
71
75
  isOpen: !1,
72
76
  unreadCount: 0,
73
77
  lastUpdated: 0
74
78
  };
75
- const l = [], o = [], u = [];
76
- let a = null, s = null, w = !1, m = !1, c = !1;
77
- const p = (t, ...i) => {
79
+ const o = [], u = [], s = [];
80
+ let a = null, d = null, m = !1, h = !1, c = !1;
81
+ const z = (t, ...i) => {
78
82
  typeof window < "u" && window.notifizz && window.notifizz(t, ...i);
79
- }, z = () => p("toggle"), g = () => p("open"), E = () => p("close"), y = () => {
80
- z();
83
+ }, w = () => z("toggle"), E = () => z("open"), g = () => z("close"), y = () => {
84
+ w();
81
85
  }, C = () => ({
82
86
  unreadCount: n.unreadCount,
83
87
  isOpen: n.isOpen,
84
88
  isReady: n.isReady,
85
- toggle: z,
86
- open: g,
87
- close: E
88
- }), U = () => {
89
+ toggle: w,
90
+ open: E,
91
+ close: g
92
+ }), v = () => {
89
93
  const t = C();
90
- u.forEach((i) => i(t)), a && a.setAttribute("data-unread", String(n.unreadCount));
91
- }, v = () => {
92
- k({
94
+ s.forEach((i) => i(t)), a && a.setAttribute("data-unread", String(n.unreadCount));
95
+ }, U = () => {
96
+ R({
93
97
  apiKey: e.apiKey,
94
98
  token: e.token,
95
99
  authType: e.authType,
96
100
  userEmail: e.userEmail,
97
101
  userId: e.userId
98
- }), e.position && R(e.position), e.notificationCenterStyles && b(e.notificationCenterStyles), e.bellStyles && I(e.bellStyles);
99
- }, h = () => {
100
- m || (m = !0, v(), n = { ...n, isReady: !0, lastUpdated: Date.now() }, l.forEach((t) => t()), U());
102
+ }), e.position && b(e.position), e.notificationCenterStyles && B(e.notificationCenterStyles), e.bellStyles && k(e.bellStyles);
103
+ }, p = () => {
104
+ h || (h = !0, U(), n = { ...n, isReady: !0, lastUpdated: Date.now() }, o.forEach((t) => t()), v());
101
105
  }, S = (t) => {
102
106
  const r = t.detail;
103
107
  r && typeof r == "object" && (n = {
@@ -108,11 +112,11 @@ function A(e) {
108
112
  lastUpdated: r.lastUpdated ?? n.lastUpdated,
109
113
  hasError: r.hasError,
110
114
  errorCode: r.errorCode
111
- }, o.forEach((L) => L(n)), U());
112
- }, _ = () => {
113
- if (w || c) return;
114
- w = !0, window.addEventListener("notifizz:ready", h), window.addEventListener("notifizz:state", S);
115
- const t = e.serverUrl ?? B, i = e.apiUrl ?? O, r = e.widgetPath ?? x;
115
+ }, u.forEach((_) => _(n)), v());
116
+ }, L = () => {
117
+ if (m || c) return;
118
+ m = !0, window.addEventListener("notifizz:ready", p), window.addEventListener("notifizz:state", S);
119
+ const t = e.serverUrl ?? O, i = e.apiUrl ?? x, r = e.widgetPath ?? D;
116
120
  T(
117
121
  { apiKey: e.apiKey, serverUrl: t, apiUrl: i, widgetPath: r },
118
122
  () => {
@@ -121,8 +125,8 @@ function A(e) {
121
125
  ...n,
122
126
  ...f,
123
127
  lastUpdated: f.lastUpdated ?? n.lastUpdated
124
- }), f?.isReady && h();
125
- }), document.getElementById(d) && v();
128
+ }), f?.isReady && p();
129
+ }), document.getElementById(l) && U();
126
130
  }
127
131
  );
128
132
  };
@@ -130,48 +134,48 @@ function A(e) {
130
134
  mount(t) {
131
135
  let i;
132
136
  if (t)
133
- i = t, i.id || (i.id = d);
137
+ i = t, i.id || (i.id = l);
134
138
  else {
135
- const r = document.getElementById(d);
139
+ const r = document.getElementById(l);
136
140
  if (r)
137
- return s = r, _(), r;
138
- i = document.createElement("div"), i.id = d, document.body.appendChild(i);
141
+ return d = r, L(), r;
142
+ i = document.createElement("div"), i.id = l, document.body.appendChild(i);
139
143
  }
140
- return s = i, _(), i;
144
+ return d = i, L(), i;
141
145
  },
142
146
  getState() {
143
147
  return { ...n };
144
148
  },
145
149
  onReady(t) {
146
- return l.push(t), n.isReady && t(), () => {
147
- const i = l.indexOf(t);
148
- i !== -1 && l.splice(i, 1);
149
- };
150
- },
151
- onStateChange(t) {
152
- return o.push(t), () => {
150
+ return o.push(t), n.isReady && t(), () => {
153
151
  const i = o.indexOf(t);
154
152
  i !== -1 && o.splice(i, 1);
155
153
  };
156
154
  },
157
- onBellUpdate(t) {
158
- return u.push(t), t(C()), () => {
155
+ onStateChange(t) {
156
+ return u.push(t), () => {
159
157
  const i = u.indexOf(t);
160
158
  i !== -1 && u.splice(i, 1);
161
159
  };
162
160
  },
163
- open: g,
164
- close: E,
165
- toggle: z,
161
+ onBellUpdate(t) {
162
+ return s.push(t), t(C()), () => {
163
+ const i = s.indexOf(t);
164
+ i !== -1 && s.splice(i, 1);
165
+ };
166
+ },
167
+ open: E,
168
+ close: g,
169
+ toggle: w,
166
170
  setBellElement(t) {
167
- a && a.removeEventListener("click", y), a = t, a && (a.addEventListener("click", y), a.setAttribute("data-unread", String(n.unreadCount)));
171
+ a && a.removeEventListener("click", y), a = t, a ? (a.addEventListener("click", y), a.setAttribute("data-unread", String(n.unreadCount)), d && (d.classList.add("notifizz-custom-bell"), K())) : d && d.classList.remove("notifizz-custom-bell");
168
172
  },
169
173
  destroy() {
170
- c || (c = !0, window.removeEventListener("notifizz:ready", h), window.removeEventListener("notifizz:state", S), a && (a.removeEventListener("click", y), a = null), l.length = 0, o.length = 0, u.length = 0, s && s.parentNode && (s.parentNode.removeChild(s), s = null));
174
+ c || (c = !0, window.removeEventListener("notifizz:ready", p), window.removeEventListener("notifizz:state", S), a && (a.removeEventListener("click", y), a = null), o.length = 0, u.length = 0, s.length = 0, d && d.parentNode && (d.parentNode.removeChild(d), d = null));
171
175
  }
172
176
  };
173
177
  }
174
178
  export {
175
- A as createNotifizz
179
+ N as createNotifizz
176
180
  };
177
181
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../notifizz-shared/src/scriptLoader.ts","../src/createNotifizz.ts"],"sourcesContent":["export interface NotifizzConfig {\n apiKey: string;\n apiUrl: string;\n serverUrl: string;\n widgetPath: string;\n}\n\nexport function importScriptInBrowser(\n config: NotifizzConfig,\n callback: () => void\n): void {\n const head = document.head;\n const existingScript = document.querySelector(\n 'script[data-widget=\"notifizz\"]'\n );\n if (existingScript) {\n callback();\n return;\n }\n const script = document.createElement('script');\n (window as Window & { notifizzCallback?: () => void }).notifizzCallback =\n callback;\n script.type = 'module';\n script.dataset['widget'] = 'notifizz';\n script.textContent = `(function (n, o, t, i, f) {\n (n._notifizz = n._notifizz || {}),\n (n._notifizz._queue = n._notifizz._queue || []),\n (n.notifizz =\n n.notifizz ||\n function () {\n let args = [];\n for (i = 0; i < arguments.length; i++) args.push(arguments[i]);\n n._notifizz._queue.push(args);\n }),\n (n._notifizz._notifizzSettings = {\n apiKey: \"${config.apiKey}\",\n loaderVersion: \"1\",\n server: t,\n apiUrl: \"${config.apiUrl}\"\n }),\n (f = o.getElementsByTagName(\"head\")[0]);\n let r = o.createElement(\"script\");\n r.type = 'module';\n r.onload = n.notifizzCallback;\n (r.async = !0), (r.src = t + i), f.appendChild(r);\n })(window, document, \"${config.serverUrl}\", \"${config.widgetPath}\")`;\n head.appendChild(script);\n}\n\nexport interface AuthOptions {\n apiKey: string;\n token: string;\n authType: 'firebase' | 'backendToken';\n userEmail?: string;\n userId?: string;\n}\n\nexport function authenticate(options: AuthOptions): void {\n if (options.authType === 'firebase') {\n window.notifizz('authenticateWithFirebase', options.token, {}, () => {});\n return;\n }\n if (options.authType === 'backendToken') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'Missing required options: userEmail and userId are required for backendToken'\n );\n }\n window.notifizz(\n 'authenticateWithBackendToken',\n options.token,\n { user: { email: options.userEmail, id: options.userId } },\n () => {}\n );\n }\n}\n\nexport function applyPosition(\n position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'\n): void {\n const valid = ['top-right', 'top-left', 'bottom-right', 'bottom-left'];\n if (valid.includes(position)) {\n window.notifizz('setPosition', position);\n }\n}\n\nexport function applyNotificationCenterStyles(styles: {\n marginTop?: string;\n}): void {\n window.notifizz('setNotificationCenterStyles', styles);\n}\n\nexport function applyBellStyles(styles: {\n marginRight?: string;\n marginLeft?: string;\n}): void {\n window.notifizz('setBellStyles', styles);\n}\n","import type { NotifizzState, NotifizzBellContext } from '../../notifizz-shared/src/types';\nimport type { NotifizzOptions, NotifizzVanillaApi } from './types';\nimport {\n importScriptInBrowser,\n authenticate,\n applyPosition,\n applyNotificationCenterStyles,\n applyBellStyles,\n} from '../../notifizz-shared/src/scriptLoader';\n\nconst DEFAULT_SERVER_URL = 'https://widget.notifizz.com';\nconst DEFAULT_API_URL = 'https://eu.api.notifizz.com/v1';\nconst DEFAULT_WIDGET_PATH = '/v1/widget.js';\nconst DEFAULT_MOUNT_ID = 'notifizz-notifications';\n\nexport function createNotifizz(options: NotifizzOptions): NotifizzVanillaApi {\n if (!options.apiKey || !options.authType) {\n throw new Error(\n 'Missing required options: apiKey and authType are required'\n );\n }\n\n const mountId = options.mountId ?? DEFAULT_MOUNT_ID;\n let state: NotifizzState = {\n isReady: false,\n isOpen: false,\n unreadCount: 0,\n lastUpdated: 0,\n };\n const readyListeners: (() => void)[] = [];\n const stateChangeListeners: ((s: NotifizzState) => void)[] = [];\n const bellUpdateListeners: ((ctx: NotifizzBellContext) => void)[] = [];\n let bellElement: HTMLElement | null = null;\n let mountElement: HTMLElement | null = null;\n let initDone = false;\n let readyFired = false;\n let destroyed = false;\n\n const send = (command: string, ...args: unknown[]): void => {\n if (typeof window !== 'undefined' && window.notifizz) {\n window.notifizz(command, ...args);\n }\n };\n\n const toggle = (): void => send('toggle');\n const open = (): void => send('open');\n const close = (): void => send('close');\n\n const onBellClick = (): void => {\n toggle();\n };\n\n const buildBellContext = (): NotifizzBellContext => ({\n unreadCount: state.unreadCount,\n isOpen: state.isOpen,\n isReady: state.isReady,\n toggle,\n open,\n close,\n });\n\n const notifyBellListeners = (): void => {\n const ctx = buildBellContext();\n bellUpdateListeners.forEach((cb) => cb(ctx));\n if (bellElement) {\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n };\n\n const applyOptions = (): void => {\n authenticate({\n apiKey: options.apiKey,\n token: options.token,\n authType: options.authType,\n userEmail: options.userEmail,\n userId: options.userId,\n });\n if (options.position) applyPosition(options.position);\n if (options.notificationCenterStyles) {\n applyNotificationCenterStyles(options.notificationCenterStyles);\n }\n if (options.bellStyles) applyBellStyles(options.bellStyles);\n };\n\n const handleReady = (): void => {\n if (readyFired) return;\n readyFired = true;\n applyOptions();\n state = { ...state, isReady: true, lastUpdated: Date.now() };\n readyListeners.forEach((cb) => cb());\n notifyBellListeners();\n };\n\n const handleState = (e: Event): void => {\n const ev = e as CustomEvent<NotifizzState>;\n const detail = ev.detail;\n if (detail && typeof detail === 'object') {\n state = {\n ...state,\n isReady: detail.isReady ?? state.isReady,\n isOpen: detail.isOpen ?? state.isOpen,\n unreadCount:\n typeof detail.unreadCount === 'number'\n ? detail.unreadCount\n : state.unreadCount,\n lastUpdated: detail.lastUpdated ?? state.lastUpdated,\n hasError: detail.hasError,\n errorCode: detail.errorCode,\n };\n stateChangeListeners.forEach((cb) => cb(state));\n notifyBellListeners();\n }\n };\n\n const init = (): void => {\n if (initDone || destroyed) return;\n initDone = true;\n window.addEventListener('notifizz:ready', handleReady);\n window.addEventListener('notifizz:state', handleState);\n\n const serverUrl = options.serverUrl ?? DEFAULT_SERVER_URL;\n const apiUrl = options.apiUrl ?? DEFAULT_API_URL;\n const widgetPath = options.widgetPath ?? DEFAULT_WIDGET_PATH;\n\n importScriptInBrowser(\n { apiKey: options.apiKey, serverUrl, apiUrl, widgetPath },\n () => {\n if (typeof window.notifizz === 'function') {\n window.notifizz('getState', (current: NotifizzState) => {\n if (current && typeof current === 'object') {\n state = {\n ...state,\n ...current,\n lastUpdated: current.lastUpdated ?? state.lastUpdated,\n };\n }\n if (current?.isReady) handleReady();\n });\n }\n const el = document.getElementById(mountId);\n if (el) applyOptions();\n }\n );\n };\n\n return {\n mount(element?: HTMLElement): HTMLElement {\n let el: HTMLElement;\n if (element) {\n el = element;\n if (!el.id) el.id = mountId;\n } else {\n const existing = document.getElementById(mountId);\n if (existing) {\n mountElement = existing;\n init();\n return existing;\n }\n el = document.createElement('div');\n el.id = mountId;\n document.body.appendChild(el);\n }\n mountElement = el;\n init();\n return el;\n },\n\n getState(): NotifizzState {\n return { ...state };\n },\n\n onReady(cb: () => void): () => void {\n readyListeners.push(cb);\n if (state.isReady) cb();\n return () => {\n const i = readyListeners.indexOf(cb);\n if (i !== -1) readyListeners.splice(i, 1);\n };\n },\n\n onStateChange(cb: (s: NotifizzState) => void): () => void {\n stateChangeListeners.push(cb);\n return () => {\n const i = stateChangeListeners.indexOf(cb);\n if (i !== -1) stateChangeListeners.splice(i, 1);\n };\n },\n\n onBellUpdate(cb: (ctx: NotifizzBellContext) => void): () => void {\n bellUpdateListeners.push(cb);\n cb(buildBellContext());\n return () => {\n const i = bellUpdateListeners.indexOf(cb);\n if (i !== -1) bellUpdateListeners.splice(i, 1);\n };\n },\n\n open,\n close,\n toggle,\n\n setBellElement(el: HTMLElement | null): void {\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n }\n bellElement = el;\n if (bellElement) {\n bellElement.addEventListener('click', onBellClick);\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n },\n\n destroy(): void {\n if (destroyed) return;\n destroyed = true;\n window.removeEventListener('notifizz:ready', handleReady);\n window.removeEventListener('notifizz:state', handleState);\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n bellElement = null;\n }\n readyListeners.length = 0;\n stateChangeListeners.length = 0;\n bellUpdateListeners.length = 0;\n if (mountElement && mountElement.parentNode) {\n mountElement.parentNode.removeChild(mountElement);\n mountElement = null;\n }\n },\n };\n}\n"],"names":["importScriptInBrowser","config","callback","head","script","authenticate","options","applyPosition","position","applyNotificationCenterStyles","styles","applyBellStyles","DEFAULT_SERVER_URL","DEFAULT_API_URL","DEFAULT_WIDGET_PATH","DEFAULT_MOUNT_ID","createNotifizz","mountId","state","readyListeners","stateChangeListeners","bellUpdateListeners","bellElement","mountElement","initDone","readyFired","destroyed","send","command","args","toggle","open","close","onBellClick","buildBellContext","notifyBellListeners","ctx","cb","applyOptions","handleReady","handleState","e","detail","init","serverUrl","apiUrl","widgetPath","current","element","el","existing"],"mappings":"AAOO,SAASA,EACdC,GACAC,GACM;AACN,QAAMC,IAAO,SAAS;AAItB,MAHuB,SAAS;AAAA,IAC9B;AAAA,EAAA,GAEkB;AAClB,IAAAD,EAAA;AACA;AAAA,EACF;AACA,QAAME,IAAS,SAAS,cAAc,QAAQ;AAC7C,SAAsD,mBACrDF,GACFE,EAAO,OAAO,UACdA,EAAO,QAAQ,SAAY,YAC3BA,EAAO,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAWJH,EAAO,MAAM;AAAA;AAAA;AAAA,mBAGbA,EAAO,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0B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AkBC,CAAW,GACpDlB,MACFA,EAAY,oBAAoB,SAASW,CAAW,GACpDX,IAAc,OAEhBH,EAAe,SAAS,GACxBC,EAAqB,SAAS,GAC9BC,EAAoB,SAAS,GACzBE,KAAgBA,EAAa,eAC/BA,EAAa,WAAW,YAAYA,CAAY,GAChDA,IAAe;AAAA,IAEnB;AAAA,EAAA;AAEJ;"}
1
+ {"version":3,"file":"index.js","sources":["../../notifizz-shared/src/scriptLoader.ts","../src/createNotifizz.ts"],"sourcesContent":["/// <reference path=\"./global.d.ts\" />\n\nexport interface NotifizzConfig {\n apiKey: string;\n apiUrl: string;\n serverUrl: string;\n widgetPath: string;\n}\n\nexport function importScriptInBrowser(\n config: NotifizzConfig,\n callback: () => void\n): void {\n const existingScript = document.querySelector(\n 'script[data-widget=\"notifizz\"]'\n );\n if (existingScript) {\n callback();\n return;\n }\n\n window._notifizz = window._notifizz || {};\n window._notifizz._queue = window._notifizz._queue || [];\n window.notifizz =\n window.notifizz ||\n function () {\n const args: unknown[] = [];\n for (let i = 0; i < arguments.length; i++) args.push(arguments[i]);\n window._notifizz!._queue!.push(args);\n };\n window._notifizz._notifizzSettings = {\n apiKey: config.apiKey,\n loaderVersion: '1',\n server: config.serverUrl,\n apiUrl: config.apiUrl,\n };\n\n const script = document.createElement('script');\n script.type = 'module';\n script.async = true;\n script.dataset.widget = 'notifizz';\n script.src = config.serverUrl + config.widgetPath;\n script.onload = callback;\n document.head.appendChild(script);\n}\n\nexport interface AuthOptions {\n apiKey: string;\n token?: string;\n authType: 'firebase' | 'backendToken' | 'none';\n userEmail?: string;\n userId?: string;\n}\n\nexport function authenticate(options: AuthOptions): void {\n if (options.authType === 'none') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'userEmail and userId are required for authType \"none\"'\n );\n }\n window.notifizz('authenticateWithNone', {\n user: { email: options.userEmail, id: options.userId },\n });\n return;\n }\n if (options.authType === 'firebase') {\n window.notifizz('authenticateWithFirebase', options.token, {}, () => {});\n return;\n }\n if (options.authType === 'backendToken') {\n if (!options.userEmail || !options.userId) {\n throw new Error(\n 'Missing required options: userEmail and userId are required for backendToken'\n );\n }\n window.notifizz(\n 'authenticateWithBackendToken',\n options.token,\n { user: { email: options.userEmail, id: options.userId } },\n () => {}\n );\n }\n}\n\nexport function applyPosition(\n position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'\n): void {\n const valid = ['top-right', 'top-left', 'bottom-right', 'bottom-left'];\n if (valid.includes(position)) {\n window.notifizz('setPosition', position);\n }\n}\n\nexport function applyNotificationCenterStyles(styles: {\n marginTop?: string;\n}): void {\n window.notifizz('setNotificationCenterStyles', styles);\n}\n\nexport function applyBellStyles(styles: {\n marginRight?: string;\n marginLeft?: string;\n}): void {\n window.notifizz('setBellStyles', styles);\n}\n","import type { NotifizzState, NotifizzBellContext } from '../../notifizz-shared/src/types';\nimport type { NotifizzOptions, NotifizzVanillaApi } from './types';\nimport {\n importScriptInBrowser,\n authenticate,\n applyPosition,\n applyNotificationCenterStyles,\n applyBellStyles,\n} from '../../notifizz-shared/src/scriptLoader';\n\nconst DEFAULT_SERVER_URL = 'https://widget.notifizz.com';\nconst DEFAULT_API_URL = 'https://eu.api.notifizz.com/v1';\nconst DEFAULT_WIDGET_PATH = '/v1/widget.js';\nconst DEFAULT_MOUNT_ID = 'notifizz-notifications';\nconst HIDE_WIDGET_BELL_CSS =\n '.notifizz-custom-bell #notifizz-isolated-css > div > a { display: none !important; }';\n\nlet customBellCssInjected = false;\nfunction injectCustomBellCss(): void {\n if (customBellCssInjected || typeof document === 'undefined') return;\n const style = document.createElement('style');\n style.textContent = HIDE_WIDGET_BELL_CSS;\n document.head.appendChild(style);\n customBellCssInjected = true;\n}\n\nexport function createNotifizz(options: NotifizzOptions): NotifizzVanillaApi {\n if (!options.apiKey || !options.authType) {\n throw new Error(\n 'Missing required options: apiKey and authType are required'\n );\n }\n\n const mountId = options.mountId ?? DEFAULT_MOUNT_ID;\n let state: NotifizzState = {\n isReady: false,\n isOpen: false,\n unreadCount: 0,\n lastUpdated: 0,\n };\n const readyListeners: (() => void)[] = [];\n const stateChangeListeners: ((s: NotifizzState) => void)[] = [];\n const bellUpdateListeners: ((ctx: NotifizzBellContext) => void)[] = [];\n let bellElement: HTMLElement | null = null;\n let mountElement: HTMLElement | null = null;\n let initDone = false;\n let readyFired = false;\n let destroyed = false;\n\n const send = (command: string, ...args: unknown[]): void => {\n if (typeof window !== 'undefined' && window.notifizz) {\n window.notifizz(command, ...args);\n }\n };\n\n const toggle = (): void => send('toggle');\n const open = (): void => send('open');\n const close = (): void => send('close');\n\n const onBellClick = (): void => {\n toggle();\n };\n\n const buildBellContext = (): NotifizzBellContext => ({\n unreadCount: state.unreadCount,\n isOpen: state.isOpen,\n isReady: state.isReady,\n toggle,\n open,\n close,\n });\n\n const notifyBellListeners = (): void => {\n const ctx = buildBellContext();\n bellUpdateListeners.forEach((cb) => cb(ctx));\n if (bellElement) {\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n }\n };\n\n const applyOptions = (): void => {\n authenticate({\n apiKey: options.apiKey,\n token: options.token,\n authType: options.authType,\n userEmail: options.userEmail,\n userId: options.userId,\n });\n if (options.position) applyPosition(options.position);\n if (options.notificationCenterStyles) {\n applyNotificationCenterStyles(options.notificationCenterStyles);\n }\n if (options.bellStyles) applyBellStyles(options.bellStyles);\n };\n\n const handleReady = (): void => {\n if (readyFired) return;\n readyFired = true;\n applyOptions();\n state = { ...state, isReady: true, lastUpdated: Date.now() };\n readyListeners.forEach((cb) => cb());\n notifyBellListeners();\n };\n\n const handleState = (e: Event): void => {\n const ev = e as CustomEvent<NotifizzState>;\n const detail = ev.detail;\n if (detail && typeof detail === 'object') {\n state = {\n ...state,\n isReady: detail.isReady ?? state.isReady,\n isOpen: detail.isOpen ?? state.isOpen,\n unreadCount:\n typeof detail.unreadCount === 'number'\n ? detail.unreadCount\n : state.unreadCount,\n lastUpdated: detail.lastUpdated ?? state.lastUpdated,\n hasError: detail.hasError,\n errorCode: detail.errorCode,\n };\n stateChangeListeners.forEach((cb) => cb(state));\n notifyBellListeners();\n }\n };\n\n const init = (): void => {\n if (initDone || destroyed) return;\n initDone = true;\n window.addEventListener('notifizz:ready', handleReady);\n window.addEventListener('notifizz:state', handleState);\n\n const serverUrl = options.serverUrl ?? DEFAULT_SERVER_URL;\n const apiUrl = options.apiUrl ?? DEFAULT_API_URL;\n const widgetPath = options.widgetPath ?? DEFAULT_WIDGET_PATH;\n\n importScriptInBrowser(\n { apiKey: options.apiKey, serverUrl, apiUrl, widgetPath },\n () => {\n if (typeof window.notifizz === 'function') {\n window.notifizz('getState', (current: NotifizzState) => {\n if (current && typeof current === 'object') {\n state = {\n ...state,\n ...current,\n lastUpdated: current.lastUpdated ?? state.lastUpdated,\n };\n }\n if (current?.isReady) handleReady();\n });\n }\n const el = document.getElementById(mountId);\n if (el) applyOptions();\n }\n );\n };\n\n return {\n mount(element?: HTMLElement): HTMLElement {\n let el: HTMLElement;\n if (element) {\n el = element;\n if (!el.id) el.id = mountId;\n } else {\n const existing = document.getElementById(mountId);\n if (existing) {\n mountElement = existing;\n init();\n return existing;\n }\n el = document.createElement('div');\n el.id = mountId;\n document.body.appendChild(el);\n }\n mountElement = el;\n init();\n return el;\n },\n\n getState(): NotifizzState {\n return { ...state };\n },\n\n onReady(cb: () => void): () => void {\n readyListeners.push(cb);\n if (state.isReady) cb();\n return () => {\n const i = readyListeners.indexOf(cb);\n if (i !== -1) readyListeners.splice(i, 1);\n };\n },\n\n onStateChange(cb: (s: NotifizzState) => void): () => void {\n stateChangeListeners.push(cb);\n return () => {\n const i = stateChangeListeners.indexOf(cb);\n if (i !== -1) stateChangeListeners.splice(i, 1);\n };\n },\n\n onBellUpdate(cb: (ctx: NotifizzBellContext) => void): () => void {\n bellUpdateListeners.push(cb);\n cb(buildBellContext());\n return () => {\n const i = bellUpdateListeners.indexOf(cb);\n if (i !== -1) bellUpdateListeners.splice(i, 1);\n };\n },\n\n open,\n close,\n toggle,\n\n setBellElement(el: HTMLElement | null): void {\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n }\n bellElement = el;\n if (bellElement) {\n bellElement.addEventListener('click', onBellClick);\n bellElement.setAttribute('data-unread', String(state.unreadCount));\n if (mountElement) {\n mountElement.classList.add('notifizz-custom-bell');\n injectCustomBellCss();\n }\n } else if (mountElement) {\n mountElement.classList.remove('notifizz-custom-bell');\n }\n },\n\n destroy(): void {\n if (destroyed) return;\n destroyed = true;\n window.removeEventListener('notifizz:ready', handleReady);\n window.removeEventListener('notifizz:state', handleState);\n if (bellElement) {\n bellElement.removeEventListener('click', onBellClick);\n bellElement = null;\n }\n readyListeners.length = 0;\n stateChangeListeners.length = 0;\n bellUpdateListeners.length = 0;\n if (mountElement && mountElement.parentNode) {\n mountElement.parentNode.removeChild(mountElement);\n mountElement = null;\n }\n },\n };\n}\n"],"names":["importScriptInBrowser","config","callback","args","i","script","authenticate","options","applyPosition","position","applyNotificationCenterStyles","styles","applyBellStyles","DEFAULT_SERVER_URL","DEFAULT_API_URL","DEFAULT_WIDGET_PATH","DEFAULT_MOUNT_ID","HIDE_WIDGET_BELL_CSS","customBellCssInjected","injectCustomBellCss","style","createNotifizz","mountId","state","readyListeners","stateChangeListeners","bellUpdateListeners","bellElement","mountElement","initDone","readyFired","destroyed","send","command","toggle","open","close","onBellClick","buildBellContext","notifyBellListeners","ctx","cb","applyOptions","handleReady","handleState","e","detail","init","serverUrl","apiUrl","widgetPath","current","element","el","existing"],"mappings":"AASO,SAASA,EACdC,GACAC,GACM;AAIN,MAHuB,SAAS;AAAA,IAC9B;AAAA,EAAA,GAEkB;AAClB,IAAAA,EAAA;AACA;AAAA,EACF;AAEA,SAAO,YAAY,OAAO,aAAa,CAAA,GACvC,OAAO,UAAU,SAAS,OAAO,UAAU,UAAU,CAAA,GACrD,OAAO,WACL,OAAO,YACP,WAAY;AACV,UAAMC,IAAkB,CAAA;AACxB,aAASC,IAAI,GAAGA,IAAI,UAAU,QAAQA,IAAK,CAAA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"1.1.2",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -18,9 +18,7 @@
18
18
  "require": "./dist/index.cjs"
19
19
  }
20
20
  },
21
- "files": [
22
- "dist"
23
- ],
21
+ "files": ["dist"],
24
22
  "scripts": {
25
23
  "build": "vite build"
26
24
  },