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/LICENSE +165 -165
- package/ReadMe.md +39 -42
- package/dist/Lark.d.ts +22 -8
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module/BITable/index.d.ts +45 -49
- package/dist/module/BITable/type.d.ts +7 -1
- package/dist/module/BITable/utility.d.ts +10 -0
- package/dist/module/InstantMessenger/index.d.ts +8 -8
- package/dist/module/Task/index.d.ts +16 -16
- package/dist/module/Wiki/type.d.ts +5 -0
- package/dist/module/index.d.ts +1 -0
- package/dist/type.d.ts +2 -2
- package/package.json +20 -20
- package/src/Lark.ts +226 -174
- package/src/index.ts +3 -3
- package/src/module/BITable/index.ts +283 -301
- package/src/module/BITable/type.ts +120 -112
- package/src/module/BITable/utility.ts +57 -0
- package/src/module/InstantMessenger/index.ts +82 -82
- package/src/module/InstantMessenger/type/index.ts +60 -60
- package/src/module/InstantMessenger/type/message.ts +141 -141
- package/src/module/SpreadSheet/index.ts +78 -78
- package/src/module/SpreadSheet/type.ts +31 -31
- package/src/module/Task/index.ts +278 -278
- package/src/module/Task/type.ts +168 -168
- package/src/module/Wiki/type.ts +16 -0
- package/src/module/base.ts +34 -34
- package/src/module/index.ts +6 -5
- package/src/type.ts +75 -71
- package/tsconfig.json +21 -21
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.client
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
return
|
|
136
|
-
}, '
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @see {@link https://open.feishu.cn/document/
|
|
140
|
-
*/
|
|
141
|
-
async
|
|
142
|
-
await this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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';
|