@octane-xplat/contacts 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -15
- package/package.json +87 -94
- package/src/contacts.macos.ts +338 -13
- package/src/contacts.ts +3 -0
- package/src/contacts.web.ts +4 -1
- package/src/contacts.windows.ts +3 -1
- package/src/types.ts +50 -0
- package/types/generated/contacts.d.ts +1 -0
- package/types/generated/contacts.macos.d.ts +1 -0
- package/types/generated/contacts.web.d.ts +1 -0
- package/types/generated/contacts.windows.d.ts +1 -0
- package/types/generated/types.d.ts +50 -0
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -1,27 +1,38 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @octane-xplat/contacts
|
|
2
|
+
|
|
3
|
+
Read the device address book on iOS and Android, or let someone select contacts
|
|
4
|
+
in a browser that supports the Contact Picker API. Native apps can also edit
|
|
5
|
+
contacts. Browser access is read-only and limited to the person's selection.
|
|
6
|
+
|
|
7
|
+
## Read selected contact fields
|
|
8
|
+
|
|
9
|
+
Install in an existing Xplat app:
|
|
2
10
|
|
|
3
11
|
```sh
|
|
4
|
-
|
|
12
|
+
bun add @octane-xplat/contacts
|
|
5
13
|
```
|
|
6
14
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
15
|
+
Configure the native contacts permission and usage description before running.
|
|
16
|
+
Call this function from a **Choose contacts** action; browsers require a user
|
|
17
|
+
action to open their picker:
|
|
10
18
|
|
|
11
19
|
```ts
|
|
12
20
|
import { contacts } from '@octane-xplat/contacts'
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
await contacts.add({ firstName: 'Ada', emails: [{ label: 'work', email: 'ada@example.com' }] })
|
|
22
|
+
export async function chooseContacts() {
|
|
23
|
+
if (!contacts.supported || (await contacts.ensure()) !== 'granted') return []
|
|
24
|
+
return contacts.getAll({ fields: ['name', 'phoneNumbers'], sort: 'lastName' })
|
|
18
25
|
}
|
|
19
26
|
```
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
On native, the result contains matching address-book entries. In a supported
|
|
29
|
+
browser, it contains only the contacts the person picked. Show an empty state
|
|
30
|
+
when none are returned and distinguish denied access in your screen. Do not
|
|
31
|
+
log contact details while testing; verify a displayed count or a test contact.
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
ID and name lookups and writes are native-only. Android ID lookup reads the
|
|
34
|
+
store and filters it, so avoid repeated lookups for large address books.
|
|
35
|
+
macOS and Windows report unsupported. See the
|
|
36
|
+
[public contract](https://github.com/octane-xplat/octane-xplat/blob/main/packages/contacts/src/types.ts)
|
|
37
|
+
and [device capability handling](https://octane-xplat.goddardai.org/platform-services#the-shared-service-shape)
|
|
38
|
+
for integration boundaries.
|
package/package.json
CHANGED
|
@@ -1,95 +1,88 @@
|
|
|
1
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
|
-
"scripts": {
|
|
91
|
-
"typegen": "tsrx-typegen --target octane",
|
|
92
|
-
"pack:check": "tsrx-typegen --pack-check",
|
|
93
|
-
"typecheck": "node ../../scripts/typecheck.ts"
|
|
94
|
-
}
|
|
95
|
-
}
|
|
2
|
+
"name": "@octane-xplat/contacts",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "Device address book for Octane xplat — contact reads and writes on iOS/Android, Contact Picker API on web",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"cross-platform",
|
|
8
|
+
"nativescript",
|
|
9
|
+
"octane",
|
|
10
|
+
"xplat",
|
|
11
|
+
"contacts",
|
|
12
|
+
"address-book"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/contacts",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
19
|
+
"directory": "packages/contacts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"platforms",
|
|
24
|
+
"types",
|
|
25
|
+
"!src/**/*.test.*",
|
|
26
|
+
"!src/**/*.spec.*"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./types/generated/index.d.ts",
|
|
32
|
+
"macos": "./src/index.macos.ts",
|
|
33
|
+
"web": "./src/index.web.ts",
|
|
34
|
+
"native": "./src/index.ts",
|
|
35
|
+
"default": "./src/index.ts"
|
|
36
|
+
},
|
|
37
|
+
"./*": {
|
|
38
|
+
"types": "./types/generated/*.d.ts",
|
|
39
|
+
"default": "./src/*"
|
|
40
|
+
},
|
|
41
|
+
"./*.ts": {
|
|
42
|
+
"types": "./types/generated/*.d.ts",
|
|
43
|
+
"default": "./src/*.ts"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"typegen": "tsrx-typegen --target octane",
|
|
51
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
52
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
53
|
+
"typecheck": "node ../../scripts/typecheck.ts",
|
|
54
|
+
"test": "node --test tests/*.test.ts"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@nativescript-community/octane": "0.2.5",
|
|
58
|
+
"@nativescript/core": "9.1.3",
|
|
59
|
+
"@nativescript/types": "9.1.4",
|
|
60
|
+
"@tsrx/typescript-plugin": "0.4.11",
|
|
61
|
+
"octane": "0.16.3",
|
|
62
|
+
"tsrx-typegen": "0.5.0",
|
|
63
|
+
"typescript": "5.9.3"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
67
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
68
|
+
"octane": ">=0.6.3 <1"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"@nativescript-community/octane": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"@nativescript/core": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"nativescript": {
|
|
79
|
+
"platforms": {
|
|
80
|
+
"android": "9.1.0",
|
|
81
|
+
"ios": "9.1.0"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@nativescript/contacts": "3.0.0",
|
|
86
|
+
"@octane-xplat/platform": "0.19.0"
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/contacts.macos.ts
CHANGED
|
@@ -1,19 +1,344 @@
|
|
|
1
|
-
// Contacts —
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// Contacts — AppKit leaf over CNContactStore. The Contacts framework is
|
|
2
|
+
// bridgeable through the JSC host; semantics mirror the iOS leaf.
|
|
3
|
+
// CNContactNoteKey requires a contacts-notes entitlement even on macOS —
|
|
4
|
+
// it is excluded from fetch keys and `note` stays absent unless the store
|
|
5
|
+
// reports the key available.
|
|
6
|
+
import {
|
|
7
|
+
CNContact,
|
|
8
|
+
CNContactFetchRequest,
|
|
9
|
+
CNContactIdentifierKey,
|
|
10
|
+
CNContactEmailAddressesKey,
|
|
11
|
+
CNContactFamilyNameKey,
|
|
12
|
+
CNContactGivenNameKey,
|
|
13
|
+
CNContactImageDataKey,
|
|
14
|
+
CNContactJobTitleKey,
|
|
15
|
+
CNContactMiddleNameKey,
|
|
16
|
+
CNContactNicknameKey,
|
|
17
|
+
CNContactNoteKey,
|
|
18
|
+
CNContactOrganizationNameKey,
|
|
19
|
+
CNContactPhoneNumbersKey,
|
|
20
|
+
CNContactPostalAddressesKey,
|
|
21
|
+
CNContactStore,
|
|
22
|
+
CNContactUrlAddressesKey,
|
|
23
|
+
CNEntityType,
|
|
24
|
+
CNLabeledValue,
|
|
25
|
+
CNMutableContact,
|
|
26
|
+
CNPhoneNumber,
|
|
27
|
+
CNSaveRequest,
|
|
28
|
+
} from '@octane-xplat/platform/macos'
|
|
29
|
+
import type {
|
|
30
|
+
Contact,
|
|
31
|
+
ContactAddress,
|
|
32
|
+
ContactField,
|
|
33
|
+
Contacts,
|
|
34
|
+
GetContactsOptions,
|
|
35
|
+
NewContact,
|
|
36
|
+
PermissionResult,
|
|
37
|
+
} from './types'
|
|
4
38
|
|
|
5
|
-
|
|
39
|
+
let store: any = null
|
|
40
|
+
const contactStore = () => {
|
|
41
|
+
if (store) {
|
|
42
|
+
return store
|
|
43
|
+
}
|
|
6
44
|
|
|
45
|
+
try {
|
|
46
|
+
if (typeof CNContactStore?.authorizationStatusForEntityType !== 'function') {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
return (store = CNContactStore.new())
|
|
50
|
+
} catch {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// CNAuthorizationStatus: 0 notDetermined 1 restricted 2 denied 3 authorized
|
|
56
|
+
const rawStatus = () =>
|
|
57
|
+
Number(CNContactStore.authorizationStatusForEntityType(CNEntityType.CNEntityTypeContacts ?? 0))
|
|
58
|
+
|
|
59
|
+
function ensureAccess(): Promise<PermissionResult> {
|
|
60
|
+
const s = contactStore()
|
|
61
|
+
if (!s) {
|
|
62
|
+
return Promise.resolve('unsupported')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const status = rawStatus()
|
|
66
|
+
if (status === 3) {
|
|
67
|
+
return Promise.resolve('granted')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (status === 1 || status === 2) {
|
|
71
|
+
return Promise.resolve('denied')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
s.requestAccessForEntityTypeCompletionHandler(CNEntityType.CNEntityTypeContacts ?? 0, (granted: boolean) =>
|
|
76
|
+
resolve(granted ? 'granted' : 'denied'),
|
|
77
|
+
)
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const BASE_KEYS = [
|
|
82
|
+
CNContactIdentifierKey,
|
|
83
|
+
CNContactGivenNameKey,
|
|
84
|
+
CNContactMiddleNameKey,
|
|
85
|
+
CNContactFamilyNameKey,
|
|
86
|
+
CNContactNicknameKey,
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
const FIELD_KEYS: Record<Exclude<ContactField, 'name'>, string[]> = {
|
|
90
|
+
emails: [CNContactEmailAddressesKey],
|
|
91
|
+
phoneNumbers: [CNContactPhoneNumbersKey],
|
|
92
|
+
addresses: [CNContactPostalAddressesKey],
|
|
93
|
+
urls: [CNContactUrlAddressesKey],
|
|
94
|
+
company: [CNContactOrganizationNameKey, CNContactJobTitleKey],
|
|
95
|
+
note: [], // entitlement-gated on macOS too — read only when the key is available
|
|
96
|
+
image: [CNContactImageDataKey],
|
|
97
|
+
nickname: [],
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function fetchKeys(fields?: ContactField[]): string[] {
|
|
101
|
+
const keys = [...BASE_KEYS]
|
|
102
|
+
for (const field of fields ?? (Object.keys(FIELD_KEYS) as ContactField[])) {
|
|
103
|
+
for (const key of FIELD_KEYS[field as keyof typeof FIELD_KEYS] ?? []) {
|
|
104
|
+
if (!keys.includes(key)) {
|
|
105
|
+
keys.push(key)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return keys
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const nsarray = (list: any): any[] => {
|
|
113
|
+
if (!list) {
|
|
114
|
+
return []
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (Array.isArray(list)) {
|
|
118
|
+
return list
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const out: any[] = []
|
|
122
|
+
for (let i = 0, count = Number(list.count ?? 0); i < count; i++) {
|
|
123
|
+
out.push(list.objectAtIndex(i))
|
|
124
|
+
}
|
|
125
|
+
return out
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const labeled = (list: any, value: (entry: any) => unknown) =>
|
|
129
|
+
nsarray(list).map((entry) => ({ label: entry?.label ? String(entry.label) : undefined, value: value(entry) }))
|
|
130
|
+
|
|
131
|
+
function asContact(native: any): Contact {
|
|
132
|
+
const firstName = native?.givenName ? String(native.givenName) : undefined
|
|
133
|
+
const lastName = native?.familyName ? String(native.familyName) : undefined
|
|
134
|
+
const name =
|
|
135
|
+
native?.name && typeof native.name === 'string'
|
|
136
|
+
? native.name
|
|
137
|
+
: [firstName, lastName].filter(Boolean).join(' ') ||
|
|
138
|
+
(native?.organizationName ? String(native.organizationName) : '')
|
|
139
|
+
|
|
140
|
+
const contact: Contact = {
|
|
141
|
+
id: String(native?.identifier ?? ''),
|
|
142
|
+
name,
|
|
143
|
+
firstName,
|
|
144
|
+
middleName: native?.middleName ? String(native.middleName) : undefined,
|
|
145
|
+
lastName,
|
|
146
|
+
nickname: native?.nickname ? String(native.nickname) : undefined,
|
|
147
|
+
emails: labeled(native?.emailAddresses, (e) => String(e?.value ?? '')).map(
|
|
148
|
+
({ label, value }) => ({ label, email: value as string }),
|
|
149
|
+
),
|
|
150
|
+
phoneNumbers: labeled(native?.phoneNumbers, (e) => String(e?.value?.stringValue ?? '')).map(
|
|
151
|
+
({ label, value }) => ({ label, number: value as string }),
|
|
152
|
+
),
|
|
153
|
+
addresses: labeled(
|
|
154
|
+
native?.postalAddresses,
|
|
155
|
+
(e): ContactAddress => ({
|
|
156
|
+
street: e?.value?.street ? String(e.value.street) : undefined,
|
|
157
|
+
city: e?.value?.city ? String(e.value.city) : undefined,
|
|
158
|
+
region: e?.value?.state ? String(e.value.state) : undefined,
|
|
159
|
+
postalCode: e?.value?.postalCode ? String(e.value.postalCode) : undefined,
|
|
160
|
+
country: e?.value?.country ? String(e.value.country) : undefined,
|
|
161
|
+
}),
|
|
162
|
+
).map(({ label, value }) => ({ label, ...(value as ContactAddress) })),
|
|
163
|
+
urls: labeled(native?.urlAddresses, (e) => String(e?.value ?? '')).map(
|
|
164
|
+
({ label, value }) => ({ label, url: value as string }),
|
|
165
|
+
),
|
|
166
|
+
company: native?.organizationName ? String(native.organizationName) : undefined,
|
|
167
|
+
jobTitle: native?.jobTitle ? String(native.jobTitle) : undefined,
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// The notes entitlement is a distribution choice; read it only when the
|
|
171
|
+
// fetched keys already carry it.
|
|
172
|
+
try {
|
|
173
|
+
if (native?.isKeyAvailable?.(CNContactNoteKey) && native?.note) {
|
|
174
|
+
contact.note = String(native.note)
|
|
175
|
+
}
|
|
176
|
+
} catch {
|
|
177
|
+
// A store without the notes entitlement leaves the field absent.
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
if (native?.imageDataAvailable) {
|
|
182
|
+
const data = native?.imageData
|
|
183
|
+
const base64 = data?.base64EncodedStringWithOptions?.(0)
|
|
184
|
+
if (base64) {
|
|
185
|
+
contact.image = { data: String(base64) }
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} catch {
|
|
189
|
+
// Unfetched or unreadable image data is omitted, never fatal.
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return contact
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function applySort(list: Contact[], sort?: GetContactsOptions['sort']): Contact[] {
|
|
196
|
+
if (!sort || sort === 'none') {
|
|
197
|
+
return list
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const key = sort === 'firstName' ? 'firstName' : 'lastName'
|
|
201
|
+
return [...list].sort((a, b) => (a[key] ?? a.name).localeCompare(b[key] ?? b.name))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function applyPage(list: Contact[], options?: GetContactsOptions): Contact[] {
|
|
205
|
+
const start = options?.pageOffset ?? 0
|
|
206
|
+
const size = options?.pageSize
|
|
207
|
+
return size === undefined ? list.slice(start) : list.slice(start, start + size)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function requireStore(): any {
|
|
211
|
+
const s = contactStore()
|
|
212
|
+
if (!s) {
|
|
213
|
+
throw new Error('contacts: the Contacts store is unavailable on this host')
|
|
214
|
+
}
|
|
215
|
+
return s
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function applyNewContact(target: any, source: NewContact): void {
|
|
219
|
+
target.givenName = source.firstName ?? ''
|
|
220
|
+
target.middleName = source.middleName ?? ''
|
|
221
|
+
target.familyName = source.lastName ?? ''
|
|
222
|
+
target.nickname = source.nickname ?? ''
|
|
223
|
+
target.organizationName = source.company ?? ''
|
|
224
|
+
target.jobTitle = source.jobTitle ?? ''
|
|
225
|
+
target.emailAddresses = (source.emails ?? []).map((e) =>
|
|
226
|
+
CNLabeledValue.labeledValueWithLabelValue(e.label ?? null, e.email),
|
|
227
|
+
)
|
|
228
|
+
target.phoneNumbers = (source.phoneNumbers ?? []).map((p) =>
|
|
229
|
+
CNLabeledValue.labeledValueWithLabelValue(
|
|
230
|
+
p.label ?? null,
|
|
231
|
+
CNPhoneNumber.phoneNumberWithStringValue(p.number),
|
|
232
|
+
),
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Contacts service — system address book. */
|
|
7
237
|
export const contacts: Contacts = {
|
|
8
|
-
supported
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
238
|
+
get supported() {
|
|
239
|
+
return contactStore() !== null
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
ensure(): Promise<PermissionResult> {
|
|
243
|
+
return ensureAccess()
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
async getAll(options?: GetContactsOptions): Promise<Contact[]> {
|
|
247
|
+
const s = requireStore()
|
|
248
|
+
const request = CNContactFetchRequest.alloc().initWithKeysToFetch(fetchKeys(options?.fields))
|
|
249
|
+
const out: Contact[] = []
|
|
250
|
+
if (
|
|
251
|
+
!s.enumerateContactsWithFetchRequestErrorUsingBlock(request, null, (contact: any) => {
|
|
252
|
+
out.push(asContact(contact))
|
|
253
|
+
})
|
|
254
|
+
) {
|
|
255
|
+
throw new Error('contacts.getAll: the store refused the fetch request')
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return applyPage(applySort(out, options?.sort), options)
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
async getByName(predicate: string, fields?: ContactField[]): Promise<Contact[]> {
|
|
262
|
+
const s = requireStore()
|
|
263
|
+
const list = s.unifiedContactsMatchingPredicateKeysToFetchError(
|
|
264
|
+
CNContact.predicateForContactsMatchingName(predicate),
|
|
265
|
+
fetchKeys(fields),
|
|
266
|
+
null,
|
|
267
|
+
)
|
|
268
|
+
return nsarray(list).map(asContact)
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
async getById(id: string, fields?: ContactField[]): Promise<Contact | null> {
|
|
272
|
+
const s = requireStore()
|
|
273
|
+
const native = s.unifiedContactWithIdentifierKeysToFetchError(id, fetchKeys(fields), null)
|
|
274
|
+
return native ? asContact(native) : null
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
async add(contact: NewContact): Promise<string> {
|
|
278
|
+
const s = requireStore()
|
|
279
|
+
const native = CNMutableContact.new()
|
|
280
|
+
applyNewContact(native, contact)
|
|
281
|
+
const request = CNSaveRequest.new()
|
|
282
|
+
request.addContactToContainerWithIdentifier(native, null)
|
|
283
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
284
|
+
throw new Error('contacts.add: the store refused the save request')
|
|
285
|
+
}
|
|
286
|
+
return String(native.identifier ?? '')
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
async update(id: string, contact: NewContact): Promise<string> {
|
|
290
|
+
const s = requireStore()
|
|
291
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(
|
|
292
|
+
id,
|
|
293
|
+
fetchKeys(),
|
|
294
|
+
null,
|
|
295
|
+
)
|
|
296
|
+
if (!existing) {
|
|
297
|
+
throw new Error(`contacts.update: no contact with id ${id}`)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const native = existing.mutableCopy()
|
|
301
|
+
applyNewContact(native, contact)
|
|
302
|
+
const request = CNSaveRequest.new()
|
|
303
|
+
request.updateContact(native)
|
|
304
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
305
|
+
throw new Error('contacts.update: the store refused the save request')
|
|
306
|
+
}
|
|
307
|
+
return id
|
|
308
|
+
},
|
|
309
|
+
|
|
310
|
+
async remove(id: string): Promise<void> {
|
|
311
|
+
const s = requireStore()
|
|
312
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(id, [CNContactIdentifierKey], null)
|
|
313
|
+
if (!existing) {
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const request = CNSaveRequest.new()
|
|
318
|
+
request.deleteContact(existing.mutableCopy())
|
|
319
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
320
|
+
throw new Error('contacts.remove: the store refused the save request')
|
|
321
|
+
}
|
|
322
|
+
},
|
|
16
323
|
}
|
|
17
324
|
|
|
18
325
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
19
|
-
permissionOwners.contacts =
|
|
326
|
+
permissionOwners.contacts = {
|
|
327
|
+
getPermissions: async () => {
|
|
328
|
+
const s = contactStore()
|
|
329
|
+
if (!s) {
|
|
330
|
+
return { status: 'unsupported' as const, granted: false, canAskAgain: false }
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const status = rawStatus()
|
|
334
|
+
return {
|
|
335
|
+
status: status === 3 ? ('granted' as const) : status === 0 ? ('undetermined' as const) : ('denied' as const),
|
|
336
|
+
granted: status === 3,
|
|
337
|
+
canAskAgain: status === 0 || status === 3,
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
requestPermissions: async () => {
|
|
341
|
+
const result = await contacts.ensure()
|
|
342
|
+
return { status: result, granted: result === 'granted', canAskAgain: result === 'granted' }
|
|
343
|
+
},
|
|
344
|
+
}
|
package/src/contacts.ts
CHANGED
|
@@ -85,9 +85,11 @@ function androidEnsure(): Promise<PermissionResult> {
|
|
|
85
85
|
) {
|
|
86
86
|
return Promise.resolve('granted')
|
|
87
87
|
}
|
|
88
|
+
|
|
88
89
|
if (!activity) {
|
|
89
90
|
return Promise.resolve('denied')
|
|
90
91
|
}
|
|
92
|
+
|
|
91
93
|
return new Promise<PermissionResult>((resolve) => {
|
|
92
94
|
const requestCode = contactRequestCode++
|
|
93
95
|
const event = Application.android.activityRequestPermissionsEvent
|
|
@@ -191,6 +193,7 @@ function applyNewContact(target: any, source: NewContact): void {
|
|
|
191
193
|
target.photo = null
|
|
192
194
|
}
|
|
193
195
|
|
|
196
|
+
/** Contacts service — system address book. */
|
|
194
197
|
export const contacts: Contacts = {
|
|
195
198
|
supported: true,
|
|
196
199
|
|
package/src/contacts.web.ts
CHANGED
|
@@ -24,8 +24,11 @@ function asContact(native: any): Contact {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
const unsupported = (what: string) => () =>
|
|
27
|
-
Promise.reject(
|
|
27
|
+
Promise.reject(
|
|
28
|
+
new Error(`unsupported: contact ${what} is not available through the web Contact Picker`),
|
|
29
|
+
)
|
|
28
30
|
|
|
31
|
+
/** Contacts service — system address book. */
|
|
29
32
|
export const contacts: Contacts = {
|
|
30
33
|
supported: picker() !== null,
|
|
31
34
|
|
package/src/contacts.windows.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// unsupported rather than breaking the import graph.
|
|
3
3
|
import type { Contacts, PermissionResult } from './types'
|
|
4
4
|
|
|
5
|
-
const unsupported = () =>
|
|
5
|
+
const unsupported = () =>
|
|
6
|
+
Promise.reject(new Error('unsupported: contacts is not available on windows'))
|
|
6
7
|
|
|
8
|
+
/** Contacts service — system address book. */
|
|
7
9
|
export const contacts: Contacts = {
|
|
8
10
|
supported: false,
|
|
9
11
|
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Coarse permission answer; unsupported means no contacts/permission path. */
|
|
1
2
|
export type PermissionResult = 'granted' | 'denied' | 'unsupported'
|
|
2
3
|
|
|
3
4
|
/** Fields the app asks for — a superset is fetched natively, this selects
|
|
@@ -13,84 +14,133 @@ export type ContactField =
|
|
|
13
14
|
| 'image'
|
|
14
15
|
| 'nickname'
|
|
15
16
|
|
|
17
|
+
/** One labeled email field. */
|
|
16
18
|
export interface ContactEmail {
|
|
19
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
17
20
|
label?: string
|
|
21
|
+
/** Email address value. */
|
|
18
22
|
email: string
|
|
19
23
|
}
|
|
20
24
|
|
|
25
|
+
/** One labeled phone field. */
|
|
21
26
|
export interface ContactPhone {
|
|
27
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
22
28
|
label?: string
|
|
29
|
+
/** Phone number string as stored. */
|
|
23
30
|
number: string
|
|
24
31
|
}
|
|
25
32
|
|
|
33
|
+
/** One labeled postal address. */
|
|
26
34
|
export interface ContactAddress {
|
|
35
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
27
36
|
label?: string
|
|
37
|
+
/** Street line(s). */
|
|
28
38
|
street?: string
|
|
39
|
+
/** City. */
|
|
29
40
|
city?: string
|
|
41
|
+
/** State/province region. */
|
|
30
42
|
region?: string
|
|
43
|
+
/** Postal/ZIP code. */
|
|
31
44
|
postalCode?: string
|
|
45
|
+
/** Country name or code. */
|
|
32
46
|
country?: string
|
|
33
47
|
}
|
|
34
48
|
|
|
49
|
+
/** One labeled URL field. */
|
|
35
50
|
export interface ContactUrl {
|
|
51
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
36
52
|
label?: string
|
|
53
|
+
/** URL value. */
|
|
37
54
|
url: string
|
|
38
55
|
}
|
|
39
56
|
|
|
57
|
+
/** A stored contact record. */
|
|
40
58
|
export interface Contact {
|
|
59
|
+
/** Store-assigned contact id. */
|
|
41
60
|
id: string
|
|
42
61
|
/** Display name — falls back to composed given/family when the store
|
|
43
62
|
* lacks a display string. */
|
|
44
63
|
name: string
|
|
64
|
+
/** Given name. */
|
|
45
65
|
firstName?: string
|
|
66
|
+
/** Middle name(s). */
|
|
46
67
|
middleName?: string
|
|
68
|
+
/** Family name. */
|
|
47
69
|
lastName?: string
|
|
70
|
+
/** Nickname. */
|
|
48
71
|
nickname?: string
|
|
72
|
+
/** Email addresses with labels. */
|
|
49
73
|
emails?: ContactEmail[]
|
|
74
|
+
/** Phone numbers with labels. */
|
|
50
75
|
phoneNumbers?: ContactPhone[]
|
|
76
|
+
/** Postal addresses with labels. */
|
|
51
77
|
addresses?: ContactAddress[]
|
|
78
|
+
/** Associated URLs with labels. */
|
|
52
79
|
urls?: ContactUrl[]
|
|
80
|
+
/** Organization name. */
|
|
53
81
|
company?: string
|
|
82
|
+
/** Role within the organization. */
|
|
54
83
|
jobTitle?: string
|
|
84
|
+
/** Free-text note field. */
|
|
55
85
|
note?: string
|
|
56
86
|
/** Platform image reference when 'image' was requested — a file path or
|
|
57
87
|
* data reference; shape is platform-dependent. */
|
|
58
88
|
image?: unknown
|
|
59
89
|
}
|
|
60
90
|
|
|
91
|
+
/** Contact fields for add/update — no store id. */
|
|
61
92
|
export interface NewContact {
|
|
93
|
+
/** Given name. */
|
|
62
94
|
firstName?: string
|
|
95
|
+
/** Middle name(s). */
|
|
63
96
|
middleName?: string
|
|
97
|
+
/** Family name. */
|
|
64
98
|
lastName?: string
|
|
99
|
+
/** Nickname. */
|
|
65
100
|
nickname?: string
|
|
101
|
+
/** Email addresses with labels. */
|
|
66
102
|
emails?: ContactEmail[]
|
|
103
|
+
/** Phone numbers with labels. */
|
|
67
104
|
phoneNumbers?: ContactPhone[]
|
|
105
|
+
/** Organization name. */
|
|
68
106
|
company?: string
|
|
107
|
+
/** Role within the organization. */
|
|
69
108
|
jobTitle?: string
|
|
109
|
+
/** Free-text note field. */
|
|
70
110
|
note?: string
|
|
71
111
|
}
|
|
72
112
|
|
|
113
|
+
/** Paging options for getAll(). */
|
|
73
114
|
export interface GetContactsOptions {
|
|
74
115
|
/** Restrict which members are populated (all fetched by default). */
|
|
75
116
|
fields?: ContactField[]
|
|
76
117
|
/** Client-side sort — the native stores return an unspecified order. */
|
|
77
118
|
sort?: 'firstName' | 'lastName' | 'none'
|
|
119
|
+
/** Max contacts per page. */
|
|
78
120
|
pageSize?: number
|
|
121
|
+
/** Start offset into the result set. */
|
|
79
122
|
pageOffset?: number
|
|
80
123
|
}
|
|
81
124
|
|
|
125
|
+
/** System address-book facade; false supported off mobile. */
|
|
82
126
|
export interface Contacts {
|
|
127
|
+
/** Whether the system address book is reachable on this target — false off mobile. */
|
|
83
128
|
readonly supported: boolean
|
|
84
129
|
/** Ensure the contacts permission without prompting twice — returns the
|
|
85
130
|
* post-request grant state, 'unsupported' when the store can't be reached. */
|
|
86
131
|
ensure(): Promise<PermissionResult>
|
|
132
|
+
/** Fetch contacts in pages. */
|
|
87
133
|
getAll(options?: GetContactsOptions): Promise<Contact[]>
|
|
134
|
+
/** Substring search over display names. */
|
|
88
135
|
getByName(predicate: string, fields?: ContactField[]): Promise<Contact[]>
|
|
89
136
|
/** iOS: direct id fetch. Android: the plugin ships no id lookup, so this
|
|
90
137
|
* fetches the full store and filters — read-all cost, call sparingly. */
|
|
91
138
|
getById(id: string, fields?: ContactField[]): Promise<Contact | null>
|
|
139
|
+
/** Insert a contact; resolves the store-assigned id. */
|
|
92
140
|
add(contact: NewContact): Promise<string>
|
|
141
|
+
/** Patch fields of an existing contact by id. */
|
|
93
142
|
update(id: string, contact: NewContact): Promise<string>
|
|
143
|
+
/** Delete a contact by id. */
|
|
94
144
|
remove(id: string): Promise<void>
|
|
95
145
|
}
|
|
96
146
|
|
|
@@ -1,78 +1,128 @@
|
|
|
1
|
+
/** Coarse permission answer; unsupported means no contacts/permission path. */
|
|
1
2
|
export type PermissionResult = 'granted' | 'denied' | 'unsupported';
|
|
2
3
|
/** Fields the app asks for — a superset is fetched natively, this selects
|
|
3
4
|
* which members are populated on the returned Contact. */
|
|
4
5
|
export type ContactField = 'name' | 'emails' | 'phoneNumbers' | 'addresses' | 'urls' | 'company' | 'note' | 'image' | 'nickname';
|
|
6
|
+
/** One labeled email field. */
|
|
5
7
|
export interface ContactEmail {
|
|
8
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
6
9
|
label?: string;
|
|
10
|
+
/** Email address value. */
|
|
7
11
|
email: string;
|
|
8
12
|
}
|
|
13
|
+
/** One labeled phone field. */
|
|
9
14
|
export interface ContactPhone {
|
|
15
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
10
16
|
label?: string;
|
|
17
|
+
/** Phone number string as stored. */
|
|
11
18
|
number: string;
|
|
12
19
|
}
|
|
20
|
+
/** One labeled postal address. */
|
|
13
21
|
export interface ContactAddress {
|
|
22
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
14
23
|
label?: string;
|
|
24
|
+
/** Street line(s). */
|
|
15
25
|
street?: string;
|
|
26
|
+
/** City. */
|
|
16
27
|
city?: string;
|
|
28
|
+
/** State/province region. */
|
|
17
29
|
region?: string;
|
|
30
|
+
/** Postal/ZIP code. */
|
|
18
31
|
postalCode?: string;
|
|
32
|
+
/** Country name or code. */
|
|
19
33
|
country?: string;
|
|
20
34
|
}
|
|
35
|
+
/** One labeled URL field. */
|
|
21
36
|
export interface ContactUrl {
|
|
37
|
+
/** Free-form label (home/work/custom) as stored. */
|
|
22
38
|
label?: string;
|
|
39
|
+
/** URL value. */
|
|
23
40
|
url: string;
|
|
24
41
|
}
|
|
42
|
+
/** A stored contact record. */
|
|
25
43
|
export interface Contact {
|
|
44
|
+
/** Store-assigned contact id. */
|
|
26
45
|
id: string;
|
|
27
46
|
/** Display name — falls back to composed given/family when the store
|
|
28
47
|
* lacks a display string. */
|
|
29
48
|
name: string;
|
|
49
|
+
/** Given name. */
|
|
30
50
|
firstName?: string;
|
|
51
|
+
/** Middle name(s). */
|
|
31
52
|
middleName?: string;
|
|
53
|
+
/** Family name. */
|
|
32
54
|
lastName?: string;
|
|
55
|
+
/** Nickname. */
|
|
33
56
|
nickname?: string;
|
|
57
|
+
/** Email addresses with labels. */
|
|
34
58
|
emails?: ContactEmail[];
|
|
59
|
+
/** Phone numbers with labels. */
|
|
35
60
|
phoneNumbers?: ContactPhone[];
|
|
61
|
+
/** Postal addresses with labels. */
|
|
36
62
|
addresses?: ContactAddress[];
|
|
63
|
+
/** Associated URLs with labels. */
|
|
37
64
|
urls?: ContactUrl[];
|
|
65
|
+
/** Organization name. */
|
|
38
66
|
company?: string;
|
|
67
|
+
/** Role within the organization. */
|
|
39
68
|
jobTitle?: string;
|
|
69
|
+
/** Free-text note field. */
|
|
40
70
|
note?: string;
|
|
41
71
|
/** Platform image reference when 'image' was requested — a file path or
|
|
42
72
|
* data reference; shape is platform-dependent. */
|
|
43
73
|
image?: unknown;
|
|
44
74
|
}
|
|
75
|
+
/** Contact fields for add/update — no store id. */
|
|
45
76
|
export interface NewContact {
|
|
77
|
+
/** Given name. */
|
|
46
78
|
firstName?: string;
|
|
79
|
+
/** Middle name(s). */
|
|
47
80
|
middleName?: string;
|
|
81
|
+
/** Family name. */
|
|
48
82
|
lastName?: string;
|
|
83
|
+
/** Nickname. */
|
|
49
84
|
nickname?: string;
|
|
85
|
+
/** Email addresses with labels. */
|
|
50
86
|
emails?: ContactEmail[];
|
|
87
|
+
/** Phone numbers with labels. */
|
|
51
88
|
phoneNumbers?: ContactPhone[];
|
|
89
|
+
/** Organization name. */
|
|
52
90
|
company?: string;
|
|
91
|
+
/** Role within the organization. */
|
|
53
92
|
jobTitle?: string;
|
|
93
|
+
/** Free-text note field. */
|
|
54
94
|
note?: string;
|
|
55
95
|
}
|
|
96
|
+
/** Paging options for getAll(). */
|
|
56
97
|
export interface GetContactsOptions {
|
|
57
98
|
/** Restrict which members are populated (all fetched by default). */
|
|
58
99
|
fields?: ContactField[];
|
|
59
100
|
/** Client-side sort — the native stores return an unspecified order. */
|
|
60
101
|
sort?: 'firstName' | 'lastName' | 'none';
|
|
102
|
+
/** Max contacts per page. */
|
|
61
103
|
pageSize?: number;
|
|
104
|
+
/** Start offset into the result set. */
|
|
62
105
|
pageOffset?: number;
|
|
63
106
|
}
|
|
107
|
+
/** System address-book facade; false supported off mobile. */
|
|
64
108
|
export interface Contacts {
|
|
109
|
+
/** Whether the system address book is reachable on this target — false off mobile. */
|
|
65
110
|
readonly supported: boolean;
|
|
66
111
|
/** Ensure the contacts permission without prompting twice — returns the
|
|
67
112
|
* post-request grant state, 'unsupported' when the store can't be reached. */
|
|
68
113
|
ensure(): Promise<PermissionResult>;
|
|
114
|
+
/** Fetch contacts in pages. */
|
|
69
115
|
getAll(options?: GetContactsOptions): Promise<Contact[]>;
|
|
116
|
+
/** Substring search over display names. */
|
|
70
117
|
getByName(predicate: string, fields?: ContactField[]): Promise<Contact[]>;
|
|
71
118
|
/** iOS: direct id fetch. Android: the plugin ships no id lookup, so this
|
|
72
119
|
* fetches the full store and filters — read-all cost, call sparingly. */
|
|
73
120
|
getById(id: string, fields?: ContactField[]): Promise<Contact | null>;
|
|
121
|
+
/** Insert a contact; resolves the store-assigned id. */
|
|
74
122
|
add(contact: NewContact): Promise<string>;
|
|
123
|
+
/** Patch fields of an existing contact by id. */
|
|
75
124
|
update(id: string, contact: NewContact): Promise<string>;
|
|
125
|
+
/** Delete a contact by id. */
|
|
76
126
|
remove(id: string): Promise<void>;
|
|
77
127
|
}
|
|
78
128
|
export declare const contacts: Contacts;
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Alec Larson
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|