@commercengine/pos 0.2.0 → 0.3.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 +477 -162
- package/dist/index.js +117 -66
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -443,18 +443,16 @@ function extractUserInfoFromToken(token) {
|
|
|
443
443
|
isLoggedIn: payload.is_logged_in,
|
|
444
444
|
customerId: payload.customer_id,
|
|
445
445
|
customerGroupId: payload.customer_group_id,
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
446
|
+
roles: payload.roles.map((role) => ({
|
|
447
|
+
roleId: role.role_id,
|
|
448
|
+
roleName: role.role_name,
|
|
449
|
+
locationId: role.location_id,
|
|
450
|
+
locationName: role.location_name
|
|
451
|
+
})),
|
|
450
452
|
location: {
|
|
451
453
|
id: payload.location.id,
|
|
452
454
|
name: payload.location.name
|
|
453
455
|
},
|
|
454
|
-
role: {
|
|
455
|
-
id: payload.role.id,
|
|
456
|
-
name: payload.role.name
|
|
457
|
-
},
|
|
458
456
|
tenantId: payload.tenant_id,
|
|
459
457
|
channel: {
|
|
460
458
|
id: payload.channel.id,
|
|
@@ -521,16 +519,6 @@ function isUserAuthenticated(token) {
|
|
|
521
519
|
return !!userInfo?.isLoggedIn;
|
|
522
520
|
}
|
|
523
521
|
/**
|
|
524
|
-
* Get the device ID from a POS JWT token
|
|
525
|
-
*
|
|
526
|
-
* @param token - The JWT token
|
|
527
|
-
* @returns Device ID or null if token is invalid
|
|
528
|
-
*/
|
|
529
|
-
function getDeviceIdFromToken(token) {
|
|
530
|
-
const userInfo = extractUserInfoFromToken(token);
|
|
531
|
-
return userInfo?.device?.deviceId || null;
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
522
|
* Get the location ID from a POS JWT token
|
|
535
523
|
*
|
|
536
524
|
* @param token - The JWT token
|
|
@@ -541,14 +529,16 @@ function getLocationIdFromToken(token) {
|
|
|
541
529
|
return userInfo?.location?.id || null;
|
|
542
530
|
}
|
|
543
531
|
/**
|
|
544
|
-
* Get the role information from a POS JWT token
|
|
532
|
+
* Get the role information from a POS JWT token for the current location
|
|
545
533
|
*
|
|
546
534
|
* @param token - The JWT token
|
|
547
|
-
* @returns Role object
|
|
535
|
+
* @returns Role object for the current location, or null if token is invalid
|
|
548
536
|
*/
|
|
549
537
|
function getRoleFromToken(token) {
|
|
550
538
|
const userInfo = extractUserInfoFromToken(token);
|
|
551
|
-
|
|
539
|
+
if (!userInfo?.roles || userInfo.roles.length === 0) return null;
|
|
540
|
+
const currentLocationRole = userInfo.roles.find((role) => role.locationId === userInfo.location.id);
|
|
541
|
+
return currentLocationRole || userInfo.roles[0] || null;
|
|
552
542
|
}
|
|
553
543
|
/**
|
|
554
544
|
* Get the tenant ID from a POS JWT token
|
|
@@ -1015,13 +1005,13 @@ var PosAPIClient = class PosAPIClient extends BaseAPIClient {
|
|
|
1015
1005
|
*/
|
|
1016
1006
|
var PosClient = class extends PosAPIClient {
|
|
1017
1007
|
/**
|
|
1018
|
-
* Login with email address
|
|
1019
|
-
* @param body - Login credentials containing
|
|
1008
|
+
* Login with email address
|
|
1009
|
+
* @param body - Login credentials containing location ID and email
|
|
1020
1010
|
* @returns Promise with OTP token and action
|
|
1021
1011
|
* @example
|
|
1022
1012
|
* ```typescript
|
|
1023
1013
|
* const { data, error } = await pos.loginWithEmail({
|
|
1024
|
-
*
|
|
1014
|
+
* location_id: "pos-location-123",
|
|
1025
1015
|
* email: "staff@example.com"
|
|
1026
1016
|
* });
|
|
1027
1017
|
*
|
|
@@ -1038,13 +1028,13 @@ var PosClient = class extends PosAPIClient {
|
|
|
1038
1028
|
return this.executeRequest(() => this.client.POST("/pos/auth/login/email", { body }));
|
|
1039
1029
|
}
|
|
1040
1030
|
/**
|
|
1041
|
-
* Login with phone number
|
|
1042
|
-
* @param body - Login credentials containing
|
|
1031
|
+
* Login with phone number
|
|
1032
|
+
* @param body - Login credentials containing location ID and phone
|
|
1043
1033
|
* @returns Promise with OTP token and action
|
|
1044
1034
|
* @example
|
|
1045
1035
|
* ```typescript
|
|
1046
1036
|
* const { data, error } = await pos.loginWithPhone({
|
|
1047
|
-
*
|
|
1037
|
+
* location_id: "pos-location-123",
|
|
1048
1038
|
* phone: "234567890",
|
|
1049
1039
|
* country_code: "+91" // default is +91
|
|
1050
1040
|
* });
|
|
@@ -1062,13 +1052,13 @@ var PosClient = class extends PosAPIClient {
|
|
|
1062
1052
|
return this.executeRequest(() => this.client.POST("/pos/auth/login/phone", { body }));
|
|
1063
1053
|
}
|
|
1064
1054
|
/**
|
|
1065
|
-
* Login with WhatsApp
|
|
1066
|
-
* @param body - Login credentials containing
|
|
1055
|
+
* Login with WhatsApp
|
|
1056
|
+
* @param body - Login credentials containing location ID and phone
|
|
1067
1057
|
* @returns Promise with OTP token and action
|
|
1068
1058
|
* @example
|
|
1069
1059
|
* ```typescript
|
|
1070
1060
|
* const { data, error } = await pos.loginWithWhatsapp({
|
|
1071
|
-
*
|
|
1061
|
+
* location_id: "pos-location-123",
|
|
1072
1062
|
* phone: "234567890",
|
|
1073
1063
|
* country_code: "+91" // default is +91
|
|
1074
1064
|
* });
|
|
@@ -1086,29 +1076,7 @@ var PosClient = class extends PosAPIClient {
|
|
|
1086
1076
|
return this.executeRequest(() => this.client.POST("/pos/auth/login/whatsapp", { body }));
|
|
1087
1077
|
}
|
|
1088
1078
|
/**
|
|
1089
|
-
*
|
|
1090
|
-
* @param body - Pairing code received via phone/email
|
|
1091
|
-
* @returns Promise with device information
|
|
1092
|
-
* @example
|
|
1093
|
-
* ```typescript
|
|
1094
|
-
* const { data, error } = await pos.pairDevice({
|
|
1095
|
-
* pairing_code: "ABC123"
|
|
1096
|
-
* });
|
|
1097
|
-
*
|
|
1098
|
-
* if (error) {
|
|
1099
|
-
* console.error("Device pairing failed:", error.message);
|
|
1100
|
-
* } else {
|
|
1101
|
-
* console.log("Device paired:", data.device.id);
|
|
1102
|
-
* console.log("Device name:", data.device.name);
|
|
1103
|
-
* // Store device_id for future use
|
|
1104
|
-
* }
|
|
1105
|
-
* ```
|
|
1106
|
-
*/
|
|
1107
|
-
async pairDevice(body) {
|
|
1108
|
-
return this.executeRequest(() => this.client.POST("/pos/auth/pair-device", { body }));
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* Verify OTP for POS login
|
|
1079
|
+
* Verify OTP
|
|
1112
1080
|
* @param body - OTP verification data
|
|
1113
1081
|
* @returns Promise with user info and tokens
|
|
1114
1082
|
* @example
|
|
@@ -1155,7 +1123,7 @@ var PosClient = class extends PosAPIClient {
|
|
|
1155
1123
|
return this.executeRequest(() => this.client.POST("/pos/auth/refresh-token", { body }));
|
|
1156
1124
|
}
|
|
1157
1125
|
/**
|
|
1158
|
-
* Logout
|
|
1126
|
+
* Logout
|
|
1159
1127
|
* @returns Promise with logout confirmation
|
|
1160
1128
|
* @example
|
|
1161
1129
|
* ```typescript
|
|
@@ -1174,6 +1142,100 @@ var PosClient = class extends PosAPIClient {
|
|
|
1174
1142
|
return this.executeRequest(() => this.client.POST("/pos/auth/logout"));
|
|
1175
1143
|
}
|
|
1176
1144
|
/**
|
|
1145
|
+
* List store locations
|
|
1146
|
+
* @returns Promise with list of locations
|
|
1147
|
+
* @example
|
|
1148
|
+
* ```typescript
|
|
1149
|
+
* const { data, error } = await pos.listLocations();
|
|
1150
|
+
* if (error) {
|
|
1151
|
+
* console.error("Failed to list locations:", error.message);
|
|
1152
|
+
* } else {
|
|
1153
|
+
* console.log("Locations:", data.locations);
|
|
1154
|
+
* }
|
|
1155
|
+
* ```
|
|
1156
|
+
*/
|
|
1157
|
+
async listLocations() {
|
|
1158
|
+
return this.executeRequest(() => this.client.GET("/pos/locations"));
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* List devices
|
|
1162
|
+
* @returns Promise with list of devices
|
|
1163
|
+
* @example
|
|
1164
|
+
* ```typescript
|
|
1165
|
+
* const { data, error } = await pos.listDevices();
|
|
1166
|
+
* if (error) {
|
|
1167
|
+
* console.error("Failed to list devices:", error.message);
|
|
1168
|
+
* } else {
|
|
1169
|
+
* console.log("Devices:", data.devices);
|
|
1170
|
+
* }
|
|
1171
|
+
* ```
|
|
1172
|
+
*/
|
|
1173
|
+
async listDevices() {
|
|
1174
|
+
return this.executeRequest(() => this.client.GET("/pos/devices"));
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Pair POS device with pairing code
|
|
1178
|
+
* @param body - Pairing code received via phone/email
|
|
1179
|
+
* @returns Promise with device information
|
|
1180
|
+
* @example
|
|
1181
|
+
* ```typescript
|
|
1182
|
+
* const { data, error } = await pos.pairDevice({
|
|
1183
|
+
* pairing_code: "ABC123"
|
|
1184
|
+
* });
|
|
1185
|
+
*
|
|
1186
|
+
* if (error) {
|
|
1187
|
+
* console.error("Device pairing failed:", error.message);
|
|
1188
|
+
* } else {
|
|
1189
|
+
* console.log("Device paired:", data.device.id);
|
|
1190
|
+
* console.log("Device name:", data.device.name);
|
|
1191
|
+
* // Store device_id for future use
|
|
1192
|
+
* }
|
|
1193
|
+
* ```
|
|
1194
|
+
*/
|
|
1195
|
+
async pairDevice(body) {
|
|
1196
|
+
return this.executeRequest(() => this.client.POST("/pos/auth/pair-device", { body }));
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Unclaim POS device - Unlinks a device from the user session
|
|
1200
|
+
* @param pathParams - Device ID
|
|
1201
|
+
* @returns Promise with unclaim confirmation
|
|
1202
|
+
* @example
|
|
1203
|
+
* ```typescript
|
|
1204
|
+
* const { data, error } = await pos.unclaimDevice({
|
|
1205
|
+
* id: "01H9DEVICE12345ABCDE"
|
|
1206
|
+
* });
|
|
1207
|
+
*
|
|
1208
|
+
* if (error) {
|
|
1209
|
+
* console.error("Failed to unclaim device:", error.message);
|
|
1210
|
+
* } else {
|
|
1211
|
+
* console.log("Device unclaimed:", data.message);
|
|
1212
|
+
* }
|
|
1213
|
+
* ```
|
|
1214
|
+
*/
|
|
1215
|
+
async unclaimDevice(pathParams) {
|
|
1216
|
+
return this.executeRequest(() => this.client.POST("/pos/devices/{id}/unclaim", { params: { path: pathParams } }));
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Claim POS device - Links a device to the user session
|
|
1220
|
+
* @param pathParams - Device ID
|
|
1221
|
+
* @returns Promise with claim confirmation
|
|
1222
|
+
* @example
|
|
1223
|
+
* ```typescript
|
|
1224
|
+
* const { data, error } = await pos.claimDevice({
|
|
1225
|
+
* id: "01H9DEVICE12345ABCDE"
|
|
1226
|
+
* });
|
|
1227
|
+
*
|
|
1228
|
+
* if (error) {
|
|
1229
|
+
* console.error("Failed to claim device:", error.message);
|
|
1230
|
+
* } else {
|
|
1231
|
+
* console.log("Device claimed:", data.message);
|
|
1232
|
+
* }
|
|
1233
|
+
* ```
|
|
1234
|
+
*/
|
|
1235
|
+
async claimDevice(pathParams) {
|
|
1236
|
+
return this.executeRequest(() => this.client.POST("/pos/devices/{id}/claim", { params: { path: pathParams } }));
|
|
1237
|
+
}
|
|
1238
|
+
/**
|
|
1177
1239
|
* Create a new cart
|
|
1178
1240
|
* @param body - Cart creation data with items
|
|
1179
1241
|
* @returns Promise with created cart
|
|
@@ -2916,17 +2978,6 @@ var PosSDK = class {
|
|
|
2916
2978
|
return isUserAuthenticated(token);
|
|
2917
2979
|
}
|
|
2918
2980
|
/**
|
|
2919
|
-
* Get the device ID from the current access token
|
|
2920
|
-
* This is commonly needed for POS API requests
|
|
2921
|
-
*
|
|
2922
|
-
* @returns Device ID or null if no token or invalid token
|
|
2923
|
-
*/
|
|
2924
|
-
async getDeviceId() {
|
|
2925
|
-
const token = await this.getAccessToken();
|
|
2926
|
-
if (!token) return null;
|
|
2927
|
-
return getDeviceIdFromToken(token);
|
|
2928
|
-
}
|
|
2929
|
-
/**
|
|
2930
2981
|
* Get the location ID from the current access token
|
|
2931
2982
|
* This is commonly needed for POS API requests
|
|
2932
2983
|
*
|
|
@@ -2940,7 +2991,7 @@ var PosSDK = class {
|
|
|
2940
2991
|
/**
|
|
2941
2992
|
* Get the role information from the current access token
|
|
2942
2993
|
*
|
|
2943
|
-
* @returns Role object with
|
|
2994
|
+
* @returns Role object with roleId, roleName, locationId, and locationName, or null if no token or invalid token
|
|
2944
2995
|
*/
|
|
2945
2996
|
async getRole() {
|
|
2946
2997
|
const token = await this.getAccessToken();
|