mobx-lark 2.1.0 → 2.1.1

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,5 @@
1
+ export interface LarkWikiNode extends Record<'title' | 'creator' | 'owner' | `${'' | 'origin_'}space_id` | `node_create_time` | `${'' | 'parent_' | 'origin_'}node_token` | `obj_token` | `obj_${'create' | 'edit'}_time`, string> {
2
+ node_type: 'origin' | 'shortcut';
3
+ obj_type: `doc${'' | 'x'}` | 'sheet' | 'mindnote' | 'bitable' | 'file';
4
+ has_child: boolean;
5
+ }
@@ -3,3 +3,4 @@ export * from './InstantMessenger';
3
3
  export * from './Task';
4
4
  export * from './SpreadSheet';
5
5
  export * from './BITable';
6
+ export * from './Wiki/type';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-lark",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "license": "LGPL-3.0",
5
5
  "author": "shiy2008@gmail.com",
6
6
  "description": "Unofficial TypeScript SDK for FeiShu/Lark API, which is based on MobX-RESTful.",
@@ -26,26 +26,31 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@swc/helpers": "^0.5.15",
29
- "koajax": "^3.1.1",
29
+ "koajax": "^3.1.2",
30
30
  "mobx": "^6.13.7",
31
31
  "mobx-restful": "^2.1.0",
32
32
  "regenerator-runtime": "^0.14.1",
33
33
  "web-utility": "^4.4.3"
34
34
  },
35
35
  "devDependencies": {
36
- "@parcel/config-default": "~2.14.0",
37
- "@parcel/transformer-typescript-tsc": "~2.14.0",
38
- "@types/node": "^22.13.10",
36
+ "@parcel/config-default": "~2.14.4",
37
+ "@parcel/transformer-typescript-tsc": "~2.14.4",
38
+ "@types/node": "^22.14.0",
39
39
  "dotenv": "^16.4.7",
40
40
  "husky": "^9.1.7",
41
41
  "lint-staged": "^15.5.0",
42
42
  "mobx": "^6.13.7",
43
- "parcel": "~2.14.0",
43
+ "parcel": "~2.14.4",
44
44
  "prettier": "^3.5.3",
45
45
  "ts-node": "^10.9.2",
46
- "typedoc": "^0.28.0",
46
+ "typedoc": "^0.28.2",
47
47
  "typedoc-plugin-mdn-links": "^5.0.1",
48
- "typescript": "~5.8.2"
48
+ "typescript": "~5.8.3"
49
+ },
50
+ "pnpm": {
51
+ "onlyBuiltDependencies": [
52
+ "@swc/core"
53
+ ]
49
54
  },
50
55
  "prettier": {
51
56
  "singleQuote": true,
package/src/Lark.ts CHANGED
@@ -1,198 +1,226 @@
1
- import { Context, HTTPClient, makeFormData } from 'koajax';
2
- import { buildURLData, cache, Second } from 'web-utility';
3
-
4
- import {
5
- isLarkError,
6
- JSTicket,
7
- LarkData,
8
- TenantAccessToken,
9
- UploadTargetType,
10
- UserMeta
11
- } from './type';
12
-
13
- export interface LarkAppBaseOption {
14
- host?: string;
15
- id: string;
16
- }
17
-
18
- export interface LarkAppServerOption extends LarkAppBaseOption {
19
- secret: string;
20
- }
21
-
22
- export interface LarkAppClientOption extends LarkAppBaseOption {
23
- accessToken: string;
24
- }
25
-
26
- export interface LarkAppOption
27
- extends LarkAppServerOption,
28
- LarkAppClientOption {}
29
-
30
- export class LarkApp implements LarkAppOption {
31
- host = 'https://open.feishu.cn/open-apis/';
32
- id = '';
33
- secret = '';
34
-
35
- client: HTTPClient<Context>;
36
- accessToken = '';
37
-
38
- constructor(option: LarkAppServerOption | LarkAppClientOption) {
39
- Object.assign(this, option);
40
-
41
- console.assert(
42
- !globalThis.window || !this.secret,
43
- "App Secret can't be used in client"
44
- );
45
- this.client = new HTTPClient({
46
- baseURI: this.host,
47
- responseType: 'json'
48
- });
49
- this.boot();
50
- }
51
-
52
- private boot() {
53
- this.client.use(async ({ request, response }, next) => {
54
- const { accessToken } = this;
55
-
56
- if (accessToken)
57
- request.headers = {
58
- Authorization: `Bearer ${accessToken}`,
59
- ...request.headers
60
- };
61
- try {
62
- await next();
63
-
64
- const { body } = response;
65
-
66
- if (isLarkError(body)) {
67
- console.error(body);
68
- throw new URIError(body.msg);
69
- }
70
- } catch (error) {
71
- const { method, path } = request,
72
- { status, body } = response;
73
-
74
- console.error(method, path, status, body);
75
- throw error;
76
- }
77
- });
78
- }
79
-
80
- getAccessToken(code = '') {
81
- return code
82
- ? this.getUserAccessToken(code)
83
- : this.getTenantAccessToken();
84
- }
85
-
86
- /**
87
- * Back-end only
88
- *
89
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/tenant_access_token_internal}
90
- */
91
- getTenantAccessToken = cache(async clean => {
92
- const { id, secret } = this;
93
-
94
- console.assert(id && secret, 'Id & Secret of Lark App are required');
95
-
96
- const { body } = await this.client.post<TenantAccessToken>(
97
- 'auth/v3/tenant_access_token/internal',
98
- { app_id: id, app_secret: secret }
99
- );
100
- setTimeout(() => {
101
- this.accessToken = '';
102
- clean();
103
- }, body!.expire * Second);
104
-
105
- return (this.accessToken = body!.tenant_access_token);
106
- }, 'Tenant Access Token');
107
-
108
- /**
109
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukzN4UjL5cDO14SO3gTN}
110
- */
111
- getWebSignInURL(redirect_uri: string, state?: string) {
112
- return `${this.client.baseURI}authen/v1/index?${buildURLData({
113
- app_id: this.id,
114
- redirect_uri,
115
- state
116
- })}`;
117
- }
118
-
119
- /**
120
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/access_token}
121
- */
122
- getUserAccessToken = cache(async (clean, code: string) => {
123
- const { body } = await this.client.post<LarkData<UserMeta>>(
124
- 'authen/v1/access_token',
125
- { grant_type: 'authorization_code', code }
126
- );
127
- const { expires_in, access_token } = body!.data!;
128
-
129
- setTimeout(() => {
130
- this.accessToken = '';
131
- clean();
132
- }, expires_in * Second);
133
-
134
- return (this.accessToken = access_token);
135
- }, 'User Access Token');
136
-
137
- /**
138
- * @see {@link https://open.feishu.cn/document/server-docs/authentication-management/login-state-management/get}
139
- */
140
- async getUserMeta() {
141
- const { body } = await this.client.get<LarkData<UserMeta>>(
142
- 'authen/v1/user_info'
143
- );
144
- return body!.data!;
145
- }
146
-
147
- /**
148
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/uYTM5UjL2ETO14iNxkTN/h5_js_sdk/authorization}
149
- */
150
- getJSTicket = cache(async clean => {
151
- await this.getAccessToken();
152
-
153
- const { body } =
154
- await this.client.post<LarkData<JSTicket>>('jssdk/ticket/get');
155
- const { expire_in, ticket } = body!.data!;
156
-
157
- setTimeout(clean, expire_in * Second);
158
-
159
- return ticket;
160
- }, 'JS ticket');
161
-
162
- /**
163
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download}
164
- */
165
- async downloadFile(id: string) {
166
- await this.getAccessToken();
167
-
168
- const { body } = await this.client.request<ArrayBuffer>({
169
- path: `drive/v1/medias/${id}/download`,
170
- responseType: 'arraybuffer'
171
- });
172
- return body!;
173
- }
174
-
175
- /**
176
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all}
177
- */
178
- async uploadFile(
179
- file: File,
180
- parent_type: UploadTargetType,
181
- parent_node: string
182
- ) {
183
- await this.getAccessToken();
184
-
185
- const form = makeFormData({
186
- file,
187
- file_name: file.name,
188
- size: file.size,
189
- parent_type,
190
- parent_node
191
- });
192
- const { body } = await this.client.post<
193
- LarkData<{ file_token: string }>
194
- >('drive/v1/medias/upload_all', form);
195
-
196
- return body!.data!.file_token;
197
- }
198
- }
1
+ import { Context, HTTPClient, makeFormData } from 'koajax';
2
+ import { buildURLData, cache, Second } from 'web-utility';
3
+
4
+ import { LarkWikiNode } from './module';
5
+ import {
6
+ isLarkError,
7
+ JSTicket,
8
+ LarkData,
9
+ TenantAccessToken,
10
+ UploadTargetType,
11
+ UserMeta
12
+ } from './type';
13
+
14
+ export interface LarkAppBaseOption {
15
+ host?: string;
16
+ id: string;
17
+ }
18
+
19
+ export interface LarkAppServerOption extends LarkAppBaseOption {
20
+ secret: string;
21
+ }
22
+
23
+ export interface LarkAppClientOption extends LarkAppBaseOption {
24
+ accessToken: string;
25
+ }
26
+
27
+ export interface LarkAppOption
28
+ extends LarkAppServerOption,
29
+ LarkAppClientOption {}
30
+
31
+ export class LarkApp implements LarkAppOption {
32
+ host = 'https://open.feishu.cn/open-apis/';
33
+ id = '';
34
+ secret = '';
35
+
36
+ client: HTTPClient<Context>;
37
+ accessToken = '';
38
+
39
+ constructor(option: LarkAppServerOption | LarkAppClientOption) {
40
+ Object.assign(this, option);
41
+
42
+ console.assert(
43
+ !globalThis.window || !this.secret,
44
+ "App Secret can't be used in client"
45
+ );
46
+ this.client = new HTTPClient({
47
+ baseURI: this.host,
48
+ responseType: 'json'
49
+ });
50
+ this.boot();
51
+ }
52
+
53
+ private boot() {
54
+ this.client.use(async ({ request, response }, next) => {
55
+ const { accessToken } = this;
56
+
57
+ if (accessToken)
58
+ request.headers = {
59
+ Authorization: `Bearer ${accessToken}`,
60
+ ...request.headers
61
+ };
62
+ try {
63
+ await next();
64
+
65
+ const { body } = response;
66
+
67
+ if (isLarkError(body)) {
68
+ console.error(body);
69
+ throw new URIError(body.msg);
70
+ }
71
+ } catch (error) {
72
+ const { method, path } = request,
73
+ { status, body } = response;
74
+
75
+ console.error(method, path, status, body);
76
+ throw error;
77
+ }
78
+ });
79
+ }
80
+
81
+ getAccessToken(code = '') {
82
+ return code
83
+ ? this.getUserAccessToken(code)
84
+ : this.getTenantAccessToken();
85
+ }
86
+
87
+ /**
88
+ * Back-end only
89
+ *
90
+ * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/tenant_access_token_internal}
91
+ */
92
+ getTenantAccessToken = cache(async clean => {
93
+ const { id, secret } = this;
94
+
95
+ console.assert(id && secret, 'Id & Secret of Lark App are required');
96
+
97
+ const { body } = await this.client.post<TenantAccessToken>(
98
+ 'auth/v3/tenant_access_token/internal',
99
+ { app_id: id, app_secret: secret }
100
+ );
101
+ setTimeout(() => {
102
+ this.accessToken = '';
103
+ clean();
104
+ }, body!.expire * Second);
105
+
106
+ return (this.accessToken = body!.tenant_access_token);
107
+ }, 'Tenant Access Token');
108
+
109
+ /**
110
+ * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukzN4UjL5cDO14SO3gTN}
111
+ */
112
+ getWebSignInURL(redirect_uri: string, state?: string) {
113
+ return `${this.client.baseURI}authen/v1/index?${buildURLData({
114
+ app_id: this.id,
115
+ redirect_uri,
116
+ state
117
+ })}`;
118
+ }
119
+
120
+ /**
121
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/access_token}
122
+ */
123
+ getUserAccessToken = cache(async (clean, code: string) => {
124
+ const { body } = await this.client.post<LarkData<UserMeta>>(
125
+ 'authen/v1/access_token',
126
+ { grant_type: 'authorization_code', code }
127
+ );
128
+ const { expires_in, access_token } = body!.data!;
129
+
130
+ setTimeout(() => {
131
+ this.accessToken = '';
132
+ clean();
133
+ }, expires_in * Second);
134
+
135
+ return (this.accessToken = access_token);
136
+ }, 'User Access Token');
137
+
138
+ /**
139
+ * @see {@link https://open.feishu.cn/document/server-docs/authentication-management/login-state-management/get}
140
+ */
141
+ async getUserMeta() {
142
+ const { body } = await this.client.get<LarkData<UserMeta>>(
143
+ 'authen/v1/user_info'
144
+ );
145
+ return body!.data!;
146
+ }
147
+
148
+ /**
149
+ * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/uYTM5UjL2ETO14iNxkTN/h5_js_sdk/authorization}
150
+ */
151
+ getJSTicket = cache(async clean => {
152
+ await this.getAccessToken();
153
+
154
+ const { body } =
155
+ await this.client.post<LarkData<JSTicket>>('jssdk/ticket/get');
156
+ const { expire_in, ticket } = body!.data!;
157
+
158
+ setTimeout(clean, expire_in * Second);
159
+
160
+ return ticket;
161
+ }, 'JS ticket');
162
+
163
+ /**
164
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download}
165
+ */
166
+ async downloadFile(id: string) {
167
+ await this.getAccessToken();
168
+
169
+ const { body } = await this.client.request<ArrayBuffer>({
170
+ path: `drive/v1/medias/${id}/download`,
171
+ responseType: 'arraybuffer'
172
+ });
173
+ return body!;
174
+ }
175
+
176
+ /**
177
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all}
178
+ */
179
+ async uploadFile(
180
+ file: File,
181
+ parent_type: UploadTargetType,
182
+ parent_node: string
183
+ ) {
184
+ await this.getAccessToken();
185
+
186
+ const form = makeFormData({
187
+ file,
188
+ file_name: file.name,
189
+ size: file.size,
190
+ parent_type,
191
+ parent_node
192
+ });
193
+ const { body } = await this.client.post<
194
+ LarkData<{ file_token: string }>
195
+ >('drive/v1/medias/upload_all', form);
196
+
197
+ return body!.data!.file_token;
198
+ }
199
+
200
+ async wiki2docx(id: string) {
201
+ const { body } = await this.client.get<
202
+ LarkData<{ node: LarkWikiNode }>
203
+ >(`wiki/v2/spaces/get_node?token=${id}`);
204
+
205
+ const { obj_type, obj_token } = body!.data!.node;
206
+
207
+ return obj_type === 'docx' ? obj_token : '';
208
+ }
209
+
210
+ static documentPathPattern = /(wiki|docx)\/(\w+)/;
211
+
212
+ async downloadMarkdown(URI: string) {
213
+ const [, type, id] = URI.match(LarkApp.documentPathPattern) || [];
214
+
215
+ const doc_token = type === 'wiki' ? await this.wiki2docx(id) : id;
216
+
217
+ const { body } = await this.client.get<LarkData<{ content: string }>>(
218
+ `docs/v1/content?${new URLSearchParams({
219
+ doc_type: 'docx',
220
+ doc_token,
221
+ content_type: 'markdown'
222
+ })}`
223
+ );
224
+ return body!.data!.content;
225
+ }
226
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './type';
2
- export * from './Lark';
3
- export * from './module';
1
+ export * from './type';
2
+ export * from './Lark';
3
+ export * from './module';