@blueking/bk-user-display-name
Version:
多租户环境用于显示用户名称的组件
159 lines (158 loc) • 4.49 kB
TypeScript
export declare const CUSTOM_ELEMENT_NAME = "bk-user-display-name";
export declare const USER_ID_FIELD = "user-id";
/**
* 配置选项接口
* @interface ConfigOptions
*/
export type ConfigOptions = {
/** 租户ID */
tenantId: string;
/** API基础URL */
apiBaseUrl: string;
/** 缓存持续时间(毫秒),可选 */
cacheDuration?: number;
/** 空值时显示的文本,可选 */
emptyText?: string;
};
/**
* 用户基本信息接口
* @interface User
*/
interface User {
/** 用户显示名称 */
display_name: string;
/** 用户名 */
bk_username: string;
}
/**
* 单个用户响应数据接口
* @interface UserResponse
*/
type UserResponse<T extends Record<string, unknown> = Record<string, any>> = T & {
data: User;
};
/**
* 多个用户响应数据接口
* @interface UsersResponse
*/
interface UsersResponse {
data: User[];
}
/**
* 缓存项接口
* @interface UserCacheItem
*/
interface UserCacheItem {
/** 缓存的用户数据 */
data: UserResponse;
/** 缓存时间戳 */
timestamp: number;
}
/**
* 蓝鲸用户显示名称自定义元素类
* 用于在页面中显示用户的友好显示名称,支持缓存和批量查询
* @class BkUserDisplayName
* @extends HTMLElement
*/
export default class BkUserDisplayName extends HTMLElement {
/** 用户数据缓存 */
static userCache: Map<string, UserCacheItem>;
/** 缓存过期时间(毫秒),默认5分钟 */
static cacheDuration: number;
/** 存储进行中的请求,避免重复请求 */
static pendingRequests: Map<string, Promise<UserResponse>>;
/** API基础URL */
static apiBaseUrl: string;
/** 租户ID */
static tenantId: string;
/** 当输入为空时显示的文本 */
static emptyText: string;
/** 定义需要监听的属性列表 */
static observedAttributes: string[];
constructor();
/**
* 配置类的静态参数
* @static
* @param {ConfigOptions} config - 配置选项
* @returns {void}
*/
static configure(config: ConfigOptions): void;
/**
* 通用请求配置
* @private
* @returns {RequestInit} 通用请求配置
*/
private createFetchConfig;
/**
* 获取单个用户信息
* 支持请求去重,避免同时发起多个相同的请求
* @async
* @param {string} id - 用户ID
* @returns {Promise<UserResponse>} 用户信息
*/
fetchUser(id: string): Promise<UserResponse>;
/**
* 批量获取多个用户信息
* @async
* @param {string[]} ids - 用户ID数组
* @returns {Promise<UsersResponse>} 用户信息列表
*/
fetchUsers(ids: string[]): Promise<UsersResponse>;
/**
* 检查缓存是否仍然有效
* @private
* @param {UserCacheItem} cached - 缓存项
* @returns {boolean} 缓存是否有效
*/
private isCacheValid;
/**
* 解析用户ID字符串为ID数组
* 支持逗号、中文逗号、分号分隔
* @private
* @param {string} userIdString - 用户ID字符串
* @returns {string[]} 解析后的用户ID数组
*/
private parseUserIds;
/**
* 获取单个用户的显示名称
* 优先使用缓存,缓存失效时重新请求
* @private
* @async
* @param {string} id - 用户ID
* @returns {Promise<string>} 用户显示名称
*/
getSingleUserDisplayName(id: string): Promise<string>;
/**
* 获取多个用户的显示名称
* @private
* @async
* @param {string[]} ids - 用户ID数组
* @returns {Promise<string>} 逗号分隔的用户显示名称字符串
*/
getMultipleUsersDisplayName(ids: string[]): Promise<string>;
/**
* 获取用户显示名称
* 根据用户ID的数量选择单个或批量获取方式
* @async
* @returns {Promise<string>} 用户显示名称或用户ID(获取失败时)
*/
getDisplayName(): Promise<string>;
/**
* 更新元素显示内容
* @async
* @returns {Promise<void>}
*/
updateDisplay(): Promise<void>;
/**
* 当user-id属性发生变化时触发显示更新
*/
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
}
declare global {
interface HTMLElementTagNameMap {
'bk-user-display-name': {
'user-id': string;
} & HTMLElement;
}
}
export {};