import { isString , isFunction } from './is' ; import { unsupport , setUniqueKeyToRoute } from './utils' ; declare const getCurrentPages ; declare const getApp ; declare const requirePlugin ; type IObject = Record < string , any > ; interface IProcessApisIOptions { noPromiseApis ? : Set < string > ; needPromiseApis ? : Set < string > ; handleSyncApis ? : ( key : string , global : IObject , args : any [ ] ) => any ; transformMeta ? : ( key : string , options : IObject ) => { key : string ; options : IObject ; } ; modifyApis ? : ( apis : Set < string > ) => void ; modifyAsyncResult ? : ( key : string , res ) => void ; isOnlyPromisify ? : boolean ; [ propName ] : any ; } const needPromiseApis = new Set < string > ( [ 'addPhoneContact' , 'authorize' , 'canvasGetImageData' , 'canvasPutImageData' , 'canvasToTempFilePath' , 'checkSession' , 'chooseAddress' , 'chooseImage' , 'chooseInvoiceTitle' , 'chooseLocation' , 'chooseVideo' , 'clearStorage' , 'closeBLEConnection' , 'closeBluetoothAdapter' , 'closeSocket' , 'compressImage' , 'connectSocket' , 'createBLEConnection' , 'downloadFile' , 'exitMiniProgram' , 'getAvailableAudioSources' , 'getBLEDeviceCharacteristics' , 'getBLEDeviceServices' , 'getBatteryInfo' , 'getBeacons' , 'getBluetoothAdapterState' , 'getBluetoothDevices' , 'getClipboardData' , 'getConnectedBluetoothDevices' , 'getConnectedWifi' , 'getExtConfig' , 'getFileInfo' , 'getImageInfo' , 'getLocation' , 'getNetworkType' , 'getSavedFileInfo' , 'getSavedFileList' , 'getScreenBrightness' , 'getSetting' , 'getStorage' , 'getStorageInfo' , 'getSystemInfo' , 'getUserInfo' , 'getWifiList' , 'hideHomeButton' , 'hideShareMenu' , 'hideTabBar' , 'hideTabBarRedDot' , 'loadFontFace' , 'login' , 'makePhoneCall' , 'navigateBack' , 'navigateBackMiniProgram' , 'navigateTo' , 'navigateToBookshelf' , 'navigateToMiniProgram' , 'notifyBLECharacteristicValueChange' , 'hideKeyboard' , 'hideLoading' , 'hideNavigationBarLoading' , 'hideToast' , 'openBluetoothAdapter' , 'openDocument' , 'openLocation' , 'openSetting' , 'pageScrollTo' , 'previewImage' , 'queryBookshelf' , 'reLaunch' , 'readBLECharacteristicValue' , 'redirectTo' , 'removeSavedFile' , 'removeStorage' , 'removeTabBarBadge' , 'requestSubscribeMessage' , 'saveFile' , 'saveImageToPhotosAlbum' , 'saveVideoToPhotosAlbum' , 'scanCode' , 'sendSocketMessage' , 'setBackgroundColor' , 'setBackgroundTextStyle' , 'setClipboardData' , 'setEnableDebug' , 'setInnerAudioOption' , 'setKeepScreenOn' , 'setNavigationBarColor' , 'setNavigationBarTitle' , 'setScreenBrightness' , 'setStorage' , 'setTabBarBadge' , 'setTabBarItem' , 'setTabBarStyle' , 'showActionSheet' , 'showFavoriteGuide' , 'showLoading' , 'showModal' , 'showShareMenu' , 'showTabBar' , 'showTabBarRedDot' , 'showToast' , 'startBeaconDiscovery' , 'startBluetoothDevicesDiscovery' , 'startDeviceMotionListening' , 'startPullDownRefresh' , 'stopBeaconDiscovery' , 'stopBluetoothDevicesDiscovery' , 'stopCompass' , 'startCompass' , 'startAccelerometer' , 'stopAccelerometer' , 'showNavigationBarLoading' , 'stopDeviceMotionListening' , 'stopPullDownRefresh' , 'switchTab' , 'uploadFile' , 'vibrateLong' , 'vibrateShort' , 'writeBLECharacteristicValue' ] ) ; function getCanIUseWebp ( taro ) { return function ( ) { const res = taro . getSystemInfoSync ?. ( ) ; if ( ! res ) { if ( process . env . NODE_ENV !== 'production' ) { console . error ( '不支持 API canIUseWebp' ) ; } return false ; } const { platform } = res ; const platformLower = platform . toLowerCase ( ) ; if ( platformLower === 'android' || platformLower === 'devtools' ) { return true ; } return false ; } ; } function getNormalRequest ( global ) { return function request ( options ) { options = options ? ( isString ( options ) ? { url : options } : options ) : { } ; const originSuccess = options . success ; const originFail = options . fail ; const originComplete = options . complete ; let requestTask ; const p = new Promise ( ( resolve , reject ) => { options . success = res => { originSuccess && originSuccess ( res ) ; resolve ( res ) ; } ; options . fail = res => { originFail && originFail ( res ) ; reject ( res ) ; } ; options . complete = res => { originComplete && originComplete ( res ) ; } ; requestTask = global . request ( options ) ; } ) ; p . abort = ( cb ) => { cb && cb ( ) ; if ( requestTask ) { requestTask . abort ( ) ; } return p ; } ; return p ; } ; } function processApis ( taro , global , config = { } ) { const patchNeedPromiseApis = config . needPromiseApis || [ ] ; const _needPromiseApis = new Set < string > ( [ ... patchNeedPromiseApis , ... needPromiseApis ] ) ; const preserved = [ 'getEnv' , 'interceptors' , 'Current' , 'getCurrentInstance' , 'options' , 'nextTick' , 'eventCenter' , 'Events' , 'preload' , 'webpackJsonp' ] ; const apis = new Set ( ! config . isOnlyPromisify ? Object . keys ( global ) . filter ( api => preserved . indexOf ( api ) === - 1 ) : patchNeedPromiseApis ) ; if ( config . modifyApis ) { config . modifyApis ( apis ) ; } apis . forEach ( key => { if ( _needPromiseApis . has ( key ) ) { const originKey = key ; taro [ originKey ] = ( options = { } , ... args ) => { let key = originKey ; if ( typeof options === 'string' ) { if ( args . length ) { return global [ key ] ( options , ... args ) ; } return global [ key ] ( options ) ; } if ( config . transformMeta ) { const transformResult = config . transformMeta ( key , options ) ; key = transformResult . key ; ( options as Record < string , any > ) = transformResult . options ; if ( ! global . hasOwnProperty ( key ) ) { return unsupport ( key ) ( ) ; } } let task = null ; const obj = Object . assign ( { } , options ) ; setUniqueKeyToRoute ( key , options ) ; const p = new Promise ( ( resolve , reject ) => { obj . success = res => { config . modifyAsyncResult ?. ( key , res ) ; options . success ?. ( res ) ; if ( key === 'connectSocket' ) { resolve ( Promise . resolve ( ) . then ( ( ) => task ? Object . assign ( task , res ) : res ) ) ; } else { resolve ( res ) ; } } ; obj . fail = res => { options . fail ?. ( res ) ; reject ( res ) ; } ; obj . complete = res => { options . complete ?. ( res ) ; } ; if ( args . length ) { task = global [ key ] ( obj , ... args ) ; } else { task = global [ key ] ( obj ) ; } } ) ; if ( key === 'uploadFile' || key === 'downloadFile' ) { p . progress = cb => { task ?. onProgressUpdate ( cb ) ; return p ; } ; p . abort = cb => { cb ?. ( ) ; task ?. abort ( ) ; return p ; } ; } return p ; } ; } else { let platformKey = key ; if ( config . transformMeta ) { platformKey = config . transformMeta ( key , { } ) . key ; } if ( ! global . hasOwnProperty ( platformKey ) ) { taro [ key ] = unsupport ( key ) ; return ; } if ( isFunction ( global [ key ] ) ) { taro [ key ] = ( ... args ) => { if ( config . handleSyncApis ) { return config . handleSyncApis ( key , global , args ) ; } else { return global [ platformKey ] . apply ( global , args ) ; } } ; } else { taro [ key ] = global [ platformKey ] ; } } } ) ; ! config . isOnlyPromisify && equipCommonApis ( taro , global , config ) ; } param taro param global function equipCommonApis ( taro , global , apis = { } ) { taro . canIUseWebp = getCanIUseWebp ( taro ) ; taro . getCurrentPages = getCurrentPages || unsupport ( 'getCurrentPages' ) ; taro . getApp = getApp || unsupport ( 'getApp' ) ; taro . env = global . env || { } ; try { taro . requirePlugin = requirePlugin || unsupport ( 'requirePlugin' ) ; } catch ( error ) { taro . requirePlugin = unsupport ( 'requirePlugin' ) ; } const request = apis . request || getNormalRequest ( global ) ; function taroInterceptor ( chain ) { return request ( chain . requestParams ) ; } const link = new taro . Link ( taroInterceptor ) ; taro . request = link . request . bind ( link ) ; taro . addInterceptor = link . addInterceptor . bind ( link ) ; taro . cleanInterceptors = link . cleanInterceptors . bind ( link ) ; taro . miniGlobal = taro . options . miniGlobal = global ; } export { processApis } ;