@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.
@@ -1,38 +1,13 @@
1
- import { StorefrontAPIClient, StorefrontAPIConfig } from "./client";
2
- import type { components } from "../types/storefront";
1
+ import { StorefrontAPIClient } from "./client";
2
+ import type { AddProfileImageContent, AddProfileImageFormData, AddProfileImagePathParams, ApiResult, ChangePasswordBody, ChangePasswordContent, CheckVerificationStatusBody, CheckVerificationStatusContent, CreateNotificationPreferencesBody, CreateNotificationPreferencesContent, CreateNotificationPreferencesPathParams, DeactivateUserPathParams, DeactivateUserResponse, ForgotPasswordBody, ForgotPasswordContent, GenerateOtpBody, GenerateOtpContent, GetAnonymousTokenContent, GetNotificationPreferencesContent, GetNotificationPreferencesPathParams, GetProfileImageContent, GetProfileImagePathParams, GetUserDetailContent, GetUserDetailPathParams, LoginWithEmailBody, LoginWithEmailContent, LoginWithPasswordBody, LoginWithPasswordContent, LoginWithPhoneBody, LoginWithPhoneContent, LoginWithWhatsappBody, LoginWithWhatsappContent, LogoutContent, RefreshTokenBody, RefreshTokenContent, RegisterWithEmailBody, RegisterWithEmailContent, RegisterWithPhoneBody, RegisterWithPhoneContent, RemoveProfileImagePathParams, RemoveProfileImageResponse, ResetPasswordBody, ResetPasswordContent, UpdateNotificationPreferencesBody, UpdateNotificationPreferencesContent, UpdateNotificationPreferencesPathParams, UpdateProfileImageContent, UpdateProfileImageFormData, UpdateProfileImagePathParams, UpdateUserBody, UpdateUserContent, UpdateUserPathParams, VerifyOtpBody, VerifyOtpContent } from "../types/storefront-api-types";
3
3
  /**
4
4
  * Client for interacting with authentication endpoints
5
5
  */
6
6
  export declare class AuthClient extends StorefrontAPIClient {
7
- private readonly tokenStorage;
8
- private autoRefreshTimer;
9
- constructor(config: StorefrontAPIConfig, tokenStorage: TokenStorage);
10
- setToken(token: string): Promise<void>;
11
- protected setRefreshToken(token: string): Promise<void>;
12
- clearToken(): Promise<void>;
13
- private setupAutoRefresh;
14
- private clearAutoRefresh;
15
- private handleTokenRefresh;
16
- private getTokenExpiry;
17
- /**
18
- * Store auth data including tokens and user info
19
- */
20
- private storeAuthData;
21
- /**
22
- * Get stored user data
23
- * @returns Promise that resolves to the stored user data or null if none exists
24
- */
25
- getStoredUser(): Promise<components["schemas"]["User"] | components["schemas"]["AnonymousUser"] | null>;
26
7
  /**
27
8
  * Get anonymous token for guest users
28
9
  */
29
- getAnonymousToken(options?: {
30
- apiKey?: string;
31
- }): Promise<{
32
- access_token: string;
33
- refresh_token: string;
34
- user: components["schemas"]["AnonymousUser"];
35
- }>;
10
+ getAnonymousToken(): Promise<ApiResult<GetAnonymousTokenContent>>;
36
11
  /**
37
12
  * Login with phone number
38
13
  *
@@ -41,10 +16,16 @@ export declare class AuthClient extends StorefrontAPIClient {
41
16
  * @param registerIfNotExists - Whether to register if user doesn't exist
42
17
  * @returns Promise with OTP token and action
43
18
  */
44
- loginWithPhone(phoneNumber: string, countryCode?: string, registerIfNotExists?: boolean): Promise<{
45
- otp_token: string;
46
- otp_action: string;
47
- }>;
19
+ loginWithPhone(body: LoginWithPhoneBody): Promise<ApiResult<LoginWithPhoneContent>>;
20
+ /**
21
+ * Login with WhatsApp
22
+ *
23
+ * @param phoneNumber - Phone number (without country code)
24
+ * @param countryCode - Country code (defaults to +91)
25
+ * @param registerIfNotExists - Whether to register if user doesn't exist
26
+ * @returns Promise with OTP token and action
27
+ */
28
+ loginWithWhatsApp(body: LoginWithWhatsappBody): Promise<ApiResult<LoginWithWhatsappContent>>;
48
29
  /**
49
30
  * Login with email
50
31
  *
@@ -52,24 +33,37 @@ export declare class AuthClient extends StorefrontAPIClient {
52
33
  * @param registerIfNotExists - Whether to register if user doesn't exist
53
34
  * @returns Promise with OTP token and action
54
35
  */
55
- loginWithEmail(email: string, registerIfNotExists?: boolean): Promise<{
56
- otp_token: string;
57
- otp_action: string;
58
- }>;
36
+ loginWithEmail(body: LoginWithEmailBody): Promise<ApiResult<LoginWithEmailContent>>;
59
37
  /**
60
38
  * Login with password
61
39
  *
62
40
  * @param credentials - Login credentials
63
41
  * @returns Promise with user info and tokens
64
42
  */
65
- loginWithPassword(credentials: {
66
- email: string;
67
- password: string;
68
- }): Promise<{
69
- access_token: string;
70
- refresh_token: string;
71
- user: components["schemas"]["User"];
72
- }>;
43
+ loginWithPassword(body: LoginWithPasswordBody): Promise<ApiResult<LoginWithPasswordContent>>;
44
+ /**
45
+ * Forgot password
46
+ *
47
+ * @param email - Email address
48
+ * @returns Promise with user info and tokens
49
+ */
50
+ forgotPassword(body: ForgotPasswordBody): Promise<ApiResult<ForgotPasswordContent>>;
51
+ /**
52
+ * Reset password
53
+ *
54
+ * @param email - Email address
55
+ * @returns Promise with user info and tokens
56
+ */
57
+ resetPassword(body: ResetPasswordBody): Promise<ApiResult<ResetPasswordContent>>;
58
+ /**
59
+ * Change password
60
+ *
61
+ * @param oldPassword - Old password
62
+ * @param newPassword - New password
63
+ * @param newPasswordConfirmation - New password confirmation
64
+ * @returns Promise with new access token and refresh token
65
+ */
66
+ changePassword(body: ChangePasswordBody): Promise<ApiResult<ChangePasswordContent>>;
73
67
  /**
74
68
  * Verify OTP
75
69
  *
@@ -78,199 +72,115 @@ export declare class AuthClient extends StorefrontAPIClient {
78
72
  * @param otpAction - OTP action from login request
79
73
  * @returns Promise with user info and tokens
80
74
  */
81
- verifyOtp(otp: string, otpToken: string, otpAction: "login" | "register" | "reset-password" | "verify-phone" | "verify-email" | "update-phone" | "update-email"): Promise<{
82
- user?: components["schemas"]["User"];
83
- access_token?: string;
84
- refresh_token?: string;
85
- }>;
75
+ verifyOtp(body: VerifyOtpBody): Promise<ApiResult<VerifyOtpContent>>;
86
76
  /**
87
77
  * Register with phone
88
78
  *
89
79
  * @param options - Registration details
90
80
  * @returns Promise with user info and tokens
91
81
  */
92
- registerWithPhone(options: {
93
- country_code?: string;
94
- phone: string;
95
- first_name: string;
96
- last_name?: string;
97
- email: string;
98
- otp_token?: string;
99
- }): Promise<{
100
- user: components["schemas"]["User"];
101
- access_token: string;
102
- refresh_token: string;
103
- }>;
82
+ registerWithPhone(body: RegisterWithPhoneBody): Promise<ApiResult<RegisterWithPhoneContent>>;
104
83
  /**
105
84
  * Register with email
106
85
  *
107
86
  * @param options - Registration details
108
87
  * @returns Promise with user info and tokens
109
88
  */
110
- registerWithEmail(options: {
111
- email: string;
112
- first_name: string;
113
- last_name?: string;
114
- phone: string;
115
- otp_token?: string;
116
- }): Promise<{
117
- user: components["schemas"]["User"];
118
- access_token: string;
119
- refresh_token: string;
120
- }>;
89
+ registerWithEmail(body: RegisterWithEmailBody): Promise<ApiResult<RegisterWithEmailContent>>;
121
90
  /**
122
91
  * Refresh the access token using a refresh token
92
+ * @param refreshToken - The refresh token to use for refreshing the access token
93
+ * @returns Promise with the new access token and refresh token
123
94
  */
124
- refreshToken(refreshToken: string): Promise<{
125
- access_token: string;
126
- refresh_token: string;
127
- }>;
95
+ refreshToken(body: RefreshTokenBody): Promise<ApiResult<RefreshTokenContent>>;
128
96
  /**
129
97
  * Logout
130
98
  *
131
99
  * @returns Promise that resolves when logout is complete
132
100
  */
133
- logout(): Promise<void>;
101
+ logout(): Promise<ApiResult<LogoutContent>>;
134
102
  /**
135
- * Override the base client's attemptTokenRefresh method
136
- * to implement token refresh for 401 errors
103
+ * Get user details
137
104
  *
138
- * @returns Promise that resolves to true if token was refreshed, false otherwise
105
+ * @param userId - User ID
106
+ * @returns Promise with user details
139
107
  */
140
- protected attemptTokenRefresh(): Promise<boolean>;
108
+ getUserDetails(pathParams: GetUserDetailPathParams): Promise<ApiResult<GetUserDetailContent>>;
141
109
  /**
142
- * Execute a request with automatic token refresh handling
143
- * This wraps any API call in logic that will catch authentication errors,
144
- * attempt to refresh the token, and retry the request once
110
+ * Update user details
145
111
  *
146
- * @param requestFn - Function that executes the API request
147
- * @returns Promise with the API response
112
+ * @param userId - User ID
113
+ * @returns Promise with user details
148
114
  */
149
- executeWithTokenRefresh<T>(requestFn: () => Promise<T>): Promise<T>;
150
- }
151
- /**
152
- * Interface for token storage implementations
153
- */
154
- export interface TokenStorage {
155
- getItem(key: string): Promise<string | null>;
156
- setItem(key: string, value: string): Promise<void>;
157
- removeItem(key: string): Promise<void>;
158
- }
159
- /**
160
- * Interface for client storage implementations
161
- */
162
- export interface ClientStorage extends TokenStorage {
163
- getCartId(): Promise<string | null>;
164
- setCartId(cartId: string): Promise<void>;
165
- clearCartId(): Promise<void>;
166
- }
167
- /**
168
- * Default in-memory implementation of client storage
169
- */
170
- export declare class MemoryTokenStorage implements ClientStorage {
171
- private accessToken;
172
- private refreshToken;
173
- private cartId;
174
- private userData;
175
- getItem(key: string): Promise<string | null>;
176
- setItem(key: string, value: string): Promise<void>;
177
- removeItem(key: string): Promise<void>;
178
- getCartId(): Promise<string | null>;
179
- setCartId(cartId: string): Promise<void>;
180
- clearCartId(): Promise<void>;
181
- }
182
- /**
183
- * Browser storage implementation using localStorage
184
- */
185
- export declare class BrowserTokenStorage implements ClientStorage {
186
- private accessTokenKey;
187
- private refreshTokenKey;
188
- private cartIdKey;
189
- private userDataKey;
190
- constructor(prefix?: string);
191
- getItem(key: string): Promise<string | null>;
192
- setItem(key: string, value: string): Promise<void>;
193
- removeItem(key: string): Promise<void>;
194
- getCartId(): Promise<string | null>;
195
- setCartId(cartId: string): Promise<void>;
196
- clearCartId(): Promise<void>;
197
- }
198
- /**
199
- * Cookie-based token storage for browser or server environments
200
- */
201
- export declare class CookieTokenStorage implements ClientStorage {
202
- private accessTokenKey;
203
- private refreshTokenKey;
204
- private cartIdKey;
205
- private userDataKey;
206
- private cookieOptions;
207
- constructor(prefix?: string, cookieOptions?: {
208
- path: string;
209
- secure: boolean;
210
- sameSite: string;
211
- httpOnly: boolean;
212
- maxAge: number;
213
- });
115
+ updateUserDetails(pathParams: UpdateUserPathParams, body: UpdateUserBody): Promise<ApiResult<UpdateUserContent>>;
116
+ /**
117
+ * Add profile image
118
+ *
119
+ * @param userId - User ID
120
+ * @returns Promise with user details
121
+ */
122
+ addProfileImage(pathParams: AddProfileImagePathParams, formData: AddProfileImageFormData): Promise<ApiResult<AddProfileImageContent>>;
214
123
  /**
215
- * Get value from cookies
216
- * Works in both browser and Next.js server components
124
+ * Update profile image
125
+ *
126
+ * @param userId - User ID
127
+ * @returns Promise with user details
217
128
  */
218
- getItem(key: string): Promise<string | null>;
129
+ updateProfileImage(pathParams: UpdateProfileImagePathParams, formData: UpdateProfileImageFormData): Promise<ApiResult<UpdateProfileImageContent>>;
219
130
  /**
220
- * Set value in cookies
221
- * Works in browser environment
131
+ * Delete profile image
132
+ *
133
+ * @param userId - User ID
134
+ * @returns Promise with user details
222
135
  */
223
- setItem(key: string, value: string): Promise<void>;
136
+ deleteProfileImage(pathParams: RemoveProfileImagePathParams): Promise<ApiResult<RemoveProfileImageResponse>>;
224
137
  /**
225
- * Remove value from cookies
138
+ * Get profile image
139
+ *
140
+ * @param userId - User ID
141
+ * @returns Promise with user details
226
142
  */
227
- removeItem(key: string): Promise<void>;
143
+ getProfileImage(pathParams: GetProfileImagePathParams): Promise<ApiResult<GetProfileImageContent>>;
228
144
  /**
229
- * Get cart ID from cookies
145
+ * Deactivate user account
146
+ *
147
+ * @param userId - User ID
148
+ * @returns Promise with user details
230
149
  */
231
- getCartId(): Promise<string | null>;
150
+ deactivateUserAccount(pathParams: DeactivateUserPathParams): Promise<ApiResult<DeactivateUserResponse>>;
232
151
  /**
233
- * Set cart ID in cookies
152
+ * Get user notification preferences
153
+ *
154
+ * @param userId - User ID
155
+ * @returns Promise with user details
234
156
  */
235
- setCartId(cartId: string): Promise<void>;
157
+ getUserNotificationPreferences(pathParams: GetNotificationPreferencesPathParams): Promise<ApiResult<GetNotificationPreferencesContent>>;
236
158
  /**
237
- * Clear cart ID from cookies
159
+ * Update user notification preferences
160
+ *
161
+ * @param userId - User ID
162
+ * @returns Promise with user details
238
163
  */
239
- clearCartId(): Promise<void>;
240
- }
241
- /**
242
- * Next.js specific cookie storage implementation
243
- * Works with the Next.js cookies API
244
- */
245
- export declare class NextCookieTokenStorage implements ClientStorage {
246
- private accessTokenKey;
247
- private refreshTokenKey;
248
- private cartIdKey;
249
- private userDataKey;
250
- private cookieStore;
251
- constructor(cookieStore: any, prefix?: string);
252
- getItem(key: string): Promise<string | null>;
253
- setItem(key: string, value: string): Promise<void>;
254
- removeItem(key: string): Promise<void>;
164
+ updateUserNotificationPreferences(pathParams: UpdateNotificationPreferencesPathParams, body: UpdateNotificationPreferencesBody): Promise<ApiResult<UpdateNotificationPreferencesContent>>;
255
165
  /**
256
- * Get cart ID from Next.js cookies
166
+ * Create user notification preference
167
+ *
168
+ * @param userId - User ID
169
+ * @returns Promise with user details
257
170
  */
258
- getCartId(): Promise<string | null>;
171
+ createUserNotificationPreference(pathParams: CreateNotificationPreferencesPathParams, body: CreateNotificationPreferencesBody): Promise<ApiResult<CreateNotificationPreferencesContent>>;
259
172
  /**
260
- * Set cart ID in Next.js cookies
173
+ * Generate OTP
174
+ *
175
+ * @param body - OTP generation body
176
+ * @returns Promise with OTP generation response
261
177
  */
262
- setCartId(cartId: string): Promise<void>;
178
+ generateOtp(body: GenerateOtpBody): Promise<ApiResult<GenerateOtpContent>>;
263
179
  /**
264
- * Clear cart ID from Next.js cookies
180
+ * Check whether email or phone is already verified
181
+ *
182
+ * @param body - OTP generation body
183
+ * @returns Promise with OTP generation response
265
184
  */
266
- clearCartId(): Promise<void>;
185
+ checkEmailOrPhoneIsVerified(body: CheckVerificationStatusBody): Promise<ApiResult<CheckVerificationStatusContent>>;
267
186
  }
268
- /**
269
- * Helper to create a token storage instance based on environment
270
- * Automatically selects the best storage method based on context
271
- */
272
- export declare function createTokenStorage(options?: {
273
- prefix?: string;
274
- cookieStore?: any;
275
- useLocalStorage?: boolean;
276
- }): TokenStorage;