xchange-rates 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ name: Publish Exchange Rates
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 * * *' # daily at midnight UTC
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ update:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+
15
+ - uses: actions/setup-node@v4
16
+ with:
17
+ node-version: 18
18
+ registry-url: 'https://registry.npmjs.org'
19
+
20
+ - name: Install dependencies
21
+ run: npm install
22
+
23
+ - name: Update exchange rates
24
+ run: npm run update
25
+ env:
26
+ XCR_KEY1: ${{ secrets.XCR_KEY1 }}
27
+ XCR_URL1: ${{ secrets.XCR_URL1 }}
28
+
29
+ - name: Package and publish to npm
30
+ run: |
31
+ mkdir package
32
+ cp -r package.json index.js v1 package/
33
+ cd package
34
+ npm publish --access public
35
+ env:
36
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/index.js ADDED
@@ -0,0 +1,33 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const fetch = require('node-fetch');
4
+
5
+ const localPath = path.resolve('./v1/currencies.json');
6
+ let localRates = fs.existsSync(localPath) ? JSON.parse(fs.readFileSync(localPath, 'utf-8')) : null;
7
+
8
+ export async function xchangerate(baseCurrency, targetCurrency, useCDN = false) {
9
+ baseCurrency = baseCurrency.toUpperCase();
10
+ targetCurrency = targetCurrency.toUpperCase();
11
+
12
+ let data;
13
+
14
+ if (useCDN) {
15
+ const res = await fetch('https://cdn.jsdelivr.net/npm/@jayadevpanthaplavil/xchange-rates@latest/v1/currencies.json');
16
+ data = await res.json();
17
+ } else {
18
+ if (!localRates) throw new Error('Local JSON not available');
19
+ data = localRates;
20
+ }
21
+
22
+ if (!data.rates[baseCurrency]) throw new Error(`Base currency ${baseCurrency} not found`);
23
+ if (!data.rates[targetCurrency]) throw new Error(`Target currency ${targetCurrency} not found`);
24
+
25
+ const rate = data.rates[targetCurrency] / data.rates[baseCurrency];
26
+
27
+ return {
28
+ date: data.date,
29
+ base: baseCurrency,
30
+ target: targetCurrency,
31
+ rate
32
+ };
33
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "xchange-rates",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight Node.js package to get real-time currency exchange rates for any base currency using a pivot currency, fetched directly from a CDN for fast and reliable access.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "update": "node scripts/updateRates.js",
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "author": "Jayadev Panthaplavil",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "date-fns": "^4.1.0",
14
+ "dotenv": "^17.2.1",
15
+ "node-fetch": "^3.3.2"
16
+ }
17
+ }
@@ -0,0 +1,37 @@
1
+ const fs = require('fs');
2
+ const fetch = require('node-fetch');
3
+ const dotenv = require('dotenv');
4
+ const { format } = require('date-fns');
5
+ const path = require('path');
6
+
7
+
8
+ dotenv.config();
9
+
10
+ const KEY = process.env.XCR_KEY1;
11
+ const URL = process.env.XCR_URL1;
12
+ const API_URL = `${URL}${APP_ID}`;
13
+ const outputDir = path.resolve('./v1');
14
+ const outputFile = path.join(outputDir, 'currencies.json');
15
+
16
+ async function updateRates() {
17
+ try {
18
+ const res = await fetch(API_URL);
19
+ const data = await res.json();
20
+
21
+ // Remove disclaimer and license
22
+ const { disclaimer, license, ...cleanData } = data;
23
+
24
+ // Add today's date
25
+ cleanData.date = format(new Date(), 'yyyy-MM-dd');
26
+
27
+ // Ensure directory exists
28
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
29
+
30
+ fs.writeFileSync(outputFile, JSON.stringify(cleanData, null, 2));
31
+ console.log('Exchange rates updated successfully!');
32
+ } catch (err) {
33
+ console.error('Error updating rates:', err);
34
+ }
35
+ }
36
+
37
+ updateRates();