@amaster.ai/client 1.1.0-beta.5 → 1.1.0-beta.50

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,43 +1,33 @@
1
1
  /**
2
- * ============================================================================
3
- * Authentication Module - Unified Entry Point
4
- * ============================================================================
2
+ * Authentication module - provides user authentication and management capabilities.
5
3
  *
6
- * This module provides comprehensive authentication capabilities.
7
- * Import from submodules for better tree-shaking and AI readability:
8
- *
9
- * - `@amaster.ai/client/auth/user` - User types
10
- * - `@amaster.ai/client/auth/password-auth` - Password login/register
11
- * - `@amaster.ai/client/auth/code-auth` - Verification code login
12
- * - `@amaster.ai/client/auth/oauth` - OAuth & social login
13
- * - `@amaster.ai/client/auth/permissions` - Permission checking helpers
14
- * - `@amaster.ai/client/auth/profile` - User profile management
4
+ * This module exports all auth-related types for convenient importing:
15
5
  *
16
6
  * @example
17
- * Import complete API:
18
- * ```typescript
19
- * import type { AuthClientAPI } from '@amaster.ai/client/auth';
20
- * ```
7
+ * // Import specific types
8
+ * import type { LoginParams, User, OAuthProvider } from '@amaster.ai/client/auth';
21
9
  *
22
10
  * @example
23
- * Import specific types (recommended for AI):
24
- * ```typescript
25
- * import type { LoginParams, User } from '@amaster.ai/client/auth/password-auth';
26
- * import type { hasPermission } from '@amaster.ai/client/auth/permissions';
27
- * import type { OAuthProvider } from '@amaster.ai/client/auth/oauth';
28
- * ```
11
+ * // Import multiple types
12
+ * import type {
13
+ * LoginParams,
14
+ * RegisterParams,
15
+ * User,
16
+ * Role,
17
+ * Permission
18
+ * } from '@amaster.ai/client/auth';
29
19
  *
30
20
  * @module auth
21
+ * @since 1.0.0
31
22
  */
32
23
 
33
24
  import type { ClientResult } from '../common';
34
25
 
35
- // Re-export all submodule types
26
+ // Re-export all submodule types for convenient importing
36
27
  export * from './user';
37
28
  export * from './password-auth';
38
29
  export * from './code-auth';
39
30
  export * from './oauth';
40
- export * from './permissions';
41
31
  export * from './profile';
42
32
 
43
33
  // Import for unified API
