bankcode-bic
Version:
Convert bank codes from IBAN to BICs, Name of bank. Currently supports only some selected EU countries.
31 lines (29 loc) • 1.44 kB
JavaScript
import { fieldNames } from "../download/download.js";
import { packData } from "./pack-data.js";
//#region src/libs/fetch-data.ts
/**
* Fetches bank data for a specified country and packs it into a structured format.
*
* @param country - The country code for which to fetch bank data.
* @param options - Optional parameters including key names, field names, and a custom fetch function.
* @returns A promise that resolves to a packed data object containing bank information.
* @throws {Error} If the download URL is not found, if the data fails to download, if parsing fails, or if packing fails.
*/
async function fetchData(country, options) {
const importFilePath = `../download/${country}.js`;
const { getDownloadUrl, downloadCSV, parseCSV } = await import(importFilePath);
const fieldNames$1 = options?.fieldNames ?? fieldNames;
const keyNames = options?.keyNames ?? ["bankcode"];
const fetchFn = options?.fetchFn ?? globalThis.fetch;
const downloadUrl = await getDownloadUrl(fetchFn);
if (!downloadUrl) throw new Error("Download URL not found");
const csv = await downloadCSV(downloadUrl, fetchFn);
if (!csv) throw new Error("Failed to download data");
const data = parseCSV(csv);
if (!data) throw new Error("Failed to parse CSV data");
const outputObject = packData(data, keyNames, fieldNames$1);
if (!outputObject) throw new Error("Failed to pack data");
return outputObject;
}
//#endregion
export { fetchData };