@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.19
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 +21 -0
- package/README.md +128 -10
- package/dist/abi/canvas-live.d.ts +72 -0
- package/dist/abi/canvas-live.js +159 -0
- package/dist/abi/cart.d.ts +60 -0
- package/dist/abi/cart.js +396 -0
- package/dist/abi/convert.d.ts +360 -0
- package/dist/abi/convert.js +1330 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +213 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +264 -0
- package/dist/abi/page-data.js +746 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +46 -0
- package/dist/abi/wire.js +114 -0
- package/dist/build.d.ts +121 -0
- package/dist/build.js +401 -0
- package/dist/chunks.d.ts +23 -0
- package/dist/chunks.js +41 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +101 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +276 -0
- package/dist/headless/hooks.js +544 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +44 -0
- package/dist/headless/index.js +48 -0
- package/dist/headless/lazy-component.d.ts +72 -0
- package/dist/headless/lazy-component.js +225 -0
- package/dist/headless/lazy-section.d.ts +38 -0
- package/dist/headless/lazy-section.js +97 -0
- package/dist/headless/model.d.ts +73 -0
- package/dist/headless/model.js +115 -0
- package/dist/headless/money.d.ts +17 -0
- package/dist/headless/money.js +63 -0
- package/dist/headless/page.d.ts +45 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +187 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +198 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/tanqory-money.generated.d.ts +493 -0
- package/dist/headless/tanqory-money.generated.js +507 -0
- package/dist/headless/theme.d.ts +109 -0
- package/dist/headless/theme.js +62 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +380 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +158 -0
- package/dist/runtime/data/to-ui.d.ts +286 -0
- package/dist/runtime/data/to-ui.js +1009 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +250 -0
- package/dist/runtime/kit/cart.d.ts +222 -0
- package/dist/runtime/kit/cart.js +545 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +213 -0
- package/dist/runtime/kit/contract/content.js +119 -0
- package/dist/runtime/kit/contract/field-types.d.ts +360 -0
- package/dist/runtime/kit/contract/field-types.js +216 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +77 -0
- package/dist/runtime/kit/contract/validate.js +520 -0
- package/dist/runtime/kit/data.d.ts +732 -0
- package/dist/runtime/kit/data.js +1131 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/runtime/kit/dynamic-source.d.ts +118 -0
- package/dist/runtime/kit/dynamic-source.js +311 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/runtime/kit/storefront.d.ts +652 -0
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +155 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +199 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +243 -0
- package/dist/runtime/render/resolved.d.ts +47 -0
- package/dist/runtime/render/resolved.js +80 -0
- package/dist/runtime/render/section-actions.d.ts +67 -0
- package/dist/runtime/render/section-actions.js +205 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +96 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +118 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +18 -0
- package/dist/runtime/shell/RuntimeBridge.js +124 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/font-hooks.d.ts +7 -0
- package/dist/sdk/font-hooks.js +21 -0
- package/dist/sdk/fonts.d.ts +144 -0
- package/dist/sdk/fonts.js +241 -0
- package/dist/sdk/index.d.ts +32 -0
- package/dist/sdk/index.js +36 -0
- package/dist/sdk/motion-hooks.d.ts +23 -0
- package/dist/sdk/motion-hooks.js +46 -0
- package/dist/sdk/motion.d.ts +78 -0
- package/dist/sdk/motion.js +292 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +230 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +249 -0
- package/dist/sdk/schema.js +168 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +32 -0
- package/dist/sdk/store-lookup.js +48 -0
- package/dist/sdk/store.d.ts +164 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +45 -0
- package/dist/sdk/sx.js +117 -0
- package/dist/sdk/theme.d.ts +212 -0
- package/dist/sdk/theme.js +27 -0
- package/dist/sdk/types.d.ts +789 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +112 -0
- package/dist/server.js +589 -0
- package/dist/vite-chunks.d.ts +10 -0
- package/dist/vite-chunks.js +44 -0
- package/docs/headless-api.md +250 -0
- package/package.json +72 -26
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/data.d.ts +0 -357
- package/dist/types/dynamic-source.d.ts +0 -84
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// GENERATED from packages/money/index.js by tools/money/vendor.mjs. Do not edit: change the package and run the tool.
|
|
3
|
+
/**
|
|
4
|
+
* @tanqory/money: the one currency table and the one money arithmetic of the platform (docs/STANDARDS.md API-6).
|
|
5
|
+
*
|
|
6
|
+
* - Digits per currency are ISO 4217's minor units, from the maintenance agency's list one (SIX, published
|
|
7
|
+
* 2026-09-17, ISO_4217 below). Nothing else decides how many digits an amount has: not Intl (CLDR's display digits
|
|
8
|
+
* differ for IQD, LBP, RSD, AFN, ...), not a provider's list.
|
|
9
|
+
* - A payment provider that counts some currency in another exponent (Stripe sends ISK as two decimals "00") is a
|
|
10
|
+
* documented entry of PROVIDER_UNITS here, with its source, and is only used at that provider's boundary.
|
|
11
|
+
* - Amounts are decimal strings ("12.34", "1200", "1.250") or integer minor units at a provider boundary. The
|
|
12
|
+
* arithmetic here is exact (BigInt scaled integers), never a JS number: a JSON number is an IEEE 754 binary64
|
|
13
|
+
* (RFC 8259 §6), and 0.1 + 0.2 is not 0.3 in it.
|
|
14
|
+
* - Rounding is half up (away from zero at the half, as Prisma.Decimal ROUND_HALF_UP and DATABASE_STANDARD 3.5's
|
|
15
|
+
* roundMoney) unless the caller names another mode.
|
|
16
|
+
*
|
|
17
|
+
* Zero dependencies; runs on Node, Workers, browsers and Next. The api keeps Prisma.Decimal for its own arithmetic
|
|
18
|
+
* (API-6 allows a decimal type) and takes its digits and provider units from here.
|
|
19
|
+
*/
|
|
20
|
+
/** Where the table comes from. Re-check against list one when SIX publishes a new one (amendments are rare). */
|
|
21
|
+
const ISO_4217 = Object.freeze({
|
|
22
|
+
source: 'https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml',
|
|
23
|
+
published: '2026-09-17',
|
|
24
|
+
read: '2026-11-19',
|
|
25
|
+
});
|
|
26
|
+
const DIGITS_0 = ['BIF', 'CLP', 'DJF', 'GNF', 'ISK', 'JPY', 'KMF', 'KRW', 'PYG', 'RWF', 'UGX', 'UYI', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'];
|
|
27
|
+
const DIGITS_3 = ['BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND'];
|
|
28
|
+
const DIGITS_4 = ['CLF', 'UYW'];
|
|
29
|
+
const DIGITS_2 = [
|
|
30
|
+
'AED', 'AFN', 'ALL', 'AMD', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', 'BAM', 'BBD', 'BDT', 'BMD', 'BND', 'BOB', 'BOV', 'BRL',
|
|
31
|
+
'BSD', 'BTN', 'BWP', 'BYN', 'BZD', 'CAD', 'CDF', 'CHE', 'CHF', 'CHW', 'CNY', 'COP', 'COU', 'CRC', 'CUP', 'CVE', 'CZK',
|
|
32
|
+
'DKK', 'DOP', 'DZD', 'EGP', 'ERN', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GHS', 'GIP', 'GMD', 'GTQ', 'GYD', 'HKD',
|
|
33
|
+
'HNL', 'HTG', 'HUF', 'IDR', 'ILS', 'INR', 'IRR', 'JMD', 'KES', 'KGS', 'KHR', 'KPW', 'KYD', 'KZT', 'LAK', 'LBP', 'LKR',
|
|
34
|
+
'LRD', 'LSL', 'MAD', 'MDL', 'MGA', 'MKD', 'MMK', 'MNT', 'MOP', 'MRU', 'MUR', 'MVR', 'MWK', 'MXN', 'MXV', 'MYR', 'MZN',
|
|
35
|
+
'NAD', 'NGN', 'NIO', 'NOK', 'NPR', 'NZD', 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', 'QAR', 'RON', 'RSD', 'RUB', 'SAR',
|
|
36
|
+
'SBD', 'SCR', 'SDG', 'SEK', 'SGD', 'SHP', 'SLE', 'SOS', 'SRD', 'SSP', 'STN', 'SVC', 'SYP', 'SZL', 'THB', 'TJS', 'TMT',
|
|
37
|
+
'TOP', 'TRY', 'TTD', 'TWD', 'TZS', 'UAH', 'USD', 'USN', 'UYU', 'UZS', 'VED', 'VES', 'WST', 'XAD', 'XCD', 'XCG', 'YER',
|
|
38
|
+
'ZAR', 'ZMW', 'ZWG',
|
|
39
|
+
];
|
|
40
|
+
/** List one's codes with no minor unit ("N.A."): funds, metals, the test and "no currency" codes. Not prices. */
|
|
41
|
+
const NO_MINOR_UNIT = Object.freeze(['XAG', 'XAU', 'XBA', 'XBB', 'XBC', 'XBD', 'XDR', 'XPD', 'XPT', 'XSU', 'XTS', 'XUA', 'XXX']);
|
|
42
|
+
/** Every ISO 4217 currency with a minor unit, and its digits. */
|
|
43
|
+
const CURRENCY_DIGITS = Object.freeze(Object.fromEntries([
|
|
44
|
+
...DIGITS_0.map((c) => [c, 0]),
|
|
45
|
+
...DIGITS_2.map((c) => [c, 2]),
|
|
46
|
+
...DIGITS_3.map((c) => [c, 3]),
|
|
47
|
+
...DIGITS_4.map((c) => [c, 4]),
|
|
48
|
+
]));
|
|
49
|
+
const normal = (code) => String(code == null ? '' : code).trim().toUpperCase();
|
|
50
|
+
/** The currency's ISO 4217 minor unit: JPY 0, USD 2, KWD 3, CLF 4. An unknown code, or one with none, is 2. */
|
|
51
|
+
function currencyDigits(code) {
|
|
52
|
+
const d = CURRENCY_DIGITS[normal(code)];
|
|
53
|
+
return d === undefined ? 2 : d;
|
|
54
|
+
}
|
|
55
|
+
/** True for a list-one code that is a currency with a minor unit (a price can be in it). */
|
|
56
|
+
function isCurrencyCode(code) {
|
|
57
|
+
return CURRENCY_DIGITS[normal(code)] !== undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Display only: the currencies a price is shown in whole units although ISO 4217 gives them 2 digits. The amount keeps
|
|
61
|
+
* its ISO digits everywhere it is computed, stored, charged or sent; only formatting for a reader drops the fraction.
|
|
62
|
+
* Source: CLDR common/supplemental/supplementalData.xml <currencyData> (unicode-org/cldr main, read 2026-11-19):
|
|
63
|
+
* HUF digits="0", IDR digits="0", TWD cashDigits="0" (TWD digits="2"). The list is these three on purpose: the other
|
|
64
|
+
* currencies CLDR shows with fewer digits than ISO (IQD 0, LBP 0, ...) are shown in ISO's digits, so a shopper sees the
|
|
65
|
+
* amount they are charged.
|
|
66
|
+
*/
|
|
67
|
+
const DISPLAY_WHOLE_UNITS = Object.freeze(['HUF', 'IDR', 'TWD']);
|
|
68
|
+
/** The fraction digits a price is shown with: 0 for DISPLAY_WHOLE_UNITS, else currencyDigits (JPY 0, USD 2, KWD 3). */
|
|
69
|
+
function displayDigits(code) {
|
|
70
|
+
return DISPLAY_WHOLE_UNITS.includes(normal(code)) ? 0 : currencyDigits(code);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The amount as a reader sees it, exact: rounded half up to displayDigits ("1234.5" JPY "1235", KWD "1234.500", IDR
|
|
74
|
+
* "1235"); grouping and the symbol are the formatter's. Throws InvalidAmountError when the amount is not a decimal.
|
|
75
|
+
*/
|
|
76
|
+
function displayAmount(amount, code) {
|
|
77
|
+
return toFixedDigits(amount, displayDigits(code));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* How a provider counts a currency where it differs from ISO 4217. `exponent`: the power of ten of its integer unit
|
|
81
|
+
* (the amount it is sent is amount x 10^exponent). `precision`: the digits an amount may have for it (rounded to these
|
|
82
|
+
* first). A currency absent from a provider's entry takes the entry's '*' when it has one (the provider's stated
|
|
83
|
+
* default), else ISO's: exponent = precision = currencyDigits.
|
|
84
|
+
*
|
|
85
|
+
* Each entry names its source. "existing adapter" entries were in an adapter before 2026-11-19 with the citation its
|
|
86
|
+
* file gave; they moved here unchanged (API-6: no second table in an adapter).
|
|
87
|
+
*/
|
|
88
|
+
const u = (exponent, precision) => Object.freeze({ exponent, precision });
|
|
89
|
+
const whole = (codes, unit) => Object.fromEntries(codes.map((c) => [c, unit]));
|
|
90
|
+
/** https://docs.stripe.com/currencies, "Zero-decimal currencies" (read 2026-11-19). */
|
|
91
|
+
const STRIPE_ZERO_DECIMAL = ['BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'];
|
|
92
|
+
const STRIPE_THREE_DECIMAL = ['BHD', 'JOD', 'KWD', 'OMR', 'TND'];
|
|
93
|
+
const PROVIDER_UNITS = Object.freeze({
|
|
94
|
+
// https://docs.stripe.com/currencies (read 2026-11-19): "Currencies are two-decimal currencies unless otherwise
|
|
95
|
+
// specified" ('*'), the zero-decimal list above, and the special cases: ISK and UGX "require you to represent it as
|
|
96
|
+
// a two-decimal value, where the decimal amount is always 00". UGX is also on the zero-decimal list; Stripe's API
|
|
97
|
+
// settled it (test mode, 2026-11-19): amount 2000 ugx is "20.00 USh", and 2050 is refused, "charged amounts must be
|
|
98
|
+
// evenly divisible by 100"; the same for ISK. MGA 5000 is accepted as 5000 MGA. HUF and TWD differ only for payouts
|
|
99
|
+
// (stripe_payout). Three-decimal currencies are charged in thousandths ending in 0: the rule the api followed before
|
|
100
|
+
// 2026-11-19; the page read on 2026-11-19 has no three-decimal section and the Dev (US) account does not take KWD or
|
|
101
|
+
// BHD, so it is not re-proven (open, lane SD report).
|
|
102
|
+
stripe: Object.freeze({
|
|
103
|
+
'*': u(2, 2),
|
|
104
|
+
...whole(STRIPE_ZERO_DECIMAL, u(0, 0)),
|
|
105
|
+
...whole(STRIPE_THREE_DECIMAL, u(3, 2)),
|
|
106
|
+
ISK: u(2, 0),
|
|
107
|
+
UGX: u(2, 0),
|
|
108
|
+
}),
|
|
109
|
+
// https://docs.stripe.com/currencies, "Special cases": HUF and TWD are zero-decimal "for payouts", sent as integers
|
|
110
|
+
// "evenly divisible by 100". The rest as for charges.
|
|
111
|
+
stripe_payout: Object.freeze({
|
|
112
|
+
'*': u(2, 2),
|
|
113
|
+
...whole(STRIPE_ZERO_DECIMAL, u(0, 0)),
|
|
114
|
+
...whole(STRIPE_THREE_DECIMAL, u(3, 2)),
|
|
115
|
+
ISK: u(2, 0),
|
|
116
|
+
UGX: u(2, 0),
|
|
117
|
+
HUF: u(2, 0),
|
|
118
|
+
TWD: u(2, 0),
|
|
119
|
+
}),
|
|
120
|
+
// existing adapter (providers/payments/paypal/adapter.ts, before 2026-11-19): HUF, JPY and TWD take no decimals,
|
|
121
|
+
// every other currency two.
|
|
122
|
+
paypal: Object.freeze({ '*': u(2, 2), HUF: u(0, 0), JPY: u(0, 0), TWD: u(0, 0) }),
|
|
123
|
+
// existing adapter (providers/payments/omise/adapter.ts, before 2026-11-19): JPY has none, every other currency two.
|
|
124
|
+
omise: Object.freeze({ '*': u(2, 2), JPY: u(0, 0) }),
|
|
125
|
+
// existing adapters, each header's source: ABA PayWay KHR whole riel; Barion HUF whole forint; Checkout.com CLP in
|
|
126
|
+
// cents; NewebPay and TapPay TWD whole dollars; NICEPAY IDR whole rupiah; VNPAY VND x 100. Checkout.com and VNPAY
|
|
127
|
+
// took two-decimal amounts for these before 2026-11-19 (digits override 2), so precision stays 2. Every other
|
|
128
|
+
// currency: ISO 4217.
|
|
129
|
+
aba_payway: Object.freeze({ KHR: u(0, 0) }),
|
|
130
|
+
barion: Object.freeze({ HUF: u(0, 0) }),
|
|
131
|
+
checkout_com: Object.freeze({ CLP: u(2, 2) }),
|
|
132
|
+
newebpay: Object.freeze({ TWD: u(0, 0) }),
|
|
133
|
+
tappay: Object.freeze({ TWD: u(0, 0) }),
|
|
134
|
+
nicepay: Object.freeze({ IDR: u(0, 0) }),
|
|
135
|
+
vnpay: Object.freeze({ VND: u(2, 2) }),
|
|
136
|
+
// https://docs.midtrans.com/reference/json-objects (read 2026-11-19): transaction_details.gross_amount is an Integer,
|
|
137
|
+
// "Amount to be charged", IDR only: whole rupiah.
|
|
138
|
+
midtrans: Object.freeze({ IDR: u(0, 0) }),
|
|
139
|
+
// https://tripay.co.id/developer (Request Transaksi Closed Payment, read 2026-11-20): `amount` is an int, whole rupiah
|
|
140
|
+
// (the example 1000000 for Rp 1.000.000; the order_items prices the same).
|
|
141
|
+
tripay: Object.freeze({ IDR: u(0, 0) }),
|
|
142
|
+
// https://docs.globalgetnet.com/en/products/online-payments/web-checkout/swagger (Web Checkout API 1.7.0, read
|
|
143
|
+
// 2026-11-20): "Amounts are integers where the last two digits represent the cents (for example, `9990` represents BRL
|
|
144
|
+
// 99.90). For currencies without cents, fill the last two digits with zeros." CLP (no minor unit in ISO 4217) is whole
|
|
145
|
+
// pesos times 100; every other currency ISO's two digits.
|
|
146
|
+
getnet: Object.freeze({ CLP: u(2, 0) }),
|
|
147
|
+
});
|
|
148
|
+
for (const [name, table] of Object.entries(PROVIDER_UNITS)) {
|
|
149
|
+
for (const [code, unit] of Object.entries(table)) {
|
|
150
|
+
if (!(unit.exponent >= unit.precision))
|
|
151
|
+
throw new Error(`@tanqory/money: PROVIDER_UNITS.${name}.${code}: exponent below precision`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The currencies a provider prices in, where its docs publish a closed list and its adapter refuses any other currency
|
|
156
|
+
* before a request (API-6: the list lives here beside PROVIDER_UNITS, never as a table in an adapter). Each entry names
|
|
157
|
+
* its source; the order is the docs' (an adapter's refusal lists them in it). Units stay ISO 4217's unless
|
|
158
|
+
* PROVIDER_UNITS says otherwise.
|
|
159
|
+
*/
|
|
160
|
+
const PROVIDER_CURRENCIES = Object.freeze({
|
|
161
|
+
// https://pay-docs.crypto.com/#api-reference-resources, Pricing Currencies (the list its adapter carried before
|
|
162
|
+
// 2026-11-20): the fiat currencies a payment is priced in, 0.01 the smallest unit except CLP, JPY and KRW (1), as ISO.
|
|
163
|
+
crypto_com_pay: Object.freeze([
|
|
164
|
+
'USD', 'EUR', 'AUD', 'CAD', 'GBP', 'BGN', 'BRL', 'CHF', 'CLP', 'CNY', 'COP', 'DKK', 'HKD', 'IDR', 'INR', 'JPY', 'KRW',
|
|
165
|
+
'MDL', 'MOP', 'MXN', 'MYR', 'NOK', 'PHP', 'RON', 'RUB', 'SAR', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'UAH', 'ZAR',
|
|
166
|
+
]),
|
|
167
|
+
// https://developer.fastspring.com/reference/createsession, PriceMap (the list its adapter carried before 2026-11-20):
|
|
168
|
+
// the currencies a custom price may be in.
|
|
169
|
+
fastspring: Object.freeze([
|
|
170
|
+
'AED', 'ARS', 'AUD', 'BRL', 'CAD', 'CHF', 'CLP', 'CNY', 'COP', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'IDR',
|
|
171
|
+
'INR', 'JPY', 'KRW', 'MXN', 'MYR', 'NOK', 'NZD', 'PEN', 'PHP', 'PLN', 'RUB', 'SAR', 'SEK', 'SGD', 'THB', 'TRY', 'TWD',
|
|
172
|
+
'USD', 'VND', 'ZAR',
|
|
173
|
+
]),
|
|
174
|
+
// https://developer.paddle.com/concepts/sell/supported-currencies (the list its adapter carried before 2026-11-20):
|
|
175
|
+
// the currencies a transaction may be in.
|
|
176
|
+
paddle: Object.freeze([
|
|
177
|
+
'USD', 'GBP', 'EUR', 'ARS', 'AUD', 'BRL', 'CAD', 'CHF', 'CLP', 'CNY', 'COP', 'CZK', 'DKK', 'HKD', 'HUF', 'ILS',
|
|
178
|
+
'INR', 'JPY', 'KRW', 'MXN', 'NOK', 'NZD', 'PEN', 'PLN', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'TWD', 'UAH', 'VND', 'ZAR',
|
|
179
|
+
]),
|
|
180
|
+
// https://developers.upayments.com/reference/multi-currency (the list its adapter carried before 2026-11-20): what
|
|
181
|
+
// UInterfaceV2 takes (other than KWD once UPayments enables multi-currency for the account), each at its ISO 4217
|
|
182
|
+
// digits (KWD, BHD, OMR and JOD three).
|
|
183
|
+
upayments: Object.freeze(['KWD', 'SAR', 'USD', 'BHD', 'EUR', 'OMR', 'QAR', 'AED', 'INR', 'GBP', 'JOD']),
|
|
184
|
+
});
|
|
185
|
+
for (const [name, list] of Object.entries(PROVIDER_CURRENCIES)) {
|
|
186
|
+
for (const code of list) {
|
|
187
|
+
// A three-letter code, not necessarily on today's list one: a provider may still list a withdrawn code (BGN left
|
|
188
|
+
// list one when Bulgaria took the euro), which then counts with currencyDigits' default.
|
|
189
|
+
if (!/^[A-Z]{3}$/.test(code))
|
|
190
|
+
throw new Error(`@tanqory/money: PROVIDER_CURRENCIES.${name}: ${code} is not a currency code`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** The currencies a provider of PROVIDER_CURRENCIES prices in, in its docs' order. */
|
|
194
|
+
function providerCurrencies(provider) {
|
|
195
|
+
const list = PROVIDER_CURRENCIES[provider];
|
|
196
|
+
if (!list)
|
|
197
|
+
throw new Error(`@tanqory/money: no provider "${provider}" in PROVIDER_CURRENCIES`);
|
|
198
|
+
return list;
|
|
199
|
+
}
|
|
200
|
+
/** True when the provider prices in the currency (case- and space-insensitive). */
|
|
201
|
+
function providerTakes(provider, code) {
|
|
202
|
+
return providerCurrencies(provider).includes(normal(code));
|
|
203
|
+
}
|
|
204
|
+
/** The units a provider counts a currency in (ISO's when the provider has no entry for it). */
|
|
205
|
+
function providerUnits(provider, code) {
|
|
206
|
+
const c = normal(code);
|
|
207
|
+
if (provider) {
|
|
208
|
+
const table = PROVIDER_UNITS[provider];
|
|
209
|
+
if (!table)
|
|
210
|
+
throw new Error(`@tanqory/money: no provider "${provider}" in PROVIDER_UNITS`);
|
|
211
|
+
if (table[c])
|
|
212
|
+
return table[c];
|
|
213
|
+
if (table['*'])
|
|
214
|
+
return table['*'];
|
|
215
|
+
}
|
|
216
|
+
const d = currencyDigits(c);
|
|
217
|
+
return { exponent: d, precision: d };
|
|
218
|
+
}
|
|
219
|
+
// ─── exact decimals ──────────────────────────────────────────────────────────────────────────────────────────────────
|
|
220
|
+
const DECIMAL = /^([+-])?(\d+)?(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
|
|
221
|
+
/** The largest exponent an amount's text may carry (1e40 is far past any money). */
|
|
222
|
+
const MAX_EXPONENT = 40;
|
|
223
|
+
class InvalidAmountError extends Error {
|
|
224
|
+
constructor(value) {
|
|
225
|
+
super(`@tanqory/money: not a decimal amount: ${String(value).slice(0, 40)}`);
|
|
226
|
+
this.name = 'InvalidAmountError';
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
class InexactAmountError extends Error {
|
|
230
|
+
constructor(amount, currency) {
|
|
231
|
+
super(`amount ${amount} ${currency} has no exact value in the provider's unit`);
|
|
232
|
+
this.name = 'InexactAmountError';
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* A decimal as { n: BigInt, s: scale } (value = n / 10^s). Takes a decimal string, a finite number (read through its
|
|
237
|
+
* shortest round-trip text, so 0.1 is "0.1"), a BigInt, or a decimal object (Prisma.Decimal / decimal.js: toFixed()).
|
|
238
|
+
*/
|
|
239
|
+
function parse(value) {
|
|
240
|
+
let text;
|
|
241
|
+
if (typeof value === 'bigint')
|
|
242
|
+
return { n: value, s: 0 };
|
|
243
|
+
if (typeof value === 'number') {
|
|
244
|
+
if (!Number.isFinite(value))
|
|
245
|
+
throw new InvalidAmountError(value);
|
|
246
|
+
text = String(value);
|
|
247
|
+
}
|
|
248
|
+
else if (typeof value === 'string') {
|
|
249
|
+
text = value.trim();
|
|
250
|
+
}
|
|
251
|
+
else if (value && typeof value.toFixed === 'function' && typeof value.isFinite === 'function') {
|
|
252
|
+
if (!value.isFinite())
|
|
253
|
+
throw new InvalidAmountError(value);
|
|
254
|
+
text = value.toFixed();
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
throw new InvalidAmountError(value);
|
|
258
|
+
}
|
|
259
|
+
// A text longer than any amount, or an exponent past MAX_EXPONENT, is refused before any BigInt is built: 10n ** 3e8n
|
|
260
|
+
// would block the process (a provider's answer reaches here).
|
|
261
|
+
if (text.length > 100)
|
|
262
|
+
throw new InvalidAmountError(value);
|
|
263
|
+
const m = DECIMAL.exec(text);
|
|
264
|
+
if (!m || (m[2] === undefined && (m[3] === undefined || m[3] === '')))
|
|
265
|
+
throw new InvalidAmountError(value);
|
|
266
|
+
if (m[4] && Math.abs(Number(m[4])) > MAX_EXPONENT)
|
|
267
|
+
throw new InvalidAmountError(value);
|
|
268
|
+
const frac = m[3] || '';
|
|
269
|
+
let n = BigInt((m[2] || '0') + frac);
|
|
270
|
+
let s = frac.length - (m[4] ? Number(m[4]) : 0);
|
|
271
|
+
if (s < 0) {
|
|
272
|
+
n *= 10n ** BigInt(-s);
|
|
273
|
+
s = 0;
|
|
274
|
+
}
|
|
275
|
+
if (m[1] === '-')
|
|
276
|
+
n = -n;
|
|
277
|
+
return { n, s };
|
|
278
|
+
}
|
|
279
|
+
const pow10 = (k) => 10n ** BigInt(k);
|
|
280
|
+
const abs = (x) => (x < 0n ? -x : x);
|
|
281
|
+
/** n / 10^s at scale t (t >= s): exact. */
|
|
282
|
+
function rescale(d, t) {
|
|
283
|
+
return d.n * pow10(t - d.s);
|
|
284
|
+
}
|
|
285
|
+
const MODES = ['half_up', 'half_even', 'half_down', 'up', 'down', 'ceil', 'floor'];
|
|
286
|
+
const ROUNDING_MODES = Object.freeze(MODES.slice());
|
|
287
|
+
/** BigInt division q = n / m rounded by `mode` (m > 0). */
|
|
288
|
+
function divRound(n, m, mode) {
|
|
289
|
+
const q = n / m; // truncated toward zero
|
|
290
|
+
const r = n - q * m;
|
|
291
|
+
if (r === 0n)
|
|
292
|
+
return q;
|
|
293
|
+
const neg = n < 0n;
|
|
294
|
+
const away = neg ? q - 1n : q + 1n;
|
|
295
|
+
const twice = abs(r) * 2n;
|
|
296
|
+
switch (mode) {
|
|
297
|
+
case 'down': return q;
|
|
298
|
+
case 'up': return away;
|
|
299
|
+
case 'floor': return neg ? away : q;
|
|
300
|
+
case 'ceil': return neg ? q : away;
|
|
301
|
+
case 'half_up': return twice >= m ? away : q;
|
|
302
|
+
case 'half_down': return twice > m ? away : q;
|
|
303
|
+
case 'half_even': return twice > m || (twice === m && (abs(q) % 2n === 1n)) ? away : q;
|
|
304
|
+
default: throw new Error(`@tanqory/money: rounding mode must be one of ${MODES.join(', ')}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** The value rounded to `digits` decimals (mode default half_up), as BigInt units of 10^-digits. */
|
|
308
|
+
function unitsAt(value, digits, mode = 'half_up') {
|
|
309
|
+
if (!MODES.includes(mode))
|
|
310
|
+
throw new Error(`@tanqory/money: rounding mode must be one of ${MODES.join(', ')}`);
|
|
311
|
+
const d = parse(value);
|
|
312
|
+
if (d.s <= digits)
|
|
313
|
+
return d.n * pow10(digits - d.s);
|
|
314
|
+
return divRound(d.n, pow10(d.s - digits), mode);
|
|
315
|
+
}
|
|
316
|
+
/** BigInt units of 10^-digits as a decimal string with exactly `digits` decimals ("-0.05", "1200", "1.250"). */
|
|
317
|
+
function unitsText(units, digits) {
|
|
318
|
+
const neg = units < 0n;
|
|
319
|
+
const s = abs(units).toString().padStart(digits + 1, '0');
|
|
320
|
+
const out = digits ? `${s.slice(0, -digits)}.${s.slice(-digits)}` : s;
|
|
321
|
+
return neg && units !== 0n ? `-${out}` : out;
|
|
322
|
+
}
|
|
323
|
+
/** The value as a decimal string with exactly `digits` decimals, rounded by `mode` (half_up). */
|
|
324
|
+
function toFixedDigits(value, digits, mode = 'half_up') {
|
|
325
|
+
if (!Number.isInteger(digits) || digits < 0 || digits > 18)
|
|
326
|
+
throw new RangeError('@tanqory/money: digits must be 0-18');
|
|
327
|
+
return unitsText(unitsAt(value, digits, mode), digits);
|
|
328
|
+
}
|
|
329
|
+
/** The amount rounded to its currency's ISO 4217 digits: "12.35" (USD), "1235" (JPY), "1.235" (KWD). */
|
|
330
|
+
function roundMoney(amount, currency, mode = 'half_up') {
|
|
331
|
+
return toFixedDigits(amount, currencyDigits(currency), mode);
|
|
332
|
+
}
|
|
333
|
+
/** The exact value as the shortest decimal string ("1.5", "-0.25", "3"). */
|
|
334
|
+
function decimalText(value) {
|
|
335
|
+
const d = parse(value);
|
|
336
|
+
let { n, s } = d;
|
|
337
|
+
while (s > 0 && n % 10n === 0n) {
|
|
338
|
+
n /= 10n;
|
|
339
|
+
s -= 1;
|
|
340
|
+
}
|
|
341
|
+
return unitsText(n, s);
|
|
342
|
+
}
|
|
343
|
+
/** a + b, exact. */
|
|
344
|
+
function add(a, b) {
|
|
345
|
+
const x = parse(a), y = parse(b);
|
|
346
|
+
const t = Math.max(x.s, y.s);
|
|
347
|
+
return decimalText(unitsText(rescale(x, t) + rescale(y, t), t));
|
|
348
|
+
}
|
|
349
|
+
/** a - b, exact. */
|
|
350
|
+
function sub(a, b) {
|
|
351
|
+
const x = parse(a), y = parse(b);
|
|
352
|
+
const t = Math.max(x.s, y.s);
|
|
353
|
+
return decimalText(unitsText(rescale(x, t) - rescale(y, t), t));
|
|
354
|
+
}
|
|
355
|
+
/** a x b, exact (a quantity, a rate or a ratio as b). */
|
|
356
|
+
function mul(a, b) {
|
|
357
|
+
const x = parse(a), y = parse(b);
|
|
358
|
+
return decimalText(unitsText(x.n * y.n, x.s + y.s));
|
|
359
|
+
}
|
|
360
|
+
/** The sum of a list, exact ("0" for none). */
|
|
361
|
+
function sum(values) {
|
|
362
|
+
let acc = '0';
|
|
363
|
+
for (const v of values)
|
|
364
|
+
acc = add(acc, v);
|
|
365
|
+
return acc;
|
|
366
|
+
}
|
|
367
|
+
/** -1, 0 or 1 as a is below, equal to or above b. */
|
|
368
|
+
function compare(a, b) {
|
|
369
|
+
const x = parse(a), y = parse(b);
|
|
370
|
+
const t = Math.max(x.s, y.s);
|
|
371
|
+
const l = rescale(x, t), r = rescale(y, t);
|
|
372
|
+
return l < r ? -1 : l > r ? 1 : 0;
|
|
373
|
+
}
|
|
374
|
+
function isZero(a) {
|
|
375
|
+
return parse(a).n === 0n;
|
|
376
|
+
}
|
|
377
|
+
function negate(a) {
|
|
378
|
+
return decimalText(unitsText(-parse(a).n, parse(a).s));
|
|
379
|
+
}
|
|
380
|
+
/** a / b rounded to `digits` decimals by `mode` (half_up). b must not be 0. */
|
|
381
|
+
function div(a, b, digits, mode = 'half_up') {
|
|
382
|
+
const x = parse(a), y = parse(b);
|
|
383
|
+
if (y.n === 0n)
|
|
384
|
+
throw new RangeError('@tanqory/money: division by zero');
|
|
385
|
+
// a/b = (x.n / 10^x.s) / (y.n / 10^y.s); scaled to 10^digits.
|
|
386
|
+
let num = x.n * pow10(digits + y.s);
|
|
387
|
+
let den = y.n * pow10(x.s);
|
|
388
|
+
if (den < 0n) {
|
|
389
|
+
num = -num;
|
|
390
|
+
den = -den;
|
|
391
|
+
}
|
|
392
|
+
return unitsText(divRound(num, den, mode), digits);
|
|
393
|
+
}
|
|
394
|
+
/** `pct` percent of the amount, rounded to the currency's digits by `mode` (half_up): percentOf("19.99", "7", "USD") = "1.40". */
|
|
395
|
+
function percentOf(amount, pct, currency, mode = 'half_up') {
|
|
396
|
+
return div(mul(amount, pct), '100', currencyDigits(currency), mode);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* The amount split in proportion to `weights` (decimal strings or numbers >= 0), each part in the currency's digits,
|
|
400
|
+
* the parts summing exactly to the amount rounded to the currency (largest remainder; ties go to the earlier part).
|
|
401
|
+
*/
|
|
402
|
+
function allocate(amount, weights, currency) {
|
|
403
|
+
const d = currencyDigits(currency);
|
|
404
|
+
const total = unitsAt(amount, d);
|
|
405
|
+
const ws = weights.map((w) => parse(w));
|
|
406
|
+
if (!ws.length)
|
|
407
|
+
return [];
|
|
408
|
+
if (ws.some((w) => w.n < 0n))
|
|
409
|
+
throw new RangeError('@tanqory/money: weights must be >= 0');
|
|
410
|
+
const t = Math.max(...ws.map((w) => w.s));
|
|
411
|
+
const wi = ws.map((w) => rescale(w, t));
|
|
412
|
+
const wsum = wi.reduce((a, b) => a + b, 0n);
|
|
413
|
+
if (wsum === 0n)
|
|
414
|
+
throw new RangeError('@tanqory/money: weights must not all be 0');
|
|
415
|
+
const sign = total < 0n ? -1n : 1n;
|
|
416
|
+
const T = abs(total);
|
|
417
|
+
const parts = wi.map((w) => (T * w) / wsum);
|
|
418
|
+
const rems = wi.map((w, i) => ({ i, r: T * w - parts[i] * wsum }));
|
|
419
|
+
let left = T - parts.reduce((a, b) => a + b, 0n);
|
|
420
|
+
rems.sort((a, b) => (a.r === b.r ? a.i - b.i : a.r > b.r ? -1 : 1));
|
|
421
|
+
for (let k = 0; left > 0n; k++, left--)
|
|
422
|
+
parts[rems[k % rems.length].i] += 1n;
|
|
423
|
+
return parts.map((p) => unitsText(sign * p, d));
|
|
424
|
+
}
|
|
425
|
+
// ─── provider boundary ───────────────────────────────────────────────────────────────────────────────────────────────
|
|
426
|
+
/**
|
|
427
|
+
* The amount as the integer a provider is sent: rounded to the currency's precision for that provider (half up),
|
|
428
|
+
* times 10^exponent. ISO's digits when `opts.provider` is absent. Throws InexactAmountError when the result is not a
|
|
429
|
+
* safe integer, or when `opts.exact` and the amount had more digits than the precision (never charge a near amount).
|
|
430
|
+
*/
|
|
431
|
+
function toMinorUnits(amount, currency, opts = {}) {
|
|
432
|
+
const u = providerUnits(opts.provider, currency);
|
|
433
|
+
const d = parse(amount);
|
|
434
|
+
if (opts.exact && d.s > u.precision && d.n % pow10(d.s - u.precision) !== 0n)
|
|
435
|
+
throw new InexactAmountError(String(amount), normal(currency));
|
|
436
|
+
const units = unitsAt(amount, u.precision, opts.mode || 'half_up');
|
|
437
|
+
const minor = units * pow10(u.exponent - u.precision);
|
|
438
|
+
if (abs(minor) > BigInt(Number.MAX_SAFE_INTEGER))
|
|
439
|
+
throw new InexactAmountError(String(amount), normal(currency));
|
|
440
|
+
return Number(minor);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* A provider's integer amount back to a decimal string in the currency's ISO digits ("10.50", "1050", "1.050").
|
|
444
|
+
* Not an integer: InvalidAmountError. More digits than ISO has (a provider's sub-unit): kept exact, not rounded.
|
|
445
|
+
*/
|
|
446
|
+
function fromMinorUnits(minor, currency, opts = {}) {
|
|
447
|
+
const raw = typeof minor === 'string' ? minor.trim() : minor;
|
|
448
|
+
if (typeof raw === 'number' && !Number.isSafeInteger(raw))
|
|
449
|
+
throw new InvalidAmountError(minor);
|
|
450
|
+
if (typeof raw === 'string' && !/^[+-]?\d{1,30}$/.test(raw))
|
|
451
|
+
throw new InvalidAmountError(minor);
|
|
452
|
+
if (typeof raw !== 'number' && typeof raw !== 'string' && typeof raw !== 'bigint')
|
|
453
|
+
throw new InvalidAmountError(minor);
|
|
454
|
+
const u = providerUnits(opts.provider, currency);
|
|
455
|
+
const n = BigInt(raw);
|
|
456
|
+
const iso = currencyDigits(currency);
|
|
457
|
+
const exact = { n, s: u.exponent };
|
|
458
|
+
// In the ISO digits when that loses nothing, else every digit the provider sent.
|
|
459
|
+
const atIso = iso >= u.exponent ? n * pow10(iso - u.exponent) : n % pow10(u.exponent - iso) === 0n ? n / pow10(u.exponent - iso) : null;
|
|
460
|
+
return atIso === null ? unitsText(exact.n, exact.s) : unitsText(atIso, iso);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* The local currency of each country (ISO 3166-1 alpha-2 → ISO 4217), for every CountryCode value of the Admin API but
|
|
464
|
+
* ZZ ("unknown region", no currency). Used by the markets domain (a market's currency follows its countries;
|
|
465
|
+
* MarketRegionCountry.currency when a market sells in local currencies), by store creation and by platform billing.
|
|
466
|
+
* Not stated by any standard: a country whose own currency is not a CurrencyCode value (CU, KP, ZW) uses USD.
|
|
467
|
+
*/
|
|
468
|
+
const COUNTRY_CURRENCY = Object.freeze({
|
|
469
|
+
AF: 'AFN', AX: 'EUR', AL: 'ALL', DZ: 'DZD', AD: 'EUR', AO: 'AOA', AI: 'XCD', AG: 'XCD', AR: 'ARS', AM: 'AMD', AW: 'AWG',
|
|
470
|
+
AC: 'SHP', AU: 'AUD', AT: 'EUR', AZ: 'AZN', BS: 'BSD', BH: 'BHD', BD: 'BDT', BB: 'BBD', BY: 'BYN', BE: 'EUR', BZ: 'BZD',
|
|
471
|
+
BJ: 'XOF', BM: 'BMD', BT: 'BTN', BO: 'BOB', BA: 'BAM', BW: 'BWP', BV: 'NOK', BR: 'BRL', IO: 'USD', BN: 'BND', BG: 'EUR',
|
|
472
|
+
BF: 'XOF', BI: 'BIF', KH: 'KHR', CA: 'CAD', CV: 'CVE', BQ: 'USD', KY: 'KYD', CF: 'XAF', TD: 'XAF', CL: 'CLP', CN: 'CNY',
|
|
473
|
+
CX: 'AUD', CC: 'AUD', CO: 'COP', KM: 'KMF', CG: 'XAF', CD: 'CDF', CK: 'NZD', CR: 'CRC', HR: 'EUR', CU: 'USD', CW: 'XCG',
|
|
474
|
+
CY: 'EUR', CZ: 'CZK', CI: 'XOF', DK: 'DKK', DJ: 'DJF', DM: 'XCD', DO: 'DOP', EC: 'USD', EG: 'EGP', SV: 'USD', GQ: 'XAF',
|
|
475
|
+
ER: 'ERN', EE: 'EUR', SZ: 'SZL', ET: 'ETB', FK: 'FKP', FO: 'DKK', FJ: 'FJD', FI: 'EUR', FR: 'EUR', GF: 'EUR', PF: 'XPF',
|
|
476
|
+
TF: 'EUR', GA: 'XAF', GM: 'GMD', GE: 'GEL', DE: 'EUR', GH: 'GHS', GI: 'GIP', GR: 'EUR', GL: 'DKK', GD: 'XCD', GP: 'EUR',
|
|
477
|
+
GT: 'GTQ', GG: 'GBP', GN: 'GNF', GW: 'XOF', GY: 'GYD', HT: 'HTG', HM: 'AUD', VA: 'EUR', HN: 'HNL', HK: 'HKD', HU: 'HUF',
|
|
478
|
+
IS: 'ISK', IN: 'INR', ID: 'IDR', IR: 'IRR', IQ: 'IQD', IE: 'EUR', IM: 'GBP', IL: 'ILS', IT: 'EUR', JM: 'JMD', JP: 'JPY',
|
|
479
|
+
JE: 'GBP', JO: 'JOD', KZ: 'KZT', KE: 'KES', KI: 'AUD', KP: 'USD', XK: 'EUR', KW: 'KWD', KG: 'KGS', LA: 'LAK', LV: 'EUR',
|
|
480
|
+
LB: 'LBP', LS: 'LSL', LR: 'LRD', LY: 'LYD', LI: 'CHF', LT: 'EUR', LU: 'EUR', MO: 'MOP', MG: 'MGA', MW: 'MWK', MY: 'MYR',
|
|
481
|
+
MV: 'MVR', ML: 'XOF', MT: 'EUR', MQ: 'EUR', MR: 'MRU', MU: 'MUR', YT: 'EUR', MX: 'MXN', MD: 'MDL', MC: 'EUR', MN: 'MNT',
|
|
482
|
+
ME: 'EUR', MS: 'XCD', MA: 'MAD', MZ: 'MZN', MM: 'MMK', NA: 'NAD', NR: 'AUD', NP: 'NPR', NL: 'EUR', AN: 'ANG', NC: 'XPF',
|
|
483
|
+
NZ: 'NZD', NI: 'NIO', NE: 'XOF', NG: 'NGN', NU: 'NZD', NF: 'AUD', MK: 'MKD', NO: 'NOK', OM: 'OMR', PK: 'PKR', PS: 'ILS',
|
|
484
|
+
PA: 'PAB', PG: 'PGK', PY: 'PYG', PE: 'PEN', PH: 'PHP', PN: 'NZD', PL: 'PLN', PT: 'EUR', QA: 'QAR', CM: 'XAF', RE: 'EUR',
|
|
485
|
+
RO: 'RON', RU: 'RUB', RW: 'RWF', BL: 'EUR', SH: 'SHP', KN: 'XCD', LC: 'XCD', MF: 'EUR', PM: 'EUR', WS: 'WST', SM: 'EUR',
|
|
486
|
+
ST: 'STN', SA: 'SAR', SN: 'XOF', RS: 'RSD', SC: 'SCR', SL: 'SLL', SG: 'SGD', SX: 'XCG', SK: 'EUR', SI: 'EUR', SB: 'SBD',
|
|
487
|
+
SO: 'SOS', ZA: 'ZAR', GS: 'GBP', KR: 'KRW', SS: 'SSP', ES: 'EUR', LK: 'LKR', VC: 'XCD', SD: 'SDG', SR: 'SRD', SJ: 'NOK',
|
|
488
|
+
SE: 'SEK', CH: 'CHF', SY: 'SYP', TW: 'TWD', TJ: 'TJS', TZ: 'TZS', TH: 'THB', TL: 'USD', TG: 'XOF', TK: 'NZD', TO: 'TOP',
|
|
489
|
+
TT: 'TTD', TA: 'GBP', TN: 'TND', TR: 'TRY', TM: 'TMT', TC: 'USD', TV: 'AUD', UG: 'UGX', UA: 'UAH', AE: 'AED', GB: 'GBP',
|
|
490
|
+
US: 'USD', UM: 'USD', UY: 'UYU', UZ: 'UZS', VU: 'VUV', VE: 'VES', VN: 'VND', VG: 'USD', WF: 'XPF', EH: 'MAD', YE: 'YER',
|
|
491
|
+
ZM: 'ZMW', ZW: 'USD',
|
|
492
|
+
});
|
|
493
|
+
/** The local currency of a country code (upper case), or null for an unknown code or ZZ. */
|
|
494
|
+
function countryCurrency(code) {
|
|
495
|
+
return (code && COUNTRY_CURRENCY[String(code).trim().toUpperCase()]) || null;
|
|
496
|
+
}
|
|
497
|
+
/** The distinct local currencies of a list of countries, in first-seen order; unknown codes are skipped. */
|
|
498
|
+
function countryCurrencies(codes) {
|
|
499
|
+
const out = [];
|
|
500
|
+
for (const c of codes || []) {
|
|
501
|
+
const cur = countryCurrency(c);
|
|
502
|
+
if (cur && !out.includes(cur))
|
|
503
|
+
out.push(cur);
|
|
504
|
+
}
|
|
505
|
+
return out;
|
|
506
|
+
}
|
|
507
|
+
export { COUNTRY_CURRENCY, countryCurrency, countryCurrencies, ISO_4217, CURRENCY_DIGITS, NO_MINOR_UNIT, DISPLAY_WHOLE_UNITS, displayDigits, displayAmount, PROVIDER_UNITS, PROVIDER_CURRENCIES, ROUNDING_MODES, InvalidAmountError, InexactAmountError, currencyDigits, isCurrencyCode, providerUnits, providerCurrencies, providerTakes, toFixedDigits, roundMoney, decimalText, add, sub, mul, div, sum, compare, isZero, negate, percentOf, allocate, toMinorUnits, fromMinorUnits, };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declaring a headless theme: its sections and blocks with their settings schema, and the theme itself.
|
|
3
|
+
*
|
|
4
|
+
* const hero = defineSection({
|
|
5
|
+
* type: 'hero', title: 'Hero', settings: { heading: field.text('After dark', { label: 'Heading' }) },
|
|
6
|
+
* presets: [{ name: 'Hero' }],
|
|
7
|
+
* ai: ai.hero({ heading: 'heading' }),
|
|
8
|
+
* component: ({ settings }) => <section><h1>{settings.heading}</h1></section>,
|
|
9
|
+
* })
|
|
10
|
+
* export default defineTheme({ name: 'Aurora', sections: [header, hero, footer], Layout })
|
|
11
|
+
*
|
|
12
|
+
* A section component receives `{ settings, blocks }`: every setting resolved against its schema (defaults filled,
|
|
13
|
+
* legacy values mapped, types coerced; a resource setting is its handle, for `useMenu` / `useCollection` /
|
|
14
|
+
* `useProduct` / `<Image>`), and its blocks in order, each with its own resolved settings. The section renderer
|
|
15
|
+
* wraps each section in its `data-tq-node` element, and the canvas applies live edits to these props.
|
|
16
|
+
*/
|
|
17
|
+
import type { ComponentType, ReactNode } from 'react';
|
|
18
|
+
import { type Fields, type HandlesOf, type Schema, type SettingsOf } from '../sdk/schema.js';
|
|
19
|
+
import type { SectionZone, ThemeDefinition, ThemeSection } from '../sdk/theme.js';
|
|
20
|
+
import type { AiRole } from './ai.js';
|
|
21
|
+
import { APP_BLOCKS } from '../runtime/kit/contract/content.js';
|
|
22
|
+
/** A section's or block's settings as its component receives them. */
|
|
23
|
+
export type SettingsFor<F extends Fields> = SettingsOf<Schema<F>> & HandlesOf<Schema<F>>;
|
|
24
|
+
/**
|
|
25
|
+
* One block of a section, in the merchant's order. A block of an installed app (`app:<app>/<block>`, where the section
|
|
26
|
+
* lists `'@app'`) keeps its saved settings: render it with `<AppBlock block={b} />` (theme-abi §7.2).
|
|
27
|
+
*/
|
|
28
|
+
export interface BlockInstance {
|
|
29
|
+
/** The node id the merchant's content gave it (the placeholder's `data-tq-node`); absent on an id-less block. */
|
|
30
|
+
id?: string;
|
|
31
|
+
type: string;
|
|
32
|
+
settings: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export interface SectionComponentProps<F extends Fields = Fields> {
|
|
35
|
+
settings: SettingsFor<F>;
|
|
36
|
+
blocks: BlockInstance[];
|
|
37
|
+
}
|
|
38
|
+
/** A block definition (`defineBlock`): a settings schema that nests in sections. */
|
|
39
|
+
export type BlockSchema<F extends Fields = Fields> = Schema<F> & {
|
|
40
|
+
readonly ai?: AiRole | readonly AiRole[];
|
|
41
|
+
};
|
|
42
|
+
/** Declare a block: its persisted type, its settings, and the sections it may be placed in. */
|
|
43
|
+
export declare function defineBlock<const F extends Fields>(def: {
|
|
44
|
+
type: string;
|
|
45
|
+
title: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
settings: F;
|
|
48
|
+
parents?: readonly string[];
|
|
49
|
+
requires?: readonly string[];
|
|
50
|
+
}): BlockSchema<F>;
|
|
51
|
+
export interface SectionPresetDef {
|
|
52
|
+
name: string;
|
|
53
|
+
settings?: Record<string, unknown>;
|
|
54
|
+
blocks?: {
|
|
55
|
+
type: string;
|
|
56
|
+
settings?: Record<string, unknown>;
|
|
57
|
+
}[];
|
|
58
|
+
}
|
|
59
|
+
export interface SectionDefinition<F extends Fields> {
|
|
60
|
+
/** The persisted section type: never changes once stores have saved it. */
|
|
61
|
+
type: string;
|
|
62
|
+
title: string;
|
|
63
|
+
/** One line for the editor's "Add section" picker. */
|
|
64
|
+
description?: string;
|
|
65
|
+
/** The picker group. */
|
|
66
|
+
category?: string;
|
|
67
|
+
/** Where the page places it: the header zone, the page body (default), the footer zone. */
|
|
68
|
+
zone?: SectionZone;
|
|
69
|
+
settings: F;
|
|
70
|
+
/**
|
|
71
|
+
* The blocks this section accepts. `'@app'` (`APP_BLOCKS`) accepts the blocks of the store's installed apps: render
|
|
72
|
+
* each with `<AppBlock>`. Every theme lists it on its product section and on a general section (theme-abi §7.2
|
|
73
|
+
* AppBlocks-1); a header or footer may list it too.
|
|
74
|
+
*/
|
|
75
|
+
blocks?: readonly (BlockSchema | typeof APP_BLOCKS)[];
|
|
76
|
+
/** "Add section" starting points; `presets[0]` is what the editor previews. */
|
|
77
|
+
presets?: readonly SectionPresetDef[];
|
|
78
|
+
/** Route kinds the section needs (`product`, `collection`, `cart`, …): offered only on those templates. */
|
|
79
|
+
requires?: readonly string[];
|
|
80
|
+
/** The roles it plays for Tanqory AI (`ai.hero`, `ai.headerMenu`, …). */
|
|
81
|
+
ai?: AiRole | readonly AiRole[];
|
|
82
|
+
component: ComponentType<SectionComponentProps<F>>;
|
|
83
|
+
}
|
|
84
|
+
/** A section as the runtime and the build read it. */
|
|
85
|
+
export type HeadlessSection<F extends Fields = Fields> = ThemeSection<SectionComponentProps<F>> & {
|
|
86
|
+
readonly ai?: AiRole | readonly AiRole[];
|
|
87
|
+
readonly headless: true;
|
|
88
|
+
};
|
|
89
|
+
/** Declare a section of a headless theme. */
|
|
90
|
+
export declare function defineSection<const F extends Fields>(def: SectionDefinition<F>): HeadlessSection<F>;
|
|
91
|
+
export interface HeadlessThemeDefinition {
|
|
92
|
+
name: string;
|
|
93
|
+
/** Every section, in the editor picker's order. A section of type `not-found` renders the 404 body. */
|
|
94
|
+
sections: readonly HeadlessSection<any>[];
|
|
95
|
+
/** The page layout around the sections (the runtime renders header zone → `<main id="main">` → footer zone inside it); a place for `<CartDrawer>` and `<ConsentBanner>`. */
|
|
96
|
+
Layout?: ComponentType<{
|
|
97
|
+
children: ReactNode;
|
|
98
|
+
}>;
|
|
99
|
+
/** Wraps every page and the editor's single-section preview. */
|
|
100
|
+
Provider?: ThemeDefinition['Provider'];
|
|
101
|
+
/** Words for the breadcrumb trail; the kit's strings by default. */
|
|
102
|
+
labels?: ThemeDefinition['labels'];
|
|
103
|
+
/** External stylesheets every page loads (web fonts). */
|
|
104
|
+
stylesheets?: readonly string[];
|
|
105
|
+
/** English words the theme ships in defaults and presets, rendered in the page's language (`copy.*` locale keys). */
|
|
106
|
+
shippedCopy?: ThemeDefinition['shippedCopy'];
|
|
107
|
+
}
|
|
108
|
+
/** Declare a headless theme. */
|
|
109
|
+
export declare function defineTheme(def: HeadlessThemeDefinition): ThemeDefinition;
|