Skip to content

Commit b4b565e

Browse files
committed
[add] Copy methods of Lark documents
[add] independent Drive & Document Block modules [optimize] update Upstream packages
1 parent 49f46be commit b4b565e

File tree

9 files changed

+1213
-1058
lines changed

9 files changed

+1213
-1058
lines changed

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-lark",
3-
"version": "2.5.0",
3+
"version": "2.6.0-rc.0",
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.",
@@ -25,37 +25,37 @@
2525
"url": "https://github.com/idea2app/MobX-Lark/issues"
2626
},
2727
"dependencies": {
28-
"@swc/helpers": "^0.5.17",
29-
"@types/react": "^19.2.2",
28+
"@swc/helpers": "^0.5.18",
29+
"@types/react": "^19.2.7",
3030
"koajax": "^3.1.2",
3131
"lodash.memoize": "^4.1.2",
3232
"mobx": "^6.15.0",
3333
"mobx-restful": "^2.1.4",
3434
"regenerator-runtime": "^0.14.1",
35-
"web-utility": "^4.6.2"
35+
"web-utility": "^4.6.4"
3636
},
3737
"peerDependencies": {
3838
"react": ">=16"
3939
},
4040
"devDependencies": {
41-
"@parcel/config-default": "~2.16.0",
42-
"@parcel/transformer-typescript-tsc": "~2.16.0",
41+
"@parcel/config-default": "~2.16.3",
42+
"@parcel/transformer-typescript-tsc": "~2.16.3",
4343
"@types/fs-extra": "^11.0.4",
4444
"@types/lodash.memoize": "^4.1.9",
45-
"@types/node": "^22.18.12",
46-
"@types/react-dom": "^19.2.2",
47-
"@types/turndown": "^5.0.5",
45+
"@types/node": "^22.19.3",
46+
"@types/react-dom": "^19.2.3",
47+
"@types/turndown": "^5.0.6",
4848
"dotenv": "^17.2.3",
49-
"fs-extra": "^11.3.2",
49+
"fs-extra": "^11.3.3",
5050
"husky": "^9.1.7",
51-
"lint-staged": "^16.2.5",
52-
"parcel": "~2.16.0",
53-
"prettier": "^3.6.2",
54-
"react": "^19.2.0",
55-
"react-dom": "^19.2.0",
51+
"lint-staged": "^16.2.7",
52+
"parcel": "~2.16.3",
53+
"prettier": "^3.7.4",
54+
"react": "^19.2.3",
55+
"react-dom": "^19.2.3",
5656
"ts-node": "^10.9.2",
57-
"turndown": "^7.2.1",
58-
"typedoc": "^0.28.14",
57+
"turndown": "^7.2.2",
58+
"typedoc": "^0.28.15",
5959
"typedoc-plugin-mdn-links": "^5.0.10",
6060
"typescript": "~5.8.3"
6161
},

pnpm-lock.yaml

Lines changed: 685 additions & 685 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Lark.ts

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { Context, HTTPClient, makeFormData } from 'koajax';
1+
import { Context, HTTPClient } from 'koajax';
22
import { buildURLData, cache, sleep } from 'web-utility';
33

