xchange-rates 1.0.0 → 2025.8.16-2

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.
@@ -1,5 +1,6 @@
1
1
  name: Publish Exchange Rates
2
2
 
3
+ # Triggers: daily cron + manual trigger
3
4
  on:
4
5
  schedule:
5
6
  - cron: '0 0 * * *' # daily at midnight UTC
@@ -10,27 +11,62 @@ jobs:
10
11
  runs-on: ubuntu-latest
11
12
 
12
13
  steps:
14
+ # 1. Checkout the repository
13
15
  - uses: actions/checkout@v4
14
16
 
17
+ # 2. Setup Node.js
15
18
  - uses: actions/setup-node@v4
16
19
  with:
17
20
  node-version: 18
18
21
  registry-url: 'https://registry.npmjs.org'
19
22
 
23
+ # 3. Install dependencies
20
24
  - name: Install dependencies
21
25
  run: npm install
22
26
 
27
+ # 4. Update exchange rates JSON
23
28
  - name: Update exchange rates
24
29
  run: npm run update
25
30
  env:
26
31
  XCR_KEY1: ${{ secrets.XCR_KEY1 }}
27
32
  XCR_URL1: ${{ secrets.XCR_URL1 }}
28
33
 
29
- - name: Package and publish to npm
34
+ # 5. Generate date-based version
35
+ - name: Set date-based version
36
+ id: version
30
37
  run: |
38
+ VERSION=$(node scripts/semver-date.js)
39
+ # convert dots to dashes for npm tag
40
+ TAG=$(echo $VERSION | tr '.' '-')
41
+ echo "VERSION=$VERSION" >> "$GITHUB_ENV"
42
+ echo "TAG=$TAG" >> "$GITHUB_ENV"
43
+
44
+ # 6. Bump package.json version without git tag
45
+ - name: Bump version
46
+ run: npm version ${{ env.VERSION }} --no-git-tag-version
47
+
48
+ # 7. Prepare package folder
49
+ - name: Prepare package
50
+ run: |
51
+ rm -rf package
31
52
  mkdir package
32
53
  cp -r package.json index.js v1 package/
54
+ ls -la package # debug to verify files
55
+
56
+ # 8. Publish to npm
57
+ - name: Publish to npm
58
+ run: |
33
59
  cd package
34
60
  npm publish --access public
35
61
  env:
36
62
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
63
+
64
+ # 9. Add date-based npm tag
65
+ - name: Add npm date tag
66
+ run: npm dist-tag add xchange-rates@${{ env.VERSION }} ${{ env.TAG }}
67
+ env:
68
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
69
+
70
+ # 10. Success message
71
+ - name: Success
72
+ run: echo "Exchange rates published successfully! Version ${{ env.VERSION }}"
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const fetch = require('node-fetch');
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import fetch from 'node-fetch';
4
4
 
5
5
  const localPath = path.resolve('./v1/currencies.json');
6
6
  let localRates = fs.existsSync(localPath) ? JSON.parse(fs.readFileSync(localPath, 'utf-8')) : null;
@@ -12,7 +12,7 @@ export async function xchangerate(baseCurrency, targetCurrency, useCDN = false)
12
12
  let data;
13
13
 
14
14
  if (useCDN) {
15
- const res = await fetch('https://cdn.jsdelivr.net/npm/@jayadevpanthaplavil/xchange-rates@latest/v1/currencies.json');
15
+ const res = await fetch('https://cdn.jsdelivr.net/npm/xchange-rates@latest/v1/currencies.json');
16
16
  data = await res.json();
17
17
  } else {
18
18
  if (!localRates) throw new Error('Local JSON not available');
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "xchange-rates",
3
- "version": "1.0.0",
3
+ "version": "2025.8.16-2",
4
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
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "scripts": {
7
8
  "update": "node scripts/updateRates.js",
8
9
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -0,0 +1,41 @@
1
+ // // semver-date.js
2
+ // const today = new Date();
3
+ // const version = `${today.getFullYear()}.${today.getMonth()+1}.${today.getDate()}`;
4
+ // console.log(version);
5
+
6
+
7
+ import fetch from 'node-fetch';
8
+
9
+ async function generateVersion() {
10
+ const today = new Date();
11
+ const year = today.getFullYear();
12
+ const month = today.getMonth() + 1; // 1–12
13
+ const day = today.getDate(); // 1–31
14
+
15
+ // Ensure no leading zeros
16
+ const baseVersion = `${year}.${month}.${day}`;
17
+
18
+ try {
19
+ const res = await fetch('https://registry.npmjs.org/xchange-rates');
20
+ const data = await res.json();
21
+
22
+ const versions = new Set(Object.keys(data.versions || {}));
23
+ const timeKeys = new Set(Object.keys(data.time || {})); // includes removed versions
24
+ const allVersions = new Set([...versions, ...timeKeys]);
25
+
26
+ let finalVersion = baseVersion;
27
+ let patch = 0;
28
+
29
+ while (allVersions.has(finalVersion)) {
30
+ patch += 1;
31
+ finalVersion = `${baseVersion}-${patch}`;
32
+ }
33
+
34
+ console.log(finalVersion);
35
+ } catch (err) {
36
+ console.error("Error fetching registry:", err);
37
+ console.log(baseVersion);
38
+ }
39
+ }
40
+
41
+ generateVersion();
@@ -1,15 +1,14 @@
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
-
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import fetch from 'node-fetch';
4
+ import dotenv from 'dotenv';
5
+ import { format } from 'date-fns';
7
6
 
8
7
  dotenv.config();
9
8
 
10
9
  const KEY = process.env.XCR_KEY1;
11
10
  const URL = process.env.XCR_URL1;
12
- const API_URL = `${URL}${APP_ID}`;
11
+ const API_URL = `${URL}${KEY}`;
13
12
  const outputDir = path.resolve('./v1');
14
13
  const outputFile = path.join(outputDir, 'currencies.json');
15
14