@commercengine/pos 0.3.4 → 0.3.6
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/admin-types.d.ts +1936 -0
- package/dist/admin-types.js +1 -0
- package/dist/index.js +17 -30
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
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
|
}
|
|
@@ -492,8 +488,7 @@ function isTokenExpired(token, bufferSeconds = 30) {
|
|
|
492
488
|
* @returns User ID (ulid) or null if token is invalid
|
|
493
489
|
*/
|
|
494
490
|
function getUserIdFromToken(token) {
|
|
495
|
-
|
|
496
|
-
return userInfo?.id || null;
|
|
491
|
+
return extractUserInfoFromToken(token)?.id || null;
|
|
497
492
|
}
|
|
498
493
|
/**
|
|
499
494
|
* Check if user is logged in based on JWT token
|
|
@@ -502,8 +497,7 @@ function getUserIdFromToken(token) {
|
|
|
502
497
|
* @returns True if user is logged in, false otherwise
|
|
503
498
|
*/
|
|
504
499
|
function isUserLoggedIn(token) {
|
|
505
|
-
|
|
506
|
-
return userInfo?.isLoggedIn || false;
|
|
500
|
+
return extractUserInfoFromToken(token)?.isLoggedIn || false;
|
|
507
501
|
}
|
|
508
502
|
/**
|
|
509
503
|
* Check if user is authenticated (has valid POS token)
|
|
@@ -513,8 +507,7 @@ function isUserLoggedIn(token) {
|
|
|
513
507
|
* @returns True if user is authenticated with valid POS access
|
|
514
508
|
*/
|
|
515
509
|
function isUserAuthenticated(token) {
|
|
516
|
-
|
|
517
|
-
return !!userInfo?.isLoggedIn;
|
|
510
|
+
return !!extractUserInfoFromToken(token)?.isLoggedIn;
|
|
518
511
|
}
|
|
519
512
|
/**
|
|
520
513
|
* Get the location ID from a POS JWT token
|
|
@@ -523,8 +516,7 @@ function isUserAuthenticated(token) {
|
|
|
523
516
|
* @returns Location ID or null if token is invalid
|
|
524
517
|
*/
|
|
525
518
|
function getLocationIdFromToken(token) {
|
|
526
|
-
|
|
527
|
-
return userInfo?.location?.id || null;
|
|
519
|
+
return extractUserInfoFromToken(token)?.location?.id || null;
|
|
528
520
|
}
|
|
529
521
|
/**
|
|
530
522
|
* Get the role information from a POS JWT token for the current location
|
|
@@ -535,8 +527,7 @@ function getLocationIdFromToken(token) {
|
|
|
535
527
|
function getRoleFromToken(token) {
|
|
536
528
|
const userInfo = extractUserInfoFromToken(token);
|
|
537
529
|
if (!userInfo?.roles || userInfo.roles.length === 0) return null;
|
|
538
|
-
|
|
539
|
-
return currentLocationRole || userInfo.roles[0] || null;
|
|
530
|
+
return userInfo.roles.find((role) => role.locationId === userInfo.location.id) || userInfo.roles[0] || null;
|
|
540
531
|
}
|
|
541
532
|
/**
|
|
542
533
|
* Get the tenant ID from a POS JWT token
|
|
@@ -545,8 +536,7 @@ function getRoleFromToken(token) {
|
|
|
545
536
|
* @returns Tenant ID or null if token is invalid
|
|
546
537
|
*/
|
|
547
538
|
function getTenantIdFromToken(token) {
|
|
548
|
-
|
|
549
|
-
return userInfo?.tenantId || null;
|
|
539
|
+
return extractUserInfoFromToken(token)?.tenantId || null;
|
|
550
540
|
}
|
|
551
541
|
|
|
552
542
|
//#endregion
|
|
@@ -561,14 +551,14 @@ function getTenantIdFromToken(token) {
|
|
|
561
551
|
* - /pos/auth/verify-otp
|
|
562
552
|
*/
|
|
563
553
|
function isApiKeyEndpoint(pathname) {
|
|
564
|
-
|
|
554
|
+
return [
|
|
565
555
|
"/pos/auth/login/email",
|
|
566
556
|
"/pos/auth/login/phone",
|
|
567
557
|
"/pos/auth/login/whatsapp",
|
|
568
558
|
"/pos/auth/pair-device",
|
|
569
|
-
"/pos/auth/verify-otp"
|
|
570
|
-
|
|
571
|
-
|
|
559
|
+
"/pos/auth/verify-otp",
|
|
560
|
+
"/pos/locations"
|
|
561
|
+
].some((endpoint) => pathname.includes(endpoint));
|
|
572
562
|
}
|
|
573
563
|
/**
|
|
574
564
|
* Check if a URL path is a POS endpoint that returns tokens
|
|
@@ -577,8 +567,7 @@ function isApiKeyEndpoint(pathname) {
|
|
|
577
567
|
* - /pos/auth/refresh-token
|
|
578
568
|
*/
|
|
579
569
|
function isTokenReturningEndpoint(pathname) {
|
|
580
|
-
|
|
581
|
-
return tokenEndpoints.some((endpoint) => pathname.includes(endpoint));
|
|
570
|
+
return ["/pos/auth/verify-otp", "/pos/auth/refresh-token"].some((endpoint) => pathname.includes(endpoint));
|
|
582
571
|
}
|
|
583
572
|
/**
|
|
584
573
|
* Check if a URL path is a POS logout endpoint
|
|
@@ -814,8 +803,7 @@ let Environment = /* @__PURE__ */ function(Environment$1) {
|
|
|
814
803
|
*/
|
|
815
804
|
function buildPosURL(config) {
|
|
816
805
|
if (config.baseUrl) return config.baseUrl.replace(/\/$/, "");
|
|
817
|
-
|
|
818
|
-
switch (env) {
|
|
806
|
+
switch (config.environment || Environment.Production) {
|
|
819
807
|
case Environment.Staging: return `https://staging.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
820
808
|
case Environment.Production:
|
|
821
809
|
default: return `https://prod.api.commercengine.io/api/v1/${config.storeId}/storefront`;
|
|
@@ -872,14 +860,13 @@ var PosAPIClient = class PosAPIClient extends BaseAPIClient {
|
|
|
872
860
|
environment: config.environment,
|
|
873
861
|
baseUrl: config.baseUrl
|
|
874
862
|
});
|
|
875
|
-
const headerTransformations = { customer_group_id: "x-customer-group-id" };
|
|
876
863
|
super({
|
|
877
864
|
baseUrl,
|
|
878
865
|
timeout: config.timeout,
|
|
879
866
|
defaultHeaders: config.defaultHeaders,
|
|
880
867
|
debug: config.debug,
|
|
881
868
|
logger: config.logger
|
|
882
|
-
}, baseUrl,
|
|
869
|
+
}, baseUrl, { customer_group_id: "x-customer-group-id" });
|
|
883
870
|
this.config = { ...config };
|
|
884
871
|
this.setupPosAuth();
|
|
885
872
|
}
|