4-
import { DocumentModel, WikiNodeModel } from './module';
4+
import {
5+
CopiedFile,
6+
DocumentModel,
7+
DriveModel,
8+
UserIdType,
9+
WikiNode,
10+
WikiNodeModel
11+
} from './module';
512
import {
613
isLarkError,
714
JSTicket,
815
LarkData,
16+
LarkDocumentType,
917
TenantAccessToken,
1018
UploadTargetType,
1119
UserMeta
@@ -34,6 +42,10 @@ export class LarkApp implements LarkAppOption {
3442
client: HTTPClient<Context>;
3543
accessToken = '';
3644

45+
driveStore: DriveModel;
46+
wikiNodeStore: WikiNodeModel;
47+
documentStore: DocumentModel;
48+
3749
constructor(option: LarkAppServerOption | LarkAppClientOption) {
3850
Object.assign(this, option);
3951

@@ -44,6 +56,18 @@ export class LarkApp implements LarkAppOption {
4456
responseType: 'json'
4557
});
4658
this.boot();
59+
60+
const { client } = this;
61+
62+
this.driveStore = new (class extends DriveModel {
63+
client = client;
64+
})();
65+
this.wikiNodeStore = new (class extends WikiNodeModel {
66+
client = client;
67+
})('', '');
68+
this.documentStore = new (class extends DocumentModel {
69+
client = client;
70+
})('');
4771
}
4872

4973
private boot() {
@@ -152,56 +176,90 @@ export class LarkApp implements LarkAppOption {
152176
}, 'JS ticket');
153177

154178
/**
155-
* @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download}
179+
* @see {@link DriveModel#downloadFile}
156180
*/
157181
async downloadFile(id: string) {
158182
await this.getAccessToken();
159183

160-
const { headers, body } = await this.client.get<Blob>(
161-
`drive/v1/medias/${id}/download`,
162-
{},
163-
{ responseType: 'blob' }
164-
);
165-
const { 'Content-Disposition': CD, 'Content-Type': CT } = headers;
166-
167-
const [type] = (CT as string)?.split(';') || [],
168-
[, fileName] = (CD as string)?.match(/filename="?(.*?)"?$/) || [];
169-
170-
return new File([body!], fileName, { type });
184+
return this.driveStore.downloadFile(id);
171185
}
172186

173187
/**
174-
* @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all}
188+
* @see {@link DriveModel#uploadFile}
175189
*/
176190
async uploadFile(file: File, parent_type: UploadTargetType, parent_node: string) {
177191
await this.getAccessToken();
178192

179-
const form = makeFormData({
180-
file,
181-
file_name: file.name,
182-
size: file.size,
183-
parent_type,
184-
parent_node
185-
});
186-
const { body } = await this.client.post<LarkData<{ file_token: string }>>(
187-
'drive/v1/medias/upload_all',
188-
form
189-
);
190-
return body!.data!.file_token;
193+
return this.driveStore.uploadFile(file, parent_type, parent_node);
191194
}
192195

193196
/**
194-
* @see {@link WikiNodeModel.getOne}
197+
* @see {@link DriveModel#copyFile}
198+
* @see {@link WikiNodeModel#moveDocument}
195199
*/
196-
async wiki2docx(id: string) {
200+
copyFile(
201+
URI: `${string}/wiki/${string}`,
202+
name: string,
203+
parent_node_token?: string,
204+
user_id_type?: UserIdType
205+
): Promise<WikiNode>;
206+
copyFile(
207+
URI: `${string}/${LarkDocumentType}/${string}`,
208+
name: string,
209+
folder_token?: string,
210+
user_id_type?: UserIdType
211+
): Promise<CopiedFile>;
212+
async copyFile(URI: string, name: string, folder_token?: string, user_id_type?: UserIdType) {
197213
await this.getAccessToken();
198214

215+
let [type, token] = new URL(URI, 'http://localhost').pathname.split('/'),
216+
space_id: string | undefined,
217+
parent_node_token = folder_token;
218+
219+
if (type === 'wiki')
220+
({
221+
obj_type: type,
222+
obj_token: token,
223+
space_id,
224+
parent_node_token
225+
} = await this.wiki2drive(token));
226+
227+
const copidFile = await this.driveStore.copyFile(
228+
type as LarkDocumentType,
229+
token,
230+
name,
231+
folder_token,
232+
user_id_type
233+
);
234+
if (!space_id) return copidFile;
235+
199236
const { client } = this;
200237

201238
class InternalWikiNodeModel extends WikiNodeModel {
202239
client = client;
203240
}
204-
const { obj_type, obj_token } = await new InternalWikiNodeModel('', '').getOne(id);
241+
const wikiNodeStore = new InternalWikiNodeModel('', space_id);
242+
243+
return wikiNodeStore.moveDocument(
244+
{ obj_type: type, obj_token: token } as WikiNode,
245+
parent_node_token
246+
);
247+
}
248+
249+
/**
250+
* @see {@link WikiNodeModel#getOne}
251+
*/
252+
async wiki2drive(id: string) {
253+
await this.getAccessToken();
254+
255+
return this.wikiNodeStore.getOne(id);
256+
}
257+
258+
/**
259+
* @deprecated Use {@link LarkApp#wiki2drive} instead
260+
*/
261+
async wiki2docx(id: string) {
262+
const { obj_type, obj_token } = await this.wiki2drive(id);
205263

206264
return obj_type === 'docx' ? obj_token : '';
207265
}
@@ -216,12 +274,8 @@ export class LarkApp implements LarkAppOption {
216274

217275
const [, type, id] = URI.match(LarkApp.documentPathPattern) || [];
218276

219-
const doc_token = type === 'wiki' ? await this.wiki2docx(id) : id,
220-
{ client } = this;
277+
const doc_token = type === 'wiki' ? (await this.wiki2drive(id)).obj_token : id;
221278

222-
class InternalDocumentModel extends DocumentModel {
223-
client = client;
224-
}
225-
return new InternalDocumentModel('').getOneContent(doc_token, 'markdown');
279+
return this.documentStore.getOneContent(doc_token, 'markdown');
226280
}
227281
}

0 commit comments

Comments
 (0)