import { GoogleAuth , JWT , UserRefreshClient } from 'google-auth-library' ; import GoogleDriveApiFileManager from './GoogleDriveApiFileManager' ; const DOH_DATA_DROP_FOLDER_ID = '1w_O-vweBFbqCgzgmCpux2F0HVB4P6ni2' ; const SCOPES = [ 'https://www.googleapis.com/auth/drive.metadata.readonly' , 'https://www.googleapis.com/auth/drive.file' , 'https://www.googleapis.com/auth/drive' ] ; /**
 * Handles Authorization to Google Drive
 */ class GoogleDriveApi { private static instance : GoogleDriveApi ; auth ! : JWT | UserRefreshClient ; googleDriveApiFileManager ! : GoogleDriveApiFileManager ; return { GoogleDriveApi } constructor ( ) { if ( ! GoogleDriveApi . instance ) { GoogleDriveApi . instance = this ; } return GoogleDriveApi . instance ; } /**
       * Entry point for the class
      */ async getAuth ( ) { console . log ( 'Authorizing' ) ; return this . authorize ( ) ; } /**
       * Create an OAuth2 client with the given credentials, and then execute the
       * given callback function.
       */ async authorize ( ) { console . log ( '\nCheck if Service key exists...' ) ; if ( this . isServiceKeysValid ( ) ) { console . log ( 'Service key is valid!' ) ; const privateKey = ( process . env . PRIVATE_KEY as string ) . replace ( new RegExp ( '\\\\n' , 'g' ) , '\n' ) ; this . auth = new GoogleAuth ( { scopes : SCOPES , } ) . fromJSON ( { type : ( process . env . TYPE as string ) , project_id : ( process . env . PROJECT_ID as string ) , private_key_id : ( process . env . PRIVATE_KEY_ID as string ) , private_key : privateKey , client_email : ( process . env . CLIENT_EMAIL as string ) , client_id : ( process . env . CLIENT_ID as string ) , } ) ; this . googleDriveApiFileManager = new GoogleDriveApiFileManager ( this . auth , DOH_DATA_DROP_FOLDER_ID ) ; return this . auth ; } console . log ( 'Service key is NOT valid!' ) ; throw Error ( 'There was a problem at service key in .env' ) ; } return { boolean } isServiceKeysValid = ( ) => ( process . env . TYPE as string ) && ( process . env . PROJECT_ID as string ) && ( process . env . PRIVATE_KEY_ID as string ) && ( process . env . PRIVATE_KEY as string ) && ( process . env . CLIENT_EMAIL as string ) && ( process . env . CLIENT_ID as string ) ; return { Promise < Object > } downloadLatestFileFromArchives ( ) { return this . googleDriveApiFileManager . downloadLatestFileFromArchives ( ) ; } return { Promise < String | null > } downloadLatestFile ( ) { return this . googleDriveApiFileManager . downloadLatestFile ( ) ; } } export default GoogleDriveApi ;