@@ -77,37 +67,7 @@ export interface SuccessResponse {
77
67
  *
78
68
  * Combines all authentication capabilities into a single interface.
79
69
  * All methods return a `ClientResult<T>` for consistent error handling.
80
- *
81
- * @example
82
- * Complete authentication flow:
83
- * ```typescript
84
- * const client = createClient({ baseURL: 'https://api.amaster.ai' });
85
- *
86
- * // 1. Register new user
87
- * await client.auth.register({
88
- * email: 'user@example.com',
89
- * password: 'Password@123',
90
- * displayName: 'John Doe'
91
- * });
92
- *
93
- * // 2. Login (token is automatically stored and attached to all requests)
94
- * await client.auth.login({
95
- * email: 'user@example.com',
96
- * password: 'Password@123'
97
- * });
98
- *
99
- * // 3. Get current user info
100
- * const userResult = await client.auth.getMe();
101
- * console.log(userResult.data);
102
- *
103
- * // 4. Update profile
104
- * await client.auth.updateProfile({
105
- * displayName: 'Jane Doe'
106
- * });
107
- *
108
- * // 5. Logout
109
- * await client.auth.logout();
110
- * ```
70
+ *
111
71
  */
112
72
  export interface AuthClientAPI
113
73
  extends PasswordAuthAPI,
@@ -122,22 +82,7 @@ export interface AuthClientAPI
122
82
  * All subsequent requests will be unauthenticated until login again.
123
83
  *
124
84
  * @returns Success status
125
- *
126
- * @example
127
- * ```typescript
128
- * await client.auth.logout();
129
- * console.log('Logged out successfully');
130
- *
131
- * // Token is cleared, requests are now unauthenticated
132
- * console.log(client.isAuthenticated()); // false
133
- * ```
134
- *
135
- * @example
136
- * Logout with redirect:
137
- * ```typescript
138
- * await client.auth.logout();
139
- * window.location.href = '/login';
140
- * ```
85
+ *
141
86
  */
142
87
  logout(): Promise<ClientResult<SuccessResponse>>;
143
88
 
@@ -149,32 +94,45 @@ export interface AuthClientAPI
149
94
  * This method is exposed for manual refresh scenarios.
150
95
  *
151
96
  * @returns New access token
97
+ *
98
+ */
99
+ refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
100
+
101
+ // ==================== Permission Checks ====================
102
+
103
+ /**
104
+ * Check if current user has a specific role (local check, fast)
152
105
  *
153
- * @example
154
- * ```typescript
155
- * const result = await client.auth.refreshToken();
156
- * if (result.data) {
157
- * console.log('Token refreshed:', result.data.accessToken);
158
- * }
159
- * ```
106
+ * Works for both authenticated and anonymous users.
107
+ * Checks against locally cached user roles.
160
108
  *
161
- * @example
162
- * Manual token refresh on 401:
163
- * ```typescript
164
- * try {
165
- * const result = await client.entity.list('default', 'users');
166
- * if (result.status === 401) {
167
- * // Try to refresh token
168
- * const refreshResult = await client.auth.refreshToken();
169
- * if (refreshResult.data) {
170
- * // Retry original request
171
- * return await client.entity.list('default', 'users');
172
- * }
173
- * }
174
- * } catch (error) {
175
- * // Handle error
176
- * }
177
- * ```
109
+ * @param roleCode - Role code to check (e.g., "admin", "user", "anonymous")
110
+ * @returns True if user has the role
111
+ *
178
112
  */
179
- refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
113
+ hasRole(roleCode: string): boolean;
114
+
115
+ /**
116
+ * Check if current user has a specific permission (local check, fast)
117
+ *
118
+ * Works for both authenticated and anonymous users.
119
+ * Checks against locally cached user permissions.
120
+ *
121
+ * @param resource - Resource name (e.g., "user", "order")
122
+ * @param action - Action name (e.g., "read", "write", "delete")
123
+ * @returns True if user has the permission
124
+ *
125
+ */
126
+ hasPermission(resource: string, action: string): boolean;
127
+
128
+ /**
129
+ * Check if current user is anonymous (not authenticated)
130
+ *
131
+ * Convenience method equivalent to `hasRole('anonymous')`.
132
+ * Returns true if user has the 'anonymous' role.
133
+ *
134
+ * @returns True if user is anonymous
135
+ *
136
+ */
137
+ isAnonymous(): boolean;
180
138
  }
@@ -1,9 +1,5 @@
1
1
  /**
2
- * ============================================================================
3
- * OAuth & Social Login - Type Definitions
4
- * ============================================================================
5
- *
6
- * OAuth and social authentication including:
2
+ * * OAuth and social authentication including:
7
3
  * - GitHub OAuth
8
4
  * - Google OAuth
9
5
  * - WeChat OAuth (Web & Mini Program)
@@ -30,30 +26,7 @@ export type OAuthProvider =
30
26
 
31
27
  /**
32
28
  * OAuth login parameters
33
- *
34
- * @example
35
- * GitHub OAuth:
36
- * ```typescript
37
- * // 1. Get OAuth URL
38
- * const url = await client.auth.getOAuthUrl({
39
- * provider: 'github',
40
- * redirectUri: 'https://app.example.com/auth/callback'
41
- * });
42
- *
43
- * // 2. Redirect user to OAuth provider
44
- * window.location.href = url.data.authUrl;
45
- *
46
- * // 3. Handle callback
47
- * // User is redirected back with code in URL params
48
- * const params = new URLSearchParams(window.location.search);
49
- * const code = params.get('code');
50
- *
51
- * // 4. Complete OAuth login
52
- * const result = await client.auth.oauthLogin({
53
- * provider: 'github',
54
- * code: code!
55
- * });
56
- * ```
29
+ *
57
30
  */
