npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flatfile/plugin-convert-currency

v0.3.0

Published

A Flatfile plugin for currency conversion using Open Exchange Rates API

Downloads

139

Readme

@flatfile/plugin-convert-currency

This plugin implements a currency converter for Flatfile using the Open Exchange Rates API. It allows automatic conversion of currency amounts in your Flatfile sheets, with support for historical exchange rates.

Event Type: listener.on('commit:created')

Supported Field Types: string, number

Features

  • Automatic currency conversion based on configurable fields
  • Support for historical exchange rates using a date field
  • Configurable source and target currencies
  • Optional fields for storing exchange rates and conversion dates
  • Comprehensive error handling and validation
  • Uses the Open Exchange Rates API for up-to-date and historical exchange rates

Parameters

sheetSlug - string - (required)

The slug of the sheet where the plugin should operate.

sourceCurrency - string - (required)

The source currency code (e.g., "USD").

targetCurrency - string - (required)

The target currency code (e.g., "EUR").

amountField - string - (required)

The field name containing the amount to be converted.

dateField - string - (optional)

The field name containing the date for historical rates.

convertedAmountField - string - (required)

The field name where the converted amount will be stored.

exchangeRateField - string - (optional)

The field name where the exchange rate will be stored.

conversionDateField - string - (optional)

The field name where the conversion date will be stored.

Usage

Environment Variables

Add the following environment variables to your space:

  • OPENEXCHANGERATES_API_KEY - Your Open Exchange Rates API key

install

npm install @flatfile/plugin-convert-currency

import

import { FlatfileListener } from "@flatfile/listener";
import { currencyConverterPlugin } from "@flatfile/plugin-convert-currency";

listener.js

const listener = new FlatfileListener();

listener.use(
  currencyConverterPlugin({
    sheetSlug: "transactions",
    sourceCurrency: "USD",
    targetCurrency: "EUR",
    amountField: "amount",
    dateField: "transactionDate",
    convertedAmountField: "amountInEUR",
    exchangeRateField: "exchangeRate",
    conversionDateField: "conversionDate",
  })
);

Example

This example sets up a currency conversion plugin for the "transactions" sheet. It will automatically convert amounts from USD to EUR for each record in the sheet.

import { FlatfileListener } from "@flatfile/listener";
import { currencyConverterPlugin } from "@flatfile/plugin-convert-currency";

export default function (listener: FlatfileListener) {
  listener.use(
    currencyConverterPlugin({
      sheetSlug: "transactions",
      sourceCurrency: "USD",
      targetCurrency: "EUR",
      amountField: "amount",
      dateField: "transactionDate",
      convertedAmountField: "amountInEUR",
      exchangeRateField: "exchangeRate",
      conversionDateField: "conversionDate",
    })
  );

  listener.on("job:ready", async (event) => {
    const { jobId, workbookId } = event.context;
    await event.actions.updateJob(jobId, {
      status: "complete",
      info: "Currency conversion complete",
    });
  });
}

In this example, the plugin will:

  1. Validate the configuration upon initialization
  2. Process each record in the "transactions" sheet
  3. If autoConvert is true:
    • Check for a valid amount in the "amount" field
    • Use the date in the "transactionDate" field for historical rates (if present)
    • Call the Open Exchange Rates API to get the exchange rate
    • Calculate the converted amount and store it in the "amountInEUR" field
    • Store the exchange rate in the "exchangeRate" field
    • Store the conversion date in the "conversionDate" field
  4. Handle errors and add them to the respective fields or as general errors to the record

The job:ready event listener is used to mark the job as complete after the conversion process.

Note: This plugin requires an active subscription to the Open Exchange Rates API.