Skip to content

Instantly share code, notes, and snippets.

@HenryNguyen5
Last active September 12, 2017 22:39
Show Gist options
  • Save HenryNguyen5/b1bcb3e9445e7efefb223015552ec429 to your computer and use it in GitHub Desktop.
Save HenryNguyen5/b1bcb3e9445e7efefb223015552ec429 to your computer and use it in GitHub Desktop.
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