@commercengine/storefront-sdk 0.10.0 → 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 +255 -64
- package/dist/index.iife.js +4175 -4171
- package/dist/index.iife.js.map +1 -1
- package/dist/index.js +61 -56
- 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
|
}
|
|
@@ -480,8 +476,7 @@ function isTokenExpired(token, bufferSeconds = 30) {
|
|
|
480
476
|
* @returns User ID (ulid) or null if token is invalid
|
|
481
477
|
*/
|
|
482
478
|
function getUserIdFromToken(token) {
|
|
483
|
-
|
|
484
|
-
return userInfo?.id || null;
|
|
479
|
+
return extractUserInfoFromToken(token)?.id || null;
|
|
485
480
|
}
|
|
486
481
|
/**
|
|
487
482
|
* Check if user is logged in based on JWT token
|
|
@@ -490,8 +485,7 @@ function getUserIdFromToken(token) {
|
|
|
490
485
|
* @returns True if user is logged in, false otherwise
|
|
491
486
|
*/
|
|
492
487
|
function isUserLoggedIn(token) {
|
|
493
|
-
|
|
494
|
-
return userInfo?.isLoggedIn || false;
|
|
488
|
+
return extractUserInfoFromToken(token)?.isLoggedIn || false;
|
|
495
489
|
}
|
|
496
490
|
/**
|
|
497
491
|
* Check if user is anonymous based on JWT token
|
|
@@ -500,8 +494,7 @@ function isUserLoggedIn(token) {
|
|
|
500
494
|
* @returns True if user is anonymous, false otherwise
|
|
501
495
|
*/
|
|
502
496
|
function isUserAnonymous(token) {
|
|
503
|
-
|
|
504
|
-
return userInfo?.isAnonymous || true;
|
|
497
|
+
return extractUserInfoFromToken(token)?.isAnonymous || true;
|
|
505
498
|
}
|
|
506
499
|
|
|
507
500
|
//#endregion
|
|
@@ -516,14 +509,13 @@ function isAnonymousAuthEndpoint(pathname) {
|
|
|
516
509
|
* Check if a URL path is a login/register endpoint that returns tokens
|
|
517
510
|
*/
|
|
518
511
|
function isTokenReturningEndpoint(pathname) {
|
|
519
|
-
|
|
512
|
+
return [
|
|
520
513
|
"/auth/login/password",
|
|
521
514
|
"/auth/register/phone",
|
|
522
515
|
"/auth/register/email",
|
|
523
516
|
"/auth/verify-otp",
|
|
524
517
|
"/auth/refresh-token"
|
|
525
|
-
];
|
|
526
|
-
return tokenEndpoints.some((endpoint) => pathname.endsWith(endpoint));
|
|
518
|
+
].some((endpoint) => pathname.endsWith(endpoint));
|
|
527
519
|
}
|
|
528
520
|
/**
|
|
529
521
|
* Check if a URL path is the logout endpoint
|
|
@@ -626,8 +618,7 @@ var CookieTokenStorage = class {
|
|
|
626
618
|
}
|
|
627
619
|
getCookie(name) {
|
|
628
620
|
if (typeof document === "undefined") return null;
|
|
629
|
-
const
|
|
630
|
-
const parts = value.split(`; ${name}=`);
|
|
621
|
+
const parts = `; ${document.cookie}`.split(`; ${name}=`);
|
|
631
622
|
if (parts.length === 2) {
|
|
632
623
|
const cookieValue = parts.pop()?.split(";").shift();
|
|
633
624
|
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
|
@@ -636,8 +627,7 @@ var CookieTokenStorage = class {
|
|
|
636
627
|
}
|
|
637
628
|
setCookie(name, value) {
|
|
638
629
|
if (typeof document === "undefined") return;
|
|
639
|
-
|
|
640
|
-
let cookieString = `${name}=${encodedValue}`;
|
|
630
|
+
let cookieString = `${name}=${encodeURIComponent(value)}`;
|
|
641
631
|
if (this.options.maxAge) cookieString += `; Max-Age=${this.options.maxAge}`;
|
|
642
632
|
if (this.options.path) cookieString += `; Path=${this.options.path}`;
|
|
643
633
|
if (this.options.domain) cookieString += `; Domain=${this.options.domain}`;
|
|
@@ -689,8 +679,7 @@ function createAuthMiddleware(config) {
|
|
|
689
679
|
body: JSON.stringify({ refresh_token: refreshToken })
|
|
690
680
|
});
|
|
691
681
|
if (!response.ok) throw new Error(`Token refresh failed: ${response.status}`);
|
|
692
|
-
|
|
693
|
-
newTokens = data.content;
|
|
682
|
+
newTokens = (await response.json()).content;
|
|
694
683
|
}
|
|
695
684
|
else {
|
|
696
685
|
const currentAccessToken = await config.tokenStorage.getAccessToken();
|
|
@@ -705,8 +694,7 @@ function createAuthMiddleware(config) {
|
|
|
705
694
|
}
|
|
706
695
|
});
|
|
707
696
|
if (!response.ok) throw new Error(`Anonymous token fallback failed: ${response.status}`);
|
|
708
|
-
|
|
709
|
-
newTokens = data.content;
|
|
697
|
+
newTokens = (await response.json()).content;
|
|
710
698
|
console.info(`Token refreshed via anonymous fallback (${reason}) - user may need to re-authenticate for privileged operations`);
|
|
711
699
|
}
|
|
712
700
|
await config.tokenStorage.setAccessToken(newTokens.access_token);
|
|
@@ -752,8 +740,7 @@ function createAuthMiddleware(config) {
|
|
|
752
740
|
}
|
|
753
741
|
});
|
|
754
742
|
if (response.ok) {
|
|
755
|
-
const
|
|
756
|
-
const tokens = data.content;
|
|
743
|
+
const tokens = (await response.json()).content;
|
|
757
744
|
if (tokens?.access_token && tokens?.refresh_token) {
|
|
758
745
|
await config.tokenStorage.setAccessToken(tokens.access_token);
|
|
759
746
|
await config.tokenStorage.setRefreshToken(tokens.refresh_token);
|
|
@@ -776,8 +763,7 @@ function createAuthMiddleware(config) {
|
|
|
776
763
|
const pathname = getPathnameFromUrl(request.url);
|
|
777
764
|
if (response.ok) {
|
|
778
765
|
if (isTokenReturningEndpoint(pathname) || isAnonymousAuthEndpoint(pathname)) try {
|
|
779
|
-
const
|
|
780
|
-
const content = data.content;
|
|
766
|
+
const content = (await response.clone().json()).content;
|
|
781
767
|
if (content?.access_token && content?.refresh_token) {
|
|
782
768
|
await config.tokenStorage.setAccessToken(content.access_token);
|
|
783
769
|
await config.tokenStorage.setRefreshToken(content.refresh_token);
|
|
@@ -847,8 +833,7 @@ let Environment = /* @__PURE__ */ function(Environment$1) {
|
|
|
847
833
|
*/
|
|
848
834
|
function buildStorefrontURL(config) {
|
|
849
835
|
if (config.baseUrl) return config.baseUrl;
|
|
850
|
-
|
|
851
|
-
switch (env) {
|
|
836
|
+
switch (config.environment || Environment.Production) {
|
|
852
837
|
case Environment.Staging: return `https://staging.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
853
838
|
case Environment.Production:
|
|
854
839
|
default: return `https://prod.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
@@ -875,14 +860,16 @@ var StorefrontAPIClient = class extends BaseAPIClient {
|
|
|
875
860
|
environment: config.environment,
|
|
876
861
|
baseUrl: config.baseUrl
|
|
877
862
|
});
|
|
878
|
-
const headerTransformations = { customer_group_id: "x-customer-group-id" };
|
|
879
863
|
super({
|
|
880
864
|
baseUrl,
|
|
881
865
|
timeout: config.timeout,
|
|
882
866
|
defaultHeaders: config.defaultHeaders,
|
|
883
867
|
debug: config.debug,
|
|
884
868
|
logger: config.logger
|
|
885
|
-
}, baseUrl,
|
|
869
|
+
}, baseUrl, {
|
|
870
|
+
customer_group_id: "x-customer-group-id",
|
|
871
|
+
debug_mode: "x-debug-mode"
|
|
872
|
+
});
|
|
886
873
|
this.config = { ...config };
|
|
887
874
|
this.setupStorefrontAuth();
|
|
888
875
|
}
|
|
@@ -1152,10 +1139,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1152
1139
|
* console.log("Product with custom pricing:", slugData.product.price);
|
|
1153
1140
|
* ```
|
|
1154
1141
|
*/
|
|
1155
|
-
async getProductDetail(pathParams, headers) {
|
|
1142
|
+
async getProductDetail(pathParams, options, headers) {
|
|
1156
1143
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1157
1144
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id_or_slug}", { params: {
|
|
1158
1145
|
path: pathParams,
|
|
1146
|
+
query: options,
|
|
1159
1147
|
header: mergedHeaders
|
|
1160
1148
|
} }));
|
|
1161
1149
|
}
|
|
@@ -1192,10 +1180,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1192
1180
|
* );
|
|
1193
1181
|
* ```
|
|
1194
1182
|
*/
|
|
1195
|
-
async listProductVariants(pathParams, headers) {
|
|
1183
|
+
async listProductVariants(pathParams, options, headers) {
|
|
1196
1184
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1197
1185
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants", { params: {
|
|
1198
1186
|
path: pathParams,
|
|
1187
|
+
query: options,
|
|
1199
1188
|
header: mergedHeaders
|
|
1200
1189
|
} }));
|
|
1201
1190
|
}
|
|
@@ -1226,10 +1215,11 @@ var CatalogClient = class extends StorefrontAPIClient {
|
|
|
1226
1215
|
* console.log("Stock:", data.variant.stock);
|
|
1227
1216
|
* ```
|
|
1228
1217
|
*/
|
|
1229
|
-
async getVariantDetail(pathParams, headers) {
|
|
1218
|
+
async getVariantDetail(pathParams, options, headers) {
|
|
1230
1219
|
const mergedHeaders = this.mergeHeaders(headers);
|
|
1231
1220
|
return this.executeRequest(() => this.client.GET("/catalog/products/{product_id}/variants/{variant_id}", { params: {
|
|
1232
1221
|
path: pathParams,
|
|
1222
|
+
query: options,
|
|
1233
1223
|
header: mergedHeaders
|
|
1234
1224
|
} }));
|
|
1235
1225
|
}
|
|
@@ -1909,24 +1899,25 @@ var CartClient = class extends StorefrontAPIClient {
|
|
|
1909
1899
|
* @returns Promise with updated cart
|
|
1910
1900
|
* @example
|
|
1911
1901
|
* ```typescript
|
|
1912
|
-
* const { data, error } = await sdk.cart.
|
|
1902
|
+
* const { data, error } = await sdk.cart.updateFulfillmentPreference(
|
|
1913
1903
|
* { id: "01H9CART12345ABCDE" },
|
|
1914
1904
|
* {
|
|
1915
|
-
*
|
|
1916
|
-
*
|
|
1905
|
+
* fulfillment_type: "delivery",
|
|
1906
|
+
* shipping_provider_id: "01H9SHIP12345FAST",
|
|
1907
|
+
* courier_company_id: "01H9COY12345FAST", // Optional
|
|
1917
1908
|
* }
|
|
1918
1909
|
* );
|
|
1919
1910
|
*
|
|
1920
1911
|
* if (error) {
|
|
1921
|
-
* console.error("Failed to update
|
|
1912
|
+
* console.error("Failed to update fulfillment preference:", error.message);
|
|
1922
1913
|
* } else {
|
|
1923
|
-
* console.log("
|
|
1924
|
-
* 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);
|
|
1925
1916
|
* }
|
|
1926
1917
|
* ```
|
|
1927
1918
|
*/
|
|
1928
|
-
async
|
|
1929
|
-
return this.executeRequest(() => this.client.POST("/carts/{id}/
|
|
1919
|
+
async updateFulfillmentPreference(cartId, body) {
|
|
1920
|
+
return this.executeRequest(() => this.client.POST("/carts/{id}/fulfillment-preference", {
|
|
1930
1921
|
params: { path: cartId },
|
|
1931
1922
|
body
|
|
1932
1923
|
}));
|
|
@@ -2241,8 +2232,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2241
2232
|
* }
|
|
2242
2233
|
* ```
|
|
2243
2234
|
*/
|
|
2244
|
-
async loginWithPhone(body) {
|
|
2245
|
-
|
|
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
|
+
}));
|
|
2246
2241
|
}
|
|
2247
2242
|
/**
|
|
2248
2243
|
* Login with WhatsApp
|
|
@@ -2266,8 +2261,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2266
2261
|
* }
|
|
2267
2262
|
* ```
|
|
2268
2263
|
*/
|
|
2269
|
-
async loginWithWhatsApp(body) {
|
|
2270
|
-
|
|
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
|
+
}));
|
|
2271
2270
|
}
|
|
2272
2271
|
/**
|
|
2273
2272
|
* Login with email
|
|
@@ -2291,8 +2290,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2291
2290
|
* }
|
|
2292
2291
|
* ```
|
|
2293
2292
|
*/
|
|
2294
|
-
async loginWithEmail(body) {
|
|
2295
|
-
|
|
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
|
+
}));
|
|
2296
2299
|
}
|
|
2297
2300
|
/**
|
|
2298
2301
|
* Login with password
|
|
@@ -2816,8 +2819,12 @@ var AuthClient = class extends StorefrontAPIClient {
|
|
|
2816
2819
|
* }
|
|
2817
2820
|
* ```
|
|
2818
2821
|
*/
|
|
2819
|
-
async generateOtp(body) {
|
|
2820
|
-
|
|
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
|
+
}));
|
|
2821
2828
|
}
|
|
2822
2829
|
/**
|
|
2823
2830
|
* Check whether email or phone is already verified
|
|
@@ -3869,8 +3876,7 @@ var StorefrontSDK = class {
|
|
|
3869
3876
|
* @returns Customer ID or null if no token, invalid token, or user has no customer ID
|
|
3870
3877
|
*/
|
|
3871
3878
|
async getCustomerId() {
|
|
3872
|
-
|
|
3873
|
-
return userInfo?.customerId || null;
|
|
3879
|
+
return (await this.getUserInfo())?.customerId || null;
|
|
3874
3880
|
}
|
|
3875
3881
|
/**
|
|
3876
3882
|
* Get the customer group ID from the current access token
|
|
@@ -3878,8 +3884,7 @@ var StorefrontSDK = class {
|
|
|
3878
3884
|
* @returns Customer group ID or null if no token, invalid token, or user has no customer group
|
|
3879
3885
|
*/
|
|
3880
3886
|
async getCustomerGroupId() {
|
|
3881
|
-
|
|
3882
|
-
return userInfo?.customerGroupId || null;
|
|
3887
|
+
return (await this.getUserInfo())?.customerGroupId || null;
|
|
3883
3888
|
}
|
|
3884
3889
|
/**
|
|
3885
3890
|
* Set default headers for all clients
|