58
31
  export interface OAuthLoginParams {
59
32
  /** OAuth provider */
@@ -101,23 +74,7 @@ export interface OAuthUrlResponse {
101
74
 
102
75
  /**
103
76
  * WeChat Mini Program login parameters
104
- *
105
- * @example
106
- * ```typescript
107
- * // 1. Get code from WeChat API
108
- * wx.login({
109
- * success: async (res) => {
110
- * // 2. Login with code
111
- * const result = await client.auth.wechatMiniProgramLogin({
112
- * code: res.code
113
- * });
114
- *
115
- * if (result.data) {
116
- * console.log('Logged in:', result.data.user);
117
- * }
118
- * }
119
- * });
120
- * ```
77
+ *
121
78
  */
122
79
  export interface WeChatMiniProgramLoginParams {
123
80
  /** WeChat login code */
@@ -158,30 +115,7 @@ export interface OAuthAPI {
158
115
  *
159
116
  * @param params - OAuth provider and redirect URI
160
117
  * @returns Authorization URL and state
161
- *
162
- * @example
163
- * GitHub OAuth flow:
164
- * ```typescript
165
- * const result = await client.auth.getOAuthUrl({
166
- * provider: 'github',
167
- * redirectUri: 'https://app.example.com/auth/callback'
168
- * });
169
- *
170
- * if (result.data) {
171
- * // Redirect user to GitHub login
172
- * window.location.href = result.data.authUrl;
173
- * }
174
- * ```
175
- *
176
- * @example
177
- * Google OAuth with custom scopes:
178
- * ```typescript
179
- * const result = await client.auth.getOAuthUrl({
180
- * provider: 'google',
181
- * redirectUri: 'https://app.example.com/auth/callback',
182
- * scopes: ['email', 'profile', 'https://www.googleapis.com/auth/calendar']
183
- * });
184
- * ```
118
+ *
185
119
  */
186
120
  getOAuthUrl(params: GetOAuthUrlParams): Promise<ClientResult<OAuthUrlResponse>>;
187
121
 
@@ -192,42 +126,7 @@ export interface OAuthAPI {
192
126
  *
193
127
  * @param params - OAuth provider and authorization code
194
128
  * @returns User info and access token
195
- *
196
- * @example
197
- * Complete GitHub OAuth:
198
- * ```typescript
199
- * // After user is redirected back from GitHub
200
- * const params = new URLSearchParams(window.location.search);
201
- * const code = params.get('code');
202
- *
203
- * const result = await client.auth.oauthLogin({
204
- * provider: 'github',
205
- * code: code!,
206
- * redirectUri: 'https://app.example.com/auth/callback'
207
- * });
208
- *
209
- * if (result.data) {
210
- * console.log('Logged in as:', result.data.user.displayName);
211
- * // Token is now automatically attached to all requests
212
- * }
213
- * ```
214
- *
215
- * @example
216
- * With error handling:
217
- * ```typescript
218
- * const result = await client.auth.oauthLogin({
219
- * provider: 'google',
220
- * code: authCode
221
- * });
222
- *
223
- * if (result.error) {
224
- * if (result.status === 400) {
225
- * console.error('Invalid or expired authorization code');
226
- * } else {
227
- * console.error('OAuth login failed:', result.error.message);
228
- * }
229
- * }
230
- * ```
129
+ *
231
130
  */
232
131
  oauthLogin(params: OAuthLoginParams): Promise<ClientResult<LoginResponse>>;
233
132
 
@@ -238,43 +137,7 @@ export interface OAuthAPI {
238
137
  *
239
138
  * @param params - WeChat login code
240
139
  * @returns User info and access token
241
- *
242
- * @example
243
- * ```typescript
244
- * // In WeChat Mini Program
245
- * wx.login({
246
- * success: async (res) => {
247
- * const result = await client.auth.wechatMiniProgramLogin({
248
- * code: res.code
249
- * });
250
- *
251
- * if (result.data) {
252
- * wx.setStorageSync('token', result.data.accessToken);
253
- * wx.showToast({ title: 'Login successful!' });
254
- * }
255
- * }
256
- * });
257
- * ```
258
- *
259
- * @example
260
- * With user info:
261
- * ```typescript
262
- * wx.login({
263
- * success: async (loginRes) => {
264
- * wx.getUserInfo({
265
- * success: async (userRes) => {
266
- * await client.auth.wechatMiniProgramLogin({
267
- * code: loginRes.code,
268
- * userInfo: {
269
- * encryptedData: userRes.encryptedData,
270
- * iv: userRes.iv
271
- * }
272
- * });
273
- * }
274
- * });
275
- * }
276
- * });
277
- * ```
140
+ *
278
141
  */
279
142
  wechatMiniProgramLogin(params: WeChatMiniProgramLoginParams): Promise<ClientResult<LoginResponse>>;
280
143
  }
@@ -1,9 +1,5 @@
1
1
  /**
2
- * ============================================================================
3
- * Password Authentication - Type Definitions
4
- * ============================================================================
5
- *
6
- * Password-based authentication including:
2
+ * * Password-based authentication including:
7
3
  * - Email/username/phone + password login
8
4
  * - User registration
9
5
  * - Password change
@@ -20,45 +16,7 @@ import type { User } from './user';
20
16
  * User registration parameters
21
17
  *
22
18
  * At least one identifier (username, email, or phone) must be provided.
23
- *
24
- * @example
25
- * Register with email:
26
- * ```typescript
27
- * await client.auth.register({
28
- * email: 'user@example.com',
29
- * password: 'Password@123',
30
- * displayName: 'John Doe'
31
- * });
32
- * ```
33
- *
34
- * @example
35
- * Register with username and email:
36
- * ```typescript
37
- * await client.auth.register({
38
- * username: 'johndoe',
39
- * email: 'john@example.com',
40
- * password: 'Password@123',
41
- * displayName: 'John Doe'
42
- * });
43
- * ```
44
- *
45
- * @example
46
- * Register with captcha verification:
47
- * ```typescript
48
- * // 1. Get captcha
49
- * const captcha = await client.auth.getCaptcha();
50
- *
51
- * // 2. Show captcha.data.captchaImage to user
52
- * const userInput = prompt('Enter captcha:');
53
- *
54
- * // 3. Register with captcha
55
- * await client.auth.register({
56
- * username: 'johndoe',
57
- * email: 'john@example.com',
58
- * password: 'Password@123',
59
- * captcha: `${captcha.data.captchaId}:${userInput}`
60
- * });
61
- * ```
19
+ *
62
20
  */
63
21
  export interface RegisterParams {
64
22
  /** Username (optional, but one of username/email/phone required) */
@@ -82,7 +40,6 @@ export interface RegisterParams {
82
40
  /**
83
41
  * Captcha verification (optional)
84
42
  * Format: "captchaId:userInput"
85
- * @example "abc-123:XY7K"
86
43
  */
87
44
  captcha?: string;
88
45
  }
@@ -97,34 +54,7 @@ export type LoginType = 'username' | 'email' | 'phone';
97
54
  *
98
55
  * The `loginType` field is optional - it will be auto-detected based on
99
56
  * which identifier field (username/email/phone) you provide.
100
- *
101
- * @example
102
- * Login with email (auto-detect):
103
- * ```typescript
104
- * await client.auth.login({
105
- * email: 'user@example.com',
106
- * password: 'Password@123'
107
- * });
108
- * ```
109
- *
110
- * @example
111
- * Login with username (explicit type):
112
- * ```typescript
113
- * await client.auth.login({
114
- * loginType: 'username',
115
- * username: 'johndoe',
116
- * password: 'Password@123'
117
- * });
118
- * ```
119
- *
120
- * @example
121
- * Login with phone:
122
- * ```typescript
123
- * await client.auth.login({
124
- * phone: '+86-13800138000',
125
- * password: 'Password@123'
126
- * });
127
- * ```
57
+ *
128
58
  */
129
59
  export interface LoginParams {
130
60
  /** Login method (optional, auto-detected if not provided) */
@@ -145,18 +75,7 @@ export interface LoginParams {
145
75
 
146
76
  /**
147
77
  * Change password parameters
148
- *
149
- * @example
150
- * ```typescript
151
- * const result = await client.auth.changePassword({
152
- * oldPassword: 'OldPassword@123',
153
- * newPassword: 'NewPassword@456'
154
- * });
155
- *
156
- * if (result.data?.success) {
157
- * console.log('Password changed successfully');
158
- * }
159
- * ```
78
+ *
160
79
  */
161
80
  export interface ChangePasswordParams {
162
81
  /** Current password (for verification) */
@@ -173,23 +92,7 @@ export interface ChangePasswordParams {
173
92
 
174
93
  /**
175
94
  * Login/Registration response with user info and access token
176
- *
177
- * @example
178
- * ```typescript
179
- * const result = await client.auth.login({
180
- * email: 'user@example.com',
181
- * password: 'password123'
182
- * });
183
- *
184
- * if (result.data) {
185
- * const { user, accessToken } = result.data;
186
- * console.log(`Welcome ${user.displayName}!`);
187
- * console.log(`Token: ${accessToken}`);
188
- *
189
- * // All subsequent requests now include this token automatically
190
- * await client.entity.list('default', 'users');
191
- * }
192
- * ```
95
+ *
193
96
  */
194
97
  export interface LoginResponse {
195
98
  /** User information */
@@ -222,6 +125,8 @@ export interface SuccessResponse {
222
125
  * Password Authentication API
223
126
  *
224
127
  * Methods for password-based authentication and account management.
128
+ *
129
+ * @since 1.0.0
225
130
  */
226
131
  export interface PasswordAuthAPI {
227
132
  /**
@@ -234,30 +139,25 @@ export interface PasswordAuthAPI {
234
139
  * @returns User info and access token (if auto-login enabled)
235
140
  *
236
141
  * @example
237
- * Basic registration:
238
- * ```typescript
142
+ * // Register with email
239
143
  * const result = await client.auth.register({
240
144
  * email: 'user@example.com',
241
- * password: 'Password@123',
145
+ * password: 'SecurePass123',
242
146
  * displayName: 'John Doe'
243
147
  * });
244
148
  *
245
- * if (result.data) {
246
- * console.log('Registration successful!');
247
- * console.log('User:', result.data.user);
149
+ * if (result.success) {
150
+ * console.log('User registered:', result.data.user.uid);
248
151
  * }
249
- * ```
250
152
  *
251
153
  * @example
252
- * Registration with username:
253
- * ```typescript
254
- * await client.auth.register({
154
+ * // Register with username
155
+ * const result = await client.auth.register({
255
156
  * username: 'johndoe',
256
- * email: 'john@example.com',
257
- * password: 'Password@123',
258
- * displayName: 'John Doe'
157
+ * password: 'SecurePass123'
259
158
  * });
260
- * ```
159
+ *
160
+ * @since 1.0.0
261
161
  */
262
162
  register(params: RegisterParams): Promise<ClientResult<LoginResponse>>;
263
163
 
@@ -271,30 +171,33 @@ export interface PasswordAuthAPI {
271
171
  * @returns User info and access token
272
172
  *
273
173
  * @example
274
- * Login with email:
275
- * ```typescript
174
+ * // Login with email
276
175
  * const result = await client.auth.login({
277
176
  * email: 'user@example.com',
278
- * password: 'Password@123'
177
+ * password: 'myPassword123'
279
178
  * });
280
179
  *
281
- * if (result.data) {
282
- * console.log('Welcome', result.data.user.displayName);
283
- * // Token is now automatically attached to all requests
180
+ * if (result.success) {
181
+ * console.log('Welcome,', result.data.user.displayName);
284
182
  * } else {
285
- * console.error('Login failed:', result.error?.message);
183
+ * console.error('Login failed:', result.error.message);
286
184
  * }
287
- * ```
288
185
  *
289
186
  * @example
290
- * Login with username:
291
- * ```typescript
292
- * await client.auth.login({
293
- * loginType: 'username',
187
+ * // Login with username
188
+ * const result = await client.auth.login({
294
189
  * username: 'johndoe',
295
- * password: 'Password@123'
190
+ * password: 'myPassword123'
191
+ * });
192
+ *
193
+ * @example
194
+ * // Login with phone
195
+ * const result = await client.auth.login({
196
+ * phone: '+1234567890',
197
+ * password: 'myPassword123'
296
198
  * });
297
- * ```
199
+ *
200
+ * @since 1.0.0
298
201
  */
299
202
  login(params: LoginParams): Promise<ClientResult<LoginResponse>>;
300
203
 
@@ -308,18 +211,16 @@ export interface PasswordAuthAPI {
308
211
  * @returns Success status
309
212
  *
310
213
  * @example
311
- * ```typescript
312
214
  * const result = await client.auth.changePassword({
313
- * oldPassword: 'OldPassword@123',
314
- * newPassword: 'NewPassword@456'
215
+ * oldPassword: 'currentPass123',
216
+ * newPassword: 'newSecurePass456'
315
217
  * });
316
218
  *
317
- * if (result.data?.success) {
219
+ * if (result.success) {
318
220
  * console.log('Password changed successfully');
319
- * } else {
320
- * console.error('Failed:', result.error?.message);
321
221
  * }
322
- * ```
222
+ *
223
+ * @since 1.0.0
323
224
  */
324
225
  changePassword(params: ChangePasswordParams): Promise<ClientResult<SuccessResponse>>;
325
226
  }