@lark-apaas/client-toolkit 1.2.2-alpha.0 → 1.2.2-test.0

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 @@
1
+ export * from '../../integrations/services';
@@ -0,0 +1 @@
1
+ export * from "../../integrations/services/index.js";
@@ -0,0 +1,14 @@
1
+ import type { Department, SearchDepartmentsParams, SearchDepartmentsResponse } from './types';
2
+ export type DepartmentServiceConfig = {
3
+ getAppId?: () => string | null | undefined;
4
+ searchDepartmentUrl?: (appId: string) => string;
5
+ };
6
+ export declare class DepartmentService {
7
+ private config;
8
+ constructor(config?: DepartmentServiceConfig);
9
+ searchDepartments(params: SearchDepartmentsParams): Promise<SearchDepartmentsResponse>;
10
+ createFetcher(): (search: string) => Promise<{
11
+ items: Department[];
12
+ }>;
13
+ private getDepartmentDisplayName;
14
+ }
@@ -0,0 +1,48 @@
1
+ import { getAppId } from "../../utils/getAppId.js";
2
+ const DEFAULT_CONFIG = {
3
+ getAppId: ()=>getAppId(window.location.pathname),
4
+ searchDepartmentUrl: (appId)=>`/af/app/${appId}/runtime/api/v1/account/search_department`
5
+ };
6
+ class DepartmentService {
7
+ config;
8
+ constructor(config = {}){
9
+ this.config = {
10
+ ...DEFAULT_CONFIG,
11
+ ...config
12
+ };
13
+ }
14
+ async searchDepartments(params) {
15
+ const appId = this.config.getAppId();
16
+ if (!appId) throw new Error('Failed to get appId');
17
+ const response = await fetch(this.config.searchDepartmentUrl(appId), {
18
+ method: 'POST',
19
+ headers: {
20
+ 'Content-Type': 'application/json'
21
+ },
22
+ body: JSON.stringify(params),
23
+ credentials: 'include'
24
+ });
25
+ if (!response.ok) throw new Error('Failed to search departments');
26
+ return response.json();
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
+ }
48
+ export { DepartmentService };
@@ -0,0 +1,22 @@
1
+ import type { AccountType, SearchUsersParams, SearchUsersResponse, User } from './types';
2
+ export type UserServiceConfig = {
3
+ getAppId?: () => string | null | undefined;
4
+ searchUserUrl?: (appId: string) => string;
5
+ listUsersUrl?: (appId: string) => string;
6
+ };
7
+ export declare class UserService {
8
+ private config;
9
+ private cache;
10
+ constructor(config?: UserServiceConfig);
11
+ searchUsers(params: SearchUsersParams): Promise<SearchUsersResponse>;
12
+ fetchUsersByIds(userIds: string[], accountType?: AccountType): Promise<User[]>;
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;
22
+ }
@@ -0,0 +1,122 @@
1
+ import { getAppId } from "../../utils/getAppId.js";
2
+ const DEFAULT_CONFIG = {
3
+ getAppId: ()=>getAppId(window.location.pathname),
4
+ searchUserUrl: (appId)=>`/af/app/${appId}/runtime/api/v1/account/search_user`,
5
+ listUsersUrl: (appId)=>`/af/app/${appId}/runtime/api/v1/account/list_users`
6
+ };
7
+ class UserService {
8
+ config;
9
+ cache = new Map();
10
+ constructor(config = {}){
11
+ this.config = {
12
+ ...DEFAULT_CONFIG,
13
+ ...config
14
+ };
15
+ }
16
+ async searchUsers(params) {
17
+ const appId = this.config.getAppId();
18
+ if (!appId) throw new Error('Failed to get appId');
19
+ const response = await fetch(this.config.searchUserUrl(appId), {
20
+ method: 'POST',
21
+ headers: {
22
+ 'Content-Type': 'application/json'
23
+ },
24
+ body: JSON.stringify(params),
25
+ credentials: 'include'
26
+ });
27
+ if (!response.ok) throw new Error('Failed to search users');
28
+ return response.json();
29
+ }
30
+ async fetchUsersByIds(userIds, accountType = 'apaas') {
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));
37
+ const appId = this.config.getAppId();
38
+ if (!appId) throw new Error('Failed to get appId');
39
+ try {
40
+ const response = await fetch(this.config.listUsersUrl(appId), {
41
+ method: 'POST',
42
+ headers: {
43
+ 'Content-Type': 'application/json'
44
+ },
45
+ body: JSON.stringify({
46
+ userIDList: missingIds.map((id)=>Number(id))
47
+ }),
48
+ credentials: 'include'
49
+ });
50
+ if (!response.ok) throw new Error('Failed to fetch users by ids');
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;
120
+ }
121
+ }
122
+ export { UserService };
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './UserService';
3
+ export * from './DepartmentService';
@@ -0,0 +1,3 @@
1
+ export * from "./types.js";
2
+ export * from "./UserService.js";
3
+ export * from "./DepartmentService.js";
@@ -0,0 +1,73 @@
1
+ export type I18nText = {
2
+ zh_cn?: string;
3
+ en_us?: string;
4
+ };
5
+ export type DepartmentBasic = {
6
+ departmentID: string;
7
+ name: I18nText;
8
+ };
9
+ export type UserInfo = {
10
+ userID: string;
11
+ larkUserID: string;
12
+ name: I18nText;
13
+ avatar: string;
14
+ userType: '_employee' | '_externalUser' | '_anonymousUser';
15
+ department: DepartmentBasic;
16
+ };
17
+ export type DepartmentInfo = {
18
+ departmentID: string;
19
+ larkDepartmentID: string;
20
+ name: I18nText;
21
+ };
22
+ export type AccountType = 'apaas' | 'lark';
23
+ export type SearchAvatar = {
24
+ avatar: {
25
+ image: {
26
+ large: string;
27
+ };
28
+ };
29
+ };
30
+ export type SearchUsersParams = {
31
+ query?: string;
32
+ offset?: number;
33
+ pageSize?: number;
34
+ };
35
+ export type SearchUsersResponse = {
36
+ data: {
37
+ userList: UserInfo[];
38
+ total: number;
39
+ };
40
+ status_code: string;
41
+ };
42
+ export type BatchGetUsersResponse = {
43
+ data: {
44
+ userInfoMap: Record<string, UserInfo & SearchAvatar>;
45
+ baseResp?: {
46
+ statusCode?: number;
47
+ statusMessage?: string;
48
+ };
49
+ };
50
+ };
51
+ export type SearchDepartmentsParams = {
52
+ query?: string;
53
+ offset?: number;
54
+ pageSize?: number;
55
+ };
56
+ export type SearchDepartmentsResponse = {
57
+ data: {
58
+ departmentList: DepartmentInfo[];
59
+ total: number;
60
+ };
61
+ status_code: string;
62
+ };
63
+ export type User = {
64
+ id: string;
65
+ name: string;
66
+ avatar?: string;
67
+ raw?: UserInfo;
68
+ };
69
+ export type Department = {
70
+ id: string;
71
+ name: string;
72
+ raw?: DepartmentInfo;
73
+ };
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.2-alpha.0",
3
+ "version": "1.2.2-test.0",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -81,7 +81,7 @@
81
81
  "@ant-design/colors": "^7.2.1",
82
82
  "@ant-design/cssinjs": "^1.24.0",
83
83
  "@data-loom/js": "^0.4.3",
84
- "@lark-apaas/client-capability": "0.1.2-alpha.0",
84
+ "@lark-apaas/client-capability": "^0.1.2",
85
85
  "@lark-apaas/miaoda-inspector": "^1.0.8",
86
86
  "@lark-apaas/observable-web": "^1.0.0",
87
87
  "@radix-ui/react-avatar": "^1.1.10",