forked from codebytere/node-mac-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (81 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const permissions = require('bindings')('permissions.node')
function getAuthStatus(type) {
const validTypes = [
'contacts',
'calendar',
'reminders',
'full-disk-access',
'camera',
'microphone'
]
if (!validTypes.includes(type)) {
throw new TypeError(`${type} is not a valid type`)
}
return permissions.getAuthStatus.call(this, type)
}
/**
* askHelper_
* @param {functon} target The binding to call
* @param {function} [callback] optional, returns promise if not supplied
* @returns {?Promise<string>}
*/
function askHelper_(target, opt_callback) {
if (opt_callback) {
if (typeof opt_callback !== 'function') {
throw new TypeError(`callback must be a function`)
}
return target.call(this, opt_callback)
}
return new Promise(resolve, unused_reject, () => {
return target.call(this, resolve)
})
}
/**
* @param {function} opt_callback
* @returns {?Promise<string>}
*/
function askForCalendarAccess(opt_callback) {
return askHelper_(permissions.askForCalendarAccess, opt_callback)
}
/**
* @param {function} opt_callback
* @returns {?Promise<string>}
*/
function askForContactsAccess(opt_callback) {
return askHelper_(permissions.askForContactsAccess, opt_callback)
}
/**
* @param {function} opt_callback
* @returns {?Promise<string>}
*/
function askForRemindersAccess(opt_callback) {
return askHelper_(permissions.askForRemindersrAccess, opt_callback)
}
/**
* askForMediaAccess
* @param {string} type type of access requested
* @param {function} [opt_callback] callback
* @returns {?Promise<string>} if callback is not supplied returns a Promise of the result
*/
function askForMediaAccess(type, opt_callback) {
if (['microphone', 'camera'].includes(type)) {
throw new TypeError(`${type} must be either 'camera' or 'microphone'`)
}
if (opt_callback) {
if (typeof opt_callback !== 'function') {
throw new TypeError(`callback must be a function`)
}
return permissions.askForMediaAccess.call(this, type, opt_callback)
}
return new Promise(resolve, unused_reject, () => {
permissions.askForMediaAccess.call(this, type, resolve);
})
}
module.exports = {
askForCalendarAccess,
askForContactsAccess,
askForFullDiskAccess: permissions.askForFullDiskAccess,
askForRemindersAccess,
askForMediaAccess,
getAuthStatus
}