@commercengine/storefront-sdk 0.3.0 → 0.3.2
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 +90 -39
- package/dist/index.js +156 -127
- package/dist/lib/auth-utils.d.ts +16 -0
- package/dist/lib/auth-utils.js +38 -0
- package/dist/lib/auth.d.ts +105 -195
- package/dist/lib/auth.js +221 -653
- package/dist/lib/cart.d.ts +77 -80
- package/dist/lib/cart.js +183 -221
- package/dist/lib/catalog.d.ts +36 -69
- package/dist/lib/catalog.js +109 -118
- package/dist/lib/client.d.ts +37 -84
- package/dist/lib/client.js +148 -173
- package/dist/lib/customer.d.ts +87 -0
- package/dist/lib/customer.js +153 -0
- package/dist/lib/helper.d.ts +27 -0
- package/dist/lib/helper.js +40 -0
- package/dist/lib/jwt-utils.d.ts +75 -0
- package/dist/lib/jwt-utils.js +84 -0
- package/dist/lib/middleware.d.ts +83 -0
- package/dist/lib/middleware.js +248 -0
- package/dist/lib/order.d.ts +72 -0
- package/dist/lib/order.js +125 -0
- package/dist/lib/shipping.d.ts +14 -0
- package/dist/lib/shipping.js +17 -0
- package/dist/types/storefront-api-types.d.ts +359 -0
- package/dist/types/storefront-api-types.js +3 -0
- package/dist/types/storefront.d.ts +7976 -7369
- package/package.json +21 -12
package/dist/lib/auth.js
CHANGED
|
@@ -1,182 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NextCookieTokenStorage = exports.CookieTokenStorage = exports.BrowserTokenStorage = exports.MemoryTokenStorage = exports.AuthClient = void 0;
|
|
4
|
-
exports.createTokenStorage = createTokenStorage;
|
|
5
|
-
const client_1 = require("./client");
|
|
1
|
+
import { StorefrontAPIClient } from "./client";
|
|
6
2
|
/**
|
|
7
3
|
* Client for interacting with authentication endpoints
|
|
8
4
|
*/
|
|
9
|
-
class AuthClient extends
|
|
10
|
-
constructor(config, tokenStorage) {
|
|
11
|
-
super(config);
|
|
12
|
-
this.autoRefreshTimer = null;
|
|
13
|
-
this.tokenStorage = tokenStorage;
|
|
14
|
-
this.setupAutoRefresh();
|
|
15
|
-
}
|
|
16
|
-
// Override the setToken method to use storage
|
|
17
|
-
async setToken(token) {
|
|
18
|
-
super.setToken(token); // Set token in client
|
|
19
|
-
await this.tokenStorage.setItem("access_token", token);
|
|
20
|
-
// If we also have a refresh token, store it
|
|
21
|
-
const refreshToken = await this.tokenStorage.getItem("refresh_token");
|
|
22
|
-
if (refreshToken) {
|
|
23
|
-
this.setupAutoRefresh();
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
// Store refresh token separately
|
|
27
|
-
async setRefreshToken(token) {
|
|
28
|
-
await this.tokenStorage.setItem("refresh_token", token);
|
|
29
|
-
}
|
|
30
|
-
// Clear tokens from storage
|
|
31
|
-
async clearToken() {
|
|
32
|
-
super.clearToken(); // Clear token in client
|
|
33
|
-
await this.tokenStorage.removeItem("access_token");
|
|
34
|
-
await this.tokenStorage.removeItem("refresh_token");
|
|
35
|
-
await this.tokenStorage.removeItem("user_data");
|
|
36
|
-
this.clearAutoRefresh();
|
|
37
|
-
}
|
|
38
|
-
// Setup auto refresh based on token expiry
|
|
39
|
-
async setupAutoRefresh() {
|
|
40
|
-
this.clearAutoRefresh();
|
|
41
|
-
const token = await this.tokenStorage.getItem("access_token");
|
|
42
|
-
if (!token)
|
|
43
|
-
return;
|
|
44
|
-
const expiryTime = this.getTokenExpiry(token);
|
|
45
|
-
if (!expiryTime)
|
|
46
|
-
return;
|
|
47
|
-
// Calculate refresh time (30 seconds before expiry)
|
|
48
|
-
const currentTime = Math.floor(Date.now() / 1000);
|
|
49
|
-
const timeUntilRefresh = Math.max(0, expiryTime - currentTime - 30) * 1000;
|
|
50
|
-
// Cap the timeout to avoid integer overflow (max 2147483647 ms, ~24.8 days)
|
|
51
|
-
const maxTimeout = 2147483647;
|
|
52
|
-
const safeTimeout = Math.min(timeUntilRefresh, maxTimeout);
|
|
53
|
-
// If the token expires far in the future, we'll need to reset the timer periodically
|
|
54
|
-
if (timeUntilRefresh > maxTimeout) {
|
|
55
|
-
this.autoRefreshTimer = setTimeout(() => {
|
|
56
|
-
this.setupAutoRefresh(); // Recalculate the timer
|
|
57
|
-
}, maxTimeout);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
this.autoRefreshTimer = setTimeout(() => {
|
|
61
|
-
this.handleTokenRefresh();
|
|
62
|
-
}, safeTimeout);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
clearAutoRefresh() {
|
|
66
|
-
if (this.autoRefreshTimer) {
|
|
67
|
-
clearTimeout(this.autoRefreshTimer);
|
|
68
|
-
this.autoRefreshTimer = null;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
async handleTokenRefresh() {
|
|
72
|
-
try {
|
|
73
|
-
const refreshToken = await this.tokenStorage.getItem("refresh_token");
|
|
74
|
-
if (!refreshToken)
|
|
75
|
-
return;
|
|
76
|
-
const tokens = await this.refreshToken(refreshToken);
|
|
77
|
-
await this.setToken(tokens.access_token);
|
|
78
|
-
await this.setRefreshToken(tokens.refresh_token);
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
// If refresh fails, clear tokens
|
|
82
|
-
await this.clearToken();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
getTokenExpiry(token) {
|
|
86
|
-
try {
|
|
87
|
-
// Simple JWT parsing (payload is the middle part between dots)
|
|
88
|
-
const parts = token.split(".");
|
|
89
|
-
if (parts.length !== 3)
|
|
90
|
-
return null;
|
|
91
|
-
// Base64 decode the payload
|
|
92
|
-
// We need to fix the base64 padding for the browser's atob function
|
|
93
|
-
const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
94
|
-
const padded = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), "=");
|
|
95
|
-
// In Node.js
|
|
96
|
-
let jsonStr;
|
|
97
|
-
if (typeof Buffer !== "undefined") {
|
|
98
|
-
jsonStr = Buffer.from(padded, "base64").toString();
|
|
99
|
-
}
|
|
100
|
-
// In browser
|
|
101
|
-
else if (typeof atob !== "undefined") {
|
|
102
|
-
jsonStr = atob(padded);
|
|
103
|
-
}
|
|
104
|
-
// Fallback
|
|
105
|
-
else {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
const payload = JSON.parse(jsonStr);
|
|
109
|
-
return payload.exp;
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
console.error("Error parsing JWT token:", error);
|
|
113
|
-
return null;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
5
|
+
export class AuthClient extends StorefrontAPIClient {
|
|
116
6
|
/**
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
async storeAuthData(accessToken, refreshToken, user) {
|
|
120
|
-
await this.setToken(accessToken);
|
|
121
|
-
await this.setRefreshToken(refreshToken);
|
|
122
|
-
if (user) {
|
|
123
|
-
await this.tokenStorage.setItem("user_data", JSON.stringify(user));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Get stored user data
|
|
128
|
-
* @returns Promise that resolves to the stored user data or null if none exists
|
|
7
|
+
* Get anonymous token for guest users
|
|
129
8
|
*/
|
|
130
|
-
async
|
|
131
|
-
|
|
132
|
-
if (!userData)
|
|
133
|
-
return null;
|
|
134
|
-
try {
|
|
135
|
-
return JSON.parse(userData);
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
9
|
+
async getAnonymousToken() {
|
|
10
|
+
return this.executeRequest(() => this.client.POST("/auth/anonymous"));
|
|
140
11
|
}
|
|
141
12
|
/**
|
|
142
|
-
*
|
|
13
|
+
* Login with phone number
|
|
14
|
+
*
|
|
15
|
+
* @param phoneNumber - Phone number (without country code)
|
|
16
|
+
* @param countryCode - Country code (defaults to +91)
|
|
17
|
+
* @param registerIfNotExists - Whether to register if user doesn't exist
|
|
18
|
+
* @returns Promise with OTP token and action
|
|
143
19
|
*/
|
|
144
|
-
async
|
|
145
|
-
return this.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
});
|
|
149
|
-
if (error) {
|
|
150
|
-
this.handleError(error);
|
|
151
|
-
}
|
|
152
|
-
const response = data?.content;
|
|
153
|
-
// Store tokens and user data
|
|
154
|
-
await this.storeAuthData(response.access_token, response.refresh_token, response.user);
|
|
155
|
-
return response;
|
|
156
|
-
});
|
|
20
|
+
async loginWithPhone(body) {
|
|
21
|
+
return this.executeRequest(() => this.client.POST("/auth/login/phone", {
|
|
22
|
+
body: body,
|
|
23
|
+
}));
|
|
157
24
|
}
|
|
158
25
|
/**
|
|
159
|
-
* Login with
|
|
26
|
+
* Login with WhatsApp
|
|
160
27
|
*
|
|
161
28
|
* @param phoneNumber - Phone number (without country code)
|
|
162
29
|
* @param countryCode - Country code (defaults to +91)
|
|
163
30
|
* @param registerIfNotExists - Whether to register if user doesn't exist
|
|
164
31
|
* @returns Promise with OTP token and action
|
|
165
32
|
*/
|
|
166
|
-
async
|
|
167
|
-
return this.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
phone: phoneNumber,
|
|
171
|
-
country_code: countryCode,
|
|
172
|
-
register_if_not_exists: registerIfNotExists,
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
if (error) {
|
|
176
|
-
this.handleError(error);
|
|
177
|
-
}
|
|
178
|
-
return data?.content;
|
|
179
|
-
});
|
|
33
|
+
async loginWithWhatsApp(body) {
|
|
34
|
+
return this.executeRequest(() => this.client.POST("/auth/login/whatsapp", {
|
|
35
|
+
body: body,
|
|
36
|
+
}));
|
|
180
37
|
}
|
|
181
38
|
/**
|
|
182
39
|
* Login with email
|
|
@@ -185,19 +42,10 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
185
42
|
* @param registerIfNotExists - Whether to register if user doesn't exist
|
|
186
43
|
* @returns Promise with OTP token and action
|
|
187
44
|
*/
|
|
188
|
-
async loginWithEmail(
|
|
189
|
-
return this.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
email,
|
|
193
|
-
register_if_not_exists: registerIfNotExists,
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
if (error) {
|
|
197
|
-
this.handleError(error);
|
|
198
|
-
}
|
|
199
|
-
return data?.content;
|
|
200
|
-
});
|
|
45
|
+
async loginWithEmail(body) {
|
|
46
|
+
return this.executeRequest(() => this.client.POST("/auth/login/email", {
|
|
47
|
+
body: body,
|
|
48
|
+
}));
|
|
201
49
|
}
|
|
202
50
|
/**
|
|
203
51
|
* Login with password
|
|
@@ -205,19 +53,45 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
205
53
|
* @param credentials - Login credentials
|
|
206
54
|
* @returns Promise with user info and tokens
|
|
207
55
|
*/
|
|
208
|
-
async loginWithPassword(
|
|
209
|
-
return this.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
56
|
+
async loginWithPassword(body) {
|
|
57
|
+
return this.executeRequest(() => this.client.POST("/auth/login/password", {
|
|
58
|
+
body: body,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Forgot password
|
|
63
|
+
*
|
|
64
|
+
* @param email - Email address
|
|
65
|
+
* @returns Promise with user info and tokens
|
|
66
|
+
*/
|
|
67
|
+
async forgotPassword(body) {
|
|
68
|
+
return this.executeRequest(() => this.client.POST("/auth/forgot-password", {
|
|
69
|
+
body: body,
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reset password
|
|
74
|
+
*
|
|
75
|
+
* @param email - Email address
|
|
76
|
+
* @returns Promise with user info and tokens
|
|
77
|
+
*/
|
|
78
|
+
async resetPassword(body) {
|
|
79
|
+
return this.executeRequest(() => this.client.POST("/auth/reset-password", {
|
|
80
|
+
body: body,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Change password
|
|
85
|
+
*
|
|
86
|
+
* @param oldPassword - Old password
|
|
87
|
+
* @param newPassword - New password
|
|
88
|
+
* @param newPasswordConfirmation - New password confirmation
|
|
89
|
+
* @returns Promise with new access token and refresh token
|
|
90
|
+
*/
|
|
91
|
+
async changePassword(body) {
|
|
92
|
+
return this.executeRequest(() => this.client.POST("/auth/change-password", {
|
|
93
|
+
body: body,
|
|
94
|
+
}));
|
|
221
95
|
}
|
|
222
96
|
/**
|
|
223
97
|
* Verify OTP
|
|
@@ -227,24 +101,10 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
227
101
|
* @param otpAction - OTP action from login request
|
|
228
102
|
* @returns Promise with user info and tokens
|
|
229
103
|
*/
|
|
230
|
-
async verifyOtp(
|
|
231
|
-
return this.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
otp,
|
|
235
|
-
otp_token: otpToken,
|
|
236
|
-
otp_action: otpAction,
|
|
237
|
-
},
|
|
238
|
-
});
|
|
239
|
-
if (error) {
|
|
240
|
-
this.handleError(error);
|
|
241
|
-
}
|
|
242
|
-
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
243
|
-
await this.setToken(data.content.access_token);
|
|
244
|
-
await this.setRefreshToken(data.content.refresh_token);
|
|
245
|
-
}
|
|
246
|
-
return data?.content;
|
|
247
|
-
});
|
|
104
|
+
async verifyOtp(body) {
|
|
105
|
+
return this.executeRequest(() => this.client.POST("/auth/verify-otp", {
|
|
106
|
+
body: body,
|
|
107
|
+
}));
|
|
248
108
|
}
|
|
249
109
|
/**
|
|
250
110
|
* Register with phone
|
|
@@ -252,23 +112,10 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
252
112
|
* @param options - Registration details
|
|
253
113
|
* @returns Promise with user info and tokens
|
|
254
114
|
*/
|
|
255
|
-
async registerWithPhone(
|
|
256
|
-
return this.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
...options,
|
|
260
|
-
country_code: options.country_code,
|
|
261
|
-
},
|
|
262
|
-
});
|
|
263
|
-
if (error) {
|
|
264
|
-
this.handleError(error);
|
|
265
|
-
}
|
|
266
|
-
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
267
|
-
await this.setToken(data.content.access_token);
|
|
268
|
-
await this.setRefreshToken(data.content.refresh_token);
|
|
269
|
-
}
|
|
270
|
-
return data?.content;
|
|
271
|
-
});
|
|
115
|
+
async registerWithPhone(body) {
|
|
116
|
+
return this.executeRequest(() => this.client.POST("/auth/register/phone", {
|
|
117
|
+
body: body,
|
|
118
|
+
}));
|
|
272
119
|
}
|
|
273
120
|
/**
|
|
274
121
|
* Register with email
|
|
@@ -276,35 +123,20 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
276
123
|
* @param options - Registration details
|
|
277
124
|
* @returns Promise with user info and tokens
|
|
278
125
|
*/
|
|
279
|
-
async registerWithEmail(
|
|
280
|
-
return this.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
});
|
|
284
|
-
if (error) {
|
|
285
|
-
this.handleError(error);
|
|
286
|
-
}
|
|
287
|
-
if (data?.content?.access_token && data?.content?.refresh_token) {
|
|
288
|
-
await this.setToken(data.content.access_token);
|
|
289
|
-
await this.setRefreshToken(data.content.refresh_token);
|
|
290
|
-
}
|
|
291
|
-
return data?.content;
|
|
292
|
-
});
|
|
126
|
+
async registerWithEmail(body) {
|
|
127
|
+
return this.executeRequest(() => this.client.POST("/auth/register/email", {
|
|
128
|
+
body: body,
|
|
129
|
+
}));
|
|
293
130
|
}
|
|
294
131
|
/**
|
|
295
132
|
* Refresh the access token using a refresh token
|
|
133
|
+
* @param refreshToken - The refresh token to use for refreshing the access token
|
|
134
|
+
* @returns Promise with the new access token and refresh token
|
|
296
135
|
*/
|
|
297
|
-
async refreshToken(
|
|
298
|
-
|
|
299
|
-
body:
|
|
300
|
-
});
|
|
301
|
-
if (error) {
|
|
302
|
-
this.handleError(error);
|
|
303
|
-
}
|
|
304
|
-
const response = data?.content;
|
|
305
|
-
// Store new tokens but don't update user ID as it hasn't changed
|
|
306
|
-
await this.storeAuthData(response.access_token, response.refresh_token);
|
|
307
|
-
return response;
|
|
136
|
+
async refreshToken(body) {
|
|
137
|
+
return this.executeRequest(() => this.client.POST("/auth/refresh-token", {
|
|
138
|
+
body: body,
|
|
139
|
+
}));
|
|
308
140
|
}
|
|
309
141
|
/**
|
|
310
142
|
* Logout
|
|
@@ -312,445 +144,181 @@ class AuthClient extends client_1.StorefrontAPIClient {
|
|
|
312
144
|
* @returns Promise that resolves when logout is complete
|
|
313
145
|
*/
|
|
314
146
|
async logout() {
|
|
315
|
-
return this.
|
|
316
|
-
const { error } = await this.client.POST("/auth/logout");
|
|
317
|
-
if (error) {
|
|
318
|
-
this.handleError(error);
|
|
319
|
-
}
|
|
320
|
-
await this.clearToken();
|
|
321
|
-
});
|
|
147
|
+
return this.executeRequest(() => this.client.POST("/auth/logout"));
|
|
322
148
|
}
|
|
323
149
|
/**
|
|
324
|
-
*
|
|
325
|
-
* to implement token refresh for 401 errors
|
|
150
|
+
* Get user details
|
|
326
151
|
*
|
|
327
|
-
* @
|
|
152
|
+
* @param userId - User ID
|
|
153
|
+
* @returns Promise with user details
|
|
328
154
|
*/
|
|
329
|
-
async
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const userData = await this.getStoredUser();
|
|
336
|
-
const tokens = await this.refreshToken(refreshToken);
|
|
337
|
-
if (tokens.access_token) {
|
|
338
|
-
await this.setToken(tokens.access_token);
|
|
339
|
-
}
|
|
340
|
-
if (tokens.refresh_token) {
|
|
341
|
-
await this.setRefreshToken(tokens.refresh_token);
|
|
342
|
-
}
|
|
343
|
-
// Restore user data after refresh since it hasn't changed
|
|
344
|
-
if (userData) {
|
|
345
|
-
await this.tokenStorage.setItem("user_data", JSON.stringify(userData));
|
|
346
|
-
}
|
|
347
|
-
return true;
|
|
348
|
-
}
|
|
349
|
-
catch (error) {
|
|
350
|
-
// If refresh fails, clear tokens
|
|
351
|
-
await this.clearToken();
|
|
352
|
-
return false;
|
|
353
|
-
}
|
|
155
|
+
async getUserDetails(pathParams) {
|
|
156
|
+
return this.executeRequest(() => this.client.GET("/auth/user/{id}", {
|
|
157
|
+
params: {
|
|
158
|
+
path: pathParams,
|
|
159
|
+
},
|
|
160
|
+
}));
|
|
354
161
|
}
|
|
355
162
|
/**
|
|
356
|
-
*
|
|
357
|
-
* This wraps any API call in logic that will catch authentication errors,
|
|
358
|
-
* attempt to refresh the token, and retry the request once
|
|
163
|
+
* Update user details
|
|
359
164
|
*
|
|
360
|
-
* @param
|
|
361
|
-
* @returns Promise with
|
|
165
|
+
* @param userId - User ID
|
|
166
|
+
* @returns Promise with user details
|
|
362
167
|
*/
|
|
363
|
-
async
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (error instanceof Error &&
|
|
371
|
-
error.message === "Token refreshed, please retry the request") {
|
|
372
|
-
return await requestFn();
|
|
373
|
-
}
|
|
374
|
-
// Otherwise, re-throw the error
|
|
375
|
-
throw error;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
exports.AuthClient = AuthClient;
|
|
380
|
-
/**
|
|
381
|
-
* Default in-memory implementation of client storage
|
|
382
|
-
*/
|
|
383
|
-
class MemoryTokenStorage {
|
|
384
|
-
constructor() {
|
|
385
|
-
this.accessToken = null;
|
|
386
|
-
this.refreshToken = null;
|
|
387
|
-
this.cartId = null;
|
|
388
|
-
this.userData = null;
|
|
389
|
-
}
|
|
390
|
-
getItem(key) {
|
|
391
|
-
if (key === "access_token")
|
|
392
|
-
return Promise.resolve(this.accessToken);
|
|
393
|
-
if (key === "refresh_token")
|
|
394
|
-
return Promise.resolve(this.refreshToken);
|
|
395
|
-
if (key === "cart_id")
|
|
396
|
-
return Promise.resolve(this.cartId);
|
|
397
|
-
if (key === "user_data")
|
|
398
|
-
return Promise.resolve(this.userData);
|
|
399
|
-
return Promise.resolve(null);
|
|
400
|
-
}
|
|
401
|
-
setItem(key, value) {
|
|
402
|
-
if (key === "access_token")
|
|
403
|
-
this.accessToken = value;
|
|
404
|
-
if (key === "refresh_token")
|
|
405
|
-
this.refreshToken = value;
|
|
406
|
-
if (key === "cart_id")
|
|
407
|
-
this.cartId = value;
|
|
408
|
-
if (key === "user_data")
|
|
409
|
-
this.userData = value;
|
|
410
|
-
return Promise.resolve();
|
|
411
|
-
}
|
|
412
|
-
removeItem(key) {
|
|
413
|
-
if (key === "access_token")
|
|
414
|
-
this.accessToken = null;
|
|
415
|
-
if (key === "refresh_token")
|
|
416
|
-
this.refreshToken = null;
|
|
417
|
-
if (key === "cart_id")
|
|
418
|
-
this.cartId = null;
|
|
419
|
-
if (key === "user_data")
|
|
420
|
-
this.userData = null;
|
|
421
|
-
return Promise.resolve();
|
|
422
|
-
}
|
|
423
|
-
getCartId() {
|
|
424
|
-
return Promise.resolve(this.cartId);
|
|
425
|
-
}
|
|
426
|
-
setCartId(cartId) {
|
|
427
|
-
this.cartId = cartId;
|
|
428
|
-
return Promise.resolve();
|
|
429
|
-
}
|
|
430
|
-
clearCartId() {
|
|
431
|
-
this.cartId = null;
|
|
432
|
-
return Promise.resolve();
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
exports.MemoryTokenStorage = MemoryTokenStorage;
|
|
436
|
-
/**
|
|
437
|
-
* Browser storage implementation using localStorage
|
|
438
|
-
*/
|
|
439
|
-
class BrowserTokenStorage {
|
|
440
|
-
constructor(prefix = "storefront_") {
|
|
441
|
-
this.accessTokenKey = `${prefix}access_token`;
|
|
442
|
-
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
443
|
-
this.cartIdKey = `${prefix}cart_id`;
|
|
444
|
-
this.userDataKey = `${prefix}user_data`;
|
|
445
|
-
}
|
|
446
|
-
getItem(key) {
|
|
447
|
-
if (key === "access_token")
|
|
448
|
-
return Promise.resolve(localStorage.getItem(this.accessTokenKey));
|
|
449
|
-
if (key === "refresh_token")
|
|
450
|
-
return Promise.resolve(localStorage.getItem(this.refreshTokenKey));
|
|
451
|
-
if (key === "cart_id")
|
|
452
|
-
return Promise.resolve(localStorage.getItem(this.cartIdKey));
|
|
453
|
-
if (key === "user_data")
|
|
454
|
-
return Promise.resolve(localStorage.getItem(this.userDataKey));
|
|
455
|
-
return Promise.resolve(null);
|
|
456
|
-
}
|
|
457
|
-
setItem(key, value) {
|
|
458
|
-
if (key === "access_token")
|
|
459
|
-
localStorage.setItem(this.accessTokenKey, value);
|
|
460
|
-
if (key === "refresh_token")
|
|
461
|
-
localStorage.setItem(this.refreshTokenKey, value);
|
|
462
|
-
if (key === "cart_id")
|
|
463
|
-
localStorage.setItem(this.cartIdKey, value);
|
|
464
|
-
if (key === "user_data")
|
|
465
|
-
localStorage.setItem(this.userDataKey, value);
|
|
466
|
-
return Promise.resolve();
|
|
467
|
-
}
|
|
468
|
-
removeItem(key) {
|
|
469
|
-
if (key === "access_token")
|
|
470
|
-
localStorage.removeItem(this.accessTokenKey);
|
|
471
|
-
if (key === "refresh_token")
|
|
472
|
-
localStorage.removeItem(this.refreshTokenKey);
|
|
473
|
-
if (key === "cart_id")
|
|
474
|
-
localStorage.removeItem(this.cartIdKey);
|
|
475
|
-
if (key === "user_data")
|
|
476
|
-
localStorage.removeItem(this.userDataKey);
|
|
477
|
-
return Promise.resolve();
|
|
478
|
-
}
|
|
479
|
-
getCartId() {
|
|
480
|
-
return Promise.resolve(localStorage.getItem(this.cartIdKey));
|
|
481
|
-
}
|
|
482
|
-
setCartId(cartId) {
|
|
483
|
-
localStorage.setItem(this.cartIdKey, cartId);
|
|
484
|
-
return Promise.resolve();
|
|
485
|
-
}
|
|
486
|
-
clearCartId() {
|
|
487
|
-
localStorage.removeItem(this.cartIdKey);
|
|
488
|
-
return Promise.resolve();
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
exports.BrowserTokenStorage = BrowserTokenStorage;
|
|
492
|
-
/**
|
|
493
|
-
* Cookie-based token storage for browser or server environments
|
|
494
|
-
*/
|
|
495
|
-
class CookieTokenStorage {
|
|
496
|
-
constructor(prefix = "storefront_", cookieOptions = {
|
|
497
|
-
path: "/",
|
|
498
|
-
secure: true,
|
|
499
|
-
sameSite: "lax",
|
|
500
|
-
httpOnly: true,
|
|
501
|
-
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
502
|
-
}) {
|
|
503
|
-
this.accessTokenKey = `${prefix}access_token`;
|
|
504
|
-
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
505
|
-
this.cartIdKey = `${prefix}cart_id`;
|
|
506
|
-
this.userDataKey = `${prefix}user_data`;
|
|
507
|
-
this.cookieOptions = cookieOptions;
|
|
168
|
+
async updateUserDetails(pathParams, body) {
|
|
169
|
+
return this.executeRequest(() => this.client.PUT("/auth/user/{id}", {
|
|
170
|
+
params: {
|
|
171
|
+
path: pathParams,
|
|
172
|
+
},
|
|
173
|
+
body: body,
|
|
174
|
+
}));
|
|
508
175
|
}
|
|
509
176
|
/**
|
|
510
|
-
*
|
|
511
|
-
*
|
|
177
|
+
* Add profile image
|
|
178
|
+
*
|
|
179
|
+
* @param userId - User ID
|
|
180
|
+
* @returns Promise with user details
|
|
512
181
|
*/
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
182
|
+
async addProfileImage(pathParams, formData) {
|
|
183
|
+
return this.executeRequest(() => this.client.POST("/auth/user/{id}/profile-image", {
|
|
184
|
+
params: {
|
|
185
|
+
path: pathParams,
|
|
186
|
+
},
|
|
187
|
+
body: formData,
|
|
188
|
+
bodySerializer: (body) => {
|
|
189
|
+
const fd = new FormData();
|
|
190
|
+
for (const [key, value] of Object.entries(body)) {
|
|
191
|
+
if (value !== undefined && value !== null) {
|
|
192
|
+
fd.append(key, value);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return fd;
|
|
196
|
+
}
|
|
197
|
+
}));
|
|
527
198
|
}
|
|
528
199
|
/**
|
|
529
|
-
*
|
|
530
|
-
*
|
|
200
|
+
* Update profile image
|
|
201
|
+
*
|
|
202
|
+
* @param userId - User ID
|
|
203
|
+
* @returns Promise with user details
|
|
531
204
|
*/
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
205
|
+
async updateProfileImage(pathParams, formData) {
|
|
206
|
+
return this.executeRequest(() => this.client.PUT("/auth/user/{id}/profile-image", {
|
|
207
|
+
params: {
|
|
208
|
+
path: pathParams,
|
|
209
|
+
},
|
|
210
|
+
body: formData,
|
|
211
|
+
bodySerializer: (body) => {
|
|
212
|
+
const fd = new FormData();
|
|
213
|
+
for (const [key, value] of Object.entries(body)) {
|
|
214
|
+
if (value !== undefined && value !== null) {
|
|
215
|
+
fd.append(key, value);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return fd;
|
|
219
|
+
}
|
|
220
|
+
}));
|
|
546
221
|
}
|
|
547
222
|
/**
|
|
548
|
-
*
|
|
223
|
+
* Delete profile image
|
|
224
|
+
*
|
|
225
|
+
* @param userId - User ID
|
|
226
|
+
* @returns Promise with user details
|
|
549
227
|
*/
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
deleteCookie(this.refreshTokenKey);
|
|
557
|
-
if (key === "cart_id")
|
|
558
|
-
deleteCookie(this.cartIdKey);
|
|
559
|
-
if (key === "user_data")
|
|
560
|
-
deleteCookie(this.userDataKey);
|
|
561
|
-
}
|
|
562
|
-
// Next.js - would need to be implemented by user
|
|
563
|
-
return Promise.resolve();
|
|
228
|
+
async deleteProfileImage(pathParams) {
|
|
229
|
+
return this.executeRequest(() => this.client.DELETE("/auth/user/{id}/profile-image", {
|
|
230
|
+
params: {
|
|
231
|
+
path: pathParams,
|
|
232
|
+
},
|
|
233
|
+
}));
|
|
564
234
|
}
|
|
565
235
|
/**
|
|
566
|
-
* Get
|
|
236
|
+
* Get profile image
|
|
237
|
+
*
|
|
238
|
+
* @param userId - User ID
|
|
239
|
+
* @returns Promise with user details
|
|
567
240
|
*/
|
|
568
|
-
|
|
569
|
-
return this.
|
|
241
|
+
async getProfileImage(pathParams) {
|
|
242
|
+
return this.executeRequest(() => this.client.GET("/auth/user/{id}/profile-image", {
|
|
243
|
+
params: {
|
|
244
|
+
path: pathParams,
|
|
245
|
+
},
|
|
246
|
+
}));
|
|
570
247
|
}
|
|
571
248
|
/**
|
|
572
|
-
*
|
|
249
|
+
* Deactivate user account
|
|
250
|
+
*
|
|
251
|
+
* @param userId - User ID
|
|
252
|
+
* @returns Promise with user details
|
|
573
253
|
*/
|
|
574
|
-
|
|
575
|
-
return this.
|
|
254
|
+
async deactivateUserAccount(pathParams) {
|
|
255
|
+
return this.executeRequest(() => this.client.PUT("/auth/user/{id}/deactivate", {
|
|
256
|
+
params: {
|
|
257
|
+
path: pathParams,
|
|
258
|
+
},
|
|
259
|
+
}));
|
|
576
260
|
}
|
|
577
261
|
/**
|
|
578
|
-
*
|
|
262
|
+
* Get user notification preferences
|
|
263
|
+
*
|
|
264
|
+
* @param userId - User ID
|
|
265
|
+
* @returns Promise with user details
|
|
579
266
|
*/
|
|
580
|
-
|
|
581
|
-
return this.
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
* Next.js specific cookie storage implementation
|
|
587
|
-
* Works with the Next.js cookies API
|
|
588
|
-
*/
|
|
589
|
-
class NextCookieTokenStorage {
|
|
590
|
-
constructor(cookieStore, prefix = "storefront_") {
|
|
591
|
-
this.accessTokenKey = `${prefix}access_token`;
|
|
592
|
-
this.refreshTokenKey = `${prefix}refresh_token`;
|
|
593
|
-
this.cartIdKey = `${prefix}cart_id`;
|
|
594
|
-
this.userDataKey = `${prefix}user_data`;
|
|
595
|
-
this.cookieStore = cookieStore;
|
|
596
|
-
}
|
|
597
|
-
getItem(key) {
|
|
598
|
-
try {
|
|
599
|
-
// Assuming cookieStore.get method exists (like in next/headers)
|
|
600
|
-
let cookieKey = "";
|
|
601
|
-
if (key === "access_token")
|
|
602
|
-
cookieKey = this.accessTokenKey;
|
|
603
|
-
if (key === "refresh_token")
|
|
604
|
-
cookieKey = this.refreshTokenKey;
|
|
605
|
-
if (key === "cart_id")
|
|
606
|
-
cookieKey = this.cartIdKey;
|
|
607
|
-
if (key === "user_data")
|
|
608
|
-
cookieKey = this.userDataKey;
|
|
609
|
-
if (!cookieKey)
|
|
610
|
-
return Promise.resolve(null);
|
|
611
|
-
const cookie = this.cookieStore.get(cookieKey);
|
|
612
|
-
return cookie ? Promise.resolve(cookie.value) : Promise.resolve(null);
|
|
613
|
-
}
|
|
614
|
-
catch (error) {
|
|
615
|
-
return Promise.resolve(null);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
setItem(key, value) {
|
|
619
|
-
try {
|
|
620
|
-
// Assuming cookieStore.set method exists
|
|
621
|
-
let cookieKey = "";
|
|
622
|
-
if (key === "access_token")
|
|
623
|
-
cookieKey = this.accessTokenKey;
|
|
624
|
-
if (key === "refresh_token")
|
|
625
|
-
cookieKey = this.refreshTokenKey;
|
|
626
|
-
if (key === "cart_id")
|
|
627
|
-
cookieKey = this.cartIdKey;
|
|
628
|
-
if (key === "user_data")
|
|
629
|
-
cookieKey = this.userDataKey;
|
|
630
|
-
if (!cookieKey)
|
|
631
|
-
return Promise.resolve();
|
|
632
|
-
this.cookieStore.set(cookieKey, value, {
|
|
633
|
-
path: "/",
|
|
634
|
-
secure: process.env.NODE_ENV === "production",
|
|
635
|
-
httpOnly: true,
|
|
636
|
-
sameSite: "strict",
|
|
637
|
-
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
638
|
-
});
|
|
639
|
-
return Promise.resolve();
|
|
640
|
-
}
|
|
641
|
-
catch (error) {
|
|
642
|
-
// Silently fail - might be in an environment where cookies can't be set
|
|
643
|
-
return Promise.resolve();
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
removeItem(key) {
|
|
647
|
-
try {
|
|
648
|
-
// Assuming cookieStore.delete method exists
|
|
649
|
-
let cookieKey = "";
|
|
650
|
-
if (key === "access_token")
|
|
651
|
-
cookieKey = this.accessTokenKey;
|
|
652
|
-
if (key === "refresh_token")
|
|
653
|
-
cookieKey = this.refreshTokenKey;
|
|
654
|
-
if (key === "cart_id")
|
|
655
|
-
cookieKey = this.cartIdKey;
|
|
656
|
-
if (key === "user_data")
|
|
657
|
-
cookieKey = this.userDataKey;
|
|
658
|
-
if (!cookieKey)
|
|
659
|
-
return Promise.resolve();
|
|
660
|
-
this.cookieStore.delete(cookieKey);
|
|
661
|
-
return Promise.resolve();
|
|
662
|
-
}
|
|
663
|
-
catch (error) {
|
|
664
|
-
// Silently fail
|
|
665
|
-
return Promise.resolve();
|
|
666
|
-
}
|
|
267
|
+
async getUserNotificationPreferences(pathParams) {
|
|
268
|
+
return this.executeRequest(() => this.client.GET("/auth/user/{id}/notification-preferences", {
|
|
269
|
+
params: {
|
|
270
|
+
path: pathParams,
|
|
271
|
+
},
|
|
272
|
+
}));
|
|
667
273
|
}
|
|
668
274
|
/**
|
|
669
|
-
*
|
|
275
|
+
* Update user notification preferences
|
|
276
|
+
*
|
|
277
|
+
* @param userId - User ID
|
|
278
|
+
* @returns Promise with user details
|
|
670
279
|
*/
|
|
671
|
-
|
|
672
|
-
return this.
|
|
280
|
+
async updateUserNotificationPreferences(pathParams, body) {
|
|
281
|
+
return this.executeRequest(() => this.client.PUT("/auth/user/{id}/notification-preferences", {
|
|
282
|
+
params: {
|
|
283
|
+
path: pathParams,
|
|
284
|
+
},
|
|
285
|
+
body: body,
|
|
286
|
+
}));
|
|
673
287
|
}
|
|
674
288
|
/**
|
|
675
|
-
*
|
|
289
|
+
* Create user notification preference
|
|
290
|
+
*
|
|
291
|
+
* @param userId - User ID
|
|
292
|
+
* @returns Promise with user details
|
|
676
293
|
*/
|
|
677
|
-
|
|
678
|
-
return this.
|
|
294
|
+
async createUserNotificationPreference(pathParams, body) {
|
|
295
|
+
return this.executeRequest(() => this.client.POST("/auth/user/{id}/notification-preferences", {
|
|
296
|
+
params: {
|
|
297
|
+
path: pathParams,
|
|
298
|
+
},
|
|
299
|
+
body: body,
|
|
300
|
+
}));
|
|
679
301
|
}
|
|
680
302
|
/**
|
|
681
|
-
*
|
|
303
|
+
* Generate OTP
|
|
304
|
+
*
|
|
305
|
+
* @param body - OTP generation body
|
|
306
|
+
* @returns Promise with OTP generation response
|
|
682
307
|
*/
|
|
683
|
-
|
|
684
|
-
return this.
|
|
685
|
-
|
|
686
|
-
}
|
|
687
|
-
exports.NextCookieTokenStorage = NextCookieTokenStorage;
|
|
688
|
-
/**
|
|
689
|
-
* Helper function to get cookie value
|
|
690
|
-
*/
|
|
691
|
-
function getCookieValue(name) {
|
|
692
|
-
if (typeof document === "undefined")
|
|
693
|
-
return null;
|
|
694
|
-
const match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
|
|
695
|
-
return match ? decodeURIComponent(match[2]) : null;
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Helper function to set cookie
|
|
699
|
-
*/
|
|
700
|
-
function setCookie(name, value, options = {}) {
|
|
701
|
-
if (typeof document === "undefined")
|
|
702
|
-
return;
|
|
703
|
-
const cookieOptions = {
|
|
704
|
-
path: "/",
|
|
705
|
-
...options,
|
|
706
|
-
};
|
|
707
|
-
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
708
|
-
if (cookieOptions.maxAge) {
|
|
709
|
-
cookie += `; max-age=${cookieOptions.maxAge}`;
|
|
710
|
-
}
|
|
711
|
-
if (cookieOptions.domain) {
|
|
712
|
-
cookie += `; domain=${cookieOptions.domain}`;
|
|
308
|
+
async generateOtp(body) {
|
|
309
|
+
return this.executeRequest(() => this.client.POST("/auth/generate-otp", {
|
|
310
|
+
body: body,
|
|
311
|
+
}));
|
|
713
312
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Helper function to delete cookie
|
|
727
|
-
*/
|
|
728
|
-
function deleteCookie(name, path = "/") {
|
|
729
|
-
if (typeof document === "undefined")
|
|
730
|
-
return;
|
|
731
|
-
document.cookie = `${name}=; path=${path}; expires=Thu, 01 Jan 1970 00:00:01 GMT`;
|
|
732
|
-
}
|
|
733
|
-
/**
|
|
734
|
-
* Helper to create a token storage instance based on environment
|
|
735
|
-
* Automatically selects the best storage method based on context
|
|
736
|
-
*/
|
|
737
|
-
function createTokenStorage(options) {
|
|
738
|
-
const prefix = options?.prefix || "storefront_";
|
|
739
|
-
// Node.js environment without a cookieStore - use memory
|
|
740
|
-
if (typeof window === "undefined" && !options?.cookieStore) {
|
|
741
|
-
return new MemoryTokenStorage();
|
|
742
|
-
}
|
|
743
|
-
// Next.js server component with cookieStore
|
|
744
|
-
if (typeof window === "undefined" && options?.cookieStore) {
|
|
745
|
-
return new NextCookieTokenStorage(options.cookieStore, prefix);
|
|
746
|
-
}
|
|
747
|
-
// Browser environment - use localStorage by default unless cookies are specified
|
|
748
|
-
if (typeof window !== "undefined") {
|
|
749
|
-
if (options?.useLocalStorage === false) {
|
|
750
|
-
return new CookieTokenStorage(prefix);
|
|
751
|
-
}
|
|
752
|
-
return new BrowserTokenStorage(prefix);
|
|
313
|
+
/**
|
|
314
|
+
* Check whether email or phone is already verified
|
|
315
|
+
*
|
|
316
|
+
* @param body - OTP generation body
|
|
317
|
+
* @returns Promise with OTP generation response
|
|
318
|
+
*/
|
|
319
|
+
async checkEmailOrPhoneIsVerified(body) {
|
|
320
|
+
return this.executeRequest(() => this.client.POST("/auth/verified-email-phone", {
|
|
321
|
+
body: body,
|
|
322
|
+
}));
|
|
753
323
|
}
|
|
754
|
-
// Fallback to memory storage
|
|
755
|
-
return new MemoryTokenStorage();
|
|
756
324
|
}
|