@commercengine/storefront-sdk 0.3.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.
@@ -1,38 +1,14 @@
1
1
  import { StorefrontAPIClient, StorefrontAPIConfig } from "./client";
2
- import type { components } from "../types/storefront";
2
+ import { 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>;
7
+ constructor(config: StorefrontAPIConfig);
26
8
  /**
27
9
  * Get anonymous token for guest users
28
10
  */
29
- getAnonymousToken(options?: {
30
- apiKey?: string;
31
- }): Promise<{
32
- access_token: string;
33
- refresh_token: string;
34
- user: components["schemas"]["AnonymousUser"];
35
- }>;
11
+ getAnonymousToken(): Promise<ApiResult<GetAnonymousTokenContent>>;
36
12
  /**
37
13
  * Login with phone number
38
14
  *
@@ -41,10 +17,16 @@ export declare class AuthClient extends StorefrontAPIClient {
41
17
  * @param registerIfNotExists - Whether to register if user doesn't exist
42
18
  * @returns Promise with OTP token and action
43
19
  */
44
- loginWithPhone(phoneNumber: string, countryCode?: string, registerIfNotExists?: boolean): Promise<{
45
- otp_token: string;
46
- otp_action: string;
47
- }>;
20
+ loginWithPhone(body: LoginWithPhoneBody): Promise<ApiResult<LoginWithPhoneContent>>;
21
+ /**
22
+ * Login with WhatsApp
23
+ *
24
+ * @param phoneNumber - Phone number (without country code)
25
+ * @param countryCode - Country code (defaults to +91)
26
+ * @param registerIfNotExists - Whether to register if user doesn't exist
27
+ * @returns Promise with OTP token and action
28
+ */
29
+ loginWithWhatsApp(body: LoginWithWhatsappBody): Promise<ApiResult<LoginWithWhatsappContent>>;
48
30
  /**
49
31
  * Login with email
50
32
  *
@@ -52,24 +34,37 @@ export declare class AuthClient extends StorefrontAPIClient {
52
34
  * @param registerIfNotExists - Whether to register if user doesn't exist
53
35
  * @returns Promise with OTP token and action
54
36
  */
55
- loginWithEmail(email: string, registerIfNotExists?: boolean): Promise<{
56
- otp_token: string;
57
- otp_action: string;
58
- }>;
37
+ loginWithEmail(body: LoginWithEmailBody): Promise<ApiResult<LoginWithEmailContent>>;
59
38
  /**
60
39
  * Login with password
61
40
  *
62
41
  * @param credentials - Login credentials
63
42
  * @returns Promise with user info and tokens
64
43
  */
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
- }>;
44
+ loginWithPassword(body: LoginWithPasswordBody): Promise<ApiResult<LoginWithPasswordContent>>;
45
+ /**
46
+ * Forgot password
47
+ *
48
+ * @param email - Email address
49
+ * @returns Promise with user info and tokens
50
+ */
51
+ forgotPassword(body: ForgotPasswordBody): Promise<ApiResult<ForgotPasswordContent>>;
52
+ /**
53
+ * Reset password
54
+ *
55
+ * @param email - Email address
56
+ * @returns Promise with user info and tokens
57
+ */
58
+ resetPassword(body: ResetPasswordBody): Promise<ApiResult<ResetPasswordContent>>;
59
+ /**
60
+ * Change password
61
+ *
62
+ * @param oldPassword - Old password
63
+ * @param newPassword - New password
64
+ * @param newPasswordConfirmation - New password confirmation
65
+ * @returns Promise with new access token and refresh token
66
+ */
67
+ changePassword(body: ChangePasswordBody): Promise<ApiResult<ChangePasswordContent>>;
73
68
  /**
74
69
  * Verify OTP
75
70
  *
@@ -78,199 +73,115 @@ export declare class AuthClient extends StorefrontAPIClient {
78
73
  * @param otpAction - OTP action from login request
79
74
  * @returns Promise with user info and tokens
80
75
  */
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
- }>;
76
+ verifyOtp(body: VerifyOtpBody): Promise<ApiResult<VerifyOtpContent>>;
86
77
  /**
87
78
  * Register with phone
88
79
  *
89
80
  * @param options - Registration details
90
81
  * @returns Promise with user info and tokens
91
82
  */
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
- }>;
83
+ registerWithPhone(body: RegisterWithPhoneBody): Promise<ApiResult<RegisterWithPhoneContent>>;
104
84
  /**
105
85
  * Register with email
106
86
  *
107
87
  * @param options - Registration details
108
88
  * @returns Promise with user info and tokens
109
89
  */
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
- }>;
90
+ registerWithEmail(body: RegisterWithEmailBody): Promise<ApiResult<RegisterWithEmailContent>>;
121
91
  /**
122
92
  * Refresh the access token using a refresh token
93
+ * @param refreshToken - The refresh token to use for refreshing the access token
94
+ * @returns Promise with the new access token and refresh token
123
95
  */
124
- refreshToken(refreshToken: string): Promise<{
125
- access_token: string;
126
- refresh_token: string;
127
- }>;
96
+ refreshToken(body: RefreshTokenBody): Promise<ApiResult<RefreshTokenContent>>;
128
97
  /**
129
98
  * Logout
130
99
  *
131
100
  * @returns Promise that resolves when logout is complete
132
101
  */
