Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@magicbell/webpush

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@magicbell/webpush - npm Package Compare versions

Comparing version 1.4.0 to 1.4.1

2

dist/index.js
/**
* @license @magicbell/webpush v1.4.0
* @license @magicbell/webpush v1.4.1
*

@@ -4,0 +4,0 @@ * Copyright (c) MagicBell Inc. and its affiliates.

/**
* @license @magicbell/webpush v1.4.0
* @license @magicbell/webpush v1.4.1
*

@@ -131,6 +131,7 @@ * Copyright (c) MagicBell Inc. and its affiliates.

async function isSubscribed(options) {
var _a;
const baseURL = options.host || DEFAULT_HOST;
const subscriptions = await api.getSubscriptions(__spreadProps(__spreadValues({}, options), { baseURL }));
const registration = await registerServiceWorker({ path: options.serviceWorkerPath });
const activeSubscription = await registration.pushManager.getSubscription();
const activeSubscription = await ((_a = registration == null ? void 0 : registration.pushManager) == null ? void 0 : _a.getSubscription());
if (!(activeSubscription == null ? void 0 : activeSubscription.endpoint))

@@ -147,2 +148,5 @@ return false;

const registration = await registerServiceWorker({ path: options.serviceWorkerPath });
if (!(registration == null ? void 0 : registration.pushManager)) {
throw new Error("Push notifications are not supported in this browser");
}
const activeSubscription = await registration.pushManager.getSubscription();

@@ -149,0 +153,0 @@ if (activeSubscription) {

/**
* @license @magicbell/webpush v1.4.0
* @license @magicbell/webpush v1.4.1
*

@@ -10,3 +10,3 @@ * Copyright (c) MagicBell Inc. and its affiliates.

"use strict";var m=Object.defineProperty,y=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var g=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var b=(e,t,r)=>t in e?m(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,a=(e,t)=>{for(var r in t||(t={}))k.call(t,r)&&b(e,r,t[r]);if(g)for(var r of g(t))j.call(t,r)&&b(e,r,t[r]);return e},c=(e,t)=>y(e,_(t));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});let p=null,f="";const o="https://api.magicbell.com",u={async getAuthToken(e){const t={accept:"application/json","content-type":"application/json","x-magicbell-api-key":e.apiKey};if("userExternalId"in e&&e.userExternalId)t["x-magicbell-user-external-id"]=e.userExternalId;else if("userEmail"in e&&e.userEmail)t["x-magicbell-user-email"]=e.userEmail;else throw new Error("Missing user email or external ID");return e.userHmac&&(t["x-magicbell-user-hmac"]=e.userHmac),fetch(`${e.host||o}/config`,{method:"GET",headers:t}).then(r=>r.json()).then(r=>{const i=new URL(r.web_push_notifications.subscribe_url);return{host:e.host||o,token:i.searchParams.get("access_token")||null,project:i.searchParams.get("project")||null}})},async getConfig({token:e,project:t,baseURL:r}){const i=[e,t,r].join("-");return p&&f===i?p:fetch(`${r}/web_push_subscriptions?access_token=${e}&project=${t}`,{method:"GET",headers:{"Content-Type":"application/json",Accept:"application/json"}}).then(n=>n.json()).then(n=>(p=n.push_subscription,f=i,p))},async getSubscriptions({token:e,project:t,baseURL:r}){const i=await this.getConfig({token:e,project:t,baseURL:r});if(!i.project.api_key)throw new Error("Missing API key");const n={accept:"application/json","content-type":"application/json","x-magicbell-api-key":i.project.api_key};return i.user.email?n["x-magicbell-user-email"]=i.user.email:i.user.external_id&&(n["x-magicbell-user-external-id"]=i.user.external_id),i.user.hmac&&(n["x-magicbell-user-hmac"]=i.user.hmac),fetch(`${r}/push_subscriptions`,{method:"GET",headers:n}).then(s=>s.json()).then(s=>(s==null?void 0:s.push_subscriptions)||[])},async updateSubscription({token:e,project:t,baseURL:r},i){return fetch(`${r}/web_push_subscriptions?access_token=${e}&project=${t}`,{method:"POST",headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({web_push_subscription:{data:i}})}).then(n=>n.json()).then(n=>n.web_push_subscription)}};function d(){return typeof window>"u"||typeof navigator>"u"?!1:"PushManager"in window&&"serviceWorker"in navigator}async function v(e){await u.getConfig(c(a({},e),{baseURL:e.host||o}))}async function h({path:e="/sw.js"}={}){return navigator.serviceWorker.controller||await navigator.serviceWorker.register(e),navigator.serviceWorker.ready}async function S(e){const t=e.host||o,r=await u.getSubscriptions(c(a({},e),{baseURL:t})),n=await(await h({path:e.serviceWorkerPath})).pushManager.getSubscription();return n!=null&&n.endpoint?r.some(s=>s.device_token===n.endpoint):!1}async function x(e){if(!d())throw new Error("Push notifications are not supported in this browser");const t=e.host||o,r=await u.getConfig(c(a({},e),{baseURL:t})),i=await h({path:e.serviceWorkerPath}),n=await i.pushManager.getSubscription();n&&await n.unsubscribe().catch(()=>{});const s=r.project.vapid_public_key.replace(/=/g,""),l=await i.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:s}).then(w=>w.toJSON());if(!("endpoint"in l))throw new Error("Failed to subscribe to push notifications, browser did not return an subscription endpoint.");await u.updateSubscription(c(a({},e),{baseURL:t}),l)}async function E(e){return u.getAuthToken(e)}exports.getAuthToken=E;exports.isSubscribed=S;exports.isSupported=d;exports.prefetchConfig=v;exports.registerServiceWorker=h;exports.subscribe=x;
"use strict";var m=Object.defineProperty,y=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var g=(e,t,r)=>t in e?m(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,a=(e,t)=>{for(var r in t||(t={}))k.call(t,r)&&g(e,r,t[r]);if(b)for(var r of b(t))j.call(t,r)&&g(e,r,t[r]);return e},c=(e,t)=>y(e,_(t));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});let h=null,f="";const o="https://api.magicbell.com",u={async getAuthToken(e){const t={accept:"application/json","content-type":"application/json","x-magicbell-api-key":e.apiKey};if("userExternalId"in e&&e.userExternalId)t["x-magicbell-user-external-id"]=e.userExternalId;else if("userEmail"in e&&e.userEmail)t["x-magicbell-user-email"]=e.userEmail;else throw new Error("Missing user email or external ID");return e.userHmac&&(t["x-magicbell-user-hmac"]=e.userHmac),fetch(`${e.host||o}/config`,{method:"GET",headers:t}).then(r=>r.json()).then(r=>{const i=new URL(r.web_push_notifications.subscribe_url);return{host:e.host||o,token:i.searchParams.get("access_token")||null,project:i.searchParams.get("project")||null}})},async getConfig({token:e,project:t,baseURL:r}){const i=[e,t,r].join("-");return h&&f===i?h:fetch(`${r}/web_push_subscriptions?access_token=${e}&project=${t}`,{method:"GET",headers:{"Content-Type":"application/json",Accept:"application/json"}}).then(n=>n.json()).then(n=>(h=n.push_subscription,f=i,h))},async getSubscriptions({token:e,project:t,baseURL:r}){const i=await this.getConfig({token:e,project:t,baseURL:r});if(!i.project.api_key)throw new Error("Missing API key");const n={accept:"application/json","content-type":"application/json","x-magicbell-api-key":i.project.api_key};return i.user.email?n["x-magicbell-user-email"]=i.user.email:i.user.external_id&&(n["x-magicbell-user-external-id"]=i.user.external_id),i.user.hmac&&(n["x-magicbell-user-hmac"]=i.user.hmac),fetch(`${r}/push_subscriptions`,{method:"GET",headers:n}).then(s=>s.json()).then(s=>(s==null?void 0:s.push_subscriptions)||[])},async updateSubscription({token:e,project:t,baseURL:r},i){return fetch(`${r}/web_push_subscriptions?access_token=${e}&project=${t}`,{method:"POST",headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({web_push_subscription:{data:i}})}).then(n=>n.json()).then(n=>n.web_push_subscription)}};function d(){return typeof window>"u"||typeof navigator>"u"?!1:"PushManager"in window&&"serviceWorker"in navigator}async function v(e){await u.getConfig(c(a({},e),{baseURL:e.host||o}))}async function l({path:e="/sw.js"}={}){return navigator.serviceWorker.controller||await navigator.serviceWorker.register(e),navigator.serviceWorker.ready}async function S(e){var s;const t=e.host||o,r=await u.getSubscriptions(c(a({},e),{baseURL:t})),i=await l({path:e.serviceWorkerPath}),n=await((s=i==null?void 0:i.pushManager)==null?void 0:s.getSubscription());return n!=null&&n.endpoint?r.some(p=>p.device_token===n.endpoint):!1}async function x(e){if(!d())throw new Error("Push notifications are not supported in this browser");const t=e.host||o,r=await u.getConfig(c(a({},e),{baseURL:t})),i=await l({path:e.serviceWorkerPath});if(!(i!=null&&i.pushManager))throw new Error("Push notifications are not supported in this browser");const n=await i.pushManager.getSubscription();n&&await n.unsubscribe().catch(()=>{});const s=r.project.vapid_public_key.replace(/=/g,""),p=await i.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:s}).then(w=>w.toJSON());if(!("endpoint"in p))throw new Error("Failed to subscribe to push notifications, browser did not return an subscription endpoint.");await u.updateSubscription(c(a({},e),{baseURL:t}),p)}async function E(e){return u.getAuthToken(e)}exports.getAuthToken=E;exports.isSubscribed=S;exports.isSupported=d;exports.prefetchConfig=v;exports.registerServiceWorker=l;exports.subscribe=x;
//# sourceMappingURL=magicbell-webpush.cjs.min.js.map
/**
* @license @magicbell/webpush v1.4.0
* @license @magicbell/webpush v1.4.1
*

@@ -129,6 +129,7 @@ * Copyright (c) MagicBell Inc. and its affiliates.

async function isSubscribed(options) {
var _a;
const baseURL = options.host || DEFAULT_HOST;
const subscriptions = await api.getSubscriptions(__spreadProps(__spreadValues({}, options), { baseURL }));
const registration = await registerServiceWorker({ path: options.serviceWorkerPath });
const activeSubscription = await registration.pushManager.getSubscription();
const activeSubscription = await ((_a = registration == null ? void 0 : registration.pushManager) == null ? void 0 : _a.getSubscription());
if (!(activeSubscription == null ? void 0 : activeSubscription.endpoint))

@@ -145,2 +146,5 @@ return false;

const registration = await registerServiceWorker({ path: options.serviceWorkerPath });
if (!(registration == null ? void 0 : registration.pushManager)) {
throw new Error("Push notifications are not supported in this browser");
}
const activeSubscription = await registration.pushManager.getSubscription();

@@ -147,0 +151,0 @@ if (activeSubscription) {

/**
* @license @magicbell/webpush v1.4.0
* @license @magicbell/webpush v1.4.1
*

@@ -14,11 +14,11 @@ * Copyright (c) MagicBell Inc. and its affiliates.

var _ = Object.prototype.hasOwnProperty, k = Object.prototype.propertyIsEnumerable;
var g = (e, t, i) => t in e ? w(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i, a = (e, t) => {
for (var i in t || (t = {}))
_.call(t, i) && g(e, i, t[i]);
var b = (e, t, n) => t in e ? w(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, a = (e, t) => {
for (var n in t || (t = {}))
_.call(t, n) && b(e, n, t[n]);
if (l)
for (var i of l(t))
k.call(t, i) && g(e, i, t[i]);
for (var n of l(t))
k.call(t, n) && b(e, n, t[n]);
return e;
}, c = (e, t) => m(e, y(t));
let p = null, b = "";
let h = null, f = "";
const o = "https://api.magicbell.com", u = {

@@ -40,4 +40,4 @@ async getAuthToken(e) {

headers: t
}).then((i) => i.json()).then((i) => {
const r = new URL(i.web_push_notifications.subscribe_url);
}).then((n) => n.json()).then((n) => {
const r = new URL(n.web_push_notifications.subscribe_url);
return {

@@ -50,5 +50,5 @@ host: e.host || o,

},
async getConfig({ token: e, project: t, baseURL: i }) {
const r = [e, t, i].join("-");
return p && b === r ? p : fetch(`${i}/web_push_subscriptions?access_token=${e}&project=${t}`, {
async getConfig({ token: e, project: t, baseURL: n }) {
const r = [e, t, n].join("-");
return h && f === r ? h : fetch(`${n}/web_push_subscriptions?access_token=${e}&project=${t}`, {
method: "GET",

@@ -59,9 +59,9 @@ headers: {

}
}).then((n) => n.json()).then((n) => (p = n.push_subscription, b = r, p));
}).then((i) => i.json()).then((i) => (h = i.push_subscription, f = r, h));
},
async getSubscriptions({ token: e, project: t, baseURL: i }) {
const r = await this.getConfig({ token: e, project: t, baseURL: i });
async getSubscriptions({ token: e, project: t, baseURL: n }) {
const r = await this.getConfig({ token: e, project: t, baseURL: n });
if (!r.project.api_key)
throw new Error("Missing API key");
const n = {
const i = {
accept: "application/json",

@@ -71,9 +71,9 @@ "content-type": "application/json",

};
return r.user.email ? n["x-magicbell-user-email"] = r.user.email : r.user.external_id && (n["x-magicbell-user-external-id"] = r.user.external_id), r.user.hmac && (n["x-magicbell-user-hmac"] = r.user.hmac), fetch(`${i}/push_subscriptions`, {
return r.user.email ? i["x-magicbell-user-email"] = r.user.email : r.user.external_id && (i["x-magicbell-user-external-id"] = r.user.external_id), r.user.hmac && (i["x-magicbell-user-hmac"] = r.user.hmac), fetch(`${n}/push_subscriptions`, {
method: "GET",
headers: n
headers: i
}).then((s) => s.json()).then((s) => (s == null ? void 0 : s.push_subscriptions) || []);
},
async updateSubscription({ token: e, project: t, baseURL: i }, r) {
return fetch(`${i}/web_push_subscriptions?access_token=${e}&project=${t}`, {
async updateSubscription({ token: e, project: t, baseURL: n }, r) {
return fetch(`${n}/web_push_subscriptions?access_token=${e}&project=${t}`, {
method: "POST",

@@ -89,3 +89,3 @@ headers: {

})
}).then((n) => n.json()).then((n) => n.web_push_subscription);
}).then((i) => i.json()).then((i) => i.web_push_subscription);
}

@@ -99,8 +99,9 @@ };

}
async function f({ path: e = "/sw.js" } = {}) {
async function g({ path: e = "/sw.js" } = {}) {
return navigator.serviceWorker.controller || await navigator.serviceWorker.register(e), navigator.serviceWorker.ready;
}
async function E(e) {
const t = e.host || o, i = await u.getSubscriptions(c(a({}, e), { baseURL: t })), n = await (await f({ path: e.serviceWorkerPath })).pushManager.getSubscription();
return n != null && n.endpoint ? i.some((s) => s.device_token === n.endpoint) : !1;
var s;
const t = e.host || o, n = await u.getSubscriptions(c(a({}, e), { baseURL: t })), r = await g({ path: e.serviceWorkerPath }), i = await ((s = r == null ? void 0 : r.pushManager) == null ? void 0 : s.getSubscription());
return i != null && i.endpoint ? n.some((p) => p.device_token === i.endpoint) : !1;
}

@@ -110,9 +111,12 @@ async function S(e) {

throw new Error("Push notifications are not supported in this browser");
const t = e.host || o, i = await u.getConfig(c(a({}, e), { baseURL: t })), r = await f({ path: e.serviceWorkerPath }), n = await r.pushManager.getSubscription();
n && await n.unsubscribe().catch(() => {
const t = e.host || o, n = await u.getConfig(c(a({}, e), { baseURL: t })), r = await g({ path: e.serviceWorkerPath });
if (!(r != null && r.pushManager))
throw new Error("Push notifications are not supported in this browser");
const i = await r.pushManager.getSubscription();
i && await i.unsubscribe().catch(() => {
});
const s = i.project.vapid_public_key.replace(/=/g, ""), h = await r.pushManager.subscribe({ userVisibleOnly: !0, applicationServerKey: s }).then((d) => d.toJSON());
if (!("endpoint" in h))
const s = n.project.vapid_public_key.replace(/=/g, ""), p = await r.pushManager.subscribe({ userVisibleOnly: !0, applicationServerKey: s }).then((d) => d.toJSON());
if (!("endpoint" in p))
throw new Error("Failed to subscribe to push notifications, browser did not return an subscription endpoint.");
await u.updateSubscription(c(a({}, e), { baseURL: t }), h);
await u.updateSubscription(c(a({}, e), { baseURL: t }), p);
}

@@ -127,5 +131,5 @@ async function T(e) {

x as prefetchConfig,
f as registerServiceWorker,
g as registerServiceWorker,
S as subscribe
};
//# sourceMappingURL=magicbell-webpush.esm.min.js.map
{
"name": "@magicbell/webpush",
"version": "1.4.0",
"version": "1.4.1",
"description": "MagicBell WebPush SDK",

@@ -5,0 +5,0 @@ "author": "MagicBell <bot@magicbell.io> (https://magicbell.com)",

@@ -159,3 +159,3 @@ type RequestOptions = {

const registration = await registerServiceWorker({ path: options.serviceWorkerPath });
const activeSubscription = await registration.pushManager.getSubscription();
const activeSubscription = await registration?.pushManager?.getSubscription();

@@ -178,2 +178,6 @@ if (!activeSubscription?.endpoint) return false;

if (!registration?.pushManager) {
throw new Error('Push notifications are not supported in this browser');
}
// remove active subscription if there's any

@@ -180,0 +184,0 @@ const activeSubscription = await registration.pushManager.getSubscription();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc