import * as errors from './utils/errors' ; import { RequestManager } from './RequestManager' ; public export class Method < Output > { callName : string ; params : number ; inputFormatter : Array < null | ( ( a : any ) => any ) > ; outputFormatter : ( something : any ) => Output ; constructor ( options ) { this . callName = options . callName ; this . params = options . params || 0 ; this . inputFormatter = options . inputFormatter || null ; this . outputFormatter = options . outputFormatter ; } param arguments validateArgs ( args ) { if ( args . length !== this . params ) { throw errors . InvalidNumberOfRPCParams ( this . callName , args . length , this . params ) ; } } param args formatInput ( args ) { if ( ! this . inputFormatter ) { return args ; } return this . inputFormatter . map ( function ( formatter , index ) { return formatter ? formatter ( args [ index ] ) : args [ index ] ; } ) ; } param result formatOutput ( result ) { return result !== null ? this . outputFormatter ( result ) : null ; } param args toPayload ( args ) { let params = this . formatInput ( args ) ; this . validateArgs ( params ) ; return { method : this . callName , params : params } ; } async execute ( requestManager , ... args ) { let payload = this . toPayload ( args ) ; if ( ! requestManager ) throw new Error ( 'Missing RequestManager in method#exec' ) ; const result = await requestManager . sendAsync ( payload ) ; return this . formatOutput ( result ) ; } }