mobx-lark 2.0.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.
package/src/Lark.ts CHANGED
@@ -1,174 +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 LarkAppOption {
14
- host?: string;
15
- id: string;
16
- secret?: string;
17
- }
18
-
19
- export class LarkApp implements LarkAppOption {
20
- host?: string;
21
- id: string;
22
- secret?: string;
23
-
24
- client: HTTPClient<Context>;
25
- accessToken?: string;
26
-
27
- constructor({
28
- host = 'https://open.feishu.cn/open-apis/',
29
- id,
30
- secret
31
- }: LarkAppOption) {
32
- console.assert(
33
- !globalThis.window || !secret,
34
- "App Secret can't be used in client"
35
- );
36
- this.host = host;
37
- this.id = id;
38
- this.secret = secret;
39
-
40
- this.client = new HTTPClient({ baseURI: host, responseType: 'json' });
41
-
42
- this.boot();
43
- }
44
-
45
- private boot() {
46
- this.client.use(async ({ request, response }, next) => {
47
- const { accessToken } = this;
48
-
49
- if (accessToken)
50
- request.headers = {
51
- ...request.headers,
52
- Authorization: `Bearer ${accessToken}`
53
- };
54
- try {
55
- await next();
56
-
57
- const { body } = response;
58
-
59
- if (isLarkError(body)) {
60
- console.error(body);
61
- throw new URIError(body.msg);
62
- }
63
- } catch (error) {
64
- const { method, path } = request,
65
- { status, body } = response;
66
-
67
- console.error(method, path, status, body);
68
- throw error;
69
- }
70
- });
71
- }
72
-
73
- getAccessToken() {
74
- return this.getTenantAccessToken();
75
- }
76
-
77
- /**
78
- * Back-end only
79
- *
80
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/tenant_access_token_internal}
81
- */
82
- getTenantAccessToken = cache(async clean => {
83
- const { id, secret } = this;
84
-
85
- console.assert(id && secret, 'Id & Secret of Lark App are required');
86
-
87
- const { body } = await this.client.post<TenantAccessToken>(
88
- 'auth/v3/tenant_access_token/internal',
89
- { app_id: id, app_secret: secret }
90
- );
91
- setTimeout(() => {
92
- delete this.accessToken;
93
- clean();
94
- }, body!.expire * Second);
95
-
96
- return (this.accessToken = body!.tenant_access_token);
97
- }, 'Tenant Access Token');
98
-
99
- /**
100
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/ukzN4UjL5cDO14SO3gTN}
101
- */
102
- getWebSignInURL(redirect_uri: string, state?: string) {
103
- return `${this.client.baseURI}authen/v1/index?${buildURLData({
104
- app_id: this.id,
105
- redirect_uri,
106
- state
107
- })}`;
108
- }
109
-
110
- /**
111
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/authen/access_token}
112
- */
113
- async getUserMeta(code: string) {
114
- await this.getAccessToken();
115
-
116
- const { body } = await this.client.post<LarkData<UserMeta>>(
117
- 'authen/v1/access_token',
118
- { grant_type: 'authorization_code', code }
119
- );
120
- return body!.data;
121
- }
122
-
123
- /**
124
- * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/uYTM5UjL2ETO14iNxkTN/h5_js_sdk/authorization}
125
- */
126
- getJSTicket = cache(async clean => {
127
- await this.getAccessToken();
128
-
129
- const { body } =
130
- await this.client.post<LarkData<JSTicket>>('jssdk/ticket/get');
131
- const { expire_in, ticket } = body!.data!;
132
-
133
- setTimeout(clean, expire_in * Second);
134
-
135
- return ticket;
136
- }, 'JS ticket');
137
-
138
- /**
139
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download}
140
- */
141
- async downloadFile(id: string) {
142
- await this.getAccessToken();
143
-
144
- const { body } = await this.client.request<ArrayBuffer>({
145
- path: `drive/v1/medias/${id}/download`,
146
- responseType: 'arraybuffer'
147
- });
148
- return body!;
149
- }
150
-
151
- /**
152
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all}
153
- */
154
- async uploadFile(
155
- file: File,
156
- parent_type: UploadTargetType,
157
- parent_node: string
158
- ) {
159
- await this.getAccessToken();
160
-
161
- const form = makeFormData({
162
- file,
163
- file_name: file.name,
164
- size: file.size,
165
- parent_type,
166
- parent_node
167
- });
168
- const { body } = await this.client.post<
169
- LarkData<{ file_token: string }>
170
- >('drive/v1/medias/upload_all', form);
171
-
172
- return body!.data!.file_token;
173
- }
174
- }
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';