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