@allratestoday/sdk 1.2.0 → 1.3.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.
- package/README.md +444 -36
- package/dist/index.d.mts +167 -24
- package/dist/index.d.ts +167 -24
- package/dist/index.js +246 -34
- package/dist/index.mjs +246 -34
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -25,25 +25,53 @@ __export(index_exports, {
|
|
|
25
25
|
default: () => index_default
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
var
|
|
28
|
+
var DEFAULT_BASE_URL = "https://allratestoday.com";
|
|
29
|
+
var DEFAULT_TIMEOUT = 1e4;
|
|
30
|
+
var AllRatesTodayError = class extends Error {
|
|
31
|
+
constructor(message, status) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "AllRatesTodayError";
|
|
34
|
+
this.status = status;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
29
37
|
var AllRatesToday = class {
|
|
30
38
|
/**
|
|
31
39
|
* Create an AllRatesToday client.
|
|
32
|
-
*
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* const client = new AllRatesToday({ apiKey: 'art_live_...' });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration. Get a free API key at https://allratestoday.com/register
|
|
33
46
|
*/
|
|
34
47
|
constructor(options = {}) {
|
|
35
48
|
this.apiKey = options.apiKey;
|
|
36
|
-
this.baseUrl = options.baseUrl ||
|
|
37
|
-
this.timeout = options.timeout ||
|
|
49
|
+
this.baseUrl = options.baseUrl || DEFAULT_BASE_URL;
|
|
50
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT;
|
|
51
|
+
}
|
|
52
|
+
// -------------------------------------------------------------------------
|
|
53
|
+
// Internal
|
|
54
|
+
// -------------------------------------------------------------------------
|
|
55
|
+
resolveKey(override) {
|
|
56
|
+
const key = override || this.apiKey;
|
|
57
|
+
if (!key) {
|
|
58
|
+
throw new AllRatesTodayError(
|
|
59
|
+
"API key is required. Register for free at https://allratestoday.com/register"
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return key;
|
|
38
63
|
}
|
|
39
|
-
async request(path, params = {}) {
|
|
64
|
+
async request(path, params = {}, keyOverride) {
|
|
40
65
|
const url = new URL(path, this.baseUrl);
|
|
41
66
|
for (const [key, value] of Object.entries(params)) {
|
|
42
|
-
if (value
|
|
67
|
+
if (value !== void 0 && value !== "") {
|
|
68
|
+
url.searchParams.set(key, value);
|
|
69
|
+
}
|
|
43
70
|
}
|
|
44
71
|
const headers = {};
|
|
45
|
-
|
|
46
|
-
|
|
72
|
+
const apiKey = keyOverride || this.apiKey;
|
|
73
|
+
if (apiKey) {
|
|
74
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
47
75
|
}
|
|
48
76
|
const response = await fetch(url.toString(), {
|
|
49
77
|
headers,
|
|
@@ -58,38 +86,229 @@ var AllRatesToday = class {
|
|
|
58
86
|
}
|
|
59
87
|
return response.json();
|
|
60
88
|
}
|
|
89
|
+
formatDate(date) {
|
|
90
|
+
if (date instanceof Date) {
|
|
91
|
+
return date.toISOString().split("T")[0];
|
|
92
|
+
}
|
|
93
|
+
return date;
|
|
94
|
+
}
|
|
95
|
+
// -------------------------------------------------------------------------
|
|
96
|
+
// Public API
|
|
97
|
+
// -------------------------------------------------------------------------
|
|
98
|
+
/**
|
|
99
|
+
* Get the latest exchange rates.
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* // All rates from USD
|
|
103
|
+
* const data = await client.latest();
|
|
104
|
+
*
|
|
105
|
+
* // Specific symbols with EUR base
|
|
106
|
+
* const data = await client.latest({ base: 'EUR', symbols: ['USD', 'GBP', 'JPY'] });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
async latest(options = {}) {
|
|
110
|
+
const key = this.resolveKey(options.apiKey);
|
|
111
|
+
const params = {};
|
|
112
|
+
params.source = options.base || "USD";
|
|
113
|
+
if (options.symbols?.length) {
|
|
114
|
+
params.target = options.symbols.join(",");
|
|
115
|
+
}
|
|
116
|
+
const raw = await this.request(
|
|
117
|
+
"/api/v1/rates",
|
|
118
|
+
params,
|
|
119
|
+
key
|
|
120
|
+
);
|
|
121
|
+
const ratesArray = Array.isArray(raw) ? raw : [raw];
|
|
122
|
+
const rates = {};
|
|
123
|
+
let date = "";
|
|
124
|
+
for (const r of ratesArray) {
|
|
125
|
+
rates[r.target] = r.rate;
|
|
126
|
+
if (r.time) date = r.time;
|
|
127
|
+
}
|
|
128
|
+
return { base: params.source, date, rates };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get exchange rates for a specific historical date.
|
|
132
|
+
*
|
|
133
|
+
* ```ts
|
|
134
|
+
* // Using a string
|
|
135
|
+
* const data = await client.forDate('2026-01-15');
|
|
136
|
+
*
|
|
137
|
+
* // Using a Date object
|
|
138
|
+
* const data = await client.forDate(new Date('2026-01-15'));
|
|
139
|
+
*
|
|
140
|
+
* // With options
|
|
141
|
+
* const data = await client.forDate('2026-01-15', { base: 'EUR', symbols: ['USD', 'GBP'] });
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
async forDate(date, options = {}) {
|
|
145
|
+
const key = this.resolveKey(options.apiKey);
|
|
146
|
+
const dateStr = this.formatDate(date);
|
|
147
|
+
const params = {
|
|
148
|
+
source: options.base || "USD",
|
|
149
|
+
from: dateStr,
|
|
150
|
+
to: dateStr
|
|
151
|
+
};
|
|
152
|
+
if (options.symbols?.length) {
|
|
153
|
+
params.target = options.symbols.join(",");
|
|
154
|
+
}
|
|
155
|
+
const raw = await this.request(
|
|
156
|
+
"/api/historical-rates",
|
|
157
|
+
params,
|
|
158
|
+
key
|
|
159
|
+
);
|
|
160
|
+
const rates = {};
|
|
161
|
+
if (raw.rates?.length) {
|
|
162
|
+
for (const r of raw.rates) {
|
|
163
|
+
rates[raw.target] = r.rate;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (raw.current) {
|
|
167
|
+
rates[raw.target] = raw.current.rate;
|
|
168
|
+
}
|
|
169
|
+
return { base: params.source, date: dateStr, rates };
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get exchange rate time series between two dates.
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* const data = await client.timeSeries('2026-01-01', '2026-03-31', {
|
|
176
|
+
* base: 'USD',
|
|
177
|
+
* symbols: ['EUR', 'GBP'],
|
|
178
|
+
* });
|
|
179
|
+
*
|
|
180
|
+
* // Access rates by date
|
|
181
|
+
* console.log(data.rates['2026-01-15']); // { EUR: 0.92, GBP: 0.78 }
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
async timeSeries(startDate, endDate, options = {}) {
|
|
185
|
+
const key = this.resolveKey(options.apiKey);
|
|
186
|
+
const start = this.formatDate(startDate);
|
|
187
|
+
const end = this.formatDate(endDate);
|
|
188
|
+
const source = options.base || "USD";
|
|
189
|
+
const params = {
|
|
190
|
+
source,
|
|
191
|
+
from: start,
|
|
192
|
+
to: end
|
|
193
|
+
};
|
|
194
|
+
if (options.symbols?.length) {
|
|
195
|
+
params.target = options.symbols.join(",");
|
|
196
|
+
}
|
|
197
|
+
const raw = await this.request(
|
|
198
|
+
"/api/historical-rates",
|
|
199
|
+
params,
|
|
200
|
+
key
|
|
201
|
+
);
|
|
202
|
+
const rates = {};
|
|
203
|
+
if (raw.rates) {
|
|
204
|
+
for (const point of raw.rates) {
|
|
205
|
+
const dateKey = point.time.split("T")[0];
|
|
206
|
+
if (!rates[dateKey]) rates[dateKey] = {};
|
|
207
|
+
rates[dateKey][raw.target] = point.rate;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return { base: source, startDate: start, endDate: end, rates };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get all supported currency symbols.
|
|
214
|
+
*
|
|
215
|
+
* ```ts
|
|
216
|
+
* const { symbols } = await client.symbols();
|
|
217
|
+
* console.log(symbols);
|
|
218
|
+
* // { USD: 'United States Dollar', EUR: 'Euro', GBP: 'British Pound', ... }
|
|
219
|
+
*
|
|
220
|
+
* // Build a currency dropdown
|
|
221
|
+
* const options = Object.entries(symbols).map(([code, name]) => ({ code, name }));
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
async symbols(options = {}) {
|
|
225
|
+
const key = this.resolveKey(options.apiKey);
|
|
226
|
+
return this.request("/api/v1/symbols", {}, key);
|
|
227
|
+
}
|
|
61
228
|
/**
|
|
62
|
-
* Get exchange rate between two currencies
|
|
229
|
+
* Get exchange rate between two currencies.
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* const rate = await client.getRate('USD', 'EUR');
|
|
233
|
+
* console.log(`1 USD = ${rate.rate} EUR`);
|
|
234
|
+
*
|
|
235
|
+
* // With amount
|
|
236
|
+
* const rate = await client.getRate('USD', 'EUR', 1000);
|
|
237
|
+
* console.log(`$1,000 = €${rate.to.amount}`);
|
|
238
|
+
* ```
|
|
63
239
|
*/
|
|
64
|
-
async getRate(from, to, amount) {
|
|
65
|
-
|
|
240
|
+
async getRate(from, to, amount, options = {}) {
|
|
241
|
+
const key = this.resolveKey(options.apiKey);
|
|
66
242
|
const params = { source: from, target: to };
|
|
67
243
|
if (amount !== void 0) params.amount = String(amount);
|
|
68
|
-
return this.request("/api/v1/rates", params);
|
|
244
|
+
return this.request("/api/v1/rates", params, key);
|
|
69
245
|
}
|
|
70
246
|
/**
|
|
71
|
-
* Get authenticated exchange
|
|
247
|
+
* Get authenticated exchange rates with metadata.
|
|
248
|
+
*
|
|
249
|
+
* ```ts
|
|
250
|
+
* const rates = await client.getRates('USD', 'EUR');
|
|
251
|
+
* rates.forEach(r => console.log(`${r.source}/${r.target}: ${r.rate} at ${r.time}`));
|
|
252
|
+
* ```
|
|
72
253
|
*/
|
|
73
|
-
async getRates(source, target) {
|
|
74
|
-
|
|
75
|
-
return this.request("/api/v1/rates", { source, target });
|
|
254
|
+
async getRates(source, target, options = {}) {
|
|
255
|
+
const key = this.resolveKey(options.apiKey);
|
|
256
|
+
return this.request("/api/v1/rates", { source, target }, key);
|
|
76
257
|
}
|
|
77
258
|
/**
|
|
78
|
-
* Get historical rates
|
|
259
|
+
* Get historical rates by period.
|
|
260
|
+
*
|
|
261
|
+
* ```ts
|
|
262
|
+
* const history = await client.getHistoricalRates('USD', 'EUR', '30d');
|
|
263
|
+
* history.rates.forEach(point => {
|
|
264
|
+
* console.log(`${point.time}: ${point.rate}`);
|
|
265
|
+
* });
|
|
266
|
+
* ```
|
|
79
267
|
*/
|
|
80
|
-
async getHistoricalRates(source, target, period = "7d") {
|
|
81
|
-
|
|
82
|
-
return this.request(
|
|
83
|
-
|
|
84
|
-
target,
|
|
85
|
-
|
|
86
|
-
|
|
268
|
+
async getHistoricalRates(source, target, period = "7d", options = {}) {
|
|
269
|
+
const key = this.resolveKey(options.apiKey);
|
|
270
|
+
return this.request(
|
|
271
|
+
"/api/historical-rates",
|
|
272
|
+
{ source, target, period },
|
|
273
|
+
key
|
|
274
|
+
);
|
|
87
275
|
}
|
|
88
276
|
/**
|
|
89
|
-
* Convert an amount
|
|
277
|
+
* Convert an amount between two currencies.
|
|
278
|
+
*
|
|
279
|
+
* ```ts
|
|
280
|
+
* // Current rate
|
|
281
|
+
* const result = await client.convert('USD', 'EUR', 1000);
|
|
282
|
+
* console.log(`$1,000 = €${result.result}`);
|
|
283
|
+
*
|
|
284
|
+
* // Historical conversion
|
|
285
|
+
* const result = await client.convert('USD', 'EUR', 1000, { date: '2026-01-15' });
|
|
286
|
+
* console.log(`Rate on Jan 15: ${result.rate}`);
|
|
287
|
+
* ```
|
|
90
288
|
*/
|
|
91
|
-
async convert(from, to, amount) {
|
|
92
|
-
const
|
|
289
|
+
async convert(from, to, amount, options = {}) {
|
|
290
|
+
const key = this.resolveKey(options.apiKey);
|
|
291
|
+
if (options.date) {
|
|
292
|
+
const dateStr = this.formatDate(options.date);
|
|
293
|
+
const params = {
|
|
294
|
+
source: from,
|
|
295
|
+
target: to,
|
|
296
|
+
from: dateStr,
|
|
297
|
+
to: dateStr
|
|
298
|
+
};
|
|
299
|
+
const raw = await this.request(
|
|
300
|
+
"/api/historical-rates",
|
|
301
|
+
params,
|
|
302
|
+
key
|
|
303
|
+
);
|
|
304
|
+
const rate = raw.current?.rate ?? raw.rates?.[0]?.rate ?? 0;
|
|
305
|
+
return { from, to, amount, result: Number((amount * rate).toFixed(6)), rate, date: dateStr };
|
|
306
|
+
}
|
|
307
|
+
const data = await this.request(
|
|
308
|
+
"/api/v1/rates",
|
|
309
|
+
{ source: from, target: to, amount: String(amount) },
|
|
310
|
+
key
|
|
311
|
+
);
|
|
93
312
|
return {
|
|
94
313
|
from,
|
|
95
314
|
to,
|
|
@@ -99,13 +318,6 @@ var AllRatesToday = class {
|
|
|
99
318
|
};
|
|
100
319
|
}
|
|
101
320
|
};
|
|
102
|
-
var AllRatesTodayError = class extends Error {
|
|
103
|
-
constructor(message, status) {
|
|
104
|
-
super(message);
|
|
105
|
-
this.name = "AllRatesTodayError";
|
|
106
|
-
this.status = status;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
321
|
var index_default = AllRatesToday;
|
|
110
322
|
// Annotate the CommonJS export names for ESM import in node:
|
|
111
323
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,51 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
var
|
|
2
|
+
var DEFAULT_BASE_URL = "https://allratestoday.com";
|
|
3
|
+
var DEFAULT_TIMEOUT = 1e4;
|
|
4
|
+
var AllRatesTodayError = class extends Error {
|
|
5
|
+
constructor(message, status) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "AllRatesTodayError";
|
|
8
|
+
this.status = status;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
3
11
|
var AllRatesToday = class {
|
|
4
12
|
/**
|
|
5
13
|
* Create an AllRatesToday client.
|
|
6
|
-
*
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const client = new AllRatesToday({ apiKey: 'art_live_...' });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param options - Configuration. Get a free API key at https://allratestoday.com/register
|
|
7
20
|
*/
|
|
8
21
|
constructor(options = {}) {
|
|
9
22
|
this.apiKey = options.apiKey;
|
|
10
|
-
this.baseUrl = options.baseUrl ||
|
|
11
|
-
this.timeout = options.timeout ||
|
|
23
|
+
this.baseUrl = options.baseUrl || DEFAULT_BASE_URL;
|
|
24
|
+
this.timeout = options.timeout || DEFAULT_TIMEOUT;
|
|
25
|
+
}
|
|
26
|
+
// -------------------------------------------------------------------------
|
|
27
|
+
// Internal
|
|
28
|
+
// -------------------------------------------------------------------------
|
|
29
|
+
resolveKey(override) {
|
|
30
|
+
const key = override || this.apiKey;
|
|
31
|
+
if (!key) {
|
|
32
|
+
throw new AllRatesTodayError(
|
|
33
|
+
"API key is required. Register for free at https://allratestoday.com/register"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return key;
|
|
12
37
|
}
|
|
13
|
-
async request(path, params = {}) {
|
|
38
|
+
async request(path, params = {}, keyOverride) {
|
|
14
39
|
const url = new URL(path, this.baseUrl);
|
|
15
40
|
for (const [key, value] of Object.entries(params)) {
|
|
16
|
-
if (value
|
|
41
|
+
if (value !== void 0 && value !== "") {
|
|
42
|
+
url.searchParams.set(key, value);
|
|
43
|
+
}
|
|
17
44
|
}
|
|
18
45
|
const headers = {};
|
|
19
|
-
|
|
20
|
-
|
|
46
|
+
const apiKey = keyOverride || this.apiKey;
|
|
47
|
+
if (apiKey) {
|
|
48
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
21
49
|
}
|
|
22
50
|
const response = await fetch(url.toString(), {
|
|
23
51
|
headers,
|
|
@@ -32,38 +60,229 @@ var AllRatesToday = class {
|
|
|
32
60
|
}
|
|
33
61
|
return response.json();
|
|
34
62
|
}
|
|
63
|
+
formatDate(date) {
|
|
64
|
+
if (date instanceof Date) {
|
|
65
|
+
return date.toISOString().split("T")[0];
|
|
66
|
+
}
|
|
67
|
+
return date;
|
|
68
|
+
}
|
|
69
|
+
// -------------------------------------------------------------------------
|
|
70
|
+
// Public API
|
|
71
|
+
// -------------------------------------------------------------------------
|
|
72
|
+
/**
|
|
73
|
+
* Get the latest exchange rates.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* // All rates from USD
|
|
77
|
+
* const data = await client.latest();
|
|
78
|
+
*
|
|
79
|
+
* // Specific symbols with EUR base
|
|
80
|
+
* const data = await client.latest({ base: 'EUR', symbols: ['USD', 'GBP', 'JPY'] });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
async latest(options = {}) {
|
|
84
|
+
const key = this.resolveKey(options.apiKey);
|
|
85
|
+
const params = {};
|
|
86
|
+
params.source = options.base || "USD";
|
|
87
|
+
if (options.symbols?.length) {
|
|
88
|
+
params.target = options.symbols.join(",");
|
|
89
|
+
}
|
|
90
|
+
const raw = await this.request(
|
|
91
|
+
"/api/v1/rates",
|
|
92
|
+
params,
|
|
93
|
+
key
|
|
94
|
+
);
|
|
95
|
+
const ratesArray = Array.isArray(raw) ? raw : [raw];
|
|
96
|
+
const rates = {};
|
|
97
|
+
let date = "";
|
|
98
|
+
for (const r of ratesArray) {
|
|
99
|
+
rates[r.target] = r.rate;
|
|
100
|
+
if (r.time) date = r.time;
|
|
101
|
+
}
|
|
102
|
+
return { base: params.source, date, rates };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get exchange rates for a specific historical date.
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* // Using a string
|
|
109
|
+
* const data = await client.forDate('2026-01-15');
|
|
110
|
+
*
|
|
111
|
+
* // Using a Date object
|
|
112
|
+
* const data = await client.forDate(new Date('2026-01-15'));
|
|
113
|
+
*
|
|
114
|
+
* // With options
|
|
115
|
+
* const data = await client.forDate('2026-01-15', { base: 'EUR', symbols: ['USD', 'GBP'] });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
async forDate(date, options = {}) {
|
|
119
|
+
const key = this.resolveKey(options.apiKey);
|
|
120
|
+
const dateStr = this.formatDate(date);
|
|
121
|
+
const params = {
|
|
122
|
+
source: options.base || "USD",
|
|
123
|
+
from: dateStr,
|
|
124
|
+
to: dateStr
|
|
125
|
+
};
|
|
126
|
+
if (options.symbols?.length) {
|
|
127
|
+
params.target = options.symbols.join(",");
|
|
128
|
+
}
|
|
129
|
+
const raw = await this.request(
|
|
130
|
+
"/api/historical-rates",
|
|
131
|
+
params,
|
|
132
|
+
key
|
|
133
|
+
);
|
|
134
|
+
const rates = {};
|
|
135
|
+
if (raw.rates?.length) {
|
|
136
|
+
for (const r of raw.rates) {
|
|
137
|
+
rates[raw.target] = r.rate;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (raw.current) {
|
|
141
|
+
rates[raw.target] = raw.current.rate;
|
|
142
|
+
}
|
|
143
|
+
return { base: params.source, date: dateStr, rates };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get exchange rate time series between two dates.
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* const data = await client.timeSeries('2026-01-01', '2026-03-31', {
|
|
150
|
+
* base: 'USD',
|
|
151
|
+
* symbols: ['EUR', 'GBP'],
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* // Access rates by date
|
|
155
|
+
* console.log(data.rates['2026-01-15']); // { EUR: 0.92, GBP: 0.78 }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
async timeSeries(startDate, endDate, options = {}) {
|
|
159
|
+
const key = this.resolveKey(options.apiKey);
|
|
160
|
+
const start = this.formatDate(startDate);
|
|
161
|
+
const end = this.formatDate(endDate);
|
|
162
|
+
const source = options.base || "USD";
|
|
163
|
+
const params = {
|
|
164
|
+
source,
|
|
165
|
+
from: start,
|
|
166
|
+
to: end
|
|
167
|
+
};
|
|
168
|
+
if (options.symbols?.length) {
|
|
169
|
+
params.target = options.symbols.join(",");
|
|
170
|
+
}
|
|
171
|
+
const raw = await this.request(
|
|
172
|
+
"/api/historical-rates",
|
|
173
|
+
params,
|
|
174
|
+
key
|
|
175
|
+
);
|
|
176
|
+
const rates = {};
|
|
177
|
+
if (raw.rates) {
|
|
178
|
+
for (const point of raw.rates) {
|
|
179
|
+
const dateKey = point.time.split("T")[0];
|
|
180
|
+
if (!rates[dateKey]) rates[dateKey] = {};
|
|
181
|
+
rates[dateKey][raw.target] = point.rate;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { base: source, startDate: start, endDate: end, rates };
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get all supported currency symbols.
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* const { symbols } = await client.symbols();
|
|
191
|
+
* console.log(symbols);
|
|
192
|
+
* // { USD: 'United States Dollar', EUR: 'Euro', GBP: 'British Pound', ... }
|
|
193
|
+
*
|
|
194
|
+
* // Build a currency dropdown
|
|
195
|
+
* const options = Object.entries(symbols).map(([code, name]) => ({ code, name }));
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
async symbols(options = {}) {
|
|
199
|
+
const key = this.resolveKey(options.apiKey);
|
|
200
|
+
return this.request("/api/v1/symbols", {}, key);
|
|
201
|
+
}
|
|
35
202
|
/**
|
|
36
|
-
* Get exchange rate between two currencies
|
|
203
|
+
* Get exchange rate between two currencies.
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* const rate = await client.getRate('USD', 'EUR');
|
|
207
|
+
* console.log(`1 USD = ${rate.rate} EUR`);
|
|
208
|
+
*
|
|
209
|
+
* // With amount
|
|
210
|
+
* const rate = await client.getRate('USD', 'EUR', 1000);
|
|
211
|
+
* console.log(`$1,000 = €${rate.to.amount}`);
|
|
212
|
+
* ```
|
|
37
213
|
*/
|
|
38
|
-
async getRate(from, to, amount) {
|
|
39
|
-
|
|
214
|
+
async getRate(from, to, amount, options = {}) {
|
|
215
|
+
const key = this.resolveKey(options.apiKey);
|
|
40
216
|
const params = { source: from, target: to };
|
|
41
217
|
if (amount !== void 0) params.amount = String(amount);
|
|
42
|
-
return this.request("/api/v1/rates", params);
|
|
218
|
+
return this.request("/api/v1/rates", params, key);
|
|
43
219
|
}
|
|
44
220
|
/**
|
|
45
|
-
* Get authenticated exchange
|
|
221
|
+
* Get authenticated exchange rates with metadata.
|
|
222
|
+
*
|
|
223
|
+
* ```ts
|
|
224
|
+
* const rates = await client.getRates('USD', 'EUR');
|
|
225
|
+
* rates.forEach(r => console.log(`${r.source}/${r.target}: ${r.rate} at ${r.time}`));
|
|
226
|
+
* ```
|
|
46
227
|
*/
|
|
47
|
-
async getRates(source, target) {
|
|
48
|
-
|
|
49
|
-
return this.request("/api/v1/rates", { source, target });
|
|
228
|
+
async getRates(source, target, options = {}) {
|
|
229
|
+
const key = this.resolveKey(options.apiKey);
|
|
230
|
+
return this.request("/api/v1/rates", { source, target }, key);
|
|
50
231
|
}
|
|
51
232
|
/**
|
|
52
|
-
* Get historical rates
|
|
233
|
+
* Get historical rates by period.
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* const history = await client.getHistoricalRates('USD', 'EUR', '30d');
|
|
237
|
+
* history.rates.forEach(point => {
|
|
238
|
+
* console.log(`${point.time}: ${point.rate}`);
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
53
241
|
*/
|
|
54
|
-
async getHistoricalRates(source, target, period = "7d") {
|
|
55
|
-
|
|
56
|
-
return this.request(
|
|
57
|
-
|
|
58
|
-
target,
|
|
59
|
-
|
|
60
|
-
|
|
242
|
+
async getHistoricalRates(source, target, period = "7d", options = {}) {
|
|
243
|
+
const key = this.resolveKey(options.apiKey);
|
|
244
|
+
return this.request(
|
|
245
|
+
"/api/historical-rates",
|
|
246
|
+
{ source, target, period },
|
|
247
|
+
key
|
|
248
|
+
);
|
|
61
249
|
}
|
|
62
250
|
/**
|
|
63
|
-
* Convert an amount
|
|
251
|
+
* Convert an amount between two currencies.
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* // Current rate
|
|
255
|
+
* const result = await client.convert('USD', 'EUR', 1000);
|
|
256
|
+
* console.log(`$1,000 = €${result.result}`);
|
|
257
|
+
*
|
|
258
|
+
* // Historical conversion
|
|
259
|
+
* const result = await client.convert('USD', 'EUR', 1000, { date: '2026-01-15' });
|
|
260
|
+
* console.log(`Rate on Jan 15: ${result.rate}`);
|
|
261
|
+
* ```
|
|
64
262
|
*/
|
|
65
|
-
async convert(from, to, amount) {
|
|
66
|
-
const
|
|
263
|
+
async convert(from, to, amount, options = {}) {
|
|
264
|
+
const key = this.resolveKey(options.apiKey);
|
|
265
|
+
if (options.date) {
|
|
266
|
+
const dateStr = this.formatDate(options.date);
|
|
267
|
+
const params = {
|
|
268
|
+
source: from,
|
|
269
|
+
target: to,
|
|
270
|
+
from: dateStr,
|
|
271
|
+
to: dateStr
|
|
272
|
+
};
|
|
273
|
+
const raw = await this.request(
|
|
274
|
+
"/api/historical-rates",
|
|
275
|
+
params,
|
|
276
|
+
key
|
|
277
|
+
);
|
|
278
|
+
const rate = raw.current?.rate ?? raw.rates?.[0]?.rate ?? 0;
|
|
279
|
+
return { from, to, amount, result: Number((amount * rate).toFixed(6)), rate, date: dateStr };
|
|
280
|
+
}
|
|
281
|
+
const data = await this.request(
|
|
282
|
+
"/api/v1/rates",
|
|
283
|
+
{ source: from, target: to, amount: String(amount) },
|
|
284
|
+
key
|
|
285
|
+
);
|
|
67
286
|
return {
|
|
68
287
|
from,
|
|
69
288
|
to,
|
|
@@ -73,13 +292,6 @@ var AllRatesToday = class {
|
|
|
73
292
|
};
|
|
74
293
|
}
|
|
75
294
|
};
|
|
76
|
-
var AllRatesTodayError = class extends Error {
|
|
77
|
-
constructor(message, status) {
|
|
78
|
-
super(message);
|
|
79
|
-
this.name = "AllRatesTodayError";
|
|
80
|
-
this.status = status;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
295
|
var index_default = AllRatesToday;
|
|
84
296
|
export {
|
|
85
297
|
AllRatesToday,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@allratestoday/sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "The most elegant way to access real-time mid-market exchange rates in Node.js and browsers. 160+ currencies, Reuters/Refinitiv data, zero dependencies.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"homepage": "https://allratestoday.com",
|
|
33
33
|
"repository": {
|
|
34
34
|
"type": "git",
|
|
35
|
-
"url": "https://github.com/allratestoday/
|
|
35
|
+
"url": "https://github.com/allratestoday/exchange-rates-api",
|
|
36
|
+
"directory": "sdks/javascript"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"tsup": "^8.0.0",
|