JSPM

  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q59120F
  • License ISC

retrieve forex results fast

Package Exports

  • forexy

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (forexy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

forexy

dependencies npm CI Testing install size Code Scan Alerts

A 'zero dependency', light-weight and fast node.js package that retrieves the latest Forex Currency pair results.

Installation

Using npm:

$ npm i --save forexy

Usage and Examples

To retrieve forex results for the USD/GBP pair (US dollar to Great British Pound):

const Forexy = require("forexy");

const currencyCheck = new Forexy();

currencyCheck
  .get("USDGBP")
  .then((result) => {
    console.log(`USD/GBP rate is ${result}`);
  })
  .catch((err) => {
    console.error(`Error: ${err}`);
  });

Promise and Events

Forexy returns a promise, which allows you to use an async/await function, or using then().catch().

const currency = async (pair) => {
  try {
    let result = await currencyCheck.get("USDGBP");
  } catch (err) {
    console.error(`Error: ${err}`);
  }
};

console.log(`USD/GBP rate is ${currency("USDGBP")}`);

...which is the same as

currencyCheck
  .get("USDGBP")
  .then((result) => {
    console.log(`USD-GBP rate is ${result}`);
  })
  .catch((err) => {
    console.error(`Error: ${err}`);
  });

Events

Forexy also has a host of Events that you can use through the lifecycle process.

fulldata

Get a JSON object with all data.

currencyCheck.on("fulldata", (data) => {
  //returned JSON of all data.
  console.log("on fulldata:" + JSON.stringify(data));
  //eg. {"rates":{"USDLSL":{"rate":15.140088,"timestamp":1607414291}},"code":200}
});

timestamp

This returns a Date object, which can be further manipulated, this will be approximately the current timestamp.

currencyCheck.on("timestamp", (data) => {
  //This returns a Date object.
  console.log("on timestamp:" + data); //Tues Dec 08 2021 20:51:10 GMT
});

pair

Returns the currency pair values, in uppercase.

currencyCheck.on("pair", (data) => {
  console.log("on pair:" + data); //USDGBP
});

statusCode

currencyCheck.on("statusCode", (data) => {
  //If no errors should return '200'
  console.log("on statusCode:" + data);
});

rate

rate is the same as the default value returned from the get() method.

currencyCheck.on("rate", (data) => {
  console.log("on rate:" + data);
  //eg. 1.2234
});

request

Triggered before the request is made. Returns the currency pair value.

currencyCheck.on("request", (data) => {
  //called before the request is made, returns the currency pair
  console.log("on request:" + data);
});

stream

The same returned data as 'fulldata' but streamed.

currencyCheck.on("stream", (data) => {
  //streamed data
  console.log("on stream: " + data);
});

Error Handling

Forexy has built in Error Handling, which will catch any issues in Development.

Unsupported Forex Pairs

Forexy currently only offers the most popular currency pairs. I plan to add more as the demand grows.

If you enter an invalid currency pair, the promise will reject with a list of supported currency pairs available.

If you use Forexy in an async / await then put the call into a Try {} catch {}. Otherwise use the Promise then().catch((err)=> {})

const currency = async (pair) => {
  try {
    // code that we will 'try' to run
    let result = await currencyCheck.get("MONOPOLYMONEY");
  } catch (error) {
    // code to run if there are any problems
    console.error(`Error: ${err}`); //display Error message
  }
};

Or using promise then().catch():

currencyCheck
  .get("MONOPOLYMONEY")
  .then((result) => {
    // code to run if the promise returns a resolve()
  })
  .catch((err) => {
    // code to run if there are any problems
    console.error(`Error: ${err}`);
  });