Created
May 12, 2021 02:33
-
-
Save samloh84/e6b24227e99fba19df0a84f6e9561247 to your computer and use it in GitHub Desktop.
Get MAS Exchange Rate
This file contains 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
const luxon = require('luxon'); | |
const DateTime = luxon.DateTime; | |
const _ = require('lodash'); | |
const axios = require('axios'); | |
const qs = require('qs'); | |
let getMasExchangeRate = function (date) { | |
if (_.isNil(date)) { | |
date = DateTime.now(); | |
} | |
let queryDate = date.endOf('month'); | |
const format = 'yyyy-MM'; | |
return Promise.resolve(axios('https://eservices.mas.gov.sg/api/action/datastore/search.json', { | |
params: { | |
resource_id: '10eafb90-11a2-4fbd-b7a7-ac15a42d60b6', | |
filters: {end_of_month: `${queryDate.toFormat(format)}`} | |
}, | |
paramsSerializer: params => { | |
console.debug(qs.stringify(params)); | |
return qs.stringify(params); | |
} | |
})) | |
.then(function (response) { | |
console.debug(JSON.stringify(response.data, null, 4)); | |
return _.get(response.data, ['result', 'records', 0]); | |
}); | |
}; | |
module.exports = getMasExchangeRate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment