user-service.ts
Overview
user-service.ts is a centralized service module that manages user-related API interactions within the application. It abstracts the HTTP request logic for a variety of user and system management endpoints, providing a clean and consistent interface for authentication, user profile management, tenant management, language configuration, and system token administration.
This file acts as a bridge between the frontend components and backend services, encapsulating endpoint URLs, HTTP methods, and request handling in one place. It utilizes utility modules for API URLs, HTTP requests, and server registration to streamline communication with the backend.
Classes, Functions, and Methods
1. methods (constant object)
Purpose:
Defines the mapping between API endpoints and their HTTP methods for various user and system-related operations.Structure:
{ [key: string]: { url: string, method: 'get' | 'post' | 'put' | 'delete' } }Key API operations include:
login,logout,register,setting– user authentication and settings.user_info,get_tenant_info,set_tenant_info– user and tenant profile management.factories_list,llm_list,my_llm,add_llm,delete_llm– managing LLM (Large Language Model) and factories.System status, version, token list, token creation, and deletion.
Langfuse configuration management (get, set, delete).
Usage:
This object is passed to theregisterServerutility to create a typed service interface.
2. userService
Type: Registered server interface generated by
registerServerutility.Purpose:
Provides methods corresponding to themethodsobject to perform API calls. Each method automatically handles the HTTP request based on the defined URL and HTTP method.Usage Example:
import userService from './user-service'; // Login example userService.login({ username: 'user1', password: 'pass123' }).then(response => { console.log('Login successful:', response); }); // Fetch user info userService.user_info().then(userInfo => { console.log('User info:', userInfo); });Implementation Detail:
The type safety and method routing are handled byregisterServer, which uses themethodsdefinition to dynamically bind API calls.
3. Exported Utility Functions
These functions provide additional user and tenant-related operations that are not included in the main userService object but are essential for tenant user management and login channel handling.
getLoginChannels()
Description:
Fetches available login channels from the backend.Returns:
Promise<any>- resolves with the list of login channels.Usage:
getLoginChannels().then(channels => { console.log('Available login channels:', channels); });
loginWithChannel(channel: string)
Description:
Redirects the browser to the login URL for a specified channel.Parameters:
channel(string): The login channel identifier.
Returns:
No return value (redirects the browser).Usage:
loginWithChannel('google'); // Browser will navigate to the Google login URL
listTenantUser(tenantId: string)
Description:
Retrieves a list of users within a specific tenant.Parameters:
tenantId(string): Tenant identifier.
Returns:
Promise<any>- resolves with the list of tenant users.Usage:
listTenantUser('tenant_123').then(users => { console.log('Tenant users:', users); });
addTenantUser(tenantId: string, email: string)
Description:
Adds a new user to a tenant by email.Parameters:
tenantId(string): Tenant identifier.email(string): Email address of the user to add.
Returns:
Promise<any>- resolves when the user is added.Usage:
addTenantUser('tenant_123', '[email protected]').then(() => { console.log('User added to tenant'); });
deleteTenantUser({ tenantId, userId }: { tenantId: string; userId: string })
Description:
Removes a user from a tenant.Parameters:
tenantId(string): Tenant identifier.userId(string): User identifier.
Returns:
Promise<any>- resolves when the user is removed.Usage:
deleteTenantUser({ tenantId: 'tenant_123', userId: 'user_456' }).then(() => { console.log('User deleted from tenant'); });
listTenant()
Description:
Retrieves a list of tenants.Returns:
Promise<any>- resolves with the list of tenants.Usage:
listTenant().then(tenants => { console.log('Tenants:', tenants); });
agreeTenant(tenantId: string)
Description:
Sends agreement/approval action for the specified tenant.Parameters:
tenantId(string): Tenant identifier.
Returns:
Promise<any>- resolves when the agreement is accepted.Usage:
agreeTenant('tenant_123').then(() => { console.log('Tenant agreement accepted'); });
Important Implementation Details
The file imports API endpoint URLs from
@/utils/api, HTTP request utilities from@/utils/request, and a server registration helper from@/utils/register-server.The
methodsobject uses a constant assertion (as const) to ensure type safety when passed toregisterServer.The
registerServerfunction presumably creates a typed service object that automatically maps method calls to appropriate HTTP requests using the defined URLs and verbs.The distinction between general user service methods (in
userService) and tenant/user management helper functions (exported separately) suggests a clear separation between generic API interactions and specialized tenant operations.The use of
window.location.hrefforloginWithChannelindicates a full-page redirect for OAuth or external login flows.
Interaction with Other System Parts
@/utils/api:
Provides endpoint URLs for all user, tenant, system, and language configuration APIs.@/utils/request:
Handles the underlying HTTP request logic (GET, POST, PUT, DELETE), likely wrappingfetchoraxios.@/utils/register-server:
Generates a type-safe service object that abstracts API method calls, ensuring consistent usage patterns.Frontend Components:
ImportuserServiceand other exported functions to perform user authentication, profile updates, tenant user management, and system-level configurations.Backend API:
This service directly communicates with backend REST endpoints defined by the API module, facilitating CRUD operations and system configuration.
Visual Diagram
classDiagram
class userService {
+login(params)
+logout()
+register(params)
+setting(params)
+user_info()
+get_tenant_info()
+set_tenant_info(params)
+factories_list()
+llm_list()
+my_llm()
+set_api_key(params)
+add_llm(params)
+delete_llm(params)
+getSystemStatus()
+getSystemVersion()
+deleteFactory(params)
+listToken()
+createToken(params)
+removeToken(params)
+getSystemConfig()
+setLangfuseConfig(params)
+getLangfuseConfig()
+deleteLangfuseConfig()
}
class TenantUserFunctions {
+getLoginChannels()
+loginWithChannel(channel)
+listTenantUser(tenantId)
+addTenantUser(tenantId, email)
+deleteTenantUser({tenantId, userId})
+listTenant()
+agreeTenant(tenantId)
}
userService ..> api : uses URLs
userService ..> request : performs HTTP calls
TenantUserFunctions ..> api : uses URLs
TenantUserFunctions ..> request : performs HTTP calls
Summary
user-service.ts is a foundational module for managing user authentication, profile, tenant user administration, LLM management, and system configuration via RESTful API calls. It leverages utility modules for API definitions and HTTP requests, providing a robust and type-safe interface (userService) for the frontend to interact with backend services. Additional helper functions support tenant user management and login channel handling, reflecting the multi-tenant and extensible nature of the application.
This modular and declarative approach significantly simplifies API interactions and maintains consistency across the application’s user-related features.