@allratestoday/sdk 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.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # @allratestoday/sdk
2
+
3
+ Official JavaScript/TypeScript SDK for the [AllRatesToday](https://allratestoday.com) exchange rate API.
4
+
5
+ Real-time mid-market exchange rates for 160+ currencies, sourced from Reuters (Refinitiv) and interbank market feeds.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @allratestoday/sdk
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### Free Public Rates (No API Key)
16
+
17
+ ```typescript
18
+ import AllRatesToday from '@allratestoday/sdk';
19
+
20
+ const client = new AllRatesToday();
21
+
22
+ // Get exchange rate
23
+ const rate = await client.getRate('USD', 'EUR');
24
+ console.log(`1 USD = ${rate.rate} EUR`);
25
+
26
+ // Convert amount
27
+ const result = await client.convert('USD', 'EUR', 100);
28
+ console.log(`$100 = €${result.result}`);
29
+ ```
30
+
31
+ ### Authenticated Rates (API Key Required)
32
+
33
+ ```typescript
34
+ import AllRatesToday from '@allratestoday/sdk';
35
+
36
+ const client = new AllRatesToday({ apiKey: 'art_live_your_key_here' });
37
+
38
+ // Get authenticated rates
39
+ const rates = await client.getRates('USD', 'EUR');
40
+ console.log(rates);
41
+
42
+ // Get historical rates
43
+ const history = await client.getHistoricalRates('USD', 'EUR', '30d');
44
+ console.log(history.rates);
45
+ ```
46
+
47
+ ## API Reference
48
+
49
+ ### `new AllRatesToday(options?)`
50
+
51
+ | Option | Type | Default | Description |
52
+ |--------|------|---------|-------------|
53
+ | `apiKey` | `string` | — | Your API key (from [dashboard](https://allratestoday.com/profile)) |
54
+ | `baseUrl` | `string` | `https://allratestoday.com` | API base URL |
55
+ | `timeout` | `number` | `10000` | Request timeout in ms |
56
+
57
+ ### Methods
58
+
59
+ | Method | Auth | Description |
60
+ |--------|------|-------------|
61
+ | `getRate(from, to, amount?)` | No | Get exchange rate (free) |
62
+ | `convert(from, to, amount)` | No | Convert amount between currencies |
63
+ | `getRates(source, target)` | Yes | Authenticated rate lookup |
64
+ | `getHistoricalRates(source, target, period)` | Yes | Historical rates (1d/7d/30d/1y) |
65
+
66
+ ### Error Handling
67
+
68
+ ```typescript
69
+ import AllRatesToday, { AllRatesTodayError } from '@allratestoday/sdk';
70
+
71
+ try {
72
+ const rate = await client.getRate('USD', 'INVALID');
73
+ } catch (err) {
74
+ if (err instanceof AllRatesTodayError) {
75
+ console.log(err.message); // Error description
76
+ console.log(err.status); // HTTP status code
77
+ }
78
+ }
79
+ ```
80
+
81
+ ## Links
82
+
83
+ - [API Documentation](https://allratestoday.com/docs)
84
+ - [Get API Key](https://allratestoday.com/profile)
85
+ - [Pricing](https://allratestoday.com/pricing)
86
+ - [Status](https://allratestoday.com/status)
87
+
88
+ ## License
89
+
90
+ MIT
@@ -0,0 +1,71 @@
1
+ interface RateResponse {
2
+ from: {
3
+ currency: string;
4
+ amount: number;
5
+ };
6
+ to: {
7
+ currency: string;
8
+ amount: number;
9
+ };
10
+ rate: number;
11
+ source: string;
12
+ }
13
+ interface AuthRateResponse {
14
+ rate: number;
15
+ source: string;
16
+ target: string;
17
+ time: string;
18
+ }
19
+ interface HistoricalRateResponse {
20
+ source: string;
21
+ target: string;
22
+ period: string;
23
+ current: {
24
+ rate: number;
25
+ time: string;
26
+ };
27
+ rates: Array<{
28
+ rate: number;
29
+ time: string;
30
+ }>;
31
+ }
32
+ interface AllRatesTodayOptions {
33
+ apiKey?: string;
34
+ baseUrl?: string;
35
+ timeout?: number;
36
+ }
37
+ declare class AllRatesToday {
38
+ private apiKey?;
39
+ private baseUrl;
40
+ private timeout;
41
+ constructor(options?: AllRatesTodayOptions);
42
+ private request;
43
+ /**
44
+ * Get exchange rate between two currencies (free, no auth required)
45
+ */
46
+ getRate(from: string, to: string, amount?: number): Promise<RateResponse>;
47
+ /**
48
+ * Get authenticated exchange rate (requires API key)
49
+ */
50
+ getRates(source: string, target: string): Promise<AuthRateResponse[]>;
51
+ /**
52
+ * Get historical rates (requires API key)
53
+ */
54
+ getHistoricalRates(source: string, target: string, period?: '1d' | '7d' | '30d' | '1y'): Promise<HistoricalRateResponse>;
55
+ /**
56
+ * Convert an amount from one currency to another
57
+ */
58
+ convert(from: string, to: string, amount: number): Promise<{
59
+ from: string;
60
+ to: string;
61
+ amount: number;
62
+ result: number;
63
+ rate: number;
64
+ }>;
65
+ }
66
+ declare class AllRatesTodayError extends Error {
67
+ status?: number;
68
+ constructor(message: string, status?: number);
69
+ }
70
+
71
+ export { AllRatesToday, AllRatesTodayError, type AllRatesTodayOptions, type AuthRateResponse, type HistoricalRateResponse, type RateResponse, AllRatesToday as default };
@@ -0,0 +1,71 @@
1
+ interface RateResponse {
2
+ from: {
3
+ currency: string;
4
+ amount: number;
5
+ };
6
+ to: {
7
+ currency: string;
8
+ amount: number;
9
+ };
10
+ rate: number;
11
+ source: string;
12
+ }
13
+ interface AuthRateResponse {
14
+ rate: number;
15
+ source: string;
16
+ target: string;
17
+ time: string;
18
+ }
19
+ interface HistoricalRateResponse {
20
+ source: string;
21
+ target: string;
22
+ period: string;
23
+ current: {
24
+ rate: number;
25
+ time: string;
26
+ };
27
+ rates: Array<{
28
+ rate: number;
29
+ time: string;
30
+ }>;
31
+ }
32
+ interface AllRatesTodayOptions {
33
+ apiKey?: string;
34
+ baseUrl?: string;
35
+ timeout?: number;
36
+ }
37
+ declare class AllRatesToday {
38
+ private apiKey?;
39
+ private baseUrl;
40
+ private timeout;
41
+ constructor(options?: AllRatesTodayOptions);
42
+ private request;
43
+ /**
44
+ * Get exchange rate between two currencies (free, no auth required)
45
+ */
46
+ getRate(from: string, to: string, amount?: number): Promise<RateResponse>;
47
+ /**
48
+ * Get authenticated exchange rate (requires API key)
49
+ */
50
+ getRates(source: string, target: string): Promise<AuthRateResponse[]>;
51
+ /**
52
+ * Get historical rates (requires API key)
53
+ */
54
+ getHistoricalRates(source: string, target: string, period?: '1d' | '7d' | '30d' | '1y'): Promise<HistoricalRateResponse>;
55
+ /**
56
+ * Convert an amount from one currency to another
57
+ */
58
+ convert(from: string, to: string, amount: number): Promise<{
59
+ from: string;
60
+ to: string;
61
+ amount: number;
62
+ result: number;
63
+ rate: number;
64
+ }>;
65
+ }
66
+ declare class AllRatesTodayError extends Error {
67
+ status?: number;
68
+ constructor(message: string, status?: number);
69
+ }
70
+
71
+ export { AllRatesToday, AllRatesTodayError, type AllRatesTodayOptions, type AuthRateResponse, type HistoricalRateResponse, type RateResponse, AllRatesToday as default };
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AllRatesToday: () => AllRatesToday,
24
+ AllRatesTodayError: () => AllRatesTodayError,
25
+ default: () => index_default
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+ var BASE_URL = "https://allratestoday.com";
29
+ var AllRatesToday = class {
30
+ constructor(options = {}) {
31
+ this.apiKey = options.apiKey;
32
+ this.baseUrl = options.baseUrl || BASE_URL;
33
+ this.timeout = options.timeout || 1e4;
34
+ }
35
+ async request(path, params = {}) {
36
+ const url = new URL(path, this.baseUrl);
37
+ for (const [key, value] of Object.entries(params)) {
38
+ if (value) url.searchParams.set(key, value);
39
+ }
40
+ const headers = {};
41
+ if (this.apiKey) {
42
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
43
+ }
44
+ const response = await fetch(url.toString(), {
45
+ headers,
46
+ signal: AbortSignal.timeout(this.timeout)
47
+ });
48
+ if (!response.ok) {
49
+ const error = await response.json().catch(() => ({ error: response.statusText }));
50
+ throw new AllRatesTodayError(
51
+ error.error || `HTTP ${response.status}`,
52
+ response.status
53
+ );
54
+ }
55
+ return response.json();
56
+ }
57
+ /**
58
+ * Get exchange rate between two currencies (free, no auth required)
59
+ */
60
+ async getRate(from, to, amount) {
61
+ const params = { from, to };
62
+ if (amount !== void 0) params.amount = String(amount);
63
+ return this.request("/api/public/rates", params);
64
+ }
65
+ /**
66
+ * Get authenticated exchange rate (requires API key)
67
+ */
68
+ async getRates(source, target) {
69
+ if (!this.apiKey) throw new AllRatesTodayError("API key required for authenticated requests");
70
+ return this.request("/api/v1/rates", { source, target });
71
+ }
72
+ /**
73
+ * Get historical rates (requires API key)
74
+ */
75
+ async getHistoricalRates(source, target, period = "7d") {
76
+ if (!this.apiKey) throw new AllRatesTodayError("API key required for historical rates");
77
+ return this.request("/api/historical-rates", {
78
+ source,
79
+ target,
80
+ period
81
+ });
82
+ }
83
+ /**
84
+ * Convert an amount from one currency to another
85
+ */
86
+ async convert(from, to, amount) {
87
+ const data = await this.getRate(from, to, amount);
88
+ return {
89
+ from,
90
+ to,
91
+ amount,
92
+ result: data.to.amount,
93
+ rate: data.rate
94
+ };
95
+ }
96
+ };
97
+ var AllRatesTodayError = class extends Error {
98
+ constructor(message, status) {
99
+ super(message);
100
+ this.name = "AllRatesTodayError";
101
+ this.status = status;
102
+ }
103
+ };
104
+ var index_default = AllRatesToday;
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ AllRatesToday,
108
+ AllRatesTodayError
109
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,83 @@
1
+ // src/index.ts
2
+ var BASE_URL = "https://allratestoday.com";
3
+ var AllRatesToday = class {
4
+ constructor(options = {}) {
5
+ this.apiKey = options.apiKey;
6
+ this.baseUrl = options.baseUrl || BASE_URL;
7
+ this.timeout = options.timeout || 1e4;
8
+ }
9
+ async request(path, params = {}) {
10
+ const url = new URL(path, this.baseUrl);
11
+ for (const [key, value] of Object.entries(params)) {
12
+ if (value) url.searchParams.set(key, value);
13
+ }
14
+ const headers = {};
15
+ if (this.apiKey) {
16
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
17
+ }
18
+ const response = await fetch(url.toString(), {
19
+ headers,
20
+ signal: AbortSignal.timeout(this.timeout)
21
+ });
22
+ if (!response.ok) {
23
+ const error = await response.json().catch(() => ({ error: response.statusText }));
24
+ throw new AllRatesTodayError(
25
+ error.error || `HTTP ${response.status}`,
26
+ response.status
27
+ );
28
+ }
29
+ return response.json();
30
+ }
31
+ /**
32
+ * Get exchange rate between two currencies (free, no auth required)
33
+ */
34
+ async getRate(from, to, amount) {
35
+ const params = { from, to };
36
+ if (amount !== void 0) params.amount = String(amount);
37
+ return this.request("/api/public/rates", params);
38
+ }
39
+ /**
40
+ * Get authenticated exchange rate (requires API key)
41
+ */
42
+ async getRates(source, target) {
43
+ if (!this.apiKey) throw new AllRatesTodayError("API key required for authenticated requests");
44
+ return this.request("/api/v1/rates", { source, target });
45
+ }
46
+ /**
47
+ * Get historical rates (requires API key)
48
+ */
49
+ async getHistoricalRates(source, target, period = "7d") {
50
+ if (!this.apiKey) throw new AllRatesTodayError("API key required for historical rates");
51
+ return this.request("/api/historical-rates", {
52
+ source,
53
+ target,
54
+ period
55
+ });
56
+ }
57
+ /**
58
+ * Convert an amount from one currency to another
59
+ */
60
+ async convert(from, to, amount) {
61
+ const data = await this.getRate(from, to, amount);
62
+ return {
63
+ from,
64
+ to,
65
+ amount,
66
+ result: data.to.amount,
67
+ rate: data.rate
68
+ };
69
+ }
70
+ };
71
+ var AllRatesTodayError = class extends Error {
72
+ constructor(message, status) {
73
+ super(message);
74
+ this.name = "AllRatesTodayError";
75
+ this.status = status;
76
+ }
77
+ };
78
+ var index_default = AllRatesToday;
79
+ export {
80
+ AllRatesToday,
81
+ AllRatesTodayError,
82
+ index_default as default
83
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@allratestoday/sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official JavaScript/TypeScript SDK for AllRatesToday exchange rate API",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": ["dist"],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "tsup src/index.ts --format cjs,esm --dts",
18
+ "prepublishOnly": "npm run build"
19
+ },
20
+ "keywords": [
21
+ "exchange-rate",
22
+ "currency",
23
+ "forex",
24
+ "api",
25
+ "allratestoday",
26
+ "conversion",
27
+ "mid-market-rate",
28
+ "reuters"
29
+ ],
30
+ "author": "AllRatesToday <info@allratestoday.com>",
31
+ "license": "MIT",
32
+ "homepage": "https://allratestoday.com",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/allratestoday/sdk-javascript"
36
+ },
37
+ "devDependencies": {
38
+ "tsup": "^8.0.0",
39
+ "typescript": "^5.0.0"
40
+ }
41
+ }