@magicbell/webpush
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,3 +6,4 @@ /** | ||
token: string; | ||
project: string; | ||
host?: string; | ||
}): Promise<void>; |
/** | ||
* @license @magicbell/webpush v0.1.0 | ||
* @license @magicbell/webpush v0.1.1 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) MagicBell Inc. and its affiliates. |
/** | ||
* @license @magicbell/webpush v0.1.0 | ||
* @license @magicbell/webpush v0.1.1 | ||
* | ||
@@ -42,4 +42,4 @@ * Copyright (c) MagicBell Inc. and its affiliates. | ||
const api = { | ||
async getConfig({ token, baseURL }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}`, { | ||
async getConfig({ token, project, baseURL }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&${project}`, { | ||
method: "GET", | ||
@@ -46,0 +46,0 @@ headers: { |
/** | ||
* @license @magicbell/webpush v0.1.0 | ||
* @license @magicbell/webpush v0.1.1 | ||
* | ||
@@ -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,"/"),r=atob(t),s=new Uint8Array(r.length);for(let n=0;n<r.length;++n)s[n]=r.charCodeAt(n);return s}const p={async getConfig({token:i,baseURL:e}){return fetch(`${e}/web_push_subscriptions?access_token=${i}`,{method:"GET",headers:{"Content-Type":"application/json",Accept:"application/json"}}).then(t=>t.json()).then(t=>o(a({},t.push_subscription),{baseURL:e,safariPushURL:`${e}/safari/push`}))},async updateSubscription({user:i,project:e,baseURL:t},r){const s={};return i.email&&(s["x-magicbell-user-email"]=i.email),i.external_id&&(s["x-magicbell-user-external-id"]=i.external_id),i.hmac&&(s["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},s),body:JSON.stringify({web_push_subscription:{data:r}})}).then(n=>n.json()).then(n=>n.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(r,s){e.pushNotification.requestPermission(i.safariPushURL,i.website_push_id,{authenticationToken:i.user.id},n=>{if(!n.deviceToken)return s(new Error("permission denied"));r({endpoint:n.deviceToken,keys:{websitePushID:i.website_push_id},platform:"safari"})})})}exports.subscribe=_; | ||
"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}&${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=_; | ||
//# sourceMappingURL=magicbell-webpush.cjs.min.js.map |
/** | ||
* @license @magicbell/webpush v0.1.0 | ||
* @license @magicbell/webpush v0.1.1 | ||
* | ||
@@ -40,4 +40,4 @@ * Copyright (c) MagicBell Inc. and its affiliates. | ||
const api = { | ||
async getConfig({ token, baseURL }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}`, { | ||
async getConfig({ token, project, baseURL }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&${project}`, { | ||
method: "GET", | ||
@@ -44,0 +44,0 @@ headers: { |
/** | ||
* @license @magicbell/webpush v0.1.0 | ||
* @license @magicbell/webpush v0.1.1 | ||
* | ||
@@ -23,10 +23,10 @@ * Copyright (c) MagicBell Inc. and its affiliates. | ||
function l(i) { | ||
const e = "=".repeat((4 - i.length % 4) % 4), n = (i + e).replace(/-/g, "+").replace(/_/g, "/"), r = atob(n), s = new Uint8Array(r.length); | ||
for (let t = 0; t < r.length; ++t) | ||
s[t] = r.charCodeAt(t); | ||
return s; | ||
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; | ||
} | ||
const u = { | ||
async getConfig({ token: i, baseURL: e }) { | ||
return fetch(`${e}/web_push_subscriptions?access_token=${i}`, { | ||
async getConfig({ token: i, project: e, baseURL: n }) { | ||
return fetch(`${n}/web_push_subscriptions?access_token=${i}&${e}`, { | ||
method: "GET", | ||
@@ -37,7 +37,7 @@ headers: { | ||
} | ||
}).then((n) => n.json()).then((n) => o(a({}, n.push_subscription), { baseURL: e, safariPushURL: `${e}/safari/push` })); | ||
}).then((t) => t.json()).then((t) => o(a({}, t.push_subscription), { baseURL: n, safariPushURL: `${n}/safari/push` })); | ||
}, | ||
async updateSubscription({ user: i, project: e, baseURL: n }, r) { | ||
const s = {}; | ||
return i.email && (s["x-magicbell-user-email"] = i.email), i.external_id && (s["x-magicbell-user-external-id"] = i.external_id), i.hmac && (s["x-magicbell-user-hmac"] = i.hmac), fetch(`${n}/web_push_subscriptions`, { | ||
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`, { | ||
method: "POST", | ||
@@ -48,9 +48,9 @@ headers: a({ | ||
"x-magicbell-api-key": e.api_key | ||
}, s), | ||
}, r), | ||
body: JSON.stringify({ | ||
web_push_subscription: { | ||
data: r | ||
data: t | ||
} | ||
}) | ||
}).then((t) => t.json()).then((t) => t.web_push_subscription); | ||
}).then((s) => s.json()).then((s) => s.web_push_subscription); | ||
} | ||
@@ -79,3 +79,3 @@ }; | ||
throw new Error("permission denied"); | ||
return new Promise(function(r, s) { | ||
return new Promise(function(t, r) { | ||
e.pushNotification.requestPermission( | ||
@@ -85,7 +85,7 @@ i.safariPushURL, | ||
{ authenticationToken: i.user.id }, | ||
(t) => { | ||
if (!t.deviceToken) | ||
return s(new Error("permission denied")); | ||
r({ | ||
endpoint: t.deviceToken, | ||
(s) => { | ||
if (!s.deviceToken) | ||
return r(new Error("permission denied")); | ||
t({ | ||
endpoint: s.deviceToken, | ||
keys: { websitePushID: i.website_push_id }, | ||
@@ -92,0 +92,0 @@ platform: "safari" |
{ | ||
"name": "@magicbell/webpush", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "MagicBell WebPush SDK", | ||
@@ -5,0 +5,0 @@ "author": "MagicBell <bot@magicbell.io> (https://magicbell.com)", |
@@ -24,4 +24,4 @@ function stringToUint8Array(plainString: string) { | ||
const api = { | ||
async getConfig({ token, baseURL }: { token: string; baseURL: string }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}`, { | ||
async getConfig({ token, project, baseURL }: { token: string; project: string; baseURL: string }) { | ||
return fetch(`${baseURL}/web_push_subscriptions?access_token=${token}&${project}`, { | ||
method: 'GET', | ||
@@ -65,3 +65,3 @@ headers: { | ||
*/ | ||
export async function subscribe(options: { token: string; host?: string }) { | ||
export async function subscribe(options: { token: string; project: string; host?: string }) { | ||
const config = await api.getConfig({ ...options, baseURL: options.host || location.origin }); | ||
@@ -68,0 +68,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50995
466