@amaster.ai/client 1.1.1 → 1.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amaster.ai/client",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Unified API client for Amaster platform - All services in one package",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -72,16 +72,16 @@
72
72
  "registry": "https://registry.npmjs.org/"
73
73
  },
74
74
  "dependencies": {
75
- "@amaster.ai/s3-client": "1.1.1",
76
- "@amaster.ai/bpm-client": "1.1.1",
77
- "@amaster.ai/entity-client": "1.1.1",
78
- "@amaster.ai/asr-client": "1.1.1",
79
- "@amaster.ai/function-client": "1.1.1",
80
- "@amaster.ai/http-client": "1.1.1",
81
- "@amaster.ai/workflow-client": "1.1.1",
82
- "@amaster.ai/copilot-client": "1.1.1",
83
- "@amaster.ai/auth-client": "1.1.1",
84
- "@amaster.ai/tts-client": "1.1.1"
75
+ "@amaster.ai/bpm-client": "1.1.2",
76
+ "@amaster.ai/auth-client": "1.1.2",
77
+ "@amaster.ai/http-client": "1.1.2",
78
+ "@amaster.ai/asr-client": "1.1.2",
79
+ "@amaster.ai/s3-client": "1.1.2",
80
+ "@amaster.ai/copilot-client": "1.1.2",
81
+ "@amaster.ai/workflow-client": "1.1.2",
82
+ "@amaster.ai/entity-client": "1.1.2",
83
+ "@amaster.ai/function-client": "1.1.2",
84
+ "@amaster.ai/tts-client": "1.1.2"
85
85
  },
86
86
  "peerDependencies": {
87
87
  "axios": "^1.11.0"
@@ -27,7 +27,6 @@ describe('Type Tests', () => {
27
27
  data: { id: number; name: string } | null;
28
28
  error: ClientError | null;
29
29
  status: number;
30
- success: boolean;
31
30
  }>();
32
31
  });
33
32
 
@@ -37,21 +36,15 @@ describe('Type Tests', () => {
37
36
  expectTypeOf<UserResult['data']>().toEqualTypeOf<User | null>();
38
37
  });
39
38
 
40
- it('should have success field', () => {
41
- type Result = ClientResult<unknown>;
42
-
43
- expectTypeOf<Result['success']>().toEqualTypeOf<boolean>();
44
- });
45
39
  });
46
40
 
47
41
  describe('ClientError', () => {
48
42
  it('should have all required fields', () => {
49
43
  expectTypeOf<ClientError>().toMatchTypeOf<{
50
- status: number;
51
44
  message: string;
45
+ status?: number;
52
46
  code?: string;
53
47
  details?: unknown;
54
- timestamp?: string;
55
48
  }>();
56
49
  });
57
50
  });
@@ -94,6 +87,29 @@ describe('Type Tests', () => {
94
87
  expectTypeOf<AmasterClient>().toHaveProperty('setAccessToken');
95
88
  expectTypeOf<AmasterClient>().toHaveProperty('clearAuth');
96
89
  });
90
+
91
+ it('should expose the current auth client methods', () => {
92
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('getMiniProgramPhoneNumber');
93
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('loginWithMiniProgram');
94
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('handleOAuthCallback');
95
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('updateMe');
96
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('getSession');
97
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('getSessions');
98
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('revokeSession');
99
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('revokeAllSessions');
100
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('hasAnyPermission');
101
+ expectTypeOf<AmasterClient['auth']>().toHaveProperty('hasAllPermissions');
102
+ });
103
+
104
+ it('should expose the current downstream client methods', () => {
105
+ expectTypeOf<AmasterClient['entity']>().toHaveProperty('bulkUpdate');
106
+ expectTypeOf<AmasterClient['entity']>().toHaveProperty('bulkDelete');
107
+ expectTypeOf<AmasterClient['bpm']>().toHaveProperty('startProcess');
108
+ expectTypeOf<AmasterClient['workflow']>().toHaveProperty('run');
109
+ expectTypeOf<AmasterClient['copilot']>().toHaveProperty('createConversationController');
110
+ expectTypeOf<AmasterClient['function']>().toHaveProperty('invoke');
111
+ expectTypeOf<AmasterClient['s3']>().toHaveProperty('upload');
112
+ });
97
113
  });
98
114
 
99
115
  describe('LoginParams', () => {
@@ -4,7 +4,7 @@
4
4
  * - SMS verification code login
5
5
  * - Send verification code
6
6
  * - Captcha verification
7
- *
7
+ *
8
8
  * @module auth/code-auth
9
9
  */
10
10
 
@@ -18,20 +18,24 @@ import type { LoginResponse, SuccessResponse } from './password-auth';
18
18
  */
19
19
  export type CodeLoginType = 'email' | 'phone';
20
20
 
21
+ /**
22
+ * Send verification code type
23
+ */
24
+ export type SendCodeType = 'email' | 'phone';
25
+
21
26
  /**
22
27
  * Verification code login parameters
23
- *
24
28
  */
25
29
  export interface CodeLoginParams {
26
30
  /** Login method (optional, auto-detected) */
27
31
  loginType?: CodeLoginType;
28
-
32
+
29
33
  /** Email (required if using email code) */
30
34
  email?: string;
31
-
35
+
32
36
  /** Phone (required if using phone code) */
33
37
  phone?: string;
34
-
38
+
35
39
  /** Verification code received via email/SMS */
36
40
  code: string;
37
41
  }
@@ -41,11 +45,11 @@ export interface CodeLoginParams {
41
45
  */
42
46
  export interface SendCodeParams {
43
47
  /** Delivery method */
44
- type: 'email' | 'phone';
45
-
48
+ type: SendCodeType;
49
+
46
50
  /** Email address (required if type='email') */
47
51
  email?: string;
48
-
52
+
49
53
  /** Phone number (required if type='phone') */
50
54
  phone?: string;
51
55
  }
@@ -56,16 +60,19 @@ export interface SendCodeParams {
56
60
  export interface CaptchaResponse {
57
61
  /** Unique captcha identifier */
58
62
  captchaId: string;
59
-
63
+
60
64
  /** Base64 encoded captcha image */
61
65
  captchaImage: string;
66
+
67
+ /** Expiration time in seconds */
68
+ expiresIn: number;
62
69
  }
63
70
 
64
71
  // ==================== API ====================
65
72
 
66
73
  /**
67
74
  * Verification Code Authentication API
68
- *
75
+ *
69
76
  * Methods for code-based authentication and verification.
70
77
  */
71
78
  export interface CodeAuthAPI {
@@ -76,30 +83,27 @@ export interface CodeAuthAPI {
76
83
  *
77
84
  * @param params - Email/phone and verification code
78
85
  * @returns User info and access token
79
- *
80
86
  */
81
87
  loginWithCode(params: CodeLoginParams): Promise<ClientResult<LoginResponse>>;
82
88
 
83
89
  /**
84
90
  * Send verification code
85
- *
91
+ *
86
92
  * Sends a verification code to the specified email or phone.
87
93
  * Used for code login or account verification.
88
- *
94
+ *
89
95
  * @param params - Email or phone to send code to
90
96
  * @returns Success status
91
- *
92
97
  */
93
98
  sendCode(params: SendCodeParams): Promise<ClientResult<SuccessResponse>>;
94
99
 
95
100
  /**
96
101
  * Get captcha image
97
- *
102
+ *
98
103
  * Retrieves a captcha challenge for verification.
99
104
  * Used during registration or sensitive operations.
100
- *
101
- * @returns Captcha ID and image (base64)
102
105
  *
106
+ * @returns Captcha ID and image (base64)
103
107
  */
104
108
  getCaptcha(): Promise<ClientResult<CaptchaResponse>>;
105
109
  }
@@ -1,22 +1,22 @@
1
1
  /**
2
2
  * Authentication module - provides user authentication and management capabilities.
3
- *
3
+ *
4
4
  * This module exports all auth-related types for convenient importing:
5
- *
5
+ *
6
6
  * @example
7
7
  * // Import specific types
8
8
  * import type { LoginParams, User, OAuthProvider } from '@amaster.ai/client/auth';
9
- *
9
+ *
10
10
  * @example
11
11
  * // Import multiple types
12
- * import type {
13
- * LoginParams,
12
+ * import type {
13
+ * LoginParams,
14
14
  * RegisterParams,
15
15
  * User,
16
16
  * Role,
17
- * Permission
17
+ * Permission
18
18
  * } from '@amaster.ai/client/auth';
19
- *
19
+ *
20
20
  * @module auth
21
21
  * @since 1.0.0
22
22
  */
@@ -28,138 +28,118 @@ export * from './user';
28
28
  export * from './password-auth';
29
29
  export * from './code-auth';
30
30
  export * from './oauth';
31
+ export * from './permissions';
31
32
  export * from './profile';
33
+ export * from './sessions';
32
34
 
33
35
  // Import for unified API
34
- import type { PasswordAuthAPI } from './password-auth';
36
+ import type { PasswordAuthAPI, RefreshTokenResponse, SuccessResponse } from './password-auth';
35
37
  import type { CodeAuthAPI } from './code-auth';
36
38
  import type { OAuthAPI } from './oauth';
39
+ import type { PermissionsAPI } from './permissions';
37
40
  import type { ProfileAPI } from './profile';
41
+ import type { SessionsAPI } from './sessions';
38
42
 
39
- // ==================== Token Management ====================
43
+ // ==================== Events ====================
40
44
 
41
45
  /**
42
- * Token refresh response
46
+ * Authentication lifecycle events
43
47
  */
44
- export interface RefreshTokenResponse {
45
- /** New access token */
46
- accessToken: string;
47
-
48
- /** Token expiration time in seconds */
49
- expiresIn?: number;
50
- }
48
+ export type AuthEvent =
49
+ | 'login'
50
+ | 'logout'
51
+ | 'tokenExpired'
52
+ | 'tokenRefreshed'
53
+ | 'unauthorized';
51
54
 
52
55
  /**
53
- * Generic success response
56
+ * Event callback signature
54
57
  */
55
- export interface SuccessResponse {
56
- /** Whether operation was successful */
57
- success: boolean;
58
-
59
- /** Optional message */
60
- message?: string;
61
- }
58
+ export type EventHandler = (...args: any[]) => void;
62
59
 
63
60
  // ==================== Unified Authentication API ====================
64
61
 
65
62
  /**
66
63
  * Complete Authentication Client API
67
- *
64
+ *
68
65
  * Combines all authentication capabilities into a single interface.
69
66
  * All methods return a `ClientResult<T>` for consistent error handling.
70
- *
71
67
  */
72
- export interface AuthClientAPI
68
+ export interface AuthClientAPI
73
69
  extends PasswordAuthAPI,
74
70
  CodeAuthAPI,
75
71
  OAuthAPI,
76
- ProfileAPI {
77
-
72
+ PermissionsAPI,
73
+ ProfileAPI,
74
+ SessionsAPI {
75
+
78
76
  /**
79
77
  * Logout current user
80
- *
78
+ *
81
79
  * Invalidates the current session and clears local authentication state.
82
80
  * All subsequent requests will be unauthenticated until login again.
83
- *
84
- * @returns Success status
85
81
  *
82
+ * @returns Success status
86
83
  */
87
84
  logout(): Promise<ClientResult<SuccessResponse>>;
88
85
 
89
86
  /**
90
87
  * Refresh access token
91
- *
88
+ *
92
89
  * Obtains a new access token using the refresh token.
93
90
  * Note: Token refresh is handled automatically by the client.
94
91
  * This method is exposed for manual refresh scenarios.
95
- *
96
- * @returns New access token
97
92
  *
93
+ * @returns New access token
98
94
  */
99
95
  refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
100
96
 
101
- // ==================== Event Handling ====================
102
-
103
97
  /**
104
98
  * Subscribe to authentication events
105
- *
99
+ *
106
100
  * Available events:
107
101
  * - `login` - Fired after successful login, receives User object
108
102
  * - `logout` - Fired after logout
109
103
  * - `tokenExpired` - Fired when access token expires
110
104
  * - `tokenRefreshed` - Fired after token refresh, receives new token
111
105
  * - `unauthorized` - Fired on 401 response
112
- *
106
+ *
113
107
  * @param event - Event name to subscribe to
114
108
  * @param handler - Callback function
115
- *
116
109
  */
117
- on(event: "login" | "logout" | "tokenExpired" | "tokenRefreshed" | "unauthorized", handler: (...args: unknown[]) => void): void;
110
+ on(event: AuthEvent, handler: EventHandler): void;
118
111
 
119
112
  /**
120
113
  * Unsubscribe from authentication events
121
- *
114
+ *
122
115
  * @param event - Event name to unsubscribe from
123
116
  * @param handler - The same callback function that was passed to `on()`
124
- *
125
117
  */
126
- off(event: "login" | "logout" | "tokenExpired" | "tokenRefreshed" | "unauthorized", handler: (...args: unknown[]) => void): void;
118
+ off(event: AuthEvent, handler: EventHandler): void;
127
119
 
128
- // ==================== Permission Checks ====================
129
-
130
120
  /**
131
- * Check if current user has a specific role (local check, fast)
132
- *
133
- * Works for both authenticated and anonymous users.
134
- * Checks against locally cached user roles.
135
- *
136
- * @param roleCode - Role code to check (e.g., "admin", "user", "anonymous")
137
- * @returns True if user has the role
121
+ * Check whether the current client has a stored access token.
138
122
  *
123
+ * @returns True when authenticated, false otherwise
139
124
  */
140
- hasRole(roleCode: string): boolean;
141
-
125
+ isAuthenticated(): boolean;
126
+
142
127
  /**
143
- * Check if current user has a specific permission (local check, fast)
144
- *
145
- * Works for both authenticated and anonymous users.
146
- * Checks against locally cached user permissions.
147
- *
148
- * @param resource - Resource name (e.g., "user", "order")
149
- * @param action - Action name (e.g., "read", "write", "delete")
150
- * @returns True if user has the permission
128
+ * Get the current access token from local storage.
151
129
  *
130
+ * @returns Access token string, or null when not authenticated
152
131
  */
153
- hasPermission(resource: string, action: string): boolean;
154
-
132
+ getAccessToken(): string | null;
133
+
155
134
  /**
156
- * Check if current user is anonymous (not authenticated)
157
- *
158
- * Convenience method equivalent to `hasRole('anonymous')`.
159
- * Returns true if user has the 'anonymous' role.
160
- *
161
- * @returns True if user is anonymous
135
+ * Manually set the current access token.
162
136
  *
137
+ * @param token - JWT access token
138
+ */
139
+ setAccessToken(token: string): void;
140
+
141
+ /**
142
+ * Clear all local authentication state, including token and cached user info.
163
143
  */
164
- isAnonymous(): boolean;
144
+ clearAuth(): void;
165
145
  }
@@ -1,143 +1,137 @@
1
1
  /**
2
2
  * * OAuth and social authentication including:
3
- * - GitHub OAuth
4
- * - Google OAuth
5
- * - WeChat OAuth (Web & Mini Program)
6
- * - Generic OAuth flow
7
- *
3
+ * - Browser OAuth login
4
+ * - OAuth callback handling
5
+ * - WeChat Mini Program login
6
+ * - OAuth account binding management
7
+ *
8
8
  * @module auth/oauth
9
9
  */
10
10
 
11
11
  import type { ClientResult } from '../common';
12
- import type { LoginResponse } from './password-auth';
12
+ import type { LoginResponse, SuccessResponse } from './password-auth';
13
13
 
14
14
  // ==================== OAuth Providers ====================
15
15
 
16
16
  /**
17
17
  * Supported OAuth providers
18
18
  */
19
- export type OAuthProvider =
20
- | 'github'
19
+ export type OAuthProvider =
21
20
  | 'google'
21
+ | 'github'
22
22
  | 'wechat'
23
- | 'wechat-miniprogram'
24
- | 'gitlab'
25
- | 'microsoft';
23
+ | 'wechat_mini'
24
+ | 'platform';
26
25
 
27
26
  /**
28
- * OAuth login parameters
29
- *
27
+ * OAuth binding information for the current user
30
28
  */
31
- export interface OAuthLoginParams {
29
+ export interface OAuthBinding {
32
30
  /** OAuth provider */
33
31
  provider: OAuthProvider;
34
-
35
- /** Authorization code from OAuth callback */
36
- code: string;
37
-
38
- /** Redirect URI used in the OAuth flow */
39
- redirectUri?: string;
40
-
41
- /** State parameter for CSRF protection */
42
- state?: string;
43
- }
44
32
 
45
- /**
46
- * Get OAuth URL parameters
47
- */
48
- export interface GetOAuthUrlParams {
49
- /** OAuth provider */
50
- provider: OAuthProvider;
51
-
52
- /** Redirect URI after OAuth */
53
- redirectUri: string;
54
-
55
- /** State parameter for CSRF protection */
56
- state?: string;
57
-
58
- /** Additional scopes (provider-specific) */
59
- scopes?: string[];
60
- }
33
+ /** Provider-specific user ID */
34
+ providerId: string;
61
35
 
62
- /**
63
- * OAuth URL response
64
- */
65
- export interface OAuthUrlResponse {
66
- /** Authorization URL to redirect user to */
67
- authUrl: string;
68
-
69
- /** State parameter for verification */
70
- state: string;
36
+ /** OAuth account email */
37
+ email: string;
38
+
39
+ /** Display name from provider */
40
+ displayName: string;
41
+
42
+ /** Provider avatar URL */
43
+ avatarUrl: string | null;
44
+
45
+ /** Binding creation timestamp */
46
+ createdAt: string;
71
47
  }
72
48
 
73
- // ==================== WeChat Specific ====================
49
+ // ==================== WeChat Mini Program ====================
74
50
 
75
51
  /**
76
52
  * WeChat Mini Program login parameters
77
- *
78
53
  */
79
- export interface WeChatMiniProgramLoginParams {
80
- /** WeChat login code */
54
+ export interface MiniProgramLoginParams {
55
+ /** Code from `wx.login()` */
81
56
  code: string;
82
-
83
- /** User info from WeChat (optional) */
84
- userInfo?: {
85
- /** Encrypted user data */
86
- encryptedData: string;
87
- /** IV for decryption */
88
- iv: string;
89
- };
90
57
  }
91
58
 
92
59
  /**
93
- * WeChat web OAuth parameters
60
+ * WeChat Mini Program phone number parameters
94
61
  */
95
- export interface WeChatWebLoginParams {
96
- /** Authorization code from WeChat OAuth */
62
+ export interface MiniProgramPhoneParams {
63
+ /** Code from `getPhoneNumber` button event */
97
64
  code: string;
98
-
99
- /** State parameter */
100
- state?: string;
65
+ }
66
+
67
+ /**
68
+ * WeChat Mini Program phone number response
69
+ */
70
+ export interface MiniProgramPhoneResponse {
71
+ /** Phone number with country code */
72
+ phone: string;
73
+
74
+ /** Whether the phone number is verified by WeChat */
75
+ phoneVerified: boolean;
101
76
  }
102
77
 
103
78
  // ==================== API ====================
104
79
 
105
80
  /**
106
81
  * OAuth & Social Login API
107
- *
108
- * Methods for OAuth-based authentication.
82
+ *
83
+ * Methods for OAuth-based authentication and Mini Program login.
109
84
  */
110
85
  export interface OAuthAPI {
111
86
  /**
112
- * Get OAuth authorization URL
113
- *
114
- * Generates the URL to redirect users to for OAuth authentication.
115
- *
116
- * @param params - OAuth provider and redirect URI
117
- * @returns Authorization URL and state
87
+ * Redirect to OAuth provider for authentication
118
88
  *
89
+ * @param provider - OAuth provider name
90
+ * @param redirectUrl - Optional post-login redirect target
119
91
  */
120
- getOAuthUrl(params: GetOAuthUrlParams): Promise<ClientResult<OAuthUrlResponse>>;
92
+ loginWithOAuth(provider: OAuthProvider, redirectUrl?: string): void;
121
93
 
122
94
  /**
123
- * Complete OAuth login
124
- *
125
- * Exchanges the OAuth authorization code for access token and user info.
126
- *
127
- * @param params - OAuth provider and authorization code
128
- * @returns User info and access token
95
+ * Handle OAuth callback on the current browser page
129
96
  *
97
+ * Parses the callback hash, stores the access token, and returns the current user.
130
98
  */
131
- oauthLogin(params: OAuthLoginParams): Promise<ClientResult<LoginResponse>>;
99
+ handleOAuthCallback(): Promise<ClientResult<LoginResponse>>;
132
100
 
133
101
  /**
134
- * WeChat Mini Program login
135
- *
136
- * Authenticates user in WeChat Mini Program environment.
137
- *
138
- * @param params - WeChat login code
102
+ * Login with WeChat Mini Program code
103
+ *
104
+ * @param code - Code returned by `wx.login()`
139
105
  * @returns User info and access token
106
+ */
107
+ loginWithMiniProgram(code: string): Promise<ClientResult<LoginResponse>>;
108
+
109
+ /**
110
+ * Get WeChat Mini Program user phone number
111
+ *
112
+ * Requires the user to authorize via a `getPhoneNumber` button.
113
+ *
114
+ * @param code - Code returned by the Mini Program phone-number event
115
+ * @returns Phone number info
116
+ */
117
+ getMiniProgramPhoneNumber(code: string): Promise<ClientResult<MiniProgramPhoneResponse>>;
118
+
119
+ /**
120
+ * Get OAuth accounts currently bound to the logged-in user
121
+ */
122
+ getOAuthBindings(): Promise<ClientResult<OAuthBinding[]>>;
123
+
124
+ /**
125
+ * Redirect to provider binding flow for the current user
126
+ *
127
+ * @param provider - OAuth provider name
128
+ */
129
+ bindOAuth(provider: OAuthProvider): void;
130
+
131
+ /**
132
+ * Unbind an OAuth account from the current user
140
133
  *
134
+ * @param provider - OAuth provider name
141
135
  */
142
- wechatMiniProgramLogin(params: WeChatMiniProgramLoginParams): Promise<ClientResult<LoginResponse>>;
136
+ unbindOAuth(provider: OAuthProvider): Promise<ClientResult<SuccessResponse>>;
143
137
  }