Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

coin-rates

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

coin-rates - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

2

CHANGELOG.md
# Changelog
* v1.1.0:
* Replace getValueAtPath with function to parse response body - improves flexibility of response parsing.
* v1.0.0:
* Initial release

50

lib/fetch.js
const _ = require('underscore');
const async = require('async');
const formatText = require('./formatText');
const getValueAtPath = require('./getValueAtPath');
const http = require('http');

@@ -50,3 +49,3 @@ const https = require('https');

}
const provider = _.findWhere(providers, { name: options.provider });
let provider = _.findWhere(providers, { name: options.provider });
if (!provider) {

@@ -58,11 +57,11 @@ throw new Error(`Unknown provider: "${options.provider}"`);

}
if (!provider.jsonPath) {
throw new Error('Missing provider config: "jsonPath"');
}
if (!_.isObject(provider.jsonPath)) {
throw new Error('Invalid provider config ("jsonPath"): Object expected');
}
if (!_.isUndefined(provider.convertSymbols) && !_.isObject(provider.convertSymbols)) {
throw new Error('Invalid provider config ("convertSymbols"): Object expected');
}
if (_.isUndefined(provider.parseResponseBody)) {
provider.parseResponseBody = _.noop;
}
if (!_.isFunction(provider.parseResponseBody)) {
throw new Error('Invalid provider config ("parseResponseBody"): Function expected');
}
let currencies = {};

@@ -77,24 +76,2 @@ _.each(options.currencies, function(symbol, key) {

const uri = formatText(provider.url, currencies);
let jsonPath = _.mapObject(provider.jsonPath, path => {
return formatText(path, currencies);
});
let parseResponseBody = body => {
try {
let data = JSON.parse(body);
if (jsonPath.error) {
let error = getValueAtPath(data, jsonPath.error);
if (!_.isEmpty(error)) {
return { error };
}
}
let result = getValueAtPath(data, jsonPath.data);
if (_.isUndefined(result)) {
return { result: null };
}
result = result.toString();
return { result };
} catch (error) {
return { error };
}
};
return new Promise((resolve, reject) => {

@@ -113,5 +90,12 @@ async.retry(

res.on('end', function() {
let data = parseResponseBody(body);
if (data.error) return next(new Error(data.error));
next(null, data.result);
let result;
try {
result = provider.parseResponseBody(body, currencies) || null;
if (_.isNumber(result)) {
result = result.toString();
}
} catch (error) {
return next(error);
}
next(null, result);
});

@@ -118,0 +102,0 @@ });

@@ -0,1 +1,3 @@

const _ = require('underscore');
module.exports = [

@@ -6,8 +8,9 @@ {

url: 'https://api.binance.com/api/v3/ticker/price?symbol={{FROM}}{{TO}}',
jsonPath: {
data: 'price',
},
convertSymbols: {
USD: 'USDT',
},
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
return data.price;
},
},

@@ -18,5 +21,8 @@ {

url: 'https://api.bitfinex.com/v1/pubticker/{{from}}{{to}}',
jsonPath: {
error: 'message',
data: 'last_price',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.message)) {
throw new Error(data.message);
}
return data.last_price;
},

@@ -28,5 +34,8 @@ },

url: 'https://api.bitflyer.com/v1/ticker?product_code={{FROM}}_{{TO}}',
jsonPath: {
error: 'error_message',
data: 'ltp',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.error_message)) {
throw new Error(data.error_message);
}
return data.ltp;
},

@@ -38,5 +47,8 @@ },

url: 'https://www.bitstamp.net/api/v2/ticker/{{from}}{{to}}/',
jsonPath: {
error: 'message',
data: 'last',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.message)) {
throw new Error(data.message);
}
return data.last;
},

@@ -48,5 +60,9 @@ },

url: 'https://api.coinbase.com/v2/exchange-rates?currency={{FROM}}',
jsonPath: {
error: 'errors',
data: 'data.rates.{{TO}}',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.errors)) {
throw new Error(data.errors);
}
const { TO } = currencies;
return data.data && data.data.rates && data.data.rates[TO];
},

@@ -58,5 +74,8 @@ },

url: 'https://coinmate.io/api/ticker?currencyPair={{FROM}}_{{TO}}',
jsonPath: {
error: 'errorMessage',
data: 'data.last',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.errorMessage)) {
throw new Error(data.errorMessage);
}
return data.data && data.data.last;
},

@@ -71,5 +90,9 @@ },

},
jsonPath: {
error: 'error',
data: 'result.X{{FROM}}Z{{TO}}.c.0',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
if (!_.isEmpty(data.error)) {
throw new Error(data.error);
}
const { FROM, TO } = currencies;
return data.result && data.result[`X${FROM}Z${TO}`] && data.result[`X${FROM}Z${TO}`]['c'] && data.result[`X${FROM}Z${TO}`]['c'][0];
},

@@ -84,6 +107,8 @@ },

},
jsonPath: {
data: '{{TO}}_{{FROM}}.last',
parseResponseBody: function(body, currencies) {
let data = JSON.parse(body);
const { FROM, TO } = currencies;
return data[`${TO}_${FROM}`] && data[`${TO}_${FROM}`]['last'];
},
},
];
{
"name": "coin-rates",
"version": "1.0.0",
"version": "1.1.0",
"description": "Fetch currency exchange rate for coin/fiat currency pairs",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc