Skip to content
Snippets Groups Projects
Commit 6df505b0 authored by Roger Jungemann's avatar Roger Jungemann
Browse files

Address PR comments

parent 545c15d8
No related branches found
No related tags found
No related merge requests found
...@@ -14,9 +14,20 @@ class RPCError extends Error { ...@@ -14,9 +14,20 @@ class RPCError extends Error {
} }
} }
export function jsonRpc(uri, {method, id, params, fetchMethod}) { /**
* Makes a JSON-RPC request using `fetch` or a user-provided `fetchMethod`.
*
* @param {string} uri - The URI to the JSON-RPC endpoint.
* @param {string} options.method - The remote JSON-RPC method to call.
* @param {string} options.id - ID for the request, for matching to a response.
* @param {*} options.params - The params for the remote method.
* @param {function} [options.fetchMethod=fetch] - A function with the same
* signature as `fetch`, which can be used to make the network request, or for
* stubbing in tests.
*/
export function jsonRpc(uri, {method, id, params, fetchMethod=fetch}) {
const payload = {id, jsonrpc: '2.0', method, params}; const payload = {id, jsonrpc: '2.0', method, params};
return (fetchMethod || fetch)(uri, { return fetchMethod(uri, {
body: JSON.stringify(payload), body: JSON.stringify(payload),
method: 'post', method: 'post',
mode: 'cors', mode: 'cors',
...@@ -55,9 +66,7 @@ export default class HttpTransport extends Transport { ...@@ -55,9 +66,7 @@ export default class HttpTransport extends Transport {
jsonRpc(this.options.uri, { method: 'call', id, params, fetchMethod }).then( jsonRpc(this.options.uri, { method: 'call', id, params, fetchMethod }).then(
res => { callback(null, res); }, res => { callback(null, res); },
err => { err => {
console.error('An error occurred hitting the Steem API:', err);
if (retriable.retry(err)) { if (retriable.retry(err)) {
console.errror('Retrying...');
return; return;
} }
callback(retriable.mainError()); callback(retriable.mainError());
...@@ -86,12 +95,12 @@ export default class HttpTransport extends Transport { ...@@ -86,12 +95,12 @@ export default class HttpTransport extends Transport {
if (this.nonRetriableOperations.some((o) => o === data.method)) { if (this.nonRetriableOperations.some((o) => o === data.method)) {
// Do not retry if the operation is non-retriable. // Do not retry if the operation is non-retriable.
return null; return null;
} else if (Object(this.options.retry) === this.options.retry) {
// If `this.options.retry` is a map of options, pass those to operation.
return retry.operation(this.options.retry);
} else if (this.options.retry) { } else if (this.options.retry) {
// If `this.options.retry` is a map of options, use it. If // If `this.options.retry` is `true`, use default options.
// `this.options.retry` is `true`, use default options. return retry.operation();
return (Object(this.options.retry) === this.options.retry) ?
retry.operation(this.options.retry) :
retry.operation();
} else { } else {
// Otherwise, don't retry. // Otherwise, don't retry.
return null; return null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment