@commercengine/storefront-sdk 0.9.4 → 0.10.1
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/dist/index.d.ts +775 -338
- package/dist/index.iife.js +4175 -4224
- package/dist/index.iife.js.map +1 -1
- package/dist/index.js +62 -110
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -43,16 +43,14 @@ var ResponseUtils = class {
|
|
|
43
43
|
* Note: This can only be called once per response
|
|
44
44
|
*/
|
|
45
45
|
static async getText(response) {
|
|
46
|
-
|
|
47
|
-
return await cloned.text();
|
|
46
|
+
return await response.clone().text();
|
|
48
47
|
}
|
|
49
48
|
/**
|
|
50
49
|
* Clone and read response as JSON (useful for debugging)
|
|
51
50
|
* Note: This can only be called once per response
|
|
52
51
|
*/
|
|
53
52
|
static async getJSON(response) {
|
|
54
|
-
|
|
55
|
-
return await cloned.json();
|
|
53
|
+
return await response.clone().json();
|
|
56
54
|
}
|
|
57
55
|
/**
|
|
58
56
|
* Format response information for debugging
|
|
@@ -297,7 +295,7 @@ async function executeRequest(apiCall) {
|
|
|
297
295
|
status: 0,
|
|
298
296
|
statusText: "Network Error"
|
|
299
297
|
});
|
|
300
|
-
|
|
298
|
+
return {
|
|
301
299
|
data: null,
|
|
302
300
|
error: {
|
|
303
301
|
success: false,
|
|
@@ -307,7 +305,6 @@ async function executeRequest(apiCall) {
|
|
|
307
305
|
},
|
|
308
306
|
response: mockResponse
|
|
309
307
|
};
|
|
310
|
-
return errorResult;
|
|
311
308
|
}
|
|
312
309
|
}
|
|
313
310
|
/**
|
|
@@ -414,8 +411,7 @@ var BaseAPIClient = class {
|
|
|
414
411
|
*/
|
|
415
412
|
function getPathnameFromUrl(url) {
|
|
416
413
|
try {
|
|
417
|
-
|
|
418
|
-
return urlObj.pathname;
|
|
414
|
+
return new URL(url).pathname;
|
|
419
415
|
} catch {
|
|
420
416
|
return url.split("?")[0] || url;
|
|
421
417
|
}
|
|
@@ -445,6 +441,7 @@ function extractUserInfoFromToken(token) {
|
|
|
445
441
|
customerId: payload.customer_id,
|
|
446
442
|
customerGroupId: payload.customer_group_id,
|
|
447
443
|
anonymousId: payload.anonymous_id,
|
|
444
|
+
channel: payload.channel,
|
|
448
445
|
tokenExpiry: /* @__PURE__ */ new Date(payload.exp * 1e3),
|
|
449
446
|
tokenIssuedAt: /* @__PURE__ */ new Date(payload.iat * 1e3)
|
|
450
447
|
};
|
|
@@ -479,8 +476,7 @@ function isTokenExpired(token, bufferSeconds = 30) {
|
|
|
479
476
|
* @returns User ID (ulid) or null if token is invalid
|
|
480
477
|
*/
|
|
481
478
|
function getUserIdFromToken(token) {
|
|
482
|
-
|
|
483
|
-
return userInfo?.id || null;
|
|
479
|
+
return extractUserInfoFromToken(token)?.id || null;
|
|
484
480
|
}
|
|
485
481
|
/**
|
|
486
482
|
* Check if user is logged in based on JWT token
|
|
@@ -489,8 +485,7 @@ function getUserIdFromToken(token) {
|
|
|
489
485
|
* @returns True if user is logged in, false otherwise
|
|
490
486
|
*/
|
|
491
487
|
function isUserLoggedIn(token) {
|
|
492
|
-
|
|
493
|
-
return userInfo?.isLoggedIn || false;
|
|
488
|
+
return extractUserInfoFromToken(token)?.isLoggedIn || false;
|
|
494
489
|
}
|
|
495
490
|
/**
|
|
496
491
|
* Check if user is anonymous based on JWT token
|
|
@@ -499,8 +494,7 @@ function isUserLoggedIn(token) {
|
|
|
499
494
|
* @returns True if user is anonymous, false otherwise
|
|
500
495
|
*/
|
|
501
496
|
function isUserAnonymous(token) {
|
|
502
|
-
|
|
503
|
-
return userInfo?.isAnonymous || true;
|
|
497
|
+
return extractUserInfoFromToken(token)?.isAnonymous || true;
|
|
504
498
|
}
|
|
505
499
|
|
|
506
500
|
//#endregion
|
|
@@ -515,14 +509,13 @@ function isAnonymousAuthEndpoint(pathname) {
|
|
|
515
509
|
* Check if a URL path is a login/register endpoint that returns tokens
|
|
516
510
|
*/
|
|
517
511
|
function isTokenReturningEndpoint(pathname) {
|
|
518
|
-
|
|
512
|
+
return [
|
|
519
513
|
"/auth/login/password",
|
|
520
514
|
"/auth/register/phone",
|
|
521
515
|
"/auth/register/email",
|
|
522
516
|
"/auth/verify-otp",
|
|
523
517
|
"/auth/refresh-token"
|
|
524
|
-
];
|
|
525
|
-
return tokenEndpoints.some((endpoint) => pathname.endsWith(endpoint));
|
|
518
|
+
].some((endpoint) => pathname.endsWith(endpoint));
|
|
526
519
|
}
|
|
527
520
|
/**
|
|
528
521
|
* Check if a URL path is the logout endpoint
|
|
@@ -625,8 +618,7 @@ var CookieTokenStorage = class {
|
|
|
625
618
|
}
|
|
626
619
|
getCookie(name) {
|
|
627
620
|
if (typeof document === "undefined") return null;
|
|
628
|
-
const
|
|
629
|
-
const parts = value.split(`; ${name}=`);
|
|
621
|
+
const parts = `; ${document.cookie}`.split(`; ${name}=`);
|
|
630
622
|
if (parts.length === 2) {
|
|
631
623
|
const cookieValue = parts.pop()?.split(";").shift();
|
|
632
624
|
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
@@ -635,8 +627,7 @@ var CookieTokenStorage = class {
|
|
|
635
627
|
}
|
|
636
628
|
setCookie(name, value) {
|
|
637
629
|
if (typeof document === "undefined") return;
|
|
638
|
-
|
|
639
|
-
let cookieString = `${name}=${encodedValue}`;
|
|
630
|
+
let cookieString = `${name}=${encodeURIComponent(value)}`;
|
|
640
631
|
if (this.options.maxAge) cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
641
632
|
if (this.options.path) cookieString += `; Path=${this.options.path}`;
|
|
642
633
|
if (this.options.domain) cookieString += `; Domain=${this.options.domain}`;
|
|
@@ -688,8 +679,7 @@ function createAuthMiddleware(config) {
|
|
|
688
679
|
body: JSON.stringify({ refresh_token: refreshToken })
|
|
689
680
|
});
|
|
690
681
|
if (!response.ok) throw new Error(`Token refresh failed: ${response.status}`);
|
|
691
|
-
|
|
692
|
-
newTokens = data.content;
|
|
682
|
+
newTokens = (await response.json()).content;
|
|
693
683
|
}
|
|
694
684
|
else {
|
|
695
685
|
const currentAccessToken = await config.tokenStorage.getAccessToken();
|
|
@@ -704,8 +694,7 @@ function createAuthMiddleware(config) {
|
|
|
704
694
|
}
|
|
705
695
|
});
|
|
706
696
|
if (!response.ok) throw new Error(`Anonymous token fallback failed: ${response.status}`);
|
|
707
|
-
|
|
708
|
-
newTokens = data.content;
|
|
697
|
+
newTokens = (await response.json()).content;
|
|
709
698
|
console.info(`Token refreshed via anonymous fallback (${reason}) - user may need to re-authenticate for privileged operations`);
|
|
710
699
|
}
|
|
711
700
|
await config.tokenStorage.setAccessToken(newTokens.access_token);
|
|
@@ -751,8 +740,7 @@ function createAuthMiddleware(config) {
|
|
|
751
740
|
}
|
|
752
741
|
});
|
|
753
742
|
if (response.ok) {
|
|
754
|
-
const
|
|
755
|
-
const tokens = data.content;
|
|
743
|
+
const tokens = (await response.json()).content;
|
|
756
744
|
if (tokens?.access_token && tokens?.refresh_token) {
|
|
757
745
|
await config.tokenStorage.setAccessToken(tokens.access_token);
|
|
758
746
|
await config.tokenStorage.setRefreshToken(tokens.refresh_token);
|
|
@@ -775,8 +763,7 @@ function createAuthMiddleware(config) {
|
|
|
775
763
|
const pathname = getPathnameFromUrl(request.url);
|
|
776
764
|
if (response.ok) {
|
|
777
765
|
if (isTokenReturningEndpoint(pathname) || isAnonymousAuthEndpoint(pathname)) try {
|
|
778
|
-
const
|
|
779
|
-
const content = data.content;
|
|
766
|
+
const content = (await response.clone().json()).content;
|
|
780
767
|
if (content?.access_token && content?.refresh_token) {
|
|
781
768
|
await config.tokenStorage.setAccessToken(content.access_token);
|
|
782
769
|
await config.tokenStorage.setRefreshToken(content.refresh_token);
|
|
@@ -846,8 +833,7 @@ let Environment = /* @__PURE__ */ function(Environment$1) {
|
|
|
846
833
|
*/
|
|
847
834
|
function buildStorefrontURL(config) {
|
|
848
835
|
if (config.baseUrl) return config.baseUrl;
|
|
849
|
-
|
|
850
|
-
switch (env) {
|
|
836
|
+
switch (config.environment || Environment.Production) {
|
|
851
837
|
case Environment.Staging: return `https://staging.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
852
838
|
case Environment.Production:
|
|
853
839
|
default: return `https://prod.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
@@ -874,14 +860,16 @@ var StorefrontAPIClient = class extends BaseAPIClient {
|
|
|
874
860
|
environment: config.environment,
|
|
875
861
|
baseUrl: config.baseUrl
|
|
876
862
|
});
|
|
877
|
-
const headerTransformations = { customer_group_id: "x-customer-group-id" };
|
|
878
863
|
super({
|
|
879
864
|
baseUrl,
|
|
880
865
|
timeout: config.timeout,
|
|
881
866
|
defaultHeaders: config.defaultHeaders,
|
|
882
867
|
debug: config.debug,
|
|
883
868
|
logger: config.logger
|
|
884
|
-
}, baseUrl,
|
|
869
|
+
}, baseUrl, {
|
|
870
|
+
customer_group_id: "x-customer-group-id",
|
|
871
|
+
debug_mode: "x-debug-mode"
|
|
872
|
+
});
|
|
885
873
|
this.config = { ...config };
|
|
886
874
|
this.setupStorefrontAuth();
|
|
887
875
|
}
|
|
@@ -1151,10 +1139,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1151
1139
|
* console.log("Product with custom pricing:", slugData.product.price);
|
|
1152
1140
|
* ```
|
|
1153
1141
|
*/
|
|
1154
|
-
async getProductDetail(pathParams, headers) {
|
|
1142
|
+
async getProductDetail(pathParams, options, headers) {
|
|
1155
1143
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1156
1144
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id_or_slug}", { params: {
|
|
1157
1145
|
path: pathParams,
|
|
1146
|
+
query: options,
|
|
1158
1147
|
header: mergedHeaders
|
|
1159
1148
|
} }));
|
|
1160
1149
|
}
|
|
@@ -1191,10 +1180,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1191
1180
|
* );
|
|
1192
1181
|
* ```
|
|
1193
1182
|
*/
|
|
1194
|
-
async listProductVariants(pathParams, headers) {
|
|
1183
|
+
async listProductVariants(pathParams, options, headers) {
|
|
1195
1184
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1196
1185
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants", { params: {
|
|
1197
1186
|
path: pathParams,
|
|
1187
|
+
query: options,
|
|
1198
1188
|
header: mergedHeaders
|
|
1199
1189
|
} }));
|
|
1200
1190
|
}
|
|
@@ -1225,10 +1215,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1225
1215
|
* console.log("Stock:", data.variant.stock);
|
|
1226
1216
|
* ```
|
|
1227
1217
|
*/
|
|
1228
|
-
async getVariantDetail(pathParams, headers) {
|
|
1218
|
+
async getVariantDetail(pathParams, options, headers) {
|
|
1229
1219
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1230
1220
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants/{variant_id}", { params: {
|
|
1231
1221
|
path: pathParams,
|
|
1222
|
+
query: options,
|
|
1232
1223
|
header: mergedHeaders
|
|
1233
1224
|
} }));
|
|
1234
1225
|
}
|
|
@@ -1908,24 +1899,25 @@ var CartClient = class extends StorefrontAPIClient {
|
|
|
1908
1899
|
* @returns Promise with updated cart
|
|
1909
1900
|
* @example
|
|
1910
1901
|
* ```typescript
|
|
1911
|
-
* const { data, error } = await sdk.cart.
|
|
1902
|
+
* const { data, error } = await sdk.cart.updateFulfillmentPreference(
|
|
1912
1903
|
* { id: "01H9CART12345ABCDE" },
|
|
1913
1904
|
* {
|
|
1914
|
-
*
|
|
1915
|
-
*
|
|
1905
|
+
* fulfillment_type: "delivery",
|
|
1906
|
+
* shipping_provider_id: "01H9SHIP12345FAST",
|
|
1907
|
+
* courier_company_id: "01H9COY12345FAST", // Optional
|
|
1916
1908
|
* }
|
|
1917
1909
|
* );
|
|
1918
1910
|
*
|
|
1919
1911
|
* if (error) {
|
|
1920
|
-
* console.error("Failed to update
|
|
1912
|
+
* console.error("Failed to update fulfillment preference:", error.message);
|
|
1921
1913
|
* } else {
|
|
1922
|
-
* console.log("
|
|
1923
|
-
* console.log("Shipping cost:", data.cart.
|
|
1914
|
+
* console.log("Fulfillment preference updated:", data.cart.fulfillment_preference?.fulfillment_type);
|
|
1915
|
+
* console.log("Shipping cost:", data.cart.shipping_amount);
|
|
1924
1916
|
* }
|
|
1925
1917
|
* ```
|
|
1926
1918
|
*/
|
|
1927
|
-
async
|
|
1928
|
-
return this.executeRequest(() => this.client.POST("/carts/{id}/
|
|
1919
|
+
async updateFulfillmentPreference(cartId, body) {
|
|
1920
|
+
return this.executeRequest(() => this.client.POST("/carts/{id}/fulfillment-preference", {
|
|
1929
1921
|
params: { path: cartId },
|
|
1930
1922
|
body
|
|
1931
1923
|
}));
|
|
@@ -1982,60 +1974,6 @@ var CartClient = class extends StorefrontAPIClient {
|
|
|
1982
1974
|
}));
|
|
1983
1975
|
}
|
|
1984
1976
|
/**
|
|
1985
|
-
* Redeem gift card
|
|
1986
|
-
*
|
|
1987
|
-
* @param cartId - The ID of the cart
|
|
1988
|
-
* @param body - The body of the request
|
|
1989
|
-
* @returns Promise with updated cart
|
|
1990
|
-
* @example
|
|
1991
|
-
* ```typescript
|
|
1992
|
-
* const { data, error } = await sdk.cart.redeemGiftCard(
|
|
1993
|
-
* { id: "01H9CART12345ABCDE" },
|
|
1994
|
-
* {
|
|
1995
|
-
* gift_card_code: "GIFT2024-ABCD-1234",
|
|
1996
|
-
* amount: 100.00
|
|
1997
|
-
* }
|
|
1998
|
-
* );
|
|
1999
|
-
*
|
|
2000
|
-
* if (error) {
|
|
2001
|
-
* console.error("Failed to redeem gift card:", error.message);
|
|
2002
|
-
* } else {
|
|
2003
|
-
* console.log("Gift card applied, new total:", data.cart.total_amount);
|
|
2004
|
-
* console.log("Gift card discount:", data.cart.gift_card_discount_amount);
|
|
2005
|
-
* }
|
|
2006
|
-
* ```
|
|
2007
|
-
*/
|
|
2008
|
-
async redeemGiftCard(cartId, body) {
|
|
2009
|
-
return this.executeRequest(() => this.client.POST("/carts/{id}/gift-card", {
|
|
2010
|
-
params: { path: cartId },
|
|
2011
|
-
body
|
|
2012
|
-
}));
|
|
2013
|
-
}
|
|
2014
|
-
/**
|
|
2015
|
-
* Remove gift card
|
|
2016
|
-
*
|
|
2017
|
-
* @param cartId - The ID of the cart
|
|
2018
|
-
* @returns Promise with updated cart
|
|
2019
|
-
* @example
|
|
2020
|
-
* ```typescript
|
|
2021
|
-
* const { data, error } = await sdk.cart.removeGiftCard({
|
|
2022
|
-
* id: "01H9CART12345ABCDE"
|
|
2023
|
-
* });
|
|
2024
|
-
*
|
|
2025
|
-
* if (error) {
|
|
2026
|
-
* console.error("Failed to remove gift card:", error.message);
|
|
2027
|
-
* } else {
|
|
2028
|
-
* console.log("Gift card removed, new total:", data.cart.total_amount);
|
|
2029
|
-
* }
|
|
2030
|
-
* ```
|
|
2031
|
-
*/
|
|
2032
|
-
async removeGiftCard(cartId) {
|
|
2033
|
-
return this.executeRequest(() => this.client.DELETE("/carts/{id}/gift-card", {
|
|
2034
|
-
params: { path: cartId },
|
|
2035
|
-
body: void 0
|
|
2036
|
-
}));
|
|
2037
|
-
}
|
|
2038
|
-
/**
|
|
2039
1977
|
* Get wishlist items
|
|
2040
1978
|
*
|
|
2041
1979
|
* @param userId - The ID of the user
|
|
@@ -2294,8 +2232,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2294
2232
|
* }
|
|
2295
2233
|
* ```
|
|
2296
2234
|
*/
|
|
2297
|
-
async loginWithPhone(body) {
|
|
2298
|
-
|
|
2235
|
+
async loginWithPhone(body, headers) {
|
|
2236
|
+
const mergedHeaders = this.mergeHeaders(headers);
|
|
2237
|
+
return this.executeRequest(() => this.client.POST("/auth/login/phone", {
|
|
2238
|
+
body,
|
|
2239
|
+
params: { header: mergedHeaders }
|
|
2240
|
+
}));
|
|
2299
2241
|
}
|
|
2300
2242
|
/**
|
|
2301
2243
|
* Login with WhatsApp
|
|
@@ -2319,8 +2261,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2319
2261
|
* }
|
|
2320
2262
|
* ```
|
|
2321
2263
|
*/
|
|
2322
|
-
async loginWithWhatsApp(body) {
|
|
2323
|
-
|
|
2264
|
+
async loginWithWhatsApp(body, headers) {
|
|
2265
|
+
const mergedHeaders = this.mergeHeaders(headers);
|
|
2266
|
+
return this.executeRequest(() => this.client.POST("/auth/login/whatsapp", {
|
|
2267
|
+
body,
|
|
2268
|
+
params: { header: mergedHeaders }
|
|
2269
|
+
}));
|
|
2324
2270
|
}
|
|
2325
2271
|
/**
|
|
2326
2272
|
* Login with email
|
|
@@ -2344,8 +2290,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2344
2290
|
* }
|
|
2345
2291
|
* ```
|
|
2346
2292
|
*/
|
|
2347
|
-
async loginWithEmail(body) {
|
|
2348
|
-
|
|
2293
|
+
async loginWithEmail(body, headers) {
|
|
2294
|
+
const mergedHeaders = this.mergeHeaders(headers);
|
|
2295
|
+
return this.executeRequest(() => this.client.POST("/auth/login/email", {
|
|
2296
|
+
body,
|
|
2297
|
+
params: { header: mergedHeaders }
|
|
2298
|
+
}));
|
|
2349
2299
|
}
|
|
2350
2300
|
/**
|
|
2351
2301
|
* Login with password
|
|
@@ -2869,8 +2819,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2869
2819
|
* }
|
|
2870
2820
|
* ```
|
|
2871
2821
|
*/
|
|
2872
|
-
async generateOtp(body) {
|
|
2873
|
-
|
|
2822
|
+
async generateOtp(body, headers) {
|
|
2823
|
+
const mergedHeaders = this.mergeHeaders(headers);
|
|
2824
|
+
return this.executeRequest(() => this.client.POST("/auth/generate-otp", {
|
|
2825
|
+
body,
|
|
2826
|
+
params: { header: mergedHeaders }
|
|
2827
|
+
}));
|
|
2874
2828
|
}
|
|
2875
2829
|
/**
|
|
2876
2830
|
* Check whether email or phone is already verified
|
|
@@ -3922,8 +3876,7 @@ var StorefrontSDK = class {
|
|
|
3922
3876
|
* @returns Customer ID or null if no token, invalid token, or user has no customer ID
|
|
3923
3877
|
*/
|
|
3924
3878
|
async getCustomerId() {
|
|
3925
|
-
|
|
3926
|
-
return userInfo?.customerId || null;
|
|
3879
|
+
return (await this.getUserInfo())?.customerId || null;
|
|
3927
3880
|
}
|
|
3928
3881
|
/**
|
|
3929
3882
|
* Get the customer group ID from the current access token
|
|
@@ -3931,8 +3884,7 @@ var StorefrontSDK = class {
|
|
|
3931
3884
|
* @returns Customer group ID or null if no token, invalid token, or user has no customer group
|
|
3932
3885
|
*/
|
|
3933
3886
|
async getCustomerGroupId() {
|
|
3934
|
-
|
|
3935
|
-
return userInfo?.customerGroupId || null;
|
|
3887
|
+
return (await this.getUserInfo())?.customerGroupId || null;
|
|
3936
3888
|
}
|
|
3937
3889
|
/**
|
|
3938
3890
|
* Set default headers for all clients
|