Skip to content

Commit fe0dab7

Browse files
CopilotTechQuery
andauthored
[add] File Ownership transfer & BI Table schema query methods (#16)
Co-authored-by: TechQuery <shiy2008@gmail.com>
1 parent 167e079 commit fe0dab7

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-lark",
3-
"version": "2.6.1",
3+
"version": "2.6.3",
44
"license": "LGPL-3.0",
55
"author": "shiy2008@gmail.com",
66
"description": "Unofficial TypeScript SDK for FeiShu/Lark API, which is based on MobX-RESTful.",

src/Lark.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { Context, HTTPClient } from 'koajax';
22
import { buildURLData, cache, sleep } from 'web-utility';
33

44
import {
5+
BiTable,
6+
BiTableSchema,
7+
BiTableView,
58
CopiedFile,
69
DocumentModel,
710
DriveFileModel,
11+
TableFormView,
812
UserIdType,
913
WikiNode,
1014
WikiNodeModel
@@ -281,4 +285,29 @@ export class LarkApp implements LarkAppOption {
281285

282286
return this.documentStore.getOneContent(doc_token, 'markdown');
283287
}
288+
289+
async getBiTableSchema(appId: string) {
290+
const { client } = this;
291+
292+
class InternalTableModel extends BiTable() {
293+
client = client;
294+
}
295+
class InternalFormModel extends BiTableView('form') {
296+
client = client;
297+
}
298+
const tables = await new InternalTableModel(appId).getAll(),
299+
forms: Record<string, TableFormView[]> = {};
300+
301+
for (const { name, table_id } of tables)
302+
forms[name] = await new InternalFormModel(appId, table_id).getAll();
303+
304+
const tableIdMap = Object.fromEntries(tables.map(({ name, table_id }) => [name, table_id])),
305+
formLinkMap = Object.fromEntries(
306+
Object.entries(forms).map(([name, list]) => [
307+
name,
308+
Object.fromEntries(list.map(({ name, shared_url }) => [name, shared_url]))
309+
])
310+
);
311+
return { tables, tableIdMap, forms, formLinkMap } as BiTableSchema;
312+
}
284313
}

src/module/BITable/type.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ export interface TableView extends Record<'view_id' | 'view_name', string> {
1515
}
1616

1717
export interface TableFormView
18-
extends Record<'name' | 'description' | 'shared_url', string>,
18+
extends
19+
Record<'name' | 'description' | 'shared_url', string>,
1920
Record<'shared' | 'submit_limit_once', boolean> {
2021
shared_limit: 'tenant_editable';
2122
}
2223

2324
export type LarkFormData = LarkData<{ form: TableFormView }>;
2425

26+
export interface BiTableSchema {
27+
tables: BITable[];
28+
tableIdMap: Record<string, string>;
29+
forms: Record<string, TableFormView[]>;
30+
formLinkMap: Record<string, Record<string, string>>;
31+
}
32+
2533
export interface TableCellText {
2634
type: 'text';
2735
text: string;
@@ -36,14 +44,17 @@ export interface TableCellLink extends Record<'link' | 'text', string> {
3644
type: 'url';
3745
}
3846

39-
export interface TableCellMedia
40-
extends Record<'file_token' | 'name' | `${'' | 'tmp_'}url`, string> {
47+
export interface TableCellMedia extends Record<
48+
'file_token' | 'name' | `${'' | 'tmp_'}url`,
49+
string
50+
> {
4151
type: `${string}/${string}`;
4252
size: number;
4353
}
4454

4555
export interface TableCellAttachment
46-
extends Pick<TableCellMedia, 'name' | 'size'>,
56+
extends
57+
Pick<TableCellMedia, 'name' | 'size'>,
4758
Record<'id' | 'attachmentToken', string>,
4859
Record<'height' | 'timeStamp' | 'width', number> {
4960
mimeType: TableCellMedia['type'];
@@ -96,8 +107,10 @@ export type TableCellValue =
96107

97108
export type TableRecordFields = Record<string, TableCellValue>;
98109

99-
export interface TableRecord<T extends TableRecordFields>
100-
extends Record<'id' | 'record_id', string> {
110+
export interface TableRecord<T extends TableRecordFields> extends Record<
111+
'id' | 'record_id',
112+
string
113+
> {
101114
created_by: TableCellUser;
102115
created_time: number;
103116
last_modified_by?: TableCellUser;

src/module/Drive/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { buildURLData, splitArray } from 'web-utility';
44

55
import { LarkData, LarkDocumentPathTypeMap, LarkDocumentType, UploadTargetType } from '../../type';
66
import { UserIdType } from '../User/type';
7-
import { CopiedFile, DriveFile } from './type';
7+
import { CopiedFile, DriveFile, DriveFileType, TransferOwner, TransferOption } from './type';
88

99
export * from './type';
1010

@@ -105,4 +105,20 @@ export abstract class DriveFileModel extends BaseListModel<DriveFile> {
105105
);
106106
return body!.data!.file;
107107
}
108+
109+
/**
110+
* @see {@link https://open.feishu.cn/document/server-docs/docs/permission/permission-member/transfer_owner}
111+
*/
112+
@toggle('uploading')
113+
async transferOwner(
114+
type: DriveFileType,
115+
token: string,
116+
newOwner: TransferOwner,
117+
option = {} as TransferOption
118+
) {
119+
await this.client.post<LarkData>(
120+
`${this.baseURI}/permissions/${token}/members/transfer_owner?${buildURLData({ ...option, type })}`,
121+
newOwner
122+
);
123+
}
108124
}

src/module/Drive/type.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { LarkDocumentType } from '../../type';
2+
13
export type DriveFile = Record<
24
| `doc_${'token' | 'type'}`
35
| 'title'
@@ -9,4 +11,18 @@ export type DriveFile = Record<
911
string
1012
>;
1113

12-
export type CopiedFile = Record<'token' | 'type' | 'name' | 'parent_token' | 'url', string>;
14+
export type CopiedFile = Record<'type' | `${'parent_' | ''}token` | 'name' | 'url', string>;
15+
16+
export type DriveFileType = LarkDocumentType | 'minutes' | 'folder' | 'wiki';
17+
18+
export interface TransferOwner {
19+
member_type: 'email' | 'userid' | 'openid';
20+
member_id: string;
21+
}
22+
23+
export interface TransferOption {
24+
need_notification?: boolean;
25+
remove_old_owner?: boolean;
26+
stay_put?: boolean;
27+
old_owner_perm?: 'view' | 'edit' | 'full_access';
28+
}

0 commit comments

Comments
 (0)