@molecule/api-fx-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.
- package/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +70 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +94 -0
- package/dist/provider.js.map +1 -0
- package/dist/types.d.ts +116 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-fx-rates` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-fx-rates is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=browser-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,iGAAiG;QAC/F,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-agnostic foreign-exchange rates interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `FxRatesProvider` interface for currency conversion and daily
|
|
5
|
+
* reference-rate lookups. Bond packages (ECB, OpenExchange, etc.) implement
|
|
6
|
+
* this interface. Application code uses the convenience functions
|
|
7
|
+
* (`getRate`, `getDailyRates`, `convert`, `listSupportedCurrencies`) which
|
|
8
|
+
* delegate to the bonded provider.
|
|
9
|
+
*
|
|
10
|
+
* Rates are normalized as plain `number` ratios: `1 unit of FROM = rate units of TO`.
|
|
11
|
+
* Currency codes are ISO 4217 three-letter strings (e.g. `'USD'`, `'EUR'`, `'JPY'`).
|
|
12
|
+
* Amounts are integer minor units (cents) to avoid floating-point drift.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { setProvider, getRate, convert } from '@molecule/api-fx-rates'
|
|
17
|
+
* import { provider as ecb } from '@molecule/api-fx-rates-ecb'
|
|
18
|
+
*
|
|
19
|
+
* setProvider(ecb)
|
|
20
|
+
* const eurUsd = await getRate('EUR', 'USD')
|
|
21
|
+
* const usdCents = await convert(10_000, 'EUR', 'USD') // 10000 EUR cents -> USD cents
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @e2e
|
|
25
|
+
* Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
26
|
+
* each item to this app's actual currency/pricing screens, and check every box
|
|
27
|
+
* off one by one. This is money: a wrong rate silently corrupts every price, so
|
|
28
|
+
* verify the NUMBERS the app computes, not just that a value rendered. A box
|
|
29
|
+
* you can't check is an integration bug to fix — not a skip:
|
|
30
|
+
* - [ ] A known pair returns a PLAUSIBLE rate: `getRate('USD', 'EUR')` yields a
|
|
31
|
+
* real ratio (roughly 0.8-1.0 for USD->EUR), never 0, null, NaN, negative, or
|
|
32
|
+
* an absurd value like 1e9 — and the UI shows it as an actual number.
|
|
33
|
+
* - [ ] `convert` does the CORRECT MATH: converting 100 USD (amountMinor
|
|
34
|
+
* `10_000` cents) USD->EUR returns approximately `10_000 * rate` in the
|
|
35
|
+
* target's minor units, rounded sensibly for that currency (integer cents;
|
|
36
|
+
* JPY has 0 decimals), and the UI shows that converted amount — not the
|
|
37
|
+
* untouched original.
|
|
38
|
+
* - [ ] Round-trip consistency: the inverse pair is reciprocal —
|
|
39
|
+
* `getRate('EUR', 'USD')` is approximately `1 / getRate('USD', 'EUR')` — and
|
|
40
|
+
* same-currency is identity: `getRate('USD', 'USD')` is exactly `1` and
|
|
41
|
+
* `convert(x, 'USD', 'USD')` returns `x` unchanged.
|
|
42
|
+
* - [ ] Changing the selected pair changes the displayed result (USD->EUR vs
|
|
43
|
+
* USD->JPY give visibly different converted amounts) — the screen is not
|
|
44
|
+
* pinned to one hardcoded rate.
|
|
45
|
+
* - [ ] An unknown/unsupported code (e.g. `'ZZZ'`, absent from
|
|
46
|
+
* `listSupportedCurrencies()`) surfaces a clear error in the UI — NEVER a
|
|
47
|
+
* silent rate of 0 (which zeroes the price) or a pass-through of 1.
|
|
48
|
+
* - [ ] If a historical lookup is exposed (`options.asOf`), a past date returns
|
|
49
|
+
* that day's rate (different from latest for a volatile pair), not today's.
|
|
50
|
+
* - [ ] A provider/network failure surfaces gracefully (a visible error or
|
|
51
|
+
* retry) and the amount is left unconverted — the app NEVER falls back to
|
|
52
|
+
* converting at rate 0 or 1, which would corrupt the price shown or charged.
|
|
53
|
+
* - [ ] The provider API key stays server-side — fx-rates is server-only; the
|
|
54
|
+
* key never appears in the browser bundle, the network tab, or any client
|
|
55
|
+
* response.
|
|
56
|
+
*
|
|
57
|
+
* @module
|
|
58
|
+
*/
|
|
59
|
+
export * from './browser-guard.js';
|
|
60
|
+
export * from './types.js';
|
|
61
|
+
export * from './provider.js';
|
|
62
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AAGH,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAG1B,cAAc,eAAe,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-agnostic foreign-exchange rates interface for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* Defines the `FxRatesProvider` interface for currency conversion and daily
|
|
5
|
+
* reference-rate lookups. Bond packages (ECB, OpenExchange, etc.) implement
|
|
6
|
+
* this interface. Application code uses the convenience functions
|
|
7
|
+
* (`getRate`, `getDailyRates`, `convert`, `listSupportedCurrencies`) which
|
|
8
|
+
* delegate to the bonded provider.
|
|
9
|
+
*
|
|
10
|
+
* Rates are normalized as plain `number` ratios: `1 unit of FROM = rate units of TO`.
|
|
11
|
+
* Currency codes are ISO 4217 three-letter strings (e.g. `'USD'`, `'EUR'`, `'JPY'`).
|
|
12
|
+
* Amounts are integer minor units (cents) to avoid floating-point drift.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { setProvider, getRate, convert } from '@molecule/api-fx-rates'
|
|
17
|
+
* import { provider as ecb } from '@molecule/api-fx-rates-ecb'
|
|
18
|
+
*
|
|
19
|
+
* setProvider(ecb)
|
|
20
|
+
* const eurUsd = await getRate('EUR', 'USD')
|
|
21
|
+
* const usdCents = await convert(10_000, 'EUR', 'USD') // 10000 EUR cents -> USD cents
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @e2e
|
|
25
|
+
* Integration checklist — drive the real UI (live preview, no mocks), adapt
|
|
26
|
+
* each item to this app's actual currency/pricing screens, and check every box
|
|
27
|
+
* off one by one. This is money: a wrong rate silently corrupts every price, so
|
|
28
|
+
* verify the NUMBERS the app computes, not just that a value rendered. A box
|
|
29
|
+
* you can't check is an integration bug to fix — not a skip:
|
|
30
|
+
* - [ ] A known pair returns a PLAUSIBLE rate: `getRate('USD', 'EUR')` yields a
|
|
31
|
+
* real ratio (roughly 0.8-1.0 for USD->EUR), never 0, null, NaN, negative, or
|
|
32
|
+
* an absurd value like 1e9 — and the UI shows it as an actual number.
|
|
33
|
+
* - [ ] `convert` does the CORRECT MATH: converting 100 USD (amountMinor
|
|
34
|
+
* `10_000` cents) USD->EUR returns approximately `10_000 * rate` in the
|
|
35
|
+
* target's minor units, rounded sensibly for that currency (integer cents;
|
|
36
|
+
* JPY has 0 decimals), and the UI shows that converted amount — not the
|
|
37
|
+
* untouched original.
|
|
38
|
+
* - [ ] Round-trip consistency: the inverse pair is reciprocal —
|
|
39
|
+
* `getRate('EUR', 'USD')` is approximately `1 / getRate('USD', 'EUR')` — and
|
|
40
|
+
* same-currency is identity: `getRate('USD', 'USD')` is exactly `1` and
|
|
41
|
+
* `convert(x, 'USD', 'USD')` returns `x` unchanged.
|
|
42
|
+
* - [ ] Changing the selected pair changes the displayed result (USD->EUR vs
|
|
43
|
+
* USD->JPY give visibly different converted amounts) — the screen is not
|
|
44
|
+
* pinned to one hardcoded rate.
|
|
45
|
+
* - [ ] An unknown/unsupported code (e.g. `'ZZZ'`, absent from
|
|
46
|
+
* `listSupportedCurrencies()`) surfaces a clear error in the UI — NEVER a
|
|
47
|
+
* silent rate of 0 (which zeroes the price) or a pass-through of 1.
|
|
48
|
+
* - [ ] If a historical lookup is exposed (`options.asOf`), a past date returns
|
|
49
|
+
* that day's rate (different from latest for a volatile pair), not today's.
|
|
50
|
+
* - [ ] A provider/network failure surfaces gracefully (a visible error or
|
|
51
|
+
* retry) and the amount is left unconverted — the app NEVER falls back to
|
|
52
|
+
* converting at rate 0 or 1, which would corrupt the price shown or charged.
|
|
53
|
+
* - [ ] The provider API key stays server-side — fx-rates is server-only; the
|
|
54
|
+
* key never appears in the browser bundle, the network tab, or any client
|
|
55
|
+
* response.
|
|
56
|
+
*
|
|
57
|
+
* @module
|
|
58
|
+
*/
|
|
59
|
+
// Type exports
|
|
60
|
+
export * from './browser-guard.js';
|
|
61
|
+
export * from './types.js';
|
|
62
|
+
// Provider exports
|
|
63
|
+
export * from './provider.js';
|
|
64
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AAEH,eAAe;AACf,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAE1B,mBAAmB;AACnB,cAAc,eAAe,CAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FX-rates provider bond accessor and convenience functions.
|
|
3
|
+
*
|
|
4
|
+
* Bond packages (e.g. `@molecule/api-fx-rates-ecb`) call `setProvider()` during
|
|
5
|
+
* application startup. Application code uses the convenience functions
|
|
6
|
+
* (`getRate`, `getDailyRates`, `convert`, `listSupportedCurrencies`) which
|
|
7
|
+
* delegate to the bonded provider via `@molecule/api-bond`.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { CurrencyCode, FxDailyRates, FxRatesOptions, FxRatesProvider } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Registers an FX-rates provider as the active singleton. Called by bond
|
|
14
|
+
* packages (e.g. `@molecule/api-fx-rates-ecb`) during application startup.
|
|
15
|
+
*
|
|
16
|
+
* @param provider - The FX-rates provider implementation to bond.
|
|
17
|
+
*/
|
|
18
|
+
export declare const setProvider: (provider: FxRatesProvider) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves the bonded FX-rates provider, throwing if none is configured.
|
|
21
|
+
*
|
|
22
|
+
* @returns The bonded FX-rates provider.
|
|
23
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
24
|
+
*/
|
|
25
|
+
export declare const getProvider: () => FxRatesProvider;
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether an FX-rates provider is currently bonded.
|
|
28
|
+
*
|
|
29
|
+
* @returns `true` if an FX-rates provider is bonded.
|
|
30
|
+
*/
|
|
31
|
+
export declare const hasProvider: () => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Looks up the conversion rate `1 unit of from = rate units of to`.
|
|
34
|
+
*
|
|
35
|
+
* @param from - Source currency (ISO 4217).
|
|
36
|
+
* @param to - Target currency (ISO 4217).
|
|
37
|
+
* @param options - Optional asOf date for historical rates.
|
|
38
|
+
* @returns The conversion ratio as a plain number.
|
|
39
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
40
|
+
*/
|
|
41
|
+
export declare const getRate: (from: CurrencyCode, to: CurrencyCode, options?: FxRatesOptions) => Promise<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns all reference rates the bonded provider publishes for the given day,
|
|
44
|
+
* normalized against the provider's pivot currency.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Optional asOf date for the daily snapshot.
|
|
47
|
+
* @returns The full daily snapshot.
|
|
48
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
49
|
+
*/
|
|
50
|
+
export declare const getDailyRates: (options?: FxRatesOptions) => Promise<FxDailyRates>;
|
|
51
|
+
/**
|
|
52
|
+
* Converts an integer minor-unit amount (e.g. cents) from one currency to
|
|
53
|
+
* another using the bonded provider.
|
|
54
|
+
*
|
|
55
|
+
* @param amountMinor - Amount in minor units of {@link from} (e.g. cents).
|
|
56
|
+
* @param from - Source currency (ISO 4217).
|
|
57
|
+
* @param to - Target currency (ISO 4217).
|
|
58
|
+
* @param options - Optional asOf date for historical rates.
|
|
59
|
+
* @returns Converted amount in minor units of {@link to}.
|
|
60
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
61
|
+
*/
|
|
62
|
+
export declare const convert: (amountMinor: number, from: CurrencyCode, to: CurrencyCode, options?: FxRatesOptions) => Promise<number>;
|
|
63
|
+
/**
|
|
64
|
+
* Lists every currency the bonded provider currently supports.
|
|
65
|
+
*
|
|
66
|
+
* @returns Array of ISO 4217 currency codes.
|
|
67
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
68
|
+
*/
|
|
69
|
+
export declare const listSupportedCurrencies: () => Promise<CurrencyCode[]>;
|
|
70
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAK7F;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,eAAe,KAAG,IAEvD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,QAAO,eAW9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,QAAO,OAE9B,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO,GAClB,MAAM,YAAY,EAClB,IAAI,YAAY,EAChB,UAAU,cAAc,KACvB,OAAO,CAAC,MAAM,CAEhB,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAU,UAAU,cAAc,KAAG,OAAO,CAAC,YAAY,CAElF,CAAA;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO,GAClB,aAAa,MAAM,EACnB,MAAM,YAAY,EAClB,IAAI,YAAY,EAChB,UAAU,cAAc,KACvB,OAAO,CAAC,MAAM,CAEhB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,QAAa,OAAO,CAAC,YAAY,EAAE,CAEtE,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FX-rates provider bond accessor and convenience functions.
|
|
3
|
+
*
|
|
4
|
+
* Bond packages (e.g. `@molecule/api-fx-rates-ecb`) call `setProvider()` during
|
|
5
|
+
* application startup. Application code uses the convenience functions
|
|
6
|
+
* (`getRate`, `getDailyRates`, `convert`, `listSupportedCurrencies`) which
|
|
7
|
+
* delegate to the bonded provider via `@molecule/api-bond`.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { bond, expectBond, isBonded, require as bondRequire } from '@molecule/api-bond';
|
|
12
|
+
import { t } from '@molecule/api-i18n';
|
|
13
|
+
const BOND_TYPE = 'fx-rates';
|
|
14
|
+
expectBond(BOND_TYPE);
|
|
15
|
+
/**
|
|
16
|
+
* Registers an FX-rates provider as the active singleton. Called by bond
|
|
17
|
+
* packages (e.g. `@molecule/api-fx-rates-ecb`) during application startup.
|
|
18
|
+
*
|
|
19
|
+
* @param provider - The FX-rates provider implementation to bond.
|
|
20
|
+
*/
|
|
21
|
+
export const setProvider = (provider) => {
|
|
22
|
+
bond(BOND_TYPE, provider);
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves the bonded FX-rates provider, throwing if none is configured.
|
|
26
|
+
*
|
|
27
|
+
* @returns The bonded FX-rates provider.
|
|
28
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
29
|
+
*/
|
|
30
|
+
export const getProvider = () => {
|
|
31
|
+
try {
|
|
32
|
+
return bondRequire(BOND_TYPE);
|
|
33
|
+
}
|
|
34
|
+
catch (_error) {
|
|
35
|
+
throw new Error(t('fxRates.error.noProvider', undefined, {
|
|
36
|
+
defaultValue: 'FX-rates provider not configured. Call setProvider() first.',
|
|
37
|
+
}), { cause: _error });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether an FX-rates provider is currently bonded.
|
|
42
|
+
*
|
|
43
|
+
* @returns `true` if an FX-rates provider is bonded.
|
|
44
|
+
*/
|
|
45
|
+
export const hasProvider = () => {
|
|
46
|
+
return isBonded(BOND_TYPE);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Looks up the conversion rate `1 unit of from = rate units of to`.
|
|
50
|
+
*
|
|
51
|
+
* @param from - Source currency (ISO 4217).
|
|
52
|
+
* @param to - Target currency (ISO 4217).
|
|
53
|
+
* @param options - Optional asOf date for historical rates.
|
|
54
|
+
* @returns The conversion ratio as a plain number.
|
|
55
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
56
|
+
*/
|
|
57
|
+
export const getRate = async (from, to, options) => {
|
|
58
|
+
return getProvider().getRate(from, to, options);
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Returns all reference rates the bonded provider publishes for the given day,
|
|
62
|
+
* normalized against the provider's pivot currency.
|
|
63
|
+
*
|
|
64
|
+
* @param options - Optional asOf date for the daily snapshot.
|
|
65
|
+
* @returns The full daily snapshot.
|
|
66
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
67
|
+
*/
|
|
68
|
+
export const getDailyRates = async (options) => {
|
|
69
|
+
return getProvider().getDailyRates(options);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Converts an integer minor-unit amount (e.g. cents) from one currency to
|
|
73
|
+
* another using the bonded provider.
|
|
74
|
+
*
|
|
75
|
+
* @param amountMinor - Amount in minor units of {@link from} (e.g. cents).
|
|
76
|
+
* @param from - Source currency (ISO 4217).
|
|
77
|
+
* @param to - Target currency (ISO 4217).
|
|
78
|
+
* @param options - Optional asOf date for historical rates.
|
|
79
|
+
* @returns Converted amount in minor units of {@link to}.
|
|
80
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
81
|
+
*/
|
|
82
|
+
export const convert = async (amountMinor, from, to, options) => {
|
|
83
|
+
return getProvider().convert(amountMinor, from, to, options);
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Lists every currency the bonded provider currently supports.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of ISO 4217 currency codes.
|
|
89
|
+
* @throws {Error} If no FX-rates provider has been bonded.
|
|
90
|
+
*/
|
|
91
|
+
export const listSupportedCurrencies = async () => {
|
|
92
|
+
return getProvider().listSupportedCurrencies();
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACvF,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAItC,MAAM,SAAS,GAAG,UAAU,CAAA;AAC5B,UAAU,CAAC,SAAS,CAAC,CAAA;AAErB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAyB,EAAQ,EAAE;IAC7D,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAoB,EAAE;IAC/C,IAAI,CAAC;QACH,OAAO,WAAW,CAAkB,SAAS,CAAC,CAAA;IAChD,CAAC;IAAC,OAAO,MAAM,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CACb,CAAC,CAAC,0BAA0B,EAAE,SAAS,EAAE;YACvC,YAAY,EAAE,6DAA6D;SAC5E,CAAC,EACF,EAAE,KAAK,EAAE,MAAM,EAAE,CAClB,CAAA;IACH,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAY,EAAE;IACvC,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAC1B,IAAkB,EAClB,EAAgB,EAChB,OAAwB,EACP,EAAE;IACnB,OAAO,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAAA;AACjD,CAAC,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,OAAwB,EAAyB,EAAE;IACrF,OAAO,WAAW,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;AAC7C,CAAC,CAAA;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAC1B,WAAmB,EACnB,IAAkB,EAClB,EAAgB,EAChB,OAAwB,EACP,EAAE;IACnB,OAAO,WAAW,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAAA;AAC9D,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAK,IAA6B,EAAE;IACzE,OAAO,WAAW,EAAE,CAAC,uBAAuB,EAAE,CAAA;AAChD,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the fx-rates core interface.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ISO 4217 three-letter currency code (e.g. `'USD'`, `'EUR'`, `'JPY'`).
|
|
8
|
+
*
|
|
9
|
+
* Kept as a plain `string` alias rather than a string-literal union so
|
|
10
|
+
* providers can support whatever set of currencies they expose. Use
|
|
11
|
+
* {@link FxRatesProvider.listSupportedCurrencies} to discover what a given
|
|
12
|
+
* provider supports at runtime.
|
|
13
|
+
*/
|
|
14
|
+
export type CurrencyCode = string;
|
|
15
|
+
/**
|
|
16
|
+
* A single FX rate quote: `1 unit of {@link from} = rate units of {@link to}`,
|
|
17
|
+
* as observed at {@link asOf}.
|
|
18
|
+
*/
|
|
19
|
+
export interface FxRate {
|
|
20
|
+
/**
|
|
21
|
+
* Source currency (ISO 4217).
|
|
22
|
+
*/
|
|
23
|
+
from: CurrencyCode;
|
|
24
|
+
/**
|
|
25
|
+
* Target currency (ISO 4217).
|
|
26
|
+
*/
|
|
27
|
+
to: CurrencyCode;
|
|
28
|
+
/**
|
|
29
|
+
* Conversion ratio: `1 unit of {from} = rate units of {to}`.
|
|
30
|
+
*/
|
|
31
|
+
rate: number;
|
|
32
|
+
/**
|
|
33
|
+
* Timestamp the rate was observed/published.
|
|
34
|
+
*/
|
|
35
|
+
asOf: Date;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A daily snapshot of reference rates, all expressed against a common pivot.
|
|
39
|
+
*/
|
|
40
|
+
export interface FxDailyRates {
|
|
41
|
+
/**
|
|
42
|
+
* The pivot currency the snapshot is quoted against
|
|
43
|
+
* (e.g. `'EUR'` for ECB, `'USD'` for most paid feeds).
|
|
44
|
+
*/
|
|
45
|
+
pivot: CurrencyCode;
|
|
46
|
+
/**
|
|
47
|
+
* Date of the daily snapshot.
|
|
48
|
+
*/
|
|
49
|
+
asOf: Date;
|
|
50
|
+
/**
|
|
51
|
+
* Map from currency code to rate: `1 unit of pivot = rates[code] units of code`.
|
|
52
|
+
* The pivot itself is conventionally included with rate `1`.
|
|
53
|
+
*/
|
|
54
|
+
rates: Record<CurrencyCode, number>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Options accepted by all FX-rates provider methods.
|
|
58
|
+
*/
|
|
59
|
+
export interface FxRatesOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Date the rate should be observed at. Defaults to "latest"
|
|
62
|
+
* if omitted. Implementations that do not support historical
|
|
63
|
+
* lookups MAY throw if a non-latest date is requested.
|
|
64
|
+
*/
|
|
65
|
+
asOf?: Date;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Foreign-exchange rates provider interface.
|
|
69
|
+
*
|
|
70
|
+
* All FX-rate providers (ECB, OpenExchange, fixtures, etc.) implement this
|
|
71
|
+
* interface. The interface is deliberately minimal so providers with very
|
|
72
|
+
* different upstream APIs can satisfy it identically.
|
|
73
|
+
*/
|
|
74
|
+
export interface FxRatesProvider {
|
|
75
|
+
/**
|
|
76
|
+
* Looks up the conversion rate `1 unit of from = rate units of to`.
|
|
77
|
+
*
|
|
78
|
+
* Implementations SHOULD compute cross-rates through their pivot when
|
|
79
|
+
* neither side equals the pivot.
|
|
80
|
+
*
|
|
81
|
+
* @param from - Source currency (ISO 4217).
|
|
82
|
+
* @param to - Target currency (ISO 4217).
|
|
83
|
+
* @param options - Optional asOf date for historical rates.
|
|
84
|
+
* @returns The conversion ratio as a plain number.
|
|
85
|
+
*/
|
|
86
|
+
getRate(from: CurrencyCode, to: CurrencyCode, options?: FxRatesOptions): Promise<number>;
|
|
87
|
+
/**
|
|
88
|
+
* Returns all reference rates the provider publishes for the given day,
|
|
89
|
+
* normalized against the provider's pivot currency.
|
|
90
|
+
*
|
|
91
|
+
* @param options - Optional asOf date for the daily snapshot.
|
|
92
|
+
* @returns The full daily snapshot.
|
|
93
|
+
*/
|
|
94
|
+
getDailyRates(options?: FxRatesOptions): Promise<FxDailyRates>;
|
|
95
|
+
/**
|
|
96
|
+
* Converts an integer minor-unit amount (e.g. cents) from one currency
|
|
97
|
+
* to another, returning an integer minor-unit amount in the target.
|
|
98
|
+
*
|
|
99
|
+
* Implementations are expected to handle currencies with non-cent minor
|
|
100
|
+
* units (e.g. JPY has 0 decimals) consistently with the inputs.
|
|
101
|
+
*
|
|
102
|
+
* @param amountMinor - Amount in minor units of {@link from} (e.g. cents).
|
|
103
|
+
* @param from - Source currency (ISO 4217).
|
|
104
|
+
* @param to - Target currency (ISO 4217).
|
|
105
|
+
* @param options - Optional asOf date for historical rates.
|
|
106
|
+
* @returns Converted amount in minor units of {@link to}.
|
|
107
|
+
*/
|
|
108
|
+
convert(amountMinor: number, from: CurrencyCode, to: CurrencyCode, options?: FxRatesOptions): Promise<number>;
|
|
109
|
+
/**
|
|
110
|
+
* Lists every currency the provider currently supports.
|
|
111
|
+
*
|
|
112
|
+
* @returns Array of ISO 4217 currency codes the provider can quote.
|
|
113
|
+
*/
|
|
114
|
+
listSupportedCurrencies(): Promise<CurrencyCode[]>;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAA;AAEjC;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB;;OAEG;IACH,IAAI,EAAE,YAAY,CAAA;IAElB;;OAEG;IACH,EAAE,EAAE,YAAY,CAAA;IAEhB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;CACX;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,KAAK,EAAE,YAAY,CAAA;IAEnB;;OAEG;IACH,IAAI,EAAE,IAAI,CAAA;IAEV;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,IAAI,CAAC,EAAE,IAAI,CAAA;CACZ;AAED;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;;;;;OAUG;IACH,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAExF;;;;;;OAMG;IACH,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IAE9D;;;;;;;;;;;;OAYG;IACH,OAAO,CACL,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE,YAAY,EAClB,EAAE,EAAE,YAAY,EAChB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,MAAM,CAAC,CAAA;IAElB;;;;OAIG;IACH,uBAAuB,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC,CAAA;CACnD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-fx-rates",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Foreign exchange rates core interface for molecule.dev",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"fx",
|
|
25
|
+
"rates",
|
|
26
|
+
"currency",
|
|
27
|
+
"forex",
|
|
28
|
+
"exchange"
|
|
29
|
+
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@molecule/api-bond": "^1.0.0",
|
|
33
|
+
"@molecule/api-i18n": "^1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@molecule/api-bond": "1.0.0",
|
|
37
|
+
"@molecule/api-i18n": "1.0.0",
|
|
38
|
+
"@types/node": "26.1.2",
|
|
39
|
+
"typescript": "6.0.3",
|
|
40
|
+
"vitest": "4.1.10"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
45
|
+
"directory": "packages/api/core/fx-rates"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/core/fx-rates",
|
|
48
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
}
|
|
52
|
+
}
|