import { asyncApis } from "./api" ; import { AnyObj } from "../api/index.d" ; function _promisify ( func ) { return ( args = { } ) => new Promise ( ( resolve , reject ) => { func ( Object . assign ( args , { success : resolve , fail : reject } ) ) ; } ) ; } function hasCallback ( args ) { if ( ! args || typeof args !== "object" ) return false ; const callback = [ "success" , "fail" , "complete" ] ; for ( const m of callback ) { if ( typeof args [ m ] === "function" ) return true ; } return false ; } export function wrapFn ( func ) { return function ( args ) { if ( hasCallback ( args ) ) { func ( args ) ; } else { return _promisify ( func ) ( args ) ; } } ; } function PromiseAll ( wx = { } , wxp = { } ) { Object . keys ( wx ) . forEach ( key => { const fn = wx [ key ] ; if ( typeof fn === "function" && asyncApis . indexOf ( key ) >= 0 ) { wxp [ key ] = wrapFn ( fn ) ; } else { wxp [ key ] = fn ; } } ) ; } /**
 * Promise 所有 api，并绑定到 kboneAPI 上
 */ export default function intercetorApi ( KboneAPI , wx = { } ) { PromiseAll ( wx , KboneAPI ) ; }