import { Db } from '@cloudbase/database' ; import adapterForWxMp from 'cloudbase-adapter-wx_mp' ; import { Auth } from './auth' ; import * as Storage from './storage' ; import * as Functions from './functions' ; import { IRequest , initRequest } from './lib/request' ; import { addEventListener , removeEventListener } from './lib/events' ; import { useAdapters , Adapter , useDefaultAdapter , RUNTIME } from './adapters' ; import { SDKAdapterInterface , CloudbaseAdapter , IRequestConfig } from '@cloudbase/adapter-interface' ; import { AppSecret } from './types' ; import { initCache } from './lib/cache' ; interface ICloudbaseConfig { env : string ; timeout ? : number ; persistence ? : string ; adapter ? : SDKAdapterInterface ; appSecret ? : AppSecret ; appSign ? : string ; } constant const DEFAULT_INIT_CONFIG = { timeout : 15000 , persistence : 'session' } ; const MAX_TIMEOUT = 1000 * 60 * 10 ; const MIN_TIMEOUT = 100 ; type Persistence = 'local' | 'session' | 'none' ; const extensionMap = { } ; class TCB { config : ICloudbaseConfig ; authObj : Auth ; requestClient : any ; constructor ( config ? ) { this . config = config ? config : this . config ; this . authObj = undefined ; if ( Adapter . adapter ) { this . requestClient = new Adapter . adapter . reqClass ( < IRequestConfig > { timeout : this . config . timeout , timeoutMsg : `[tcb-js-sdk] 请求在${ this . config . timeout / 1000 }s内未完成，已中断` } ) ; } } init ( config ) { if ( ! Adapter . adapter ) { this . _useDefaultAdapter ( ) ; this . requestClient = new Adapter . adapter . reqClass ( < IRequestConfig > { timeout : config . timeout || 5000 , timeoutMsg : `[tcb-js-sdk] 请求在${ ( config . timeout || 5000 ) / 1000 }s内未完成，已中断` } ) ; } if ( Adapter . runtime !== RUNTIME . WEB ) { if ( ! config . appSecret ) { throw new Error ( '[tcb-js-sdk]参数错误：请正确配置appSecret' ) ; } const appSign = Adapter . adapter . getAppSign ? Adapter . adapter . getAppSign ( ) : '' ; if ( config . appSign && appSign && config . appSign !== appSign ) { throw new Error ( '[tcb-js-sdk]参数错误：非法的应用标识' ) ; } appSign && ( config . appSign = appSign ) ; if ( ! config . appSign ) { throw new Error ( '[tcb-js-sdk]参数错误：请正确配置应用标识' ) ; } } this . config = { ... DEFAULT_INIT_CONFIG , ... config } ; switch ( true ) { case this . config . timeout > MAX_TIMEOUT : console . warn ( '[tcb-js-sdk] timeout大于可配置上限[10分钟]，已重置为上限数值' ) ; this . config . timeout = MAX_TIMEOUT ; break ; case this . config . timeout < MIN_TIMEOUT : console . warn ( '[tcb-js-sdk] timeout小于可配置下限[100ms]，已重置为下限数值' ) ; this . config . timeout = MIN_TIMEOUT ; break ; } return new TCB ( this . config ) ; } database ( dbConfig ? ) { Db . reqClass = IRequest ; Db . wsClass = Adapter . adapter . wsClass ; if ( ! this . authObj ) { console . warn ( '需要app.auth()授权' ) ; return ; } Db . getAccessToken = this . authObj . getAccessToken . bind ( this . authObj ) ; Db . runtime = Adapter . runtime ; if ( ! Db . ws ) { Db . ws = null ; } return new Db ( { ... this . config , ... dbConfig } ) ; } auth ( { persistence } = { } ) { if ( this . config . env === 'prod-80f7de' ) { persistence = 'local' ; } if ( this . authObj ) { console . warn ( 'tcb实例只存在一个auth对象' ) ; return this . authObj ; } const _persistence = persistence || Adapter . adapter . primaryStorage || DEFAULT_INIT_CONFIG . persistence ; if ( _persistence !== this . config . persistence ) { this . config . persistence = _persistence ; } initCache ( this . config ) ; initRequest ( this . config ) ; this . authObj = new Auth ( this . config ) ; return this . authObj ; } on ( eventName , callback ) { return addEventListener . apply ( this , [ eventName , callback ] ) ; } off ( eventName , callback ) { return removeEventListener . apply ( this , [ eventName , callback ] ) ; } callFunction ( params , callback ? ) { return Functions . callFunction . apply ( this , [ params , callback ] ) ; } deleteFile ( params , callback ? ) { return Storage . deleteFile . apply ( this , [ params , callback ] ) ; } getTempFileURL ( params , callback ? ) { return Storage . getTempFileURL . apply ( this , [ params , callback ] ) ; } downloadFile ( params , callback ? ) { return Storage . downloadFile . apply ( this , [ params , callback ] ) ; } uploadFile ( params , callback ? ) { return Storage . uploadFile . apply ( this , [ params , callback ] ) ; } getUploadMetadata ( params , callback ? ) { return Storage . getUploadMetadata . apply ( this , [ params , callback ] ) ; } registerExtension ( ext ) { extensionMap [ ext . name ] = ext ; } async invokeExtension ( name , opts ) { const ext = extensionMap [ name ] ; if ( ! ext ) { throw Error ( `扩展${ name } 必须先注册` ) ; } let res = await ext . invoke ( opts , this ) ; return res ; } useAdapters ( adapters ) { const { adapter , runtime } = useAdapters ( adapters ) || { } ; adapter && ( Adapter . adapter = adapter as SDKAdapterInterface ) ; runtime && ( Adapter . runtime = runtime as string ) ; } private _useDefaultAdapter ( ) { const { adapter , runtime } = useDefaultAdapter ( ) ; Adapter . adapter = adapter as SDKAdapterInterface ; Adapter . runtime = runtime as string ; } } const tcb = new TCB ( ) ; tcb . useAdapters ( adapterForWxMp ) ; try { window [ 'tcb' ] = tcb ; } catch ( e ) { } export = tcb ;