@lark-apaas/client-toolkit 1.2.2-test.1 → 1.2.2-test.3
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/lib/integrations/services/DepartmentService.d.ts +1 -5
- package/lib/integrations/services/DepartmentService.js +0 -19
- package/lib/integrations/services/UserProfileService.d.ts +14 -0
- package/lib/integrations/services/UserProfileService.js +36 -0
- package/lib/integrations/services/UserService.d.ts +2 -12
- package/lib/integrations/services/UserService.js +13 -89
- package/lib/integrations/services/index.d.ts +1 -0
- package/lib/integrations/services/index.js +1 -0
- package/lib/integrations/services/types.d.ts +25 -8
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SearchDepartmentsParams, SearchDepartmentsResponse } from './types';
|
|
2
2
|
export type DepartmentServiceConfig = {
|
|
3
3
|
getAppId?: () => string | null | undefined;
|
|
4
4
|
searchDepartmentUrl?: (appId: string) => string;
|
|
@@ -7,8 +7,4 @@ export declare class DepartmentService {
|
|
|
7
7
|
private config;
|
|
8
8
|
constructor(config?: DepartmentServiceConfig);
|
|
9
9
|
searchDepartments(params: SearchDepartmentsParams): Promise<SearchDepartmentsResponse>;
|
|
10
|
-
createFetcher(): (search: string) => Promise<{
|
|
11
|
-
items: Department[];
|
|
12
|
-
}>;
|
|
13
|
-
private getDepartmentDisplayName;
|
|
14
10
|
}
|
|
@@ -25,24 +25,5 @@ class DepartmentService {
|
|
|
25
25
|
if (!response.ok) throw new Error('Failed to search departments');
|
|
26
26
|
return response.json();
|
|
27
27
|
}
|
|
28
|
-
createFetcher() {
|
|
29
|
-
return async (search)=>{
|
|
30
|
-
const response = await this.searchDepartments({
|
|
31
|
-
query: search,
|
|
32
|
-
pageSize: 100
|
|
33
|
-
});
|
|
34
|
-
const departmentList = response?.data?.departmentList || [];
|
|
35
|
-
return {
|
|
36
|
-
items: departmentList.map((department)=>({
|
|
37
|
-
id: department.departmentID,
|
|
38
|
-
name: this.getDepartmentDisplayName(department.name),
|
|
39
|
-
raw: department
|
|
40
|
-
}))
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
getDepartmentDisplayName(name) {
|
|
45
|
-
return name?.zh_cn || name?.en_us || '';
|
|
46
|
-
}
|
|
47
28
|
}
|
|
48
29
|
export { DepartmentService };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AccountType, UserProfileData } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 获取 CDN 资源 URL
|
|
4
|
+
*/
|
|
5
|
+
export declare function getAssetsUrl(path: string): string;
|
|
6
|
+
export type UserProfileServiceConfig = {
|
|
7
|
+
getAppId?: () => string | null | undefined;
|
|
8
|
+
userProfileUrl?: (appId: string) => string;
|
|
9
|
+
};
|
|
10
|
+
export declare class UserProfileService {
|
|
11
|
+
private config;
|
|
12
|
+
constructor(config?: UserProfileServiceConfig);
|
|
13
|
+
getUserProfile(userId: string, accountType?: AccountType, signal?: AbortSignal): Promise<UserProfileData>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getAppId } from "../../utils/getAppId.js";
|
|
2
|
+
const CDN_HOST = 'https://lf3-static.bytednsdoc.com';
|
|
3
|
+
function getAssetsUrl(path) {
|
|
4
|
+
return `${CDN_HOST}${path}`;
|
|
5
|
+
}
|
|
6
|
+
const DEFAULT_CONFIG = {
|
|
7
|
+
getAppId: ()=>getAppId(window.location.pathname),
|
|
8
|
+
userProfileUrl: (appId)=>`/af/app/${appId}/runtime/api/v1/account/user_profile`
|
|
9
|
+
};
|
|
10
|
+
class UserProfileService {
|
|
11
|
+
config;
|
|
12
|
+
constructor(config = {}){
|
|
13
|
+
this.config = {
|
|
14
|
+
...DEFAULT_CONFIG,
|
|
15
|
+
...config
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
async getUserProfile(userId, accountType = 'apaas', signal) {
|
|
19
|
+
const appId = this.config.getAppId();
|
|
20
|
+
if (!appId) throw new Error('Failed to get appId');
|
|
21
|
+
const params = new URLSearchParams();
|
|
22
|
+
if ('lark' === accountType) params.append('larkUserID', userId);
|
|
23
|
+
else params.append('userID', userId);
|
|
24
|
+
const response = await fetch(`${this.config.userProfileUrl(appId)}?${params.toString()}`, {
|
|
25
|
+
signal,
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'application/json'
|
|
28
|
+
},
|
|
29
|
+
credentials: 'include'
|
|
30
|
+
});
|
|
31
|
+
if (!response.ok) throw new Error(`Failed to fetch user profile: ${response.status}`);
|
|
32
|
+
const data = await response.json();
|
|
33
|
+
return data.data;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export { UserProfileService, getAssetsUrl };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BatchGetUsersResponse, SearchUsersParams, SearchUsersResponse } from './types';
|
|
2
2
|
export type UserServiceConfig = {
|
|
3
3
|
getAppId?: () => string | null | undefined;
|
|
4
4
|
searchUserUrl?: (appId: string) => string;
|
|
@@ -6,17 +6,7 @@ export type UserServiceConfig = {
|
|
|
6
6
|
};
|
|
7
7
|
export declare class UserService {
|
|
8
8
|
private config;
|
|
9
|
-
private cache;
|
|
10
9
|
constructor(config?: UserServiceConfig);
|
|
11
10
|
searchUsers(params: SearchUsersParams): Promise<SearchUsersResponse>;
|
|
12
|
-
|
|
13
|
-
createFetcher(accountType?: AccountType): (search: string) => Promise<{
|
|
14
|
-
items: User[];
|
|
15
|
-
}>;
|
|
16
|
-
clearCache(): void;
|
|
17
|
-
private getCachedUsers;
|
|
18
|
-
private createUnknownUser;
|
|
19
|
-
private userInfoToUser;
|
|
20
|
-
private getUserDisplayName;
|
|
21
|
-
private getUserId;
|
|
11
|
+
listUsersByIds(userIds: string[]): Promise<BatchGetUsersResponse>;
|
|
22
12
|
}
|
|
@@ -6,7 +6,6 @@ const DEFAULT_CONFIG = {
|
|
|
6
6
|
};
|
|
7
7
|
class UserService {
|
|
8
8
|
config;
|
|
9
|
-
cache = new Map();
|
|
10
9
|
constructor(config = {}){
|
|
11
10
|
this.config = {
|
|
12
11
|
...DEFAULT_CONFIG,
|
|
@@ -27,96 +26,21 @@ class UserService {
|
|
|
27
26
|
if (!response.ok) throw new Error('Failed to search users');
|
|
28
27
|
return response.json();
|
|
29
28
|
}
|
|
30
|
-
async
|
|
31
|
-
if (0 === userIds.length) return [];
|
|
32
|
-
const uniqueIds = [
|
|
33
|
-
...new Set(userIds)
|
|
34
|
-
];
|
|
35
|
-
const { cachedUsers, missingIds } = this.getCachedUsers(uniqueIds);
|
|
36
|
-
if (0 === missingIds.length) return uniqueIds.map((id)=>cachedUsers.get(id) || this.createUnknownUser(id));
|
|
29
|
+
async listUsersByIds(userIds) {
|
|
37
30
|
const appId = this.config.getAppId();
|
|
38
31
|
if (!appId) throw new Error('Failed to get appId');
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const data = await response.json();
|
|
52
|
-
const userInfoMap = data?.data?.userInfoMap || {};
|
|
53
|
-
for (const id of missingIds){
|
|
54
|
-
const userInfo = userInfoMap[id];
|
|
55
|
-
if (userInfo) {
|
|
56
|
-
const user = this.userInfoToUser(userInfo, accountType);
|
|
57
|
-
this.cache.set(user.id, user);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return uniqueIds.map((id)=>this.cache.get(id) || this.createUnknownUser(id));
|
|
61
|
-
} catch (error) {
|
|
62
|
-
console.error('Failed to fetch users by ids:', error);
|
|
63
|
-
return uniqueIds.map((id)=>this.cache.get(id) || this.createUnknownUser(id));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
createFetcher(accountType = 'apaas') {
|
|
67
|
-
return async (search)=>{
|
|
68
|
-
const response = await this.searchUsers({
|
|
69
|
-
query: search,
|
|
70
|
-
pageSize: 100
|
|
71
|
-
});
|
|
72
|
-
const userList = response?.data?.userList || [];
|
|
73
|
-
return {
|
|
74
|
-
items: userList.map((user)=>({
|
|
75
|
-
id: this.getUserId(user, accountType),
|
|
76
|
-
name: this.getUserDisplayName(user.name),
|
|
77
|
-
avatar: user.avatar,
|
|
78
|
-
raw: user
|
|
79
|
-
}))
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
clearCache() {
|
|
84
|
-
this.cache.clear();
|
|
85
|
-
}
|
|
86
|
-
getCachedUsers(ids) {
|
|
87
|
-
const cachedUsers = new Map();
|
|
88
|
-
const missingIds = [];
|
|
89
|
-
for (const id of ids){
|
|
90
|
-
const cached = this.cache.get(id);
|
|
91
|
-
if (cached) cachedUsers.set(id, cached);
|
|
92
|
-
else missingIds.push(id);
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
cachedUsers,
|
|
96
|
-
missingIds
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
createUnknownUser(id) {
|
|
100
|
-
return {
|
|
101
|
-
id,
|
|
102
|
-
name: '未知用户',
|
|
103
|
-
avatar: void 0,
|
|
104
|
-
raw: void 0
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
userInfoToUser(userInfo, accountType) {
|
|
108
|
-
return {
|
|
109
|
-
id: this.getUserId(userInfo, accountType),
|
|
110
|
-
name: this.getUserDisplayName(userInfo.name),
|
|
111
|
-
avatar: userInfo.avatar?.image?.large,
|
|
112
|
-
raw: userInfo
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
getUserDisplayName(name) {
|
|
116
|
-
return name.zh_cn || name.en_us || '';
|
|
117
|
-
}
|
|
118
|
-
getUserId(user, accountType) {
|
|
119
|
-
return 'lark' === accountType ? user.larkUserID : user.userID;
|
|
32
|
+
const response = await fetch(this.config.listUsersUrl(appId), {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
'Content-Type': 'application/json'
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
userIDList: userIds.map((id)=>Number(id))
|
|
39
|
+
}),
|
|
40
|
+
credentials: 'include'
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) throw new Error('Failed to fetch users by ids');
|
|
43
|
+
return response.json();
|
|
120
44
|
}
|
|
121
45
|
}
|
|
122
46
|
export { UserService };
|
|
@@ -61,14 +61,31 @@ export type SearchDepartmentsResponse = {
|
|
|
61
61
|
};
|
|
62
62
|
status_code: string;
|
|
63
63
|
};
|
|
64
|
-
export type
|
|
65
|
-
|
|
66
|
-
name
|
|
64
|
+
export type UserProfileAccountStatus = 0 | 1 | 2 | 3 | 4;
|
|
65
|
+
export type SimpleUserProfileInfo = {
|
|
66
|
+
name?: string;
|
|
67
67
|
avatar?: string;
|
|
68
|
-
|
|
68
|
+
email?: string;
|
|
69
|
+
userStatus: UserProfileAccountStatus;
|
|
70
|
+
userType: '_employee' | '_externalUser';
|
|
69
71
|
};
|
|
70
|
-
export type
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
export type LarkCardParam = {
|
|
73
|
+
needRedirect?: boolean;
|
|
74
|
+
redirectURL?: string;
|
|
75
|
+
larkAppID: string;
|
|
76
|
+
jsAPITicket: string;
|
|
77
|
+
larkOpenID: string;
|
|
78
|
+
targetLarkOpenID: string;
|
|
79
|
+
};
|
|
80
|
+
export type SimpleUserProfileData = {
|
|
81
|
+
useLarkCard: false;
|
|
82
|
+
userProfileInfo: SimpleUserProfileInfo;
|
|
83
|
+
};
|
|
84
|
+
export type OfficialUserProfileData = {
|
|
85
|
+
useLarkCard: true;
|
|
86
|
+
larkCardParam: LarkCardParam;
|
|
87
|
+
};
|
|
88
|
+
export type UserProfileData = SimpleUserProfileData | OfficialUserProfileData;
|
|
89
|
+
export type UserProfileResponse = {
|
|
90
|
+
data: UserProfileData;
|
|
74
91
|
};
|