133
- logout(): Promise<void>;
102
+ logout(): Promise<ApiResult<LogoutContent>>;
134
103
  /**
135
- * Override the base client's attemptTokenRefresh method
136
- * to implement token refresh for 401 errors
104
+ * Get user details
137
105
  *
138
- * @returns Promise that resolves to true if token was refreshed, false otherwise
106
+ * @param userId - User ID
107
+ * @returns Promise with user details
139
108
  */
140
- protected attemptTokenRefresh(): Promise<boolean>;
109
+ getUserDetails(pathParams: GetUserDetailPathParams): Promise<ApiResult<GetUserDetailContent>>;
141
110
  /**
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
111
+ * Update user details
145
112
  *
146
- * @param requestFn - Function that executes the API request
147
- * @returns Promise with the API response
113
+ * @param userId - User ID
114
+ * @returns Promise with user details
148
115
  */
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
- });
116
+ updateUserDetails(pathParams: UpdateUserPathParams, body: UpdateUserBody): Promise<ApiResult<UpdateUserContent>>;
117
+ /**
118
+ * Add profile image
119
+ *
120
+ * @param userId - User ID
121
+ * @returns Promise with user details
122
+ */
123
+ addProfileImage(pathParams: AddProfileImagePathParams, formData: AddProfileImageFormData): Promise<ApiResult<AddProfileImageContent>>;
214
124
  /**
215
- * Get value from cookies
216
- * Works in both browser and Next.js server components
125
+ * Update profile image
126
+ *
127
+ * @param userId - User ID
128
+ * @returns Promise with user details
217
129
  */
218
- getItem(key: string): Promise<string | null>;
130
+ updateProfileImage(pathParams: UpdateProfileImagePathParams, formData: UpdateProfileImageFormData): Promise<ApiResult<UpdateProfileImageContent>>;
219
131
  /**
220
- * Set value in cookies
221
- * Works in browser environment
132
+ * Delete profile image
133
+ *
134
+ * @param userId - User ID
135
+ * @returns Promise with user details
222
136
  */
223
- setItem(key: string, value: string): Promise<void>;
137
+ deleteProfileImage(pathParams: RemoveProfileImagePathParams): Promise<ApiResult<RemoveProfileImageResponse>>;
224
138
  /**
225
- * Remove value from cookies
139
+ * Get profile image
140
+ *
141
+ * @param userId - User ID
142
+ * @returns Promise with user details
226
143
  */
227
- removeItem(key: string): Promise<void>;
144
+ getProfileImage(pathParams: GetProfileImagePathParams): Promise<ApiResult<GetProfileImageContent>>;
228
145
  /**
229
- * Get cart ID from cookies
146
+ * Deactivate user account
147
+ *
148
+ * @param userId - User ID
149
+ * @returns Promise with user details
230
150
  */
231
- getCartId(): Promise<string | null>;
151
+ deactivateUserAccount(pathParams: DeactivateUserPathParams): Promise<ApiResult<DeactivateUserResponse>>;
232
152
  /**
233
- * Set cart ID in cookies
153
+ * Get user notification preferences
154
+ *
155
+ * @param userId - User ID
156
+ * @returns Promise with user details
234
157
  */
235
- setCartId(cartId: string): Promise<void>;
158
+ getUserNotificationPreferences(pathParams: GetNotificationPreferencesPathParams): Promise<ApiResult<GetNotificationPreferencesContent>>;
236
159
  /**
237
- * Clear cart ID from cookies
160
+ * Update user notification preferences
161
+ *
162
+ * @param userId - User ID
163
+ * @returns Promise with user details
238
164
  */
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>;
165
+ updateUserNotificationPreferences(pathParams: UpdateNotificationPreferencesPathParams, body: UpdateNotificationPreferencesBody): Promise<ApiResult<UpdateNotificationPreferencesContent>>;
255
166
  /**
256
- * Get cart ID from Next.js cookies
167
+ * Create user notification preference
168
+ *
169
+ * @param userId - User ID
170
+ * @returns Promise with user details
257
171
  */
258
- getCartId(): Promise<string | null>;
172
+ createUserNotificationPreference(pathParams: CreateNotificationPreferencesPathParams, body: CreateNotificationPreferencesBody): Promise<ApiResult<CreateNotificationPreferencesContent>>;
259
173
  /**
260
- * Set cart ID in Next.js cookies
174
+ * Generate OTP
175
+ *
176
+ * @param body - OTP generation body
177
+ * @returns Promise with OTP generation response
261
178
  */
262
- setCartId(cartId: string): Promise<void>;
179
+ generateOtp(body: GenerateOtpBody): Promise<ApiResult<GenerateOtpContent>>;
263
180
  /**
264
- * Clear cart ID from Next.js cookies
181
+ * Check whether email or phone is already verified
182
+ *
183
+ * @param body - OTP generation body
184
+ * @returns Promise with OTP generation response
265
185
  */
266
- clearCartId(): Promise<void>;
186
+ checkEmailOrPhoneIsVerified(body: CheckVerificationStatusBody): Promise<ApiResult<CheckVerificationStatusContent>>;
267
187
  }
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;