@amaster.ai/auth-client 1.0.0-beta.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.
@@ -0,0 +1,449 @@
1
+ /**
2
+ * ============================================================================
3
+ * @amaster.ai/auth-client - Type Definitions
4
+ * ============================================================================
5
+ *
6
+ * πŸ€– AI NAVIGATION - Read these files based on your task:
7
+ *
8
+ * 1. Need LOGIN/REGISTER/LOGOUT? β†’ Read: ./auth.d.ts
9
+ * 2. Need PERMISSION checks? β†’ Read: ./permissions.d.ts
10
+ * 3. Need USER profile management? β†’ Read: ./user.d.ts
11
+ * 4. Need OAUTH binding? β†’ Read: ./oauth.d.ts
12
+ * 5. Need SESSION management? β†’ Read: ./sessions.d.ts
13
+ *
14
+ * ============================================================================
15
+ */
16
+ /**
17
+ * Authentication SDK Types
18
+ */
19
+ /**
20
+ * Authentication client configuration options
21
+ *
22
+ * @example
23
+ * Minimal configuration (recommended):
24
+ * ```typescript
25
+ * const options: AuthClientOptions = {
26
+ * onTokenExpired: () => window.location.href = "/login",
27
+ * };
28
+ * ```
29
+ *
30
+ * @example
31
+ * Full configuration:
32
+ * ```typescript
33
+ * const options: AuthClientOptions = {
34
+ * baseURL: "https://api.example.com",
35
+ * storage: "sessionStorage",
36
+ * onTokenExpired: () => handleTokenExpired(),
37
+ * onUnauthorized: () => handleUnauthorized(),
38
+ * };
39
+ * ```
40
+ */
41
+ interface AuthClientOptions {
42
+ /**
43
+ * API base URL (defaults to window.location.origin)
44
+ * @example "https://api.example.com"
45
+ */
46
+ baseURL?: string;
47
+ /**
48
+ * Token storage type (defaults to "localStorage")
49
+ * - localStorage: Persistent across browser sessions
50
+ * - sessionStorage: Cleared when browser tab closes
51
+ */
52
+ storage?: "localStorage" | "sessionStorage";
53
+ /**
54
+ * Callback when token expires
55
+ * @example () => window.location.href = "/login"
56
+ */
57
+ onTokenExpired?: () => void;
58
+ /**
59
+ * Callback when server returns 401 Unauthorized
60
+ * @example () => alert("Session expired")
61
+ */
62
+ onUnauthorized?: () => void;
63
+ }
64
+ /**
65
+ * User information with roles and permissions
66
+ *
67
+ * Note: This is an optimized format for client-side use.
68
+ * - roles: Only role codes (e.g., ["admin", "user"])
69
+ * - permissions: Only permission names (e.g., ["user.read", "user.write"])
70
+ * - dataScopes: Not included, use getPermissionScope() to load on-demand
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const user: User = {
75
+ * uid: "123",
76
+ * email: "user@example.com",
77
+ * displayName: "John Doe",
78
+ * roles: ["admin", "user"],
79
+ * permissions: ["user.read", "user.write", "order.read"],
80
+ * // ... other fields
81
+ * };
82
+ *
83
+ * // Check permissions
84
+ * if (user.permissions.includes("user.delete")) {
85
+ * // Can delete users
86
+ * }
87
+ * ```
88
+ */
89
+ interface User {
90
+ /** Unique user ID */
91
+ uid: string;
92
+ /** Username (null if not set) */
93
+ username: string | null;
94
+ /** Email address (null if not set) */
95
+ email: string | null;
96
+ /** Phone number (null if not set) */
97
+ phone: string | null;
98
+ /** Display name for UI */
99
+ displayName: string | null;
100
+ /** Avatar image URL */
101
+ avatarUrl: string | null;
102
+ /** Whether account is active */
103
+ isActive: boolean;
104
+ /** Whether email is verified */
105
+ emailVerified: boolean;
106
+ /** Whether phone is verified */
107
+ phoneVerified: boolean;
108
+ /** Email verification timestamp */
109
+ emailVerifiedAt: string | null;
110
+ /** Phone verification timestamp */
111
+ phoneVerifiedAt: string | null;
112
+ /**
113
+ * Role codes assigned to user
114
+ * @example ["admin", "user", "manager"]
115
+ */
116
+ roles: string[];
117
+ /**
118
+ * Permission names granted to user
119
+ * @example ["user.read", "user.write", "order.read"]
120
+ */
121
+ permissions: string[];
122
+ /** Account creation timestamp */
123
+ createdAt: string;
124
+ /** Last update timestamp */
125
+ updatedAt: string;
126
+ }
127
+ /** Detailed role information (for admin/management use) */
128
+ interface RoleDetail {
129
+ id: number;
130
+ code: string;
131
+ displayName: string;
132
+ description?: string;
133
+ isSystem: boolean;
134
+ }
135
+ /** Detailed permission information (for admin/management use) */
136
+ interface PermissionDetail {
137
+ id: number;
138
+ name: string;
139
+ resource: string;
140
+ action: string;
141
+ description?: string;
142
+ sourceType: "system" | "role" | "direct";
143
+ }
144
+ /** @deprecated Use string[] for roles in User type */
145
+ interface Role {
146
+ id: number;
147
+ code: string;
148
+ displayName: string;
149
+ description?: string;
150
+ isSystem: boolean;
151
+ }
152
+ /** @deprecated Use string[] for permissions in User type */
153
+ interface Permission {
154
+ id: number;
155
+ name: string;
156
+ resource: string;
157
+ action: string;
158
+ description?: string;
159
+ sourceType: "system" | "role" | "direct";
160
+ }
161
+ /**
162
+ * Data scope defining what data a user can access for a permission
163
+ *
164
+ * @example
165
+ * All data:
166
+ * ```typescript
167
+ * { scopeType: "all", scopeFilter: null }
168
+ * ```
169
+ *
170
+ * @example
171
+ * Department-scoped:
172
+ * ```typescript
173
+ * {
174
+ * scopeType: "department",
175
+ * scopeFilter: { departmentId: 123 }
176
+ * }
177
+ * ```
178
+ *
179
+ * @example
180
+ * Self only:
181
+ * ```typescript
182
+ * { scopeType: "self", scopeFilter: null }
183
+ * ```
184
+ */
185
+ interface DataScope {
186
+ /**
187
+ * Type of data scope:
188
+ * - "all": User can access all data
189
+ * - "organization": User can access data in their organization
190
+ * - "department": User can access data in their department
191
+ * - "self": User can only access their own data
192
+ * - "custom": Custom filter defined in scopeFilter
193
+ */
194
+ scopeType: "all" | "organization" | "department" | "self" | "custom";
195
+ /**
196
+ * Custom filter conditions (used when scopeType="custom")
197
+ * @example { departmentId: 123, regionId: 456 }
198
+ */
199
+ scopeFilter: any;
200
+ }
201
+ /**
202
+ * User registration parameters
203
+ * At least one of username/email/phone must be provided
204
+ *
205
+ * @example
206
+ * Register with email:
207
+ * ```typescript
208
+ * const params: RegisterParams = {
209
+ * email: "user@example.com",
210
+ * password: "Password@123",
211
+ * displayName: "John Doe",
212
+ * };
213
+ * ```
214
+ *
215
+ * @example
216
+ * Register with captcha:
217
+ * ```typescript
218
+ * const captcha = await authClient.getCaptcha();
219
+ * const userInput = "AB12"; // User enters this
220
+ * const params: RegisterParams = {
221
+ * email: "user@example.com",
222
+ * password: "Password@123",
223
+ * captcha: `${captcha.data.captchaId}:${userInput}`,
224
+ * };
225
+ * ```
226
+ */
227
+ interface RegisterParams {
228
+ /** Username (optional, at least one of username/email/phone required) */
229
+ username?: string;
230
+ /** Email address (optional, at least one of username/email/phone required) */
231
+ email?: string;
232
+ /** Phone number (optional, at least one of username/email/phone required) */
233
+ phone?: string;
234
+ /**
235
+ * Password (required)
236
+ * @example "Password@123"
237
+ */
238
+ password: string;
239
+ /**
240
+ * Display name for UI
241
+ * @example "John Doe"
242
+ */
243
+ displayName?: string;
244
+ /**
245
+ * Captcha verification (optional)
246
+ * Format: "captchaId:userInput"
247
+ * @example "uuid-123:AB12"
248
+ */
249
+ captcha?: string;
250
+ }
251
+ /**
252
+ * Login type: username, email, or phone
253
+ */
254
+ type LoginType = "username" | "email" | "phone";
255
+ /**
256
+ * Login parameters for password-based authentication
257
+ *
258
+ * @example
259
+ * Login with email:
260
+ * ```typescript
261
+ * const params: LoginParams = {
262
+ * loginType: "email",
263
+ * email: "user@example.com",
264
+ * password: "Password@123",
265
+ * };
266
+ * ```
267
+ *
268
+ * @example
269
+ * Login with username:
270
+ * ```typescript
271
+ * const params: LoginParams = {
272
+ * loginType: "username",
273
+ * username: "john_doe",
274
+ * password: "Password@123",
275
+ * };
276
+ * ```
277
+ */
278
+ interface LoginParams {
279
+ /** Login method: "username" | "email" | "phone" */
280
+ loginType: LoginType;
281
+ /** Username (required if loginType="username") */
282
+ username?: string;
283
+ /** Email (required if loginType="email") */
284
+ email?: string;
285
+ /** Phone (required if loginType="phone") */
286
+ phone?: string;
287
+ /** Password (always required) */
288
+ password: string;
289
+ }
290
+ type CodeLoginType = "email" | "phone";
291
+ interface CodeLoginParams {
292
+ loginType: CodeLoginType;
293
+ email?: string;
294
+ phone?: string;
295
+ code: string;
296
+ }
297
+ type SendCodeType = "email" | "phone";
298
+ interface SendCodeParams {
299
+ type: SendCodeType;
300
+ email?: string;
301
+ phone?: string;
302
+ }
303
+ /**
304
+ * Login response with user info and access token
305
+ * Both user and accessToken are required for successful login
306
+ *
307
+ * @example
308
+ * ```typescript
309
+ * const result = await authClient.login({ ... });
310
+ * if (result.data) {
311
+ * const { user, accessToken } = result.data;
312
+ * console.log(`Welcome ${user.displayName}`);
313
+ * console.log(`Token: ${accessToken}`);
314
+ * }
315
+ * ```
316
+ */
317
+ interface LoginResponse {
318
+ /** User information with roles and permissions */
319
+ user: User;
320
+ /** Access token (JWT) for API authentication */
321
+ accessToken: string;
322
+ /** Refresh token for obtaining new access tokens */
323
+ refreshToken?: string;
324
+ /** Token expiration time in seconds */
325
+ expiresIn?: number;
326
+ }
327
+ /**
328
+ * Register response - user and accessToken are optional
329
+ * depending on backend configuration:
330
+ * - Both returned: Auto-login after registration
331
+ * - Only user: Registration successful but requires email verification
332
+ * - Neither: Registration successful, user must login separately
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * const result = await authClient.register({ ... });
337
+ * if (result.data?.user && result.data?.accessToken) {
338
+ * console.log("Registered and logged in:", result.data.user);
339
+ * } else {
340
+ * console.log("Registered, please verify email or login manually");
341
+ * }
342
+ * ```
343
+ */
344
+ interface RegisterResponse {
345
+ /** User information with roles and permissions (optional) */
346
+ user?: User;
347
+ /** Access token (JWT) for API authentication (optional) */
348
+ accessToken?: string;
349
+ }
350
+ /**
351
+ * Captcha response with image and verification ID
352
+ *
353
+ * @example
354
+ * ```typescript
355
+ * const result = await authClient.getCaptcha();
356
+ * if (result.data) {
357
+ * // Display image to user
358
+ * document.getElementById("img").src = result.data.captchaImage;
359
+ *
360
+ * // After user inputs code, verify with:
361
+ * const captcha = `${result.data.captchaId}:${userInputCode}`;
362
+ * }
363
+ * ```
364
+ */
365
+ interface CaptchaResponse {
366
+ /** Unique captcha ID for verification */
367
+ captchaId: string;
368
+ /** Base64 encoded captcha image (data:image/png;base64,...) */
369
+ captchaImage: string;
370
+ /** Expiration time in seconds (typically 300 = 5 minutes) */
371
+ expiresIn: number;
372
+ }
373
+ interface RefreshTokenResponse {
374
+ accessToken: string;
375
+ }
376
+ interface UpdateMeParams {
377
+ displayName?: string;
378
+ avatarUrl?: string;
379
+ }
380
+ interface ChangePasswordParams {
381
+ oldPassword: string;
382
+ newPassword: string;
383
+ }
384
+ type OAuthProvider = "google" | "github" | "wechat" | "platform";
385
+ interface OAuthBinding {
386
+ provider: OAuthProvider;
387
+ providerId: string;
388
+ email: string;
389
+ displayName: string;
390
+ avatarUrl: string | null;
391
+ createdAt: string;
392
+ }
393
+ interface Session {
394
+ id: string;
395
+ userAgent: string;
396
+ ip: string;
397
+ lastActiveAt: string;
398
+ createdAt: string;
399
+ isCurrent: boolean;
400
+ }
401
+ type AuthEvent = "login" | "logout" | "tokenExpired" | "tokenRefreshed" | "unauthorized";
402
+ type EventHandler = (...args: any[]) => void;
403
+ /**
404
+ * Standard success response from backend
405
+ * @example
406
+ * ```typescript
407
+ * {
408
+ * statusCode: 200,
409
+ * message: "ζ“δ½œζˆεŠŸ",
410
+ * timestamp: "2026-02-02T08:17:11.045072301Z"
411
+ * }
412
+ * ```
413
+ */
414
+ interface SuccessResponse {
415
+ statusCode: number;
416
+ message?: string;
417
+ timestamp?: string;
418
+ }
419
+ /**
420
+ * Revoke all sessions response
421
+ * @example
422
+ * ```typescript
423
+ * {
424
+ * statusCode: 200,
425
+ * message: "ε·²ζ’€ι”€ζ‰€ζœ‰δΌšθ―",
426
+ * timestamp: "2026-02-02T08:17:11.045072301Z",
427
+ * revokedCount: 3
428
+ * }
429
+ * ```
430
+ */
431
+ interface RevokeAllSessionsResponse {
432
+ statusCode: number;
433
+ message?: string;
434
+ timestamp?: string;
435
+ revokedCount: number;
436
+ }
437
+ interface CheckPermissionResponse {
438
+ hasPermission: boolean;
439
+ dataScope?: DataScope;
440
+ }
441
+ interface BatchCheckPermissionsResponse {
442
+ [permission: string]: boolean;
443
+ }
444
+ interface PermissionScopeResponse {
445
+ permission: string;
446
+ dataScope: DataScope;
447
+ }
448
+
449
+ export type { AuthClientOptions as A, BatchCheckPermissionsResponse as B, ChangePasswordParams as C, DataScope as D, EventHandler as E, LoginParams as L, OAuthBinding as O, Permission as P, RefreshTokenResponse as R, SendCodeParams as S, UpdateMeParams as U, AuthEvent as a, CaptchaResponse as b, CheckPermissionResponse as c, CodeLoginParams as d, CodeLoginType as e, LoginResponse as f, LoginType as g, OAuthProvider as h, PermissionDetail as i, PermissionScopeResponse as j, RegisterParams as k, RegisterResponse as l, RevokeAllSessionsResponse as m, Role as n, RoleDetail as o, SendCodeType as p, Session as q, SuccessResponse as r, User as s };