import { HttpGetClient } from '@/infra/gateways' ; import { LoadFacebookUser } from '@/domain/contracts/gateways' ; type AppToken = { access_token : string ; } ; type DebugToken = { data : { user_id : string ; } ; } ; type UserInfo = { id : string ; name : string ; email : string ; } ; type Input = LoadFacebookUser . Input ; type Output = LoadFacebookUser . Output ; export class FacebookApi implements LoadFacebookUser { private readonly baseUrl = 'https://graph.facebook.com' ; constructor ( private readonly httpClient , private readonly clientId , private readonly clientSecret ) { } async loadUser ( { token } ) { return this . getUserInfo ( token ) . then ( ( { id , name , email } ) => ( { facebookId : id , name , email } ) ) . catch ( ( ) => undefined ) ; } private async getAppToken ( ) { return this . httpClient . get ( { url : `${ this . baseUrl }/oauth/access_token` , params : { client_id : this . clientId , client_secret : this . clientSecret , grant_type : 'client_credentials' } } ) ; } private async getDebugToken ( clientToken ) { const appToken = await this . getAppToken ( ) ; return this . httpClient . get ( { url : `${ this . baseUrl }/debug_token` , params : { access_token : appToken . access_token , input_token : clientToken } } ) ; } private async getUserInfo ( clientToken ) { const debugToken = await this . getDebugToken ( clientToken ) ; return this . httpClient . get ( { url : `${ this . baseUrl }/${ debugToken . data . user_id }` , params : { fields : [ 'id' , 'name' , 'email' ] . join ( ',' ) , access_token : clientToken } } ) ; } }