arky-sdk 0.4.16 → 0.4.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -5
- package/dist/index.cjs +28 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -65
- package/dist/index.d.ts +22 -65
- package/dist/index.js +28 -148
- package/dist/index.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,9 +69,8 @@ const { items: products } = await arky.eshop.getProducts({
|
|
|
69
69
|
// Get product details (like arky.io/products/guitar)
|
|
70
70
|
const product = await arky.eshop.getProduct({ id: 'prod_123' });
|
|
71
71
|
|
|
72
|
-
//
|
|
73
|
-
const
|
|
74
|
-
console.log(`${arky.utils.formatMinor(price.amount, price.currency)}`); // "$29.99"
|
|
72
|
+
// Format price (uses currency from price object)
|
|
73
|
+
const formatted = arky.utils.formatPrice(product.variants[0].prices); // "$29.99"
|
|
75
74
|
```
|
|
76
75
|
|
|
77
76
|
### 3. Shop & Checkout
|
|
@@ -308,8 +307,8 @@ const forSubmit = arky.utils.prepareBlocksForSubmission(blocks)
|
|
|
308
307
|
const formatted = arky.utils.formatMinor(999, 'usd') // "$9.99"
|
|
309
308
|
const payment = arky.utils.formatPayment(paymentObject)
|
|
310
309
|
|
|
311
|
-
//
|
|
312
|
-
const
|
|
310
|
+
// Format prices from array
|
|
311
|
+
const formatted = arky.utils.formatPrice(prices) // "$9.99"
|
|
313
312
|
const amount = arky.utils.getPriceAmount(prices, 'US')
|
|
314
313
|
|
|
315
314
|
// Currency
|
package/dist/index.cjs
CHANGED
|
@@ -1416,162 +1416,51 @@ var createPlatformApi = (apiConfig) => {
|
|
|
1416
1416
|
},
|
|
1417
1417
|
getConfigCache() {
|
|
1418
1418
|
return cachedConfig;
|
|
1419
|
-
},
|
|
1420
|
-
setConfigCache(config) {
|
|
1421
|
-
cachedConfig = config;
|
|
1422
1419
|
}
|
|
1423
1420
|
};
|
|
1424
1421
|
};
|
|
1425
1422
|
|
|
1426
1423
|
// src/utils/currency.ts
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
return cachedCurrencies;
|
|
1424
|
+
function formatCurrency(amount, currencyCode, locale = "en") {
|
|
1425
|
+
return new Intl.NumberFormat(locale, {
|
|
1426
|
+
style: "currency",
|
|
1427
|
+
currency: currencyCode.toUpperCase()
|
|
1428
|
+
}).format(amount);
|
|
1433
1429
|
}
|
|
1434
1430
|
function getCurrencySymbol(currencyCode) {
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
}).formatToParts(0);
|
|
1442
|
-
const symbolPart = parts.find((p) => p.type === "currency");
|
|
1443
|
-
return symbolPart?.value || currencyCode.toUpperCase();
|
|
1444
|
-
} catch {
|
|
1445
|
-
return currencyCode.toUpperCase();
|
|
1446
|
-
}
|
|
1431
|
+
const parts = new Intl.NumberFormat("en", {
|
|
1432
|
+
style: "currency",
|
|
1433
|
+
currency: currencyCode.toUpperCase(),
|
|
1434
|
+
currencyDisplay: "narrowSymbol"
|
|
1435
|
+
}).formatToParts(0);
|
|
1436
|
+
return parts.find((p) => p.type === "currency")?.value || "";
|
|
1447
1437
|
}
|
|
1448
1438
|
function getCurrencyName(currencyCode) {
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
const displayNames = new Intl.DisplayNames(["en"], { type: "currency" });
|
|
1452
|
-
return displayNames.of(currencyCode.toUpperCase()) || currencyCode.toUpperCase();
|
|
1453
|
-
} catch {
|
|
1454
|
-
return currencyCode.toUpperCase();
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
var SYMBOL_AFTER_CURRENCIES = ["sek", "nok", "dkk", "pln", "czk", "huf", "ron", "bgn", "hrk"];
|
|
1458
|
-
function isSymbolAfterCurrency(currency) {
|
|
1459
|
-
return SYMBOL_AFTER_CURRENCIES.includes(currency.toLowerCase());
|
|
1439
|
+
const displayNames = new Intl.DisplayNames(["en"], { type: "currency" });
|
|
1440
|
+
return displayNames.of(currencyCode.toUpperCase()) || "";
|
|
1460
1441
|
}
|
|
1461
1442
|
|
|
1462
1443
|
// src/utils/price.ts
|
|
1463
1444
|
function convertToMajor(minorAmount) {
|
|
1464
|
-
return
|
|
1445
|
+
return minorAmount / 100;
|
|
1465
1446
|
}
|
|
1466
|
-
function
|
|
1467
|
-
|
|
1468
|
-
const { showSymbols = true, decimalPlaces = 2, customSymbol } = options;
|
|
1469
|
-
const roundedAmount = amount.toFixed(decimalPlaces);
|
|
1470
|
-
if (!showSymbols) {
|
|
1471
|
-
return `${roundedAmount} ${currency}`;
|
|
1472
|
-
}
|
|
1473
|
-
const symbol = customSymbol || getCurrencySymbol(currency);
|
|
1474
|
-
if (isSymbolAfterCurrency(currency)) {
|
|
1475
|
-
return `${roundedAmount} ${symbol ?? currency}`;
|
|
1476
|
-
}
|
|
1477
|
-
return `${symbol ?? currency}${roundedAmount}`;
|
|
1447
|
+
function formatMinor(amountMinor, currency) {
|
|
1448
|
+
return formatCurrency(convertToMajor(amountMinor), currency);
|
|
1478
1449
|
}
|
|
1479
|
-
function
|
|
1480
|
-
|
|
1481
|
-
return formatCurrencyAmount(major, currency, options);
|
|
1482
|
-
}
|
|
1483
|
-
function formatPayment(payment, options = {}) {
|
|
1484
|
-
if (!payment || !payment.currency) return "";
|
|
1485
|
-
const { showSymbols = true, decimalPlaces = 2, showBreakdown = false } = options;
|
|
1486
|
-
if (showBreakdown) {
|
|
1487
|
-
const subtotal = formatMinor(payment.subtotal, payment.currency, { showSymbols, decimalPlaces });
|
|
1488
|
-
const discount = (payment.discount ?? 0) > 0 ? formatMinor(payment.discount, payment.currency, { showSymbols, decimalPlaces }) : null;
|
|
1489
|
-
const taxAmount = payment.tax?.amount ?? 0;
|
|
1490
|
-
const tax = taxAmount > 0 ? formatMinor(taxAmount, payment.currency, { showSymbols, decimalPlaces }) : null;
|
|
1491
|
-
const total = formatMinor(payment.total, payment.currency, { showSymbols, decimalPlaces });
|
|
1492
|
-
let result = `Subtotal: ${subtotal}`;
|
|
1493
|
-
if (discount) result += `, Discount: -${discount}`;
|
|
1494
|
-
if (tax) result += `, Tax: ${tax}`;
|
|
1495
|
-
result += `, Total: ${total}`;
|
|
1496
|
-
return result;
|
|
1497
|
-
}
|
|
1498
|
-
return formatMinor(payment.total, payment.currency, { showSymbols, decimalPlaces });
|
|
1450
|
+
function formatPayment(payment) {
|
|
1451
|
+
return formatMinor(payment.total, payment.currency);
|
|
1499
1452
|
}
|
|
1500
|
-
function
|
|
1453
|
+
function formatPrice(prices, marketId) {
|
|
1501
1454
|
if (!prices || prices.length === 0) return "";
|
|
1502
|
-
const
|
|
1503
|
-
showSymbols = true,
|
|
1504
|
-
decimalPlaces = 2,
|
|
1505
|
-
showCompareAt = true,
|
|
1506
|
-
fallbackMarket
|
|
1507
|
-
} = options;
|
|
1508
|
-
let price = prices.find((p) => p.market === marketId);
|
|
1509
|
-
if (!price && fallbackMarket) {
|
|
1510
|
-
price = prices.find((p) => p.market === fallbackMarket);
|
|
1511
|
-
}
|
|
1512
|
-
if (!price) {
|
|
1513
|
-
price = prices[0];
|
|
1514
|
-
}
|
|
1455
|
+
const price = marketId ? prices.find((p) => p.market === marketId) || prices[0] : prices[0];
|
|
1515
1456
|
if (!price) return "";
|
|
1516
|
-
|
|
1517
|
-
let symbol;
|
|
1518
|
-
if (businessMarkets) {
|
|
1519
|
-
const marketData = businessMarkets.find((m) => m.id === price.market || m.code === price.market);
|
|
1520
|
-
if (marketData?.currency) {
|
|
1521
|
-
currency = marketData.currency;
|
|
1522
|
-
symbol = getCurrencySymbol(currency);
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
if (!currency) return "";
|
|
1526
|
-
const formattedPrice = formatMinor(price.amount ?? 0, currency, {
|
|
1527
|
-
showSymbols,
|
|
1528
|
-
decimalPlaces,
|
|
1529
|
-
customSymbol: symbol
|
|
1530
|
-
});
|
|
1531
|
-
if (showCompareAt && price.compareAt && price.compareAt > (price.amount ?? 0)) {
|
|
1532
|
-
const formattedCompareAt = formatMinor(price.compareAt, currency, {
|
|
1533
|
-
showSymbols,
|
|
1534
|
-
decimalPlaces,
|
|
1535
|
-
customSymbol: symbol
|
|
1536
|
-
});
|
|
1537
|
-
return `${formattedPrice} was ${formattedCompareAt}`;
|
|
1538
|
-
}
|
|
1539
|
-
return formattedPrice;
|
|
1457
|
+
return formatMinor(price.amount, price.currency);
|
|
1540
1458
|
}
|
|
1541
|
-
function getPriceAmount(prices, marketId
|
|
1459
|
+
function getPriceAmount(prices, marketId) {
|
|
1542
1460
|
if (!prices || prices.length === 0) return 0;
|
|
1543
|
-
|
|
1544
|
-
if (!price && fallbackMarket) {
|
|
1545
|
-
price = prices.find((p) => p.market === fallbackMarket);
|
|
1546
|
-
}
|
|
1547
|
-
if (!price) {
|
|
1548
|
-
price = prices[0];
|
|
1549
|
-
}
|
|
1461
|
+
const price = prices.find((p) => p.market === marketId) || prices[0];
|
|
1550
1462
|
return price?.amount || 0;
|
|
1551
1463
|
}
|
|
1552
|
-
function createPaymentForCheckout(subtotalMinor, marketId, currency, paymentMethod, options = {}) {
|
|
1553
|
-
const { discount = 0, taxAmount = 0, taxRateBps = 0, promoCode } = options;
|
|
1554
|
-
const total = subtotalMinor - discount + taxAmount;
|
|
1555
|
-
return {
|
|
1556
|
-
currency,
|
|
1557
|
-
market: marketId,
|
|
1558
|
-
subtotal: subtotalMinor,
|
|
1559
|
-
shipping: 0,
|
|
1560
|
-
discount,
|
|
1561
|
-
total,
|
|
1562
|
-
paid: 0,
|
|
1563
|
-
refunds: [],
|
|
1564
|
-
type: paymentMethod,
|
|
1565
|
-
...taxAmount > 0 && {
|
|
1566
|
-
tax: {
|
|
1567
|
-
amount: taxAmount,
|
|
1568
|
-
rateBps: taxRateBps,
|
|
1569
|
-
lines: []
|
|
1570
|
-
}
|
|
1571
|
-
},
|
|
1572
|
-
...promoCode && { promoCode }
|
|
1573
|
-
};
|
|
1574
|
-
}
|
|
1575
1464
|
|
|
1576
1465
|
// src/utils/validation.ts
|
|
1577
1466
|
function validatePhoneNumber(phone) {
|
|
@@ -1763,7 +1652,7 @@ function track(eventName, params) {
|
|
|
1763
1652
|
}
|
|
1764
1653
|
|
|
1765
1654
|
// src/index.ts
|
|
1766
|
-
var SDK_VERSION = "0.4.
|
|
1655
|
+
var SDK_VERSION = "0.4.21";
|
|
1767
1656
|
var SUPPORTED_FRAMEWORKS = [
|
|
1768
1657
|
"astro",
|
|
1769
1658
|
"react",
|
|
@@ -1794,13 +1683,6 @@ async function createArkySDK(config) {
|
|
|
1794
1683
|
}
|
|
1795
1684
|
}).catch(() => {
|
|
1796
1685
|
});
|
|
1797
|
-
const configParams = config.fetchFullConfig ? { params: { currencies: true } } : void 0;
|
|
1798
|
-
platformApi.getConfig(configParams).then((platformConfig) => {
|
|
1799
|
-
if (platformConfig.currencies) {
|
|
1800
|
-
setCurrenciesCache(platformConfig.currencies);
|
|
1801
|
-
}
|
|
1802
|
-
}).catch(() => {
|
|
1803
|
-
});
|
|
1804
1686
|
}
|
|
1805
1687
|
const sdk = {
|
|
1806
1688
|
auth: authApi,
|
|
@@ -1848,15 +1730,13 @@ async function createArkySDK(config) {
|
|
|
1848
1730
|
formatBlockValue,
|
|
1849
1731
|
prepareBlocksForSubmission,
|
|
1850
1732
|
extractBlockValues,
|
|
1851
|
-
|
|
1852
|
-
getPriceAmount,
|
|
1733
|
+
formatPrice: (prices) => formatPrice(prices, apiConfig.market),
|
|
1734
|
+
getPriceAmount: (prices) => getPriceAmount(prices, apiConfig.market),
|
|
1853
1735
|
formatPayment,
|
|
1854
1736
|
formatMinor,
|
|
1855
|
-
|
|
1737
|
+
formatCurrency,
|
|
1856
1738
|
getCurrencySymbol,
|
|
1857
1739
|
getCurrencyName,
|
|
1858
|
-
getCurrenciesCache,
|
|
1859
|
-
setCurrenciesCache,
|
|
1860
1740
|
validatePhoneNumber,
|
|
1861
1741
|
tzGroups,
|
|
1862
1742
|
findTimeZone,
|
|
@@ -1885,9 +1765,8 @@ exports.PaymentMethodType = PaymentMethodType;
|
|
|
1885
1765
|
exports.SDK_VERSION = SDK_VERSION;
|
|
1886
1766
|
exports.SUPPORTED_FRAMEWORKS = SUPPORTED_FRAMEWORKS;
|
|
1887
1767
|
exports.createArkySDK = createArkySDK;
|
|
1888
|
-
exports.
|
|
1768
|
+
exports.formatCurrency = formatCurrency;
|
|
1889
1769
|
exports.getCurrencyName = getCurrencyName;
|
|
1890
1770
|
exports.getCurrencySymbol = getCurrencySymbol;
|
|
1891
|
-
exports.setCurrenciesCache = setCurrenciesCache;
|
|
1892
1771
|
//# sourceMappingURL=index.cjs.map
|
|
1893
1772
|
//# sourceMappingURL=index.cjs.map
|