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+ }
27+ interface iCloudContactsStartupResponse {
28+ syncToken : string ;
29+ prefToken : string ;
30+ contacts : Array < iCloudContact > ;
31+ }
32+ interface iCloudContactsContactsResponse {
33+ contacts : Array < iCloudContact > ;
34+ }
35+
36+ export class iCloudContactsService {
37+ service : iCloudService ;
38+ serviceUri : string ;
39+ constructor ( service : iCloudService , serviceUri : string ) {
40+ this . service = service ;
41+ this . serviceUri = serviceUri ;
42+ }
43+ // fetch contacts
44+ async contacts ( ) {
45+ const params = {
46+ locale : "en_US" ,
47+ order : "last,first"
48+ } ;
49+ const url = new URL ( "/co/startup" , this . serviceUri ) ;
50+ url . search = new URLSearchParams ( { ...params , clientVersion : "2.1" } ) . toString ( ) ;
51+
52+ const request = await fetch ( url . href , {
53+ headers : this . service . authStore . getHeaders ( )
54+ } ) ;
55+ const json = await request . json ( ) as iCloudContactsStartupResponse ;
56+
57+ const paramsNext = {
58+ ...params ,
59+ prefToken : json . prefToken ,
60+ syncToken : json . syncToken ,
61+ limit : "0" ,
62+ offset : "0"
63+ } ;
64+ const urlNext = new URL ( "/co/contacts" , this . serviceUri ) ;
65+ urlNext . search = new URLSearchParams ( { ...paramsNext , clientVersion : "2.1" } ) . toString ( ) ;
66+ const nextRequest = await fetch ( urlNext . href , {
67+ headers : this . service . authStore . getHeaders ( )
68+ } ) ;
69+ const nextJson = await nextRequest . json ( ) as iCloudContactsContactsResponse ;
70+
71+ return nextJson . contacts ;
72+ }
73+ }
0 commit comments