import type { OhbugClient , OhbugConfig , OhbugLoggerConfig , OhbugExtension , OhbugCreateEvent , OhbugEventWithMethods , OhbugClientConstructor , OhbugClientConstructorValues , OhbugGetDevice , OhbugAction , OhbugNotifier , OhbugHooks , OhbugUser , OhbugSDK , OhbugMetaData , } from '@ohbug/types' ; import { isFunction , isString , isObject } from '@ohbug/utils' ; import { schema as baseSchema } from './config' ; import { loadExtension } from './extension' ; import { createEvent , isEvent } from './event' ; import { notify } from './notify' ; import { Action } from './action' ; import { verifyConfig } from './lib/verifyConfig' ; import { getConfigErrorMessage , getErrorMessage } from './lib/getErrorMessage' ; import { addMetaData , getMetaData , deleteMetaData } from './lib/metaData' ; export const Client = class Client implements OhbugClient { readonly _sdk : OhbugSDK ; readonly _config : OhbugConfig ; readonly _logger : OhbugLoggerConfig ; readonly _device : OhbugGetDevice ; readonly _notifier : OhbugNotifier ; readonly _extensions : OhbugExtension [ ] ; readonly _hooks : OhbugHooks ; readonly _actions : OhbugAction [ ] ; _user : OhbugUser ; readonly _metaData : OhbugMetaData ; constructor ( { sdk , config : baseConfig , schema = baseSchema , device , notifier , } ) { const { config , errors } = verifyConfig ( baseConfig , schema ) ; this . _sdk = sdk ; this . _config = config ; this . _logger = config . logger ; this . _device = device ; this . _notifier = notifier ; this . _extensions = [ ] ; this . _hooks = { created : config . created , notified : config . notified , } ; this . _actions = [ ] ; this . _user = config . user ; this . _metaData = { } ; if ( isObject ( config . metaData ) ) { Object . keys ( config . metaData ) . forEach ( ( key ) => { this . addMetaData ( key , config . metaData [ key ] ) ; } ) ; } if ( Object . keys ( errors ) . length ) { this . _logger . warn ( getConfigErrorMessage ( errors , baseConfig ) ) ; } } param extension param args use ( extension , ... args ) { return loadExtension ( extension , this , ... args ) ; } param value createEvent < D = any > ( value ) { const event = createEvent ( value , this ) ; if ( isFunction ( this . _hooks . created ) ) { return this . _hooks . created ( event , this ) ; } return event ; } param eventLike param beforeNotify notify < D = any > ( eventLike , beforeNotify ? ) { let event ; if ( Boolean ( eventLike ) && ! isEvent ( eventLike ) ) { event = this . createEvent ( eventLike ) ; } else { event = eventLike ; } if ( beforeNotify ) event = beforeNotify ( event ) ; return notify < D > ( event , this ) ; } param message param data param type param timestamp addAction ( message , data , type , timestamp ? ) { const actions = this . _actions ; const targetMessage = isString ( message ) ? message : '' ; const targetData = data || { } ; const targetType = isString ( type ) ? type : '' ; const action = new Action ( targetMessage , targetData , targetType , timestamp ) ; if ( actions . length >= this . _config . maxActions ! ) { actions . shift ( ) ; } actions . push ( action ) ; } /**
     * Get current user information
     * 获取当前的用户信息
     */ getUser ( ) { return this . _user ; } /**
     * Set current user information
     * 设置当前的用户信息
     */ setUser ( user ) { if ( isObject ( user ) && Object . keys ( user ) . length <= 6 ) { this . _user = { ... this . _user , ... user } ; return this . getUser ( ) ; } this . _logger . warn ( getErrorMessage ( 'setUser should be an object and have up to 6 attributes' , user ) ) ; return undefined ; } param section param data addMetaData ( section , data ) { return addMetaData ( this . _metaData , section , data ) ; } param section getMetaData ( section ) { return getMetaData ( this . _metaData , section ) ; } param section deleteMetaData ( section ) { return deleteMetaData ( this . _metaData , section ) ; } } ;