{"version":3,"sources":["../src/index.ts","../src/helpers.ts"],"names":["getEncoding","date","firstUtfDate","formatToTwoDigits","value","getApiUrl","url","encoding","today","year","month","day","fetchAllCurrencies","__async","apiUrl","currencyResponses","response","axios","XMLValidator","options","json","XMLParser","item","error","fetchCurrency","_0","currency","allCurrencies"],"mappings":"0EAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,IAAA,OAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,IAAA,CAAA,CAAA,CAAA,EAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,EAAA,CAAA,MAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,KAAA,CAAA,CAAA,OAAA,CAAA,OAAA,CAAA,CAAA,CAAA,KAAA,CAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,IAAA,EAAA,EAAA,CAAA,CAAA,CCEA,SAASA,CAAAA,CAAY,CAAE,IAAA,CAAAC,CAAK,CAAiC,CAAA,CAC5D,IAAMC,CAAAA,CAAqB,IAAI,IAAA,CAAK,IAAM,CAAA,CAAA,CAAG,CAAC,CAM9C,CAAA,OAL0B,IAAI,IAAA,CAC7BD,CAAK,CAAA,WAAA,EACLA,CAAAA,CAAAA,CAAK,UACLA,CAAAA,CAAAA,CAAK,OAAQ,EACd,CACmB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AAA,MAAMf,CAAmBX,CAAAA,CAAI,EAEnD,OAAK0B,CAAAA,EAIoBA,CAAc,CAAA,IAAA,CACrCL,CAASA,EAAAA,CAAAA,CAAK,YAAiBI,GAAAA,CACjC,GAE2B,IAC5B,CAAA,CAAA","file":"index.mjs","sourcesContent":["import { XMLParser, XMLValidator, type X2jOptions } from \"fast-xml-parser\";\nimport axios from \"axios\";\nimport type { TCMBResponseType, TcmbType } from \"./types\";\nimport { getApiUrl } from \"./helpers\";\n\n/// Function to fetch all exchange rates for a given date\n/// @param date - The date for which to fetch exchange rates\n/// @returns An array of all exchange rates or null if no data is found\nasync function fetchAllCurrencies(\n\tdate?: Date,\n): Promise<TCMBResponseType[] | null> {\n\tconst apiUrl = getApiUrl({ date: date });\n\tconst currencyResponses: TCMBResponseType[] = [];\n\n\ttry {\n\t\tconst response = await axios.get(apiUrl.url);\n\t\tconst result = XMLValidator.validate(response.data);\n\n\t\tif (result !== true) {\n\t\t\tthrow new Error(\"Invalid XML response\");\n\t\t}\n\n\t\tconst options: X2jOptions = {\n\t\t\tignoreAttributes: false,\n\t\t\tallowBooleanAttributes: true,\n\t\t\tignoreDeclaration: true,\n\t\t\tattributeNamePrefix: \"@_\",\n\t\t\ttrimValues: true,\n\t\t};\n\n\t\tconst parser = new XMLParser(options);\n\t\tconst json = parser.parse(response.data);\n\n\t\tfor (const item of json.Tarih_Date.Currency) {\n\t\t\tcurrencyResponses.push({\n\t\t\t\tUnit: item.Unit,\n\t\t\t\tCurrencyName: item.CurrencyName,\n\t\t\t\tCurrencyCode: item[\"@_CurrencyCode\"],\n\t\t\t\tForexBuying: item.ForexBuying,\n\t\t\t\tForexSelling: item.ForexSelling,\n\t\t\t\tBanknoteBuying: item.BanknoteBuying,\n\t\t\t\tBanknoteSelling: item.BanknoteSelling,\n\t\t\t});\n\t\t}\n\n\t\treturn currencyResponses.length > 0 ? currencyResponses : null;\n\t} catch (error) {\n\t\treturn Promise.reject(error);\n\t}\n}\n\n/// Function to fetch a specific currency's exchange rate for a given date\n/// @param currency - The currency code to fetch (e.g., \"USD\")\n/// @param date - The date for which to fetch the exchange rate\n/// @returns The exchange rate for the specified currency or null if not found\nasync function fetchCurrency({\n\tcurrency,\n\tdate,\n}: TcmbType): Promise<TCMBResponseType | null> {\n\tconst allCurrencies = await fetchAllCurrencies(date);\n\n\tif (!allCurrencies) {\n\t\treturn null;\n\t}\n\n\tconst currencyResponse = allCurrencies.find(\n\t\t(item) => item.CurrencyCode === currency,\n\t);\n\n\treturn currencyResponse || null;\n}\n\nexport { fetchAllCurrencies, fetchCurrency };\n","import type { EncodingType, UrlType } from \"./types\";\n\nfunction getEncoding({ date }: { date: Date }): EncodingType {\n\tconst firstUtfDate: Date = new Date(2016, 8, 8);\n\tconst currentDate: Date = new Date(\n\t\tdate.getFullYear(),\n\t\tdate.getMonth(),\n\t\tdate.getDate(),\n\t);\n\tif (currentDate >= firstUtfDate) return \"UTF-8\";\n\treturn \"ISO-8859-9\";\n}\n\nfunction formatToTwoDigits(value: number): string {\n\treturn value < 10 ? `0${value}` : `${value}`;\n}\n\nfunction getApiUrl({ date }: { date?: Date }): UrlType {\n\tlet url = \"https://www.tcmb.gov.tr/kurlar/today.xml\";\n\tlet encoding = \"UTF-8\";\n\n\tif (date) {\n\t\tconst today = new Date();\n\t\ttoday.setHours(0, 0, 0, 0);\n\t\tconst inputDate = new Date(\n\t\t\tdate.getFullYear(),\n\t\t\tdate.getMonth(),\n\t\t\tdate.getDate(),\n\t\t);\n\n\t\tif (inputDate <= today) {\n\t\t\tconst year = date.getFullYear();\n\t\t\tconst month = formatToTwoDigits(date.getMonth());\n\t\t\tconst day = formatToTwoDigits(date.getDate());\n\n\t\t\turl = `https://www.tcmb.gov.tr/kurlar/${year}${month}/${day}${month}${year}.xml`;\n\t\t\tencoding = getEncoding({ date });\n\t\t}\n\t}\n\n\treturn {\n\t\turl: url,\n\t\tencoding: encoding,\n\t};\n}\n\nexport { getEncoding, formatToTwoDigits, getApiUrl };\n"]}