diff --git a/TODO.md b/TODO.md index d61f631..3fe4ae1 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ # TODO - Aggiornare README.md -- getHistoricalData input deve usare mapping.js - valore di default di resolution 'D' \ No newline at end of file diff --git a/index.js b/index.js index 68b4d0f..82ca244 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const puppeteer = require('puppeteer'); -const { mapping } = require('./mapping'); +const { getPairId } = require('./mapping'); const { getJsonContent, mapResponse } = require('./functions'); const getHistoricalData = require('./src/getHistoricalData'); @@ -67,7 +67,7 @@ async function callInvesting(pairId, period, interval, pointscount, pptrLaunchOp async function investing(input, period = 'P1M', interval = 'P1D', pointscount = 120, pptrLaunchOptions) { try { checkParams(input, period, interval, pointscount); - const pairId = mapping[input]?.pairId || input; + const pairId = getPairId(input); const resInvesting = await callInvesting(pairId, period, interval, pointscount, pptrLaunchOptions); const results = mapResponse(resInvesting); if (!results.length) { diff --git a/mapping.js b/mapping.js index 45096d7..401e90e 100644 --- a/mapping.js +++ b/mapping.js @@ -1,4 +1,4 @@ -exports.mapping = { +const mapping = { 'currencies/eur-usd': { pairId: '1', title: 'EUR/USD - Euro US Dollar', @@ -945,3 +945,5 @@ exports.mapping = { name: 'PIMCO Commodity Real Return Strategy Institutional', }, }; + +exports.getPairId = (input) => mapping[input]?.pairId || input; \ No newline at end of file diff --git a/src/getHistoricalData.js b/src/getHistoricalData.js index 7a91411..a9d5b91 100644 --- a/src/getHistoricalData.js +++ b/src/getHistoricalData.js @@ -1,8 +1,9 @@ const { mapResponse } = require('../functions'); +const { getPairId } = require('../mapping'); const buildUrl = ({ input, resolution, from, to } = {}) => { const query = new URLSearchParams({ - symbol: input, + symbol: getPairId(input), resolution, from: from && from.getTime() / 1000, to: to && to.getTime() / 1000, diff --git a/test/getHistoricalData.spec.js b/test/getHistoricalData.spec.js index dd4f124..cf2bcf0 100644 --- a/test/getHistoricalData.spec.js +++ b/test/getHistoricalData.spec.js @@ -120,4 +120,62 @@ describe('Tests for getHistoricalData()', () => { ]; expect(data).toEqual(expected); }); + + it('should use mapping for input', async () => { + scope + .get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') + .query({ + symbol: '1', + resolution: 'D', + from: 1729123200, + to: 1729209600, + }) + .reply(200, { + 't': [ + 1729123200, + ], + 'c': [ + 1.08309996128082008937099089962430298328399658203125, + ], + 'o': [ + 1.086099982261659935289799250313080847263336181640625, + ], + 'h': [ + 1.0872999429702800977537435755948536098003387451171875, + ], + 'l': [ + 1.0809999704360999661645337255322374403476715087890625, + ], + 'v': [ + 1, + ], + 'vo': [ + 'n/a', + ], + 'vac': [ + 'n/a', + ], + 's': 'ok', + }); + + const data = await getHistoricalData({ + input: 'currencies/eur-usd', + resolution: 'D', + from: new Date(1729123200000), + to: new Date(1729209600000), + }); + + const expected = [ + { + date: 1729123200000, + value: 1.08309996128082008937099089962430298328399658203125, + price_open: 1.086099982261659935289799250313080847263336181640625, + price_high: 1.0872999429702800977537435755948536098003387451171875, + price_low: 1.0809999704360999661645337255322374403476715087890625, + price_close: 1.08309996128082008937099089962430298328399658203125, + volume: 1, + }, + ]; + expect(data).toEqual(expected); + }); });