Last active
September 12, 2017 22:39
-
-
Save HenryNguyen5/b1bcb3e9445e7efefb223015552ec429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
currentNodeConfig | |
currentNetworkConfig; | |
networkConfigs; | |
nodeConfigs; | |
constructor(configs) { | |
Object.assign(this, ...configs); | |
} | |
setNodeConfig(key) { | |
if (this.nodeConfigs[key]) { | |
this.currentNodeConfig = this.nodeConfigs[key]; | |
} else { | |
throw Error(`"${key}" is an invalid node config`); | |
} | |
} | |
setNetworkConfig(key) { | |
if (this.networkConfigs[key]) { | |
this.currentNetworkConfig = this.networkConfigs[key]; | |
} else { | |
throw Error(`"${key}" is an invalid network config`); | |
} | |
} | |
getConfig(): NodeConfig { | |
return this.currentNodeConfig; | |
} | |
getNetwork(): NetworkConfig { | |
return this.currentNetworkConfig; | |
} | |
sendRPCRequest = async (requestObj: RPCRequest): Promise<JsonRpcResponse> => { | |
const { txObj, parser, errorHandler } = requestObj; | |
const response = await fetch(this.currentNodeConfig.endpoint, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(txObj) | |
}).then(r => r.json()); | |
const result = response.error | |
? errorHandler(response.error) | |
: parser(response); | |
return result; | |
}; | |
} | |
const node = new Node({ networkConfigs, nodeConfigs }); | |
const proxiedNode = rerouteRPCMethodsHandler(node); | |
export default proxiedNode; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment