@shoppexio/storefront 1.0.73 → 1.0.75
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/CHANGELOG.md +10 -0
- package/dist/index.cjs +32 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +32 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -116,7 +116,10 @@ interface Shop {
|
|
|
116
116
|
* payload is denominated in it.
|
|
117
117
|
*/
|
|
118
118
|
currency: string;
|
|
119
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* The currency the shop prices in when nothing is requested. Always one of
|
|
121
|
+
* `available_currencies`, so it is always a valid `init({ currency })`.
|
|
122
|
+
*/
|
|
120
123
|
default_currency?: string;
|
|
121
124
|
/**
|
|
122
125
|
* Currencies the merchant enabled, exactly as stored (an array, or the
|
package/dist/index.d.ts
CHANGED
|
@@ -116,7 +116,10 @@ interface Shop {
|
|
|
116
116
|
* payload is denominated in it.
|
|
117
117
|
*/
|
|
118
118
|
currency: string;
|
|
119
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* The currency the shop prices in when nothing is requested. Always one of
|
|
121
|
+
* `available_currencies`, so it is always a valid `init({ currency })`.
|
|
122
|
+
*/
|
|
120
123
|
default_currency?: string;
|
|
121
124
|
/**
|
|
122
125
|
* Currencies the merchant enabled, exactly as stored (an array, or the
|
package/dist/index.js
CHANGED
|
@@ -2679,6 +2679,9 @@ function normalizeRequestedCurrency(value) {
|
|
|
2679
2679
|
const normalized = value?.trim().toUpperCase();
|
|
2680
2680
|
return normalized && /^[A-Z]{3}$/.test(normalized) ? normalized : null;
|
|
2681
2681
|
}
|
|
2682
|
+
function resolveBuyerCurrency(configCurrency) {
|
|
2683
|
+
return getRequestedCurrencyFromLocation() ?? normalizeRequestedCurrency(configCurrency);
|
|
2684
|
+
}
|
|
2682
2685
|
function getRequestedCurrencyFromLocation() {
|
|
2683
2686
|
if (typeof window === "undefined" || !window.location) {
|
|
2684
2687
|
return null;
|
|
@@ -2706,12 +2709,13 @@ var currentConfig = null;
|
|
|
2706
2709
|
var cachedShopId = null;
|
|
2707
2710
|
var DEFAULT_CHECKOUT_BASE_URL = "https://checkout.shoppex.io";
|
|
2708
2711
|
function initConfig(storeSlug, options) {
|
|
2712
|
+
const currency = normalizeInitCurrency(options?.currency);
|
|
2709
2713
|
const normalizedShopId = options?.shopId?.trim();
|
|
2710
2714
|
cachedShopId = normalizedShopId ? normalizedShopId : null;
|
|
2711
2715
|
currentConfig = {
|
|
2712
2716
|
storeSlug,
|
|
2713
2717
|
locale: options?.locale,
|
|
2714
|
-
currency
|
|
2718
|
+
currency,
|
|
2715
2719
|
apiBaseUrl: options?.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
2716
2720
|
checkoutBaseUrl: options?.checkoutBaseUrl ?? DEFAULT_CHECKOUT_BASE_URL
|
|
2717
2721
|
};
|
|
@@ -3164,9 +3168,11 @@ function clearCache() {
|
|
|
3164
3168
|
cache.clear();
|
|
3165
3169
|
pending.clear();
|
|
3166
3170
|
}
|
|
3171
|
+
var AUDIENCE_PREFIX = /^[^|]*\|[^|]*\|/;
|
|
3167
3172
|
function invalidateCache(prefixOrKey) {
|
|
3168
3173
|
for (const key of cache.keys()) {
|
|
3169
|
-
|
|
3174
|
+
const bareKey = key.replace(AUDIENCE_PREFIX, "");
|
|
3175
|
+
if (key === prefixOrKey || key.startsWith(prefixOrKey) || bareKey === prefixOrKey || bareKey.startsWith(prefixOrKey)) {
|
|
3170
3176
|
cache.delete(key);
|
|
3171
3177
|
}
|
|
3172
3178
|
}
|
|
@@ -3365,6 +3371,15 @@ function appendQueryParam(url2, key, value) {
|
|
|
3365
3371
|
const separator = url2.includes("?") ? "&" : "?";
|
|
3366
3372
|
return `${url2}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
3367
3373
|
}
|
|
3374
|
+
function forwardFailure(response, overrides = {}) {
|
|
3375
|
+
const { success: _success, data: _data, ...failure } = response;
|
|
3376
|
+
return {
|
|
3377
|
+
...failure,
|
|
3378
|
+
success: false,
|
|
3379
|
+
message: overrides.message ?? response.message,
|
|
3380
|
+
...overrides.data !== void 0 ? { data: overrides.data } : {}
|
|
3381
|
+
};
|
|
3382
|
+
}
|
|
3368
3383
|
async function request(endpoint, options = {}) {
|
|
3369
3384
|
const config = options.baseUrl ? null : getConfig();
|
|
3370
3385
|
const audience = config ?? (isInitialized() ? getConfig() : null);
|
|
@@ -3380,7 +3395,7 @@ async function request(endpoint, options = {}) {
|
|
|
3380
3395
|
} = options;
|
|
3381
3396
|
const retryCount = retries ?? (method === "GET" ? MAX_RETRIES : 0);
|
|
3382
3397
|
const apiBaseUrl = baseUrl ?? config?.apiBaseUrl ?? "";
|
|
3383
|
-
const buyerCurrency = pricesInBuyerCurrency
|
|
3398
|
+
const buyerCurrency = pricesInBuyerCurrency ? resolveBuyerCurrency(audience?.currency) : null;
|
|
3384
3399
|
const url2 = buyerCurrency ? appendQueryParam(`${apiBaseUrl}${endpoint}`, "currency", buyerCurrency) : `${apiBaseUrl}${endpoint}`;
|
|
3385
3400
|
const headers = {
|
|
3386
3401
|
"Content-Type": "application/json",
|
|
@@ -3599,10 +3614,7 @@ async function getStore() {
|
|
|
3599
3614
|
data: response.data.shop
|
|
3600
3615
|
};
|
|
3601
3616
|
}
|
|
3602
|
-
return
|
|
3603
|
-
success: false,
|
|
3604
|
-
message: response.message
|
|
3605
|
-
};
|
|
3617
|
+
return forwardFailure(response);
|
|
3606
3618
|
}
|
|
3607
3619
|
async function resolveStoreByDomain(domain, apiBaseUrl) {
|
|
3608
3620
|
const resolvedDomain = domain ?? (typeof window !== "undefined" ? window.location.hostname : "");
|
|
@@ -3637,10 +3649,7 @@ async function resolveStoreByDomain(domain, apiBaseUrl) {
|
|
|
3637
3649
|
data: response.data.shop
|
|
3638
3650
|
};
|
|
3639
3651
|
}
|
|
3640
|
-
return {
|
|
3641
|
-
success: false,
|
|
3642
|
-
message: response.message ?? "Failed to resolve store"
|
|
3643
|
-
};
|
|
3652
|
+
return forwardFailure(response, { message: response.message ?? "Failed to resolve store" });
|
|
3644
3653
|
}
|
|
3645
3654
|
async function getStorefront(options) {
|
|
3646
3655
|
const config = getConfig();
|
|
@@ -3682,10 +3691,7 @@ async function getStorefront(options) {
|
|
|
3682
3691
|
}
|
|
3683
3692
|
};
|
|
3684
3693
|
}
|
|
3685
|
-
return
|
|
3686
|
-
success: false,
|
|
3687
|
-
message: response.message
|
|
3688
|
-
};
|
|
3694
|
+
return forwardFailure(response);
|
|
3689
3695
|
}
|
|
3690
3696
|
async function getStoreLogoUrl() {
|
|
3691
3697
|
const response = await getStore();
|
|
@@ -3769,11 +3775,7 @@ async function getProducts() {
|
|
|
3769
3775
|
data: getMergedStorefrontProducts(response.data.products.map(normalizeProduct))
|
|
3770
3776
|
};
|
|
3771
3777
|
}
|
|
3772
|
-
return {
|
|
3773
|
-
success: false,
|
|
3774
|
-
message: response.message,
|
|
3775
|
-
data: []
|
|
3776
|
-
};
|
|
3778
|
+
return forwardFailure(response, { data: [] });
|
|
3777
3779
|
}
|
|
3778
3780
|
async function getStorefrontProductsPage(options) {
|
|
3779
3781
|
const config = getConfig();
|
|
@@ -3816,16 +3818,16 @@ async function getStorefrontProductsPage(options) {
|
|
|
3816
3818
|
}
|
|
3817
3819
|
};
|
|
3818
3820
|
}
|
|
3819
|
-
return
|
|
3820
|
-
success: false,
|
|
3821
|
-
message: response.message
|
|
3822
|
-
};
|
|
3821
|
+
return forwardFailure(response);
|
|
3823
3822
|
}
|
|
3824
3823
|
async function getProduct(idOrSlug) {
|
|
3825
3824
|
let shopId = getShopId();
|
|
3826
3825
|
if (!shopId) {
|
|
3827
3826
|
const store = await getStore();
|
|
3828
|
-
|
|
3827
|
+
if (!store.success) {
|
|
3828
|
+
return forwardFailure(store);
|
|
3829
|
+
}
|
|
3830
|
+
shopId = store.data?.id ?? null;
|
|
3829
3831
|
}
|
|
3830
3832
|
const queryParams = shopId ? `?slug_shop_id=${encodeURIComponent(shopId)}` : "";
|
|
3831
3833
|
const response = await get(
|
|
@@ -3845,18 +3847,12 @@ async function getProduct(idOrSlug) {
|
|
|
3845
3847
|
data: normalizeProduct(response.data.product)
|
|
3846
3848
|
};
|
|
3847
3849
|
}
|
|
3848
|
-
return
|
|
3849
|
-
success: false,
|
|
3850
|
-
message: response.message
|
|
3851
|
-
};
|
|
3850
|
+
return forwardFailure(response);
|
|
3852
3851
|
}
|
|
3853
3852
|
async function getCategories() {
|
|
3854
3853
|
const products = await getProducts();
|
|
3855
3854
|
if (!products.success || !products.data) {
|
|
3856
|
-
return
|
|
3857
|
-
success: false,
|
|
3858
|
-
message: products.message
|
|
3859
|
-
};
|
|
3855
|
+
return forwardFailure(products);
|
|
3860
3856
|
}
|
|
3861
3857
|
const categories = /* @__PURE__ */ new Set();
|
|
3862
3858
|
for (const product of products.data) {
|
|
@@ -4693,7 +4689,7 @@ function clearLatestQuoteToken() {
|
|
|
4693
4689
|
async function quoteCart(coupon, currency) {
|
|
4694
4690
|
const sequence = quoteSequence += 1;
|
|
4695
4691
|
const payload = getCartPayload(coupon);
|
|
4696
|
-
const normalizedCurrency = normalizeRequestedCurrency(currency) ??
|
|
4692
|
+
const normalizedCurrency = normalizeRequestedCurrency(currency) ?? resolveBuyerCurrency(getConfig().currency);
|
|
4697
4693
|
const response = await post("/v1/storefront/cart/quote", {
|
|
4698
4694
|
shop_slug: payload.store_slug,
|
|
4699
4695
|
cart: payload.items,
|
|
@@ -4780,10 +4776,10 @@ function resolveRequestedCheckoutCurrency(options) {
|
|
|
4780
4776
|
if (options.currency !== void 0) {
|
|
4781
4777
|
return normalizeRequestedCurrency(options.currency);
|
|
4782
4778
|
}
|
|
4783
|
-
return
|
|
4779
|
+
return resolveBuyerCurrency(getConfig().currency);
|
|
4784
4780
|
}
|
|
4785
4781
|
function getRequestedCheckoutCurrency() {
|
|
4786
|
-
return
|
|
4782
|
+
return resolveBuyerCurrency(getConfig().currency);
|
|
4787
4783
|
}
|
|
4788
4784
|
function normalizeCheckoutFailureMessage(rawMessage) {
|
|
4789
4785
|
const message = rawMessage?.trim() ?? "";
|