1+ import fetch from "node-fetch" ;
2+ import iCloudService from ".." ;
3+ interface iCloudFieldLabel {
4+ field : string ;
5+ label : string ;
6+ }
7+ interface iCloudContact {
8+ firstName ?: string ;
9+ lastName ?: string ;
10+ isGuardianApproved : boolean ;
11+ emailAddresses ?: Array < iCloudFieldLabel > ;
12+ contactId : string ;
13+ normalized : string ;
14+ phones ?: Array < iCloudFieldLabel > ;
15+ urls ?: Array < iCloudFieldLabel > ;
16+ dates ?: Array < iCloudFieldLabel > ;
17+ relatedNames ?: Array < iCloudFieldLabel > ;
18+ notes ?: string ;
19+ birthday ?: string ;
20+ etag : string ;
21+ whitelisted : boolean ;
22+ isCompany : boolean ;
23+ profiles ?: { field : string ; label : string ; user : string } [ ] ;
24+ IMs ?: { field : { IMService : string ; userName : string ; } ; label : string ; } [ ] ;
25+ photo ?: { signature : string ; url : string ; crop : { x : number ; y : number ; width : number ; height : number ; } }
26+ streetAddresses ?: {
27+ field : {
28+ country : string ;
29+ countryCode : string ;
30+ street ?: string ;
31+ city ?: string ;
32+ postalCode ?: string ;
33+ state ?: string ;
34+ } ;
35+ label : string ;
36+ } [ ] ;
37+ }
38+ interface iCloudContactsStartupResponse {
39+ syncToken : string ;
40+ prefToken : string ;
41+ contacts : Array < iCloudContact > ;
42+ }
43+ interface iCloudContactsContactsResponse {
44+ contacts : Array < iCloudContact > ;
45+ }
46+
47+ export class iCloudContactsService {
48+ service : iCloudService ;
49+ serviceUri : string ;
50+ constructor ( service : iCloudService , serviceUri : string ) {
51+ this . service = service ;
52+ this . serviceUri = serviceUri ;
53+ }
54+ // fetch contacts
55+ async contacts ( ) {
56+ const params = {
57+ locale : "en_US" ,
58+ order : "last,first"
59+ } ;
60+ const url = new URL ( "/co/startup" , this . serviceUri ) ;
61+ url . search = new URLSearchParams ( { ...params , clientVersion : "2.1" } ) . toString ( ) ;
62+
63+ const request = await fetch ( url . href , {
64+ headers : this . service . authStore . getHeaders ( )
65+ } ) ;
66+ const json = await request . json ( ) as iCloudContactsStartupResponse ;
67+
68+ const paramsNext = {
69+ ...params ,
70+ prefToken : json . prefToken ,
71+ syncToken : json . syncToken ,
72+ limit : "0" ,
73+ offset : "0"
74+ } ;
75+ const urlNext = new URL ( "/co/contacts" , this . serviceUri ) ;
76+ urlNext . search = new URLSearchParams ( { ...paramsNext , clientVersion : "2.1" } ) . toString ( ) ;
77+ const nextRequest = await fetch ( urlNext . href , {
78+ headers : this . service . authStore . getHeaders ( )
79+ } ) ;
80+ const nextJson = await nextRequest . json ( ) as iCloudContactsContactsResponse ;
81+
82+ return nextJson . contacts ;
83+ }
84+ }
0 commit comments