import { setServiceStorage , getServiceStorage , clearServiceStorage } from './helpers' ; import { User , ServiceId } from './types' ; export interface ServiceStorage { enable : boolean ; user : User | null ; token : any ; } /**
 * OAuth based service interface
 */ export abstract class Service { constructor ( id ) { this . id = id ; this . getStorage ( ) ; } readonly id : ServiceId ; /** max content length */ abstract readonly maxWordCount : number ; returns { Promise < boolean > } abstract authorize ( ) ; abstract obtainAccessToken ( code ) ; abstract checkAccessToken ( ) ; returns { Promise < string > } abstract postContent ( text , img ? ) ; enable = true ; user : User | null = null ; protected token : any = null ; countWords ( text ) { return text . length ; } async setStorage ( ) { await setServiceStorage < ServiceStorage > ( this . id , { enable : this . enable , user : this . user , token : this . token } ) ; } async getStorage ( ) { const storage = ( await getServiceStorage < ServiceStorage > ( this . id ) ) || { enable : true , user : null , token : null } ; this . enable = ! ! storage . enable ; this . user = storage . user != null ? storage . user : null ; this . token = storage . token != null ? storage . token : null ; } async clearStorage ( ) { await clearServiceStorage ( this . id ) ; this . enable = true ; this . user = null ; this . token = null ; } } export default Service ;