66 type AuthToken ,
77} from "@plotday/sdk/tools/auth" ;
88import { type Callback } from "@plotday/sdk/tools/callback" ;
9+
910import type { Contact , ContactAuth , GoogleContacts } from "./types" ;
1011
1112type ContactTokens = {
@@ -59,7 +60,7 @@ class GoogleApi {
5960 method : string ,
6061 url : string ,
6162 params ?: { [ key : string ] : any } ,
62- body ?: { [ key : string ] : any } ,
63+ body ?: { [ key : string ] : any }
6364 ) {
6465 const query = params ? `?${ new URLSearchParams ( params ) } ` : "" ;
6566 const headers = {
@@ -95,15 +96,15 @@ function parseContact(contact: GoogleContact) {
9596 const name = contact . names ?. [ 0 ] ?. displayName ;
9697 const avatar = contact . photos ?. filter (
9798 ( p : NonNullable < GoogleContact [ "photos" ] > [ number ] ) =>
98- ! p . default && p . metadata ?. primary ,
99+ ! p . default && p . metadata ?. primary
99100 ) ?. [ 0 ] ?. url ;
100101 return { name, avatar } ;
101102}
102103
103104async function getGoogleContacts (
104105 api : GoogleApi ,
105106 scopes : string [ ] ,
106- state : ContactSyncState ,
107+ state : ContactSyncState
107108) : Promise < {
108109 contacts : Contact [ ] ;
109110 state : ContactSyncState ;
@@ -115,8 +116,7 @@ async function getGoogleContacts(
115116 if ( ! state . more || tokens . connections ?. nextPageToken ) {
116117 if (
117118 scopes ?. some ?.(
118- ( scope ) =>
119- scope === "https://www.googleapis.com/auth/contacts.readonly" ,
119+ ( scope ) => scope === "https://www.googleapis.com/auth/contacts.readonly"
120120 )
121121 ) {
122122 let response : ListResponse | undefined ;
@@ -131,12 +131,12 @@ async function getGoogleContacts(
131131 pageToken : tokens . connections ?. nextPageToken ,
132132 }
133133 : tokens . connections ?. nextSyncToken
134- ? {
135- syncToken : tokens . connections ?. nextSyncToken ,
136- }
137- : { } ) ,
134+ ? {
135+ syncToken : tokens . connections ?. nextSyncToken ,
136+ }
137+ : { } ) ,
138138 personFields : "names,emailAddresses,photos" ,
139- } ,
139+ }
140140 ) ) as ListResponse ;
141141 if ( response !== null ) break ;
142142 if ( ! tokens . connections ) break ;
@@ -176,7 +176,7 @@ async function getGoogleContacts(
176176 if (
177177 scopes ?. some ?.(
178178 ( scope ) =>
179- scope === "https://www.googleapis.com/auth/contacts.other.readonly" ,
179+ scope === "https://www.googleapis.com/auth/contacts.other.readonly"
180180 )
181181 ) {
182182 let response : ListOtherResponse | undefined ;
@@ -191,12 +191,12 @@ async function getGoogleContacts(
191191 pageToken : tokens . other ?. nextPageToken ,
192192 }
193193 : tokens . other ?. nextSyncToken
194- ? {
195- syncToken : tokens . other ?. nextSyncToken ,
196- }
197- : { } ) ,
194+ ? {
195+ syncToken : tokens . other ?. nextSyncToken ,
196+ }
197+ : { } ) ,
198198 readMask : "names,emailAddresses,photos" ,
199- } ,
199+ }
200200 ) ) as ListOtherResponse ;
201201 if ( response !== null ) break ;
202202 if ( ! tokens . other ) break ;
@@ -255,7 +255,7 @@ export default class extends Tool implements GoogleContacts {
255255
256256 async requestAuth (
257257 callbackFunctionName : string ,
258- callbackContext ?: any ,
258+ callbackContext ?: any
259259 ) : Promise < any > {
260260 const contactsScopes = [
261261 "https://www.googleapis.com/auth/contacts.readonly" ,
@@ -268,7 +268,7 @@ export default class extends Tool implements GoogleContacts {
268268 // Register the callback for auth completion with opaque token
269269 const callbackToken = await this . callback (
270270 callbackFunctionName ,
271- callbackContext ,
271+ callbackContext
272272 ) ;
273273 await this . set ( `auth_callback_token:${ opaqueToken } ` , callbackToken ) ;
274274
@@ -285,17 +285,17 @@ export default class extends Tool implements GoogleContacts {
285285 level : AuthLevel . User ,
286286 scopes : contactsScopes ,
287287 } ,
288- authCallback ,
288+ authCallback
289289 ) ;
290290 }
291291
292292 async getContacts ( authToken : string ) : Promise < Contact [ ] > {
293293 const storedAuthToken = await this . get < AuthToken > (
294- `auth_token:${ authToken } ` ,
294+ `auth_token:${ authToken } `
295295 ) ;
296296 if ( ! storedAuthToken ) {
297297 throw new Error (
298- "No Google authentication token available for the provided authToken" ,
298+ "No Google authentication token available for the provided authToken"
299299 ) ;
300300 }
301301
@@ -312,21 +312,21 @@ export default class extends Tool implements GoogleContacts {
312312 callbackFunctionName : string ,
313313 options ?: {
314314 context ?: any ;
315- } ,
315+ }
316316 ) : Promise < void > {
317317 const storedAuthToken = await this . get < AuthToken > (
318- `auth_token:${ authToken } ` ,
318+ `auth_token:${ authToken } `
319319 ) ;
320320 if ( ! storedAuthToken ) {
321321 throw new Error (
322- "No Google authentication token available for the provided authToken" ,
322+ "No Google authentication token available for the provided authToken"
323323 ) ;
324324 }
325325
326326 // Register the callback
327327 const callbackToken = await this . callback (
328328 callbackFunctionName ,
329- options ?. context ,
329+ options ?. context
330330 ) ;
331331 await this . set ( `contacts_callback_token:${ authToken } ` , callbackToken ) ;
332332
@@ -360,17 +360,15 @@ export default class extends Tool implements GoogleContacts {
360360
361361 try {
362362 const storedAuthToken = await this . get < AuthToken > (
363- `auth_token:${ authToken } ` ,
363+ `auth_token:${ authToken } `
364364 ) ;
365365 if ( ! storedAuthToken ) {
366366 throw new Error (
367- "No authentication token available for the provided authToken" ,
367+ "No authentication token available for the provided authToken"
368368 ) ;
369369 }
370370
371- const state = await this . get < ContactSyncState > (
372- `sync_state:${ authToken } ` ,
373- ) ;
371+ const state = await this . get < ContactSyncState > ( `sync_state:${ authToken } ` ) ;
374372 if ( ! state ) {
375373 throw new Error ( "No sync state found" ) ;
376374 }
@@ -379,13 +377,13 @@ export default class extends Tool implements GoogleContacts {
379377 const result = await getGoogleContacts (
380378 api ,
381379 storedAuthToken . scopes ,
382- state ,
380+ state
383381 ) ;
384382
385383 if ( result . contacts . length > 0 ) {
386384 await this . processContacts ( result . contacts , authToken ) ;
387385 console . log (
388- `Synced ${ result . contacts . length } contacts in batch ${ batchNumber } ` ,
386+ `Synced ${ result . contacts . length } contacts in batch ${ batchNumber } `
389387 ) ;
390388 }
391389
@@ -399,7 +397,7 @@ export default class extends Tool implements GoogleContacts {
399397 await this . run ( callback ) ;
400398 } else {
401399 console . log (
402- `Google Contacts sync completed after ${ batchNumber } batches` ,
400+ `Google Contacts sync completed after ${ batchNumber } batches`
403401 ) ;
404402 await this . clear ( `sync_state:${ authToken } ` ) ;
405403 }
@@ -412,13 +410,13 @@ export default class extends Tool implements GoogleContacts {
412410
413411 private async processContacts (
414412 contacts : Contact [ ] ,
415- authToken : string ,
413+ authToken : string
416414 ) : Promise < void > {
417415 const callbackToken = await this . get < Callback > (
418- `contacts_callback_token:${ authToken } ` ,
416+ `contacts_callback_token:${ authToken } `
419417 ) ;
420418 if ( callbackToken ) {
421- await this . call ( callbackToken , contacts ) ;
419+ await this . callCallback ( callbackToken , contacts ) ;
422420 }
423421 }
424422
@@ -437,14 +435,14 @@ export default class extends Tool implements GoogleContacts {
437435
438436 // Retrieve and call the stored callback
439437 const callbackToken = await this . get < Callback > (
440- `auth_callback_token:${ opaqueToken } ` ,
438+ `auth_callback_token:${ opaqueToken } `
441439 ) ;
442440 if ( callbackToken ) {
443441 const authSuccessResult : ContactAuth = {
444442 authToken : opaqueToken ,
445443 } ;
446444
447- await this . call ( callbackToken , authSuccessResult ) ;
445+ await this . callCallback ( callbackToken , authSuccessResult ) ;
448446
449447 // Clean up the callback token
450448 await this . clear ( `auth_callback_token:${ opaqueToken } ` ) ;
0 commit comments