UNPKG

nitroping

Version:

JavaScript/TypeScript SDK for NitroPing push notification service

2 lines 2.74 kB
import{NitroPingError as e}from"./types.js";function t(e){let t=`=`.repeat((4-e.length%4)%4),n=(e+t).replace(/-/g,`+`).replace(/_/g,`/`),r=globalThis.atob(n),i=new Uint8Array(r.length);for(let e=0;e<r.length;++e)i[e]=r.charCodeAt(e);return i}function n(){return`user-${Math.random().toString(36).substring(2,15)}-${Date.now()}`}function r(){return typeof globalThis<`u`&&`serviceWorker`in navigator&&`PushManager`in globalThis&&`Notification`in globalThis}function i(){return globalThis.isSecureContext===!0}function a(){return Notification.permission}async function o(t,n={}){let r=await fetch(t,{headers:{"Content-Type":`application/json`,...n.headers},...n});if(!r.ok){let t=await r.text(),n=`HTTP ${r.status}: ${r.statusText}`;try{let e=JSON.parse(t);n=e.message||n}catch{t&&(n=t)}throw new e(n,`API_ERROR`,r.status)}let i=r.headers.get(`content-type`);return i&&i.includes(`application/json`)?r.json():r.text()}function s(e,t){try{globalThis.localStorage.setItem(e,JSON.stringify(t))}catch(e){console.warn(`Failed to save to localStorage:`,e)}}function c(e){try{let t=globalThis.localStorage.getItem(e);return t?JSON.parse(t):null}catch(e){return console.warn(`Failed to read from localStorage:`,e),null}}function l(e){try{globalThis.localStorage.removeItem(e)}catch(e){console.warn(`Failed to remove from localStorage:`,e)}}function u(){let e=navigator.userAgent.toLowerCase();return e.includes(`chrome`)&&!e.includes(`edg`)?`chrome`:e.includes(`firefox`)?`firefox`:e.includes(`safari`)&&!e.includes(`chrome`)?`safari`:e.includes(`edg`)?`edge`:e.includes(`opera`)?`opera`:`unknown`}function d(){let e=navigator.userAgent.toLowerCase();return e.includes(`mac`)?`mac`:e.includes(`win`)?`windows`:e.includes(`linux`)?`linux`:e.includes(`android`)?`android`:e.includes(`iphone`)||e.includes(`ipad`)?`ios`:`unknown`}function f(){let e=navigator.userAgent,t=u();try{let n=null;switch(t){case`chrome`:n=e.match(/Chrome\/(\d+\.\d+\.\d+\.\d+)/);break;case`firefox`:n=e.match(/Firefox\/(\d+\.\d+)/);break;case`safari`:n=e.match(/Version\/(\d+\.\d+)/);break;case`edge`:n=e.match(/Edg\/(\d+\.\d+\.\d+\.\d+)/);break;case`opera`:n=e.match(/OPR\/(\d+\.\d+\.\d+\.\d+)/);break}return n?n[1]:`unknown`}catch{return`unknown`}}function p(e){switch(e.toLowerCase()){case`chrome`:return`CHROME`;case`firefox`:return`FIREFOX`;case`safari`:return`SAFARI`;case`edge`:return`EDGE`;case`opera`:return`OPERA`;default:return`WEB`}}export{o as apiRequest,u as detectBrowser,d as detectOS,n as generateUserId,f as getBrowserVersion,p as getCategoryFromBrowser,c as getLocalStorage,a as getNotificationPermission,r as isPushSupported,i as isSecureContext,l as removeLocalStorage,s as setLocalStorage,t as urlBase64ToUint8Array}; //# sourceMappingURL=utils.js.map