@frak-labs/core-sdk 0.0.19 → 0.1.0-beta.00226d62
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/cdn/bundle.iife.js +14 -0
- package/dist/actions-CEEObPYc.js +1 -0
- package/dist/actions-DbQhWYx8.cjs +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -1400
- package/dist/actions.d.ts +3 -1400
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -13
- package/dist/bundle.d.cts +6 -2022
- package/dist/bundle.d.ts +6 -2022
- package/dist/bundle.js +1 -13
- package/dist/index-7OZ39x1U.d.ts +195 -0
- package/dist/index-C6FxkWPC.d.cts +511 -0
- package/dist/index-UFX7xCg3.d.ts +351 -0
- package/dist/index-d8xS4ryI.d.ts +511 -0
- package/dist/index-p4FqSp8z.d.cts +351 -0
- package/dist/index-zDq-VlKx.d.cts +195 -0
- package/dist/index.cjs +1 -13
- package/dist/index.d.cts +4 -1373
- package/dist/index.d.ts +4 -1373
- package/dist/index.js +1 -13
- package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
- package/dist/interaction-KX1h9a7V.d.ts +45 -0
- package/dist/interactions-DnfM3oe0.js +1 -0
- package/dist/interactions-EIXhNLf6.cjs +1 -0
- package/dist/interactions.cjs +1 -1
- package/dist/interactions.d.cts +2 -182
- package/dist/interactions.d.ts +2 -182
- package/dist/interactions.js +1 -1
- package/dist/openSso-D--Airj6.d.cts +1018 -0
- package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
- package/dist/productTypes-BUkXJKZ7.cjs +1 -0
- package/dist/productTypes-CGb1MmBF.js +1 -0
- package/dist/src-B_xO0AR6.cjs +13 -0
- package/dist/src-D2d52OZa.js +13 -0
- package/dist/trackEvent-CHnYa85W.js +1 -0
- package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
- package/package.json +27 -18
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.test.ts +357 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.test.ts +153 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.test.ts +253 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.test.ts +164 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.test.ts +418 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.test.ts +343 -0
- package/src/clients/setupClient.ts +73 -0
- package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
- package/src/clients/transports/iframeLifecycleManager.ts +90 -0
- package/src/constants/interactionTypes.test.ts +128 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.test.ts +130 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +101 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +99 -0
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.test.ts +56 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +71 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.test.ts +407 -0
- package/src/utils/FrakContext.ts +158 -0
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +143 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.test.ts +252 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/src/utils/trackEvent.ts +26 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for computeProductId utility function
|
|
3
|
+
* Tests product ID computation from domain names with normalization
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { keccak256, toHex } from "viem";
|
|
7
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
8
|
+
import { computeProductId } from "./computeProductId";
|
|
9
|
+
|
|
10
|
+
describe("computeProductId", () => {
|
|
11
|
+
it("should compute product ID from current domain (window.location.host)", () => {
|
|
12
|
+
// In JSDOM test environment, window.location.host is "localhost:3000"
|
|
13
|
+
const productId = computeProductId();
|
|
14
|
+
|
|
15
|
+
// Should compute keccak256 hash of window.location.host
|
|
16
|
+
const expectedId = keccak256(toHex(window.location.host));
|
|
17
|
+
expect(productId).toBe(expectedId);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should compute product ID from custom domain", () => {
|
|
21
|
+
const customDomain = "custom-domain.com";
|
|
22
|
+
const productId = computeProductId({ domain: customDomain });
|
|
23
|
+
|
|
24
|
+
// Should compute keccak256 hash of custom domain
|
|
25
|
+
const expectedId = keccak256(toHex(customDomain));
|
|
26
|
+
expect(productId).toBe(expectedId);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should remove www. prefix from domain before hashing", () => {
|
|
30
|
+
const domainWithWww = "www.example.com";
|
|
31
|
+
const productId = computeProductId({ domain: domainWithWww });
|
|
32
|
+
|
|
33
|
+
// Should compute keccak256 hash of "example.com" (www. removed)
|
|
34
|
+
const expectedId = keccak256(toHex("example.com"));
|
|
35
|
+
expect(productId).toBe(expectedId);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should produce same product ID for domain with and without www", () => {
|
|
39
|
+
const withWww = computeProductId({ domain: "www.example.com" });
|
|
40
|
+
const withoutWww = computeProductId({ domain: "example.com" });
|
|
41
|
+
|
|
42
|
+
// Both should produce the same hash
|
|
43
|
+
expect(withWww).toBe(withoutWww);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should produce different product IDs for different domains", () => {
|
|
47
|
+
const domain1ProductId = computeProductId({ domain: "domain1.com" });
|
|
48
|
+
const domain2ProductId = computeProductId({ domain: "domain2.com" });
|
|
49
|
+
|
|
50
|
+
// Different domains should produce different hashes
|
|
51
|
+
expect(domain1ProductId).not.toBe(domain2ProductId);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should use window.location.host when domain is not provided", () => {
|
|
55
|
+
// When no domain is provided, it should default to window.location.host
|
|
56
|
+
const productId = computeProductId();
|
|
57
|
+
|
|
58
|
+
// Should produce same result as using window.location.host explicitly
|
|
59
|
+
const expectedId = keccak256(toHex(window.location.host));
|
|
60
|
+
expect(productId).toBe(expectedId);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should handle subdomains correctly", () => {
|
|
64
|
+
const subdomain = "blog.example.com";
|
|
65
|
+
const productId = computeProductId({ domain: subdomain });
|
|
66
|
+
|
|
67
|
+
// Should compute keccak256 hash of "blog.example.com" (subdomain preserved)
|
|
68
|
+
const expectedId = keccak256(toHex(subdomain));
|
|
69
|
+
expect(productId).toBe(expectedId);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should only remove www prefix, not other w-prefixed subdomains", () => {
|
|
73
|
+
const domain = "web.example.com";
|
|
74
|
+
const productId = computeProductId({ domain });
|
|
75
|
+
|
|
76
|
+
// Should NOT remove "web." prefix, only "www."
|
|
77
|
+
const expectedId = keccak256(toHex("web.example.com"));
|
|
78
|
+
expect(productId).toBe(expectedId);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { keccak256, toHex } from "viem";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compute the product id from a domain
|
|
5
|
+
* @ignore
|
|
6
|
+
*/
|
|
7
|
+
export function computeProductId({ domain }: { domain?: string } = {}) {
|
|
8
|
+
const effectiveDomain = domain ?? window.location.host;
|
|
9
|
+
const normalizedDomain = effectiveDomain.replace("www.", "");
|
|
10
|
+
return keccak256(toHex(normalizedDomain));
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for constants
|
|
3
|
+
* Tests backup key constant value
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
7
|
+
import { BACKUP_KEY } from "./constants";
|
|
8
|
+
|
|
9
|
+
describe("constants", () => {
|
|
10
|
+
describe("BACKUP_KEY", () => {
|
|
11
|
+
it("should have correct backup key value", () => {
|
|
12
|
+
expect(BACKUP_KEY).toBe("nexus-wallet-backup");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should be a string", () => {
|
|
16
|
+
expect(typeof BACKUP_KEY).toBe("string");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should not be empty", () => {
|
|
20
|
+
expect(BACKUP_KEY.length).toBeGreaterThan(0);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for formatAmount utility function
|
|
3
|
+
* Tests currency formatting with proper locale support
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
7
|
+
import { formatAmount } from "./formatAmount";
|
|
8
|
+
|
|
9
|
+
describe("formatAmount", () => {
|
|
10
|
+
it("should format EUR with French locale by default", () => {
|
|
11
|
+
const formatted = formatAmount(1000, "eur");
|
|
12
|
+
|
|
13
|
+
// French locale formats EUR with space between number and symbol
|
|
14
|
+
// Expected format: "1 000 €" or "1 000,00 €" depending on locale
|
|
15
|
+
expect(formatted).toContain("€");
|
|
16
|
+
expect(formatted).toContain("1");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should format USD with US locale", () => {
|
|
20
|
+
const formatted = formatAmount(1000, "usd");
|
|
21
|
+
|
|
22
|
+
// US locale formats USD with $ prefix
|
|
23
|
+
expect(formatted).toContain("$");
|
|
24
|
+
expect(formatted).toContain("1");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should format GBP with British locale", () => {
|
|
28
|
+
const formatted = formatAmount(1000, "gbp");
|
|
29
|
+
|
|
30
|
+
// British locale formats GBP with £ symbol
|
|
31
|
+
expect(formatted).toContain("£");
|
|
32
|
+
expect(formatted).toContain("1");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should format integer amounts without decimal places", () => {
|
|
36
|
+
const formatted = formatAmount(1000, "eur");
|
|
37
|
+
|
|
38
|
+
// Integer amounts should not show .00
|
|
39
|
+
// French locale: "1 000 €" or "1 000,00 €"
|
|
40
|
+
expect(formatted).toBeDefined();
|
|
41
|
+
expect(formatted.length).toBeGreaterThan(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should format decimal amounts with up to 2 decimal places", () => {
|
|
45
|
+
const formatted = formatAmount(1234.56, "eur");
|
|
46
|
+
|
|
47
|
+
// Should include decimal separator and digits
|
|
48
|
+
expect(formatted).toBeDefined();
|
|
49
|
+
expect(formatted).toContain("1");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should handle zero amount", () => {
|
|
53
|
+
const formatted = formatAmount(0, "eur");
|
|
54
|
+
|
|
55
|
+
// Should format zero properly
|
|
56
|
+
expect(formatted).toContain("0");
|
|
57
|
+
expect(formatted).toContain("€");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should handle large amounts", () => {
|
|
61
|
+
const formatted = formatAmount(1000000, "eur");
|
|
62
|
+
|
|
63
|
+
// Should format large numbers with proper thousand separators
|
|
64
|
+
expect(formatted).toContain("€");
|
|
65
|
+
expect(formatted).toBeDefined();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should default to EUR when currency is not provided", () => {
|
|
69
|
+
const formatted = formatAmount(1000);
|
|
70
|
+
|
|
71
|
+
// Should default to EUR
|
|
72
|
+
expect(formatted).toContain("€");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should format negative amounts", () => {
|
|
76
|
+
const formatted = formatAmount(-500, "eur");
|
|
77
|
+
|
|
78
|
+
// Should handle negative amounts
|
|
79
|
+
expect(formatted).toContain("-");
|
|
80
|
+
expect(formatted).toContain("€");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should round amounts with more than 2 decimal places", () => {
|
|
84
|
+
const formatted = formatAmount(1234.5678, "eur");
|
|
85
|
+
|
|
86
|
+
// Should round to max 2 decimal places
|
|
87
|
+
expect(formatted).toBeDefined();
|
|
88
|
+
// The exact format depends on locale, but should be rounded
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should format small decimal amounts", () => {
|
|
92
|
+
const formatted = formatAmount(0.99, "usd");
|
|
93
|
+
|
|
94
|
+
// Should handle small amounts properly
|
|
95
|
+
expect(formatted).toContain("$");
|
|
96
|
+
expect(formatted).toContain("0");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should use correct locale for each currency", () => {
|
|
100
|
+
const eurFormatted = formatAmount(1000, "eur");
|
|
101
|
+
const usdFormatted = formatAmount(1000, "usd");
|
|
102
|
+
const gbpFormatted = formatAmount(1000, "gbp");
|
|
103
|
+
|
|
104
|
+
// Each should use different currency symbols
|
|
105
|
+
expect(eurFormatted).toContain("€");
|
|
106
|
+
expect(usdFormatted).toContain("$");
|
|
107
|
+
expect(gbpFormatted).toContain("£");
|
|
108
|
+
|
|
109
|
+
// All should be different formats due to different locales
|
|
110
|
+
expect(eurFormatted).not.toBe(usdFormatted);
|
|
111
|
+
expect(usdFormatted).not.toBe(gbpFormatted);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Currency } from "../types";
|
|
2
|
+
import { getSupportedCurrency } from "./getSupportedCurrency";
|
|
3
|
+
import { getSupportedLocale } from "./getSupportedLocale";
|
|
4
|
+
|
|
5
|
+
export function formatAmount(amount: number, currency?: Currency) {
|
|
6
|
+
// Get the supported locale (e.g. "fr-FR")
|
|
7
|
+
const supportedLocale = getSupportedLocale(currency);
|
|
8
|
+
|
|
9
|
+
// Get the supported currency (e.g. "eur")
|
|
10
|
+
const supportedCurrency = getSupportedCurrency(currency);
|
|
11
|
+
|
|
12
|
+
return amount.toLocaleString(supportedLocale, {
|
|
13
|
+
style: "currency",
|
|
14
|
+
currency: supportedCurrency,
|
|
15
|
+
minimumFractionDigits: 0,
|
|
16
|
+
maximumFractionDigits: 2,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for getCurrencyAmountKey utility function
|
|
3
|
+
* Tests currency amount key generation
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
7
|
+
import type { Currency } from "../types";
|
|
8
|
+
import { getCurrencyAmountKey } from "./getCurrencyAmountKey";
|
|
9
|
+
|
|
10
|
+
describe("getCurrencyAmountKey", () => {
|
|
11
|
+
it("should return eurAmount for undefined input", () => {
|
|
12
|
+
const result = getCurrencyAmountKey(undefined);
|
|
13
|
+
|
|
14
|
+
expect(result).toBe("eurAmount");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return eurAmount for EUR", () => {
|
|
18
|
+
const result = getCurrencyAmountKey("eur");
|
|
19
|
+
|
|
20
|
+
expect(result).toBe("eurAmount");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should return usdAmount for USD", () => {
|
|
24
|
+
const result = getCurrencyAmountKey("usd");
|
|
25
|
+
|
|
26
|
+
expect(result).toBe("usdAmount");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return gbpAmount for GBP", () => {
|
|
30
|
+
const result = getCurrencyAmountKey("gbp");
|
|
31
|
+
|
|
32
|
+
expect(result).toBe("gbpAmount");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should generate correct key format for all currencies", () => {
|
|
36
|
+
const validCurrencies: Currency[] = ["eur", "usd", "gbp"];
|
|
37
|
+
|
|
38
|
+
for (const currency of validCurrencies) {
|
|
39
|
+
const result = getCurrencyAmountKey(currency);
|
|
40
|
+
// Should match pattern: {currency}Amount
|
|
41
|
+
expect(result).toBe(`${currency}Amount`);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Currency, TokenAmountType } from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the currency amount key for a given currency
|
|
5
|
+
* @param currency - The currency to use
|
|
6
|
+
* @returns The currency amount key
|
|
7
|
+
*/
|
|
8
|
+
export function getCurrencyAmountKey(
|
|
9
|
+
currency?: Currency
|
|
10
|
+
): keyof TokenAmountType {
|
|
11
|
+
if (!currency) {
|
|
12
|
+
return "eurAmount";
|
|
13
|
+
}
|
|
14
|
+
return `${currency}Amount` as keyof TokenAmountType;
|
|
15
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for getSupportedCurrency utility function
|
|
3
|
+
* Tests currency validation and fallback behavior
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
7
|
+
import type { Currency } from "../types";
|
|
8
|
+
import { getSupportedCurrency } from "./getSupportedCurrency";
|
|
9
|
+
|
|
10
|
+
describe("getSupportedCurrency", () => {
|
|
11
|
+
it("should return EUR for undefined input", () => {
|
|
12
|
+
const result = getSupportedCurrency(undefined);
|
|
13
|
+
|
|
14
|
+
expect(result).toBe("eur");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should return EUR when provided", () => {
|
|
18
|
+
const result = getSupportedCurrency("eur");
|
|
19
|
+
|
|
20
|
+
expect(result).toBe("eur");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should return USD when provided", () => {
|
|
24
|
+
const result = getSupportedCurrency("usd");
|
|
25
|
+
|
|
26
|
+
expect(result).toBe("usd");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should return GBP when provided", () => {
|
|
30
|
+
const result = getSupportedCurrency("gbp");
|
|
31
|
+
|
|
32
|
+
expect(result).toBe("gbp");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should fall back to EUR for invalid currency", () => {
|
|
36
|
+
// Force cast to test runtime behavior
|
|
37
|
+
const invalidCurrency = "invalid" as Currency;
|
|
38
|
+
const result = getSupportedCurrency(invalidCurrency);
|
|
39
|
+
|
|
40
|
+
expect(result).toBe("eur");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should handle all valid currencies", () => {
|
|
44
|
+
const validCurrencies: Currency[] = ["eur", "usd", "gbp"];
|
|
45
|
+
|
|
46
|
+
for (const currency of validCurrencies) {
|
|
47
|
+
const result = getSupportedCurrency(currency);
|
|
48
|
+
expect(result).toBe(currency);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { locales } from "../constants/locales";
|
|
2
|
+
import type { Currency } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get the supported currency for a given currency
|
|
6
|
+
* @param currency - The currency to use
|
|
7
|
+
* @returns The supported currency
|
|
8
|
+
*/
|
|
9
|
+
export function getSupportedCurrency(currency?: Currency): Currency {
|
|
10
|
+
if (!currency) {
|
|
11
|
+
return "eur";
|
|
12
|
+
}
|
|
13
|
+
return currency in locales ? currency : "eur";
|
|
14
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for getSupportedLocale utility function
|
|
3
|
+
* Tests locale resolution from currency
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "../../tests/vitest-fixtures";
|
|
7
|
+
import { locales } from "../constants/locales";
|
|
8
|
+
import type { Currency } from "../types";
|
|
9
|
+
import { getSupportedLocale } from "./getSupportedLocale";
|
|
10
|
+
|
|
11
|
+
describe("getSupportedLocale", () => {
|
|
12
|
+
it("should return EUR locale for undefined input", () => {
|
|
13
|
+
const result = getSupportedLocale(undefined);
|
|
14
|
+
|
|
15
|
+
expect(result).toBe(locales.eur);
|
|
16
|
+
expect(result).toBe("fr-FR");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should return French locale for EUR", () => {
|
|
20
|
+
const result = getSupportedLocale("eur");
|
|
21
|
+
|
|
22
|
+
expect(result).toBe(locales.eur);
|
|
23
|
+
expect(result).toBe("fr-FR");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return US locale for USD", () => {
|
|
27
|
+
const result = getSupportedLocale("usd");
|
|
28
|
+
|
|
29
|
+
expect(result).toBe(locales.usd);
|
|
30
|
+
expect(result).toBe("en-US");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should return GB locale for GBP", () => {
|
|
34
|
+
const result = getSupportedLocale("gbp");
|
|
35
|
+
|
|
36
|
+
expect(result).toBe(locales.gbp);
|
|
37
|
+
expect(result).toBe("en-GB");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should fall back to EUR locale for invalid currency", () => {
|
|
41
|
+
// Force cast to test runtime behavior
|
|
42
|
+
const invalidCurrency = "invalid" as Currency;
|
|
43
|
+
const result = getSupportedLocale(invalidCurrency);
|
|
44
|
+
|
|
45
|
+
expect(result).toBe(locales.eur);
|
|
46
|
+
expect(result).toBe("fr-FR");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should return valid locale format", () => {
|
|
50
|
+
const validCurrencies: Currency[] = ["eur", "usd", "gbp"];
|
|
51
|
+
|
|
52
|
+
for (const currency of validCurrencies) {
|
|
53
|
+
const result = getSupportedLocale(currency);
|
|
54
|
+
// Should match locale format like "en-US", "fr-FR", etc.
|
|
55
|
+
expect(result).toMatch(/^[a-z]{2}-[A-Z]{2}$/);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should map all supported currencies to their locales", () => {
|
|
60
|
+
expect(getSupportedLocale("eur")).toBe("fr-FR");
|
|
61
|
+
expect(getSupportedLocale("usd")).toBe("en-US");
|
|
62
|
+
expect(getSupportedLocale("gbp")).toBe("en-GB");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type LocalesKey, locales } from "../constants/locales";
|
|
2
|
+
import type { Currency } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get the supported locale for a given currency
|
|
6
|
+
* @param currency - The currency to use
|
|
7
|
+
* @returns The supported locale
|
|
8
|
+
*/
|
|
9
|
+
export function getSupportedLocale(
|
|
10
|
+
currency?: Currency
|
|
11
|
+
): (typeof locales)[LocalesKey] {
|
|
12
|
+
if (!currency) {
|
|
13
|
+
return locales.eur;
|
|
14
|
+
}
|
|
15
|
+
return locales[currency] ?? locales.eur;
|
|
16
|
+
}
|