Vue Storefront is now Alokai! Learn More
GetCurrencies

GetCurrencies

Implements GetCurrencies Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-magento";

export const getCurrencies = defineApi.getCurrencies(async (context) => {
  const { config, req, res } = context;

  const { currencies, defaultCurrency } = config.unified;

  const availableCurrencies = Array.isArray(currencies) ? currencies : [defaultCurrency];

  let currentCurrency = req.cookies["vsf-currency"];

  if (!currentCurrency || !availableCurrencies.includes(currentCurrency)) {
    currentCurrency = defaultCurrency;
  }

  res.cookie("vsf-currency", currentCurrency, { sameSite: "strict", secure: true });

  return {
    currencies: availableCurrencies,
    defaultCurrency,
    currentCurrency,
  };
});