@therms/web-js
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,8 @@ | ||
# [2.3.0](https://bitbucket.org/thermsio/web-js/compare/v2.2.0...v2.3.0) (2022-07-13) | ||
### Features | ||
* export checkDevicePermissions ([c2b9a12](https://bitbucket.org/thermsio/web-js/commits/c2b9a1205940d02525d0c9d052545151b20c2109)) | ||
# [2.2.0](http://bitbucket.org/thermsio/web-js/compare/v2.1.2...v2.2.0) (2022-06-07) | ||
@@ -2,0 +9,0 @@ |
@@ -470,2 +470,51 @@ 'use strict'; | ||
}; | ||
async function hasPermission(permissionName, descriptor) { | ||
return new Promise((resolve) => { | ||
try { | ||
navigator.permissions | ||
.query(Object.assign({ name: permissionName }, descriptor)) | ||
.then(function (permission) { | ||
// console.log(`${permissionName}: ${permission.state}`) | ||
switch (permission.state) { | ||
case 'granted': | ||
return resolve(true); | ||
case 'denied': | ||
case 'prompt': | ||
return resolve(false); | ||
default: | ||
return resolve(false); | ||
} | ||
// permission.addEventListener('change', function (e) { | ||
// console.log( | ||
// `${permissionName} permission changed: ${permission.state}`, | ||
// ) | ||
// }) | ||
}); | ||
} | ||
catch (err) { | ||
console.error(`err checking permission "${permissionName}"`, err); | ||
resolve(false); | ||
} | ||
}); | ||
} | ||
const checkDevicePermissions = async () => { | ||
const results = await Promise.all([ | ||
hasPermission('background-sync'), | ||
hasPermission('camera'), | ||
hasPermission('geolocation'), | ||
hasPermission('microphone'), | ||
hasPermission('notifications', { userVisibleOnly: true }), | ||
hasPermission('push'), | ||
]); | ||
return { | ||
backgroundSync: results[0], | ||
bluetooth: false, | ||
camera: results[1], | ||
geolocation: results[2], | ||
backgroundGeolocation: false, | ||
microphone: results[3], | ||
nfc: false, | ||
notifications: results[4] && results[5], | ||
}; | ||
}; | ||
const getDevice = () => { | ||
@@ -487,3 +536,2 @@ const { browser, os, platform } = Browser__default["default"].parse(window?.navigator?.userAgent); | ||
type: 'browser', | ||
// todo: needs to be implemented - should look for sessionStorage, localStorage & cookie for a value | ||
uuid: '', | ||
@@ -637,2 +685,3 @@ }; | ||
exports.b2bComms = b2bComms; | ||
exports.checkDevicePermissions = checkDevicePermissions; | ||
exports.checkIsBrowserVisible = checkIsBrowserVisible; | ||
@@ -639,0 +688,0 @@ exports.dateWithinRange = dateWithinRange; |
@@ -12,3 +12,3 @@ export { checkIsBrowserVisible, onBrowserVisibilityChange } from './services/browser-visibility.js'; | ||
export { deepMerge } from './utils/deep-merge.js'; | ||
export { getDevice, isMobileDevice } from './utils/device.js'; | ||
export { checkDevicePermissions, getDevice, isMobileDevice } from './utils/device.js'; | ||
export { isEmail } from './utils/is-email.js'; | ||
@@ -15,0 +15,0 @@ export { isHexColor } from './utils/is-hex-color.js'; |
@@ -18,3 +18,13 @@ export declare type Device = { | ||
}; | ||
export declare const checkDevicePermissions: () => Promise<{ | ||
backgroundSync: boolean; | ||
bluetooth: boolean; | ||
camera: boolean; | ||
geolocation: boolean; | ||
backgroundGeolocation: boolean; | ||
microphone: boolean; | ||
nfc: boolean; | ||
notifications: boolean; | ||
}>; | ||
export declare const getDevice: () => Device; | ||
export declare const isMobileDevice: (userAgent: string) => boolean; |
@@ -13,2 +13,51 @@ import Browser from 'bowser'; | ||
}; | ||
async function hasPermission(permissionName, descriptor) { | ||
return new Promise((resolve) => { | ||
try { | ||
navigator.permissions | ||
.query(Object.assign({ name: permissionName }, descriptor)) | ||
.then(function (permission) { | ||
// console.log(`${permissionName}: ${permission.state}`) | ||
switch (permission.state) { | ||
case 'granted': | ||
return resolve(true); | ||
case 'denied': | ||
case 'prompt': | ||
return resolve(false); | ||
default: | ||
return resolve(false); | ||
} | ||
// permission.addEventListener('change', function (e) { | ||
// console.log( | ||
// `${permissionName} permission changed: ${permission.state}`, | ||
// ) | ||
// }) | ||
}); | ||
} | ||
catch (err) { | ||
console.error(`err checking permission "${permissionName}"`, err); | ||
resolve(false); | ||
} | ||
}); | ||
} | ||
const checkDevicePermissions = async () => { | ||
const results = await Promise.all([ | ||
hasPermission('background-sync'), | ||
hasPermission('camera'), | ||
hasPermission('geolocation'), | ||
hasPermission('microphone'), | ||
hasPermission('notifications', { userVisibleOnly: true }), | ||
hasPermission('push'), | ||
]); | ||
return { | ||
backgroundSync: results[0], | ||
bluetooth: false, | ||
camera: results[1], | ||
geolocation: results[2], | ||
backgroundGeolocation: false, | ||
microphone: results[3], | ||
nfc: false, | ||
notifications: results[4] && results[5], | ||
}; | ||
}; | ||
const getDevice = () => { | ||
@@ -30,3 +79,2 @@ const { browser, os, platform } = Browser.parse(window?.navigator?.userAgent); | ||
type: 'browser', | ||
// todo: needs to be implemented - should look for sessionStorage, localStorage & cookie for a value | ||
uuid: '', | ||
@@ -50,3 +98,3 @@ }; | ||
export { getDevice, isMobileDevice }; | ||
export { checkDevicePermissions, getDevice, isMobileDevice }; | ||
//# sourceMappingURL=device.js.map |
{ | ||
"name": "@therms/web-js", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Common web/JS tools & utilities", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104922
1571