import { createClient } from "@buttercup/googledrive-client" ; import { Layerr } from "layerr" ; import DatasourceAuthManager from "./DatasourceAuthManager" ; import TextDatasource from "./TextDatasource" ; import { fireInstantiationHandlers , registerDatasource } from "./register" ; import Credentials from "../credentials/Credentials" ; import { getCredentials } from "../credentials/channel" ; import { DatasourceConfigurationGoogleDrive } from "../types" ; const DATASOURCE_TYPE = "googledrive" ; augments TextDatasource memberof export default class GoogleDriveDatasource extends TextDatasource { authManager : DatasourceAuthManager ; client : any ; fileID : string ; token : string ; refreshToken : string ; param { Credentials } credentials constructor ( credentials ) { super ( credentials ) ; const { data : credentialData } = getCredentials ( credentials . id ) ; const { datasource : datasourceConfig } = credentialData as { datasource : DatasourceConfigurationGoogleDrive ; } ; const { token , refreshToken , fileID } = datasourceConfig ; this . fileID = fileID ; this . token = token ; this . refreshToken = refreshToken ; this . client = createClient ( token ) ; this . authManager = DatasourceAuthManager . getSharedManager ( ) ; this . type = DATASOURCE_TYPE ; fireInstantiationHandlers ( DATASOURCE_TYPE , this ) ; } memberof getConfiguration ( ) { return { type : "googledrive" , token : this . token , refreshToken : this . refreshToken , fileID : this . fileID } ; } param { Credentials } credentials returns { Promise . < LoadedVaultData > } memberof load ( credentials , hasAuthed = false ) { if ( this . hasContent ) { return super . load ( credentials ) ; } return this . client . getFileContents ( this . fileID ) . then ( content => { this . setContent ( content ) ; return super . load ( credentials ) ; } ) . catch ( err => { const { authFailure = false } = Layerr . info ( err ) ; if ( ! authFailure ) { throw new Layerr ( err , "Failed fetching Google Drive vault" ) ; } else if ( hasAuthed ) { throw new Layerr ( err , "Re-authentication failed" ) ; } return this . authManager . executeAuthHandlers ( DATASOURCE_TYPE , this ) . then ( ( ) => this . load ( credentials , true ) ) . catch ( err2 => { throw new Layerr ( err2 , "Failed fetching Google Drive vault" ) ; } ) ; } ) ; } param { Array . < String > } history param { Credentials } credentials returns { Promise } memberof save ( history , credentials , hasAuthed = false ) { return super . save ( history , credentials ) . then ( encryptedContent => this . client . putFileContents ( { id : this . fileID , contents : encryptedContent } ) ) . catch ( err => { const { authFailure = false } = Layerr . info ( err ) ; if ( ! authFailure ) { throw new Layerr ( err , "Failed saving Google Drive vault" ) ; } else if ( hasAuthed ) { throw new Layerr ( err , "Re-authentication failed" ) ; } return this . authManager . executeAuthHandlers ( DATASOURCE_TYPE , this ) . then ( ( ) => this . save ( history , credentials , true ) ) . catch ( err2 => { throw new Layerr ( err2 , "Failed saving Google Drive vault" ) ; } ) ; } ) ; } returns { Boolean } memberof supportsRemoteBypass ( ) { return true ; } param { String } accessToken param { String = } refreshToken memberof updateTokens ( accessToken , refreshToken ) { this . token = accessToken ; this . refreshToken = refreshToken ; this . client = createClient ( accessToken ) ; const { data : credentialData } = getCredentials ( this . credentials . id ) ; credentialData . datasource . token = accessToken ; credentialData . datasource . refreshToken = refreshToken ; this . emit ( "updated" ) ; } } registerDatasource ( DATASOURCE_TYPE , GoogleDriveDatasource ) ;