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 0.1.2 to 0.1.3

2

dist/index.js
/**
* @license @magicbell/webpush v0.1.2
* @license @magicbell/webpush v0.1.3
*

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

/**
* @license @magicbell/webpush v0.1.2
* @license @magicbell/webpush v0.1.3
*

@@ -51,17 +51,9 @@ * Copyright (c) MagicBell Inc. and its affiliates.

},
async updateSubscription({ user, project, baseURL }, subscription) {
const headers = {};
if (user.email)
headers["x-magicbell-user-email"] = user.email;
if (user.external_id)
headers["x-magicbell-user-external-id"] = user.external_id;
if (user.hmac)
headers["x-magicbell-user-hmac"] = user.hmac;
return fetch(`${baseURL}/web_push_subscriptions`, {
async updateSubscription({ token, project, baseURL }, subscription) {
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&project=${project}`, {
method: "POST",
headers: __spreadValues({
headers: {
"content-type": "application/json",
accept: "application/json",
"x-magicbell-api-key": project.api_key
}, headers),
accept: "application/json"
},
body: JSON.stringify({

@@ -76,3 +68,4 @@ web_push_subscription: {

async function subscribe(options) {
const config = await api.getConfig(__spreadProps(__spreadValues({}, options), { baseURL: options.host || location.origin }));
const requestOptions = __spreadProps(__spreadValues({}, options), { baseURL: options.host || location.origin });
const config = await api.getConfig(requestOptions);
if (!("PushManager" in window) && !("safari" in window)) {

@@ -84,3 +77,3 @@ throw new Error("Push notifications are not supported in this browser");

return;
await api.updateSubscription(config, subscription);
await api.updateSubscription(requestOptions, subscription);
}

@@ -87,0 +80,0 @@ async function createPushSubscription(config) {

/**
* @license @magicbell/webpush v0.1.2
* @license @magicbell/webpush v0.1.3
*

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

"use strict";var h=Object.defineProperty,d=Object.defineProperties;var b=Object.getOwnPropertyDescriptors;var c=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,l=Object.prototype.propertyIsEnumerable;var u=(i,e,t)=>e in i?h(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,a=(i,e)=>{for(var t in e||(e={}))w.call(e,t)&&u(i,t,e[t]);if(c)for(var t of c(e))l.call(e,t)&&u(i,t,e[t]);return i},o=(i,e)=>d(i,b(e));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});function f(i){const e="=".repeat((4-i.length%4)%4),t=(i+e).replace(/-/g,"+").replace(/_/g,"/"),n=atob(t),r=new Uint8Array(n.length);for(let s=0;s<n.length;++s)r[s]=n.charCodeAt(s);return r}const p={async getConfig({token:i,project:e,baseURL:t}){return fetch(`${t}/web_push_subscriptions?access_token=${i}&project=${e}`,{method:"GET",headers:{"Content-Type":"application/json",Accept:"application/json"}}).then(n=>n.json()).then(n=>o(a({},n.push_subscription),{baseURL:t,safariPushURL:`${t}/safari/push`}))},async updateSubscription({user:i,project:e,baseURL:t},n){const r={};return i.email&&(r["x-magicbell-user-email"]=i.email),i.external_id&&(r["x-magicbell-user-external-id"]=i.external_id),i.hmac&&(r["x-magicbell-user-hmac"]=i.hmac),fetch(`${t}/web_push_subscriptions`,{method:"POST",headers:a({"content-type":"application/json",accept:"application/json","x-magicbell-api-key":e.api_key},r),body:JSON.stringify({web_push_subscription:{data:n}})}).then(s=>s.json()).then(s=>s.web_push_subscription)}};async function _(i){const e=await p.getConfig(o(a({},i),{baseURL:i.host||location.origin}));if(!("PushManager"in window)&&!("safari"in window))throw new Error("Push notifications are not supported in this browser");const t="PushManager"in window?await g(e):await m(e);"endpoint"in t&&await p.updateSubscription(e,t)}async function g(i){await navigator.serviceWorker.register("/sw.js");const e=await navigator.serviceWorker.ready,t=f(i.project.vapid_public_key);return(await e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:t})).toJSON()}async function m(i){const e="safari"in window?window.safari:void 0;if(!e)throw new Error("This is not Safari");const t=e.pushNotification.permission(i.website_push_id);if(t.permission==="granted")return t;if(t.permission==="denied")throw new Error("permission denied");return new Promise(function(n,r){e.pushNotification.requestPermission(i.safariPushURL,i.website_push_id,{authenticationToken:i.user.id},s=>{if(!s.deviceToken)return r(new Error("permission denied"));n({endpoint:s.deviceToken,keys:{websitePushID:i.website_push_id},platform:"safari"})})})}exports.subscribe=_;
"use strict";var h=Object.defineProperty,d=Object.defineProperties;var w=Object.getOwnPropertyDescriptors;var c=Object.getOwnPropertySymbols;var b=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable;var u=(i,e,t)=>e in i?h(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,o=(i,e)=>{for(var t in e||(e={}))b.call(e,t)&&u(i,t,e[t]);if(c)for(var t of c(e))f.call(e,t)&&u(i,t,e[t]);return i},a=(i,e)=>d(i,w(e));Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});function _(i){const e="=".repeat((4-i.length%4)%4),t=(i+e).replace(/-/g,"+").replace(/_/g,"/"),s=atob(t),n=new Uint8Array(s.length);for(let r=0;r<s.length;++r)n[r]=s.charCodeAt(r);return n}const p={async getConfig({token:i,project:e,baseURL:t}){return fetch(`${t}/web_push_subscriptions?access_token=${i}&project=${e}`,{method:"GET",headers:{"Content-Type":"application/json",Accept:"application/json"}}).then(s=>s.json()).then(s=>a(o({},s.push_subscription),{baseURL:t,safariPushURL:`${t}/safari/push`}))},async updateSubscription({token:i,project:e,baseURL:t},s){return fetch(`${t}/web_push_subscriptions?access_token=${i}&project=${e}`,{method:"POST",headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({web_push_subscription:{data:s}})}).then(n=>n.json()).then(n=>n.web_push_subscription)}};async function l(i){const e=a(o({},i),{baseURL:i.host||location.origin}),t=await p.getConfig(e);if(!("PushManager"in window)&&!("safari"in window))throw new Error("Push notifications are not supported in this browser");const s="PushManager"in window?await g(t):await y(t);"endpoint"in s&&await p.updateSubscription(e,s)}async function g(i){await navigator.serviceWorker.register("/sw.js");const e=await navigator.serviceWorker.ready,t=_(i.project.vapid_public_key);return(await e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:t})).toJSON()}async function y(i){const e="safari"in window?window.safari:void 0;if(!e)throw new Error("This is not Safari");const t=e.pushNotification.permission(i.website_push_id);if(t.permission==="granted")return t;if(t.permission==="denied")throw new Error("permission denied");return new Promise(function(s,n){e.pushNotification.requestPermission(i.safariPushURL,i.website_push_id,{authenticationToken:i.user.id},r=>{if(!r.deviceToken)return n(new Error("permission denied"));s({endpoint:r.deviceToken,keys:{websitePushID:i.website_push_id},platform:"safari"})})})}exports.subscribe=l;
//# sourceMappingURL=magicbell-webpush.cjs.min.js.map
/**
* @license @magicbell/webpush v0.1.2
* @license @magicbell/webpush v0.1.3
*

@@ -49,17 +49,9 @@ * Copyright (c) MagicBell Inc. and its affiliates.

},
async updateSubscription({ user, project, baseURL }, subscription) {
const headers = {};
if (user.email)
headers["x-magicbell-user-email"] = user.email;
if (user.external_id)
headers["x-magicbell-user-external-id"] = user.external_id;
if (user.hmac)
headers["x-magicbell-user-hmac"] = user.hmac;
return fetch(`${baseURL}/web_push_subscriptions`, {
async updateSubscription({ token, project, baseURL }, subscription) {
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&project=${project}`, {
method: "POST",
headers: __spreadValues({
headers: {
"content-type": "application/json",
accept: "application/json",
"x-magicbell-api-key": project.api_key
}, headers),
accept: "application/json"
},
body: JSON.stringify({

@@ -74,3 +66,4 @@ web_push_subscription: {

async function subscribe(options) {
const config = await api.getConfig(__spreadProps(__spreadValues({}, options), { baseURL: options.host || location.origin }));
const requestOptions = __spreadProps(__spreadValues({}, options), { baseURL: options.host || location.origin });
const config = await api.getConfig(requestOptions);
if (!("PushManager" in window) && !("safari" in window)) {

@@ -82,3 +75,3 @@ throw new Error("Push notifications are not supported in this browser");

return;
await api.updateSubscription(config, subscription);
await api.updateSubscription(requestOptions, subscription);
}

@@ -85,0 +78,0 @@ async function createPushSubscription(config) {

/**
* @license @magicbell/webpush v0.1.2
* @license @magicbell/webpush v0.1.3
*

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

var h = Object.defineProperty, d = Object.defineProperties;
var w = Object.getOwnPropertyDescriptors;
var h = Object.defineProperty, w = Object.defineProperties;
var d = Object.getOwnPropertyDescriptors;
var c = Object.getOwnPropertySymbols;
var b = Object.prototype.hasOwnProperty, f = Object.prototype.propertyIsEnumerable;
var p = (i, e, n) => e in i ? h(i, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[e] = n, a = (i, e) => {
for (var n in e || (e = {}))
b.call(e, n) && p(i, n, e[n]);
var p = (i, e, t) => e in i ? h(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t, o = (i, e) => {
for (var t in e || (e = {}))
b.call(e, t) && p(i, t, e[t]);
if (c)
for (var n of c(e))
f.call(e, n) && p(i, n, e[n]);
for (var t of c(e))
f.call(e, t) && p(i, t, e[t]);
return i;
}, o = (i, e) => d(i, w(e));
function l(i) {
const e = "=".repeat((4 - i.length % 4) % 4), n = (i + e).replace(/-/g, "+").replace(/_/g, "/"), t = atob(n), r = new Uint8Array(t.length);
for (let s = 0; s < t.length; ++s)
r[s] = t.charCodeAt(s);
return r;
}, a = (i, e) => w(i, d(e));
function _(i) {
const e = "=".repeat((4 - i.length % 4) % 4), t = (i + e).replace(/-/g, "+").replace(/_/g, "/"), n = atob(t), s = new Uint8Array(n.length);
for (let r = 0; r < n.length; ++r)
s[r] = n.charCodeAt(r);
return s;
}
const u = {
async getConfig({ token: i, project: e, baseURL: n }) {
return fetch(`${n}/web_push_subscriptions?access_token=${i}&project=${e}`, {
async getConfig({ token: i, project: e, baseURL: t }) {
return fetch(`${t}/web_push_subscriptions?access_token=${i}&project=${e}`, {
method: "GET",

@@ -37,16 +37,14 @@ headers: {

}
}).then((t) => t.json()).then((t) => o(a({}, t.push_subscription), { baseURL: n, safariPushURL: `${n}/safari/push` }));
}).then((n) => n.json()).then((n) => a(o({}, n.push_subscription), { baseURL: t, safariPushURL: `${t}/safari/push` }));
},
async updateSubscription({ user: i, project: e, baseURL: n }, t) {
const r = {};
return i.email && (r["x-magicbell-user-email"] = i.email), i.external_id && (r["x-magicbell-user-external-id"] = i.external_id), i.hmac && (r["x-magicbell-user-hmac"] = i.hmac), fetch(`${n}/web_push_subscriptions`, {
async updateSubscription({ token: i, project: e, baseURL: t }, n) {
return fetch(`${t}/web_push_subscriptions?access_token=${i}&project=${e}`, {
method: "POST",
headers: a({
headers: {
"content-type": "application/json",
accept: "application/json",
"x-magicbell-api-key": e.api_key
}, r),
accept: "application/json"
},
body: JSON.stringify({
web_push_subscription: {
data: t
data: n
}

@@ -57,24 +55,24 @@ })

};
async function y(i) {
const e = await u.getConfig(o(a({}, i), { baseURL: i.host || location.origin }));
async function P(i) {
const e = a(o({}, i), { baseURL: i.host || location.origin }), t = await u.getConfig(e);
if (!("PushManager" in window) && !("safari" in window))
throw new Error("Push notifications are not supported in this browser");
const n = "PushManager" in window ? await _(e) : await g(e);
const n = "PushManager" in window ? await g(t) : await y(t);
"endpoint" in n && await u.updateSubscription(e, n);
}
async function _(i) {
async function g(i) {
await navigator.serviceWorker.register("/sw.js");
const e = await navigator.serviceWorker.ready, n = l(i.project.vapid_public_key);
return (await e.pushManager.subscribe({ userVisibleOnly: !0, applicationServerKey: n })).toJSON();
const e = await navigator.serviceWorker.ready, t = _(i.project.vapid_public_key);
return (await e.pushManager.subscribe({ userVisibleOnly: !0, applicationServerKey: t })).toJSON();
}
async function g(i) {
async function y(i) {
const e = "safari" in window ? window.safari : void 0;
if (!e)
throw new Error("This is not Safari");
const n = e.pushNotification.permission(i.website_push_id);
if (n.permission === "granted")
return n;
if (n.permission === "denied")
const t = e.pushNotification.permission(i.website_push_id);
if (t.permission === "granted")
return t;
if (t.permission === "denied")
throw new Error("permission denied");
return new Promise(function(t, r) {
return new Promise(function(n, s) {
e.pushNotification.requestPermission(

@@ -84,7 +82,7 @@ i.safariPushURL,

{ authenticationToken: i.user.id },
(s) => {
if (!s.deviceToken)
return r(new Error("permission denied"));
t({
endpoint: s.deviceToken,
(r) => {
if (!r.deviceToken)
return s(new Error("permission denied"));
n({
endpoint: r.deviceToken,
keys: { websitePushID: i.website_push_id },

@@ -98,4 +96,4 @@ platform: "safari"

export {
y as subscribe
P as subscribe
};
//# sourceMappingURL=magicbell-webpush.esm.min.js.map
{
"name": "@magicbell/webpush",
"version": "0.1.2",
"version": "0.1.3",
"description": "MagicBell WebPush SDK",

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

@@ -15,2 +15,8 @@ function stringToUint8Array(plainString: string) {

type RequestOptions = {
token: string;
project: string;
baseURL: string;
};
type Config = {

@@ -25,3 +31,3 @@ user: { id: string; email?: string | null; external_id?: string | null; hmac?: string | null };

const api = {
async getConfig({ token, project, baseURL }: { token: string; project: string; baseURL: string }) {
async getConfig({ token, project, baseURL }: RequestOptions) {
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&project=${project}`, {

@@ -38,9 +44,4 @@ method: 'GET',

async updateSubscription({ user, project, baseURL }: Config, subscription: PushSubscriptionJSON) {
const headers: Record<string, string> = {};
if (user.email) headers['x-magicbell-user-email'] = user.email;
if (user.external_id) headers['x-magicbell-user-external-id'] = user.external_id;
if (user.hmac) headers['x-magicbell-user-hmac'] = user.hmac;
return fetch(`${baseURL}/web_push_subscriptions`, {
async updateSubscription({ token, project, baseURL }: RequestOptions, subscription: PushSubscriptionJSON) {
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&project=${project}`, {
method: 'POST',

@@ -50,4 +51,2 @@ headers: {

accept: 'application/json',
'x-magicbell-api-key': project.api_key,
...headers,
},

@@ -69,3 +68,4 @@ body: JSON.stringify({

export async function subscribe(options: { token: string; project: string; host?: string }) {
const config = await api.getConfig({ ...options, baseURL: options.host || location.origin });
const requestOptions = { ...options, baseURL: options.host || location.origin };
const config = await api.getConfig(requestOptions);

@@ -81,3 +81,3 @@ if (!('PushManager' in window) && !('safari' in window)) {

if (!('endpoint' in subscription)) return;
await api.updateSubscription(config, subscription);
await api.updateSubscription(requestOptions, subscription);
}

@@ -84,0 +84,0 @@

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