mobx-lark 2.1.1 → 2.2.0

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.
@@ -1,278 +1,278 @@
1
- import {
2
- Filter,
3
- IDType,
4
- ListModel,
5
- NewData,
6
- Stream,
7
- toggle
8
- } from 'mobx-restful';
9
- import { buildURLData } from 'web-utility';
10
-
11
- import { LarkData, UserIdType } from '../../type';
12
- import { createPageStream } from '../base';
13
- import {
14
- Task,
15
- TaskField,
16
- TaskList,
17
- TaskListSection,
18
- TaskResource,
19
- TaskSummary
20
- } from './type';
21
-
22
- export * from './type';
23
-
24
- export interface BaseTaskFilter {
25
- user_id_type?: UserIdType;
26
- }
27
-
28
- export interface TaskFilter
29
- extends Filter<Task>,
30
- BaseTaskFilter,
31
- Partial<TaskResource>,
32
- Record<`created_${'from' | 'to'}`, string> {
33
- completed?: boolean;
34
- }
35
-
36
- export abstract class TaskModel extends Stream<Task, TaskFilter>(ListModel) {
37
- baseURI = 'task/v2/tasks';
38
-
39
- /**
40
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/list}
41
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/tasks}
42
- */
43
- async *openStream({
44
- resource_type = 'my_tasks',
45
- resource_id,
46
- user_id_type = 'union_id',
47
- ...rest
48
- }: TaskFilter) {
49
- if (resource_type === 'my_tasks') {
50
- const stream = createPageStream<Task>(
51
- this.client,
52
- this.baseURI,
53
- total => (this.totalCount = total),
54
- { type: resource_type, user_id_type, ...rest }
55
- );
56
- for await (const item of stream) yield item;
57
- } else {
58
- const stream = createPageStream<TaskSummary>(
59
- this.client,
60
- `task/v2/tasklists/${resource_id}/tasks`,
61
- total => (this.totalCount = total),
62
- { user_id_type, ...rest }
63
- );
64
- for await (const { guid } of stream)
65
- yield await this.getOne(guid, user_id_type);
66
- }
67
- }
68
-
69
- /**
70
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/get}
71
- */
72
- @toggle('downloading')
73
- async getOne(id: string, user_id_type: UserIdType = 'union_id') {
74
- const { body } = await this.client.get<LarkData<{ task: Task }>>(
75
- `${this.baseURI}/${id}?${buildURLData({ user_id_type })}`
76
- );
77
- return (this.currentOne = body!.data!.task);
78
- }
79
-
80
- /**
81
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/create}
82
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/patch}
83
- */
84
- @toggle('uploading')
85
- async updateOne(
86
- task: NewData<Task>,
87
- id?: string,
88
- user_id_type: UserIdType = 'union_id'
89
- ) {
90
- const path = `task/v2/tasks?${buildURLData({ user_id_type })}`;
91
- const { body } = id
92
- ? await this.client.patch<LarkData<{ task: Task }>>(path, {
93
- task,
94
- update_fields: Object.keys(task)
95
- })
96
- : await this.client.post<LarkData<{ task: Task }>>(path, task);
97
-
98
- return body!.data!.task;
99
- }
100
- }
101
-
102
- export type TaskListFilter = Filter<TaskList> & BaseTaskFilter;
103
-
104
- export abstract class TaskListModel extends Stream<TaskList, TaskListFilter>(
105
- ListModel
106
- ) {
107
- baseURI = 'task/v2/tasklists';
108
-
109
- /**
110
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/list}
111
- */
112
- async *openStream({ user_id_type = 'union_id' }: TaskListFilter) {
113
- for await (const item of createPageStream<TaskList>(
114
- this.client,
115
- this.baseURI,
116
- total => (this.totalCount = total),
117
- { user_id_type }
118
- ))
119
- yield item;
120
- }
121
-
122
- /**
123
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/get}
124
- */
125
- @toggle('downloading')
126
- async getOne(id: IDType, user_id_type: UserIdType = 'union_id') {
127
- const { body } = await this.client.get<
128
- LarkData<{ tasklist: TaskList }>
129
- >(`${this.baseURI}/${id}?${buildURLData({ user_id_type })}`);
130
-
131
- return (this.currentOne = body!.data!.tasklist);
132
- }
133
-
134
- /**
135
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/create}
136
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/patch}
137
- */
138
- @toggle('uploading')
139
- async updateOne(
140
- tasklist: NewData<TaskList>,
141
- id?: string,
142
- user_id_type: UserIdType = 'union_id'
143
- ) {
144
- const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
145
- const { body } = id
146
- ? await this.client.post<LarkData<{ tasklist: TaskList }>>(path, {
147
- tasklist,
148
- update_fields: Object.keys(tasklist)
149
- })
150
- : await this.client.post<LarkData<{ tasklist: TaskList }>>(
151
- path,
152
- tasklist
153
- );
154
- return body!.data!.tasklist;
155
- }
156
- }
157
-
158
- export type TaskListSectionFilter = Partial<TaskResource> & BaseTaskFilter;
159
-
160
- export abstract class TaskListSectionModel extends Stream<
161
- TaskListSection,
162
- TaskListSectionFilter
163
- >(ListModel) {
164
- baseURI = 'task/v2/sections';
165
-
166
- /**
167
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/list}
168
- */
169
- async *openStream({
170
- resource_type = 'my_tasks',
171
- resource_id,
172
- user_id_type = 'union_id'
173
- }: TaskListSectionFilter) {
174
- const stream = createPageStream<
175
- Pick<TaskListSection, 'guid' | 'name' | 'is_default'>
176
- >(this.client, this.baseURI, total => (this.totalCount = total), {
177
- resource_type,
178
- resource_id,
179
- user_id_type
180
- });
181
-
182
- for await (const { guid } of stream) yield await this.getOne(guid);
183
- }
184
-
185
- /**
186
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/get}
187
- */
188
- @toggle('downloading')
189
- async getOne(id: string) {
190
- const { body } = await this.client.get<
191
- LarkData<{ section: TaskListSection }>
192
- >(`${this.baseURI}/${id}`);
193
-
194
- return (this.currentOne = body!.data!.section);
195
- }
196
-
197
- /**
198
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/create}
199
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/patch}
200
- */
201
- @toggle('uploading')
202
- async updateOne(
203
- section: NewData<TaskListSection>,
204
- id?: string,
205
- user_id_type: UserIdType = 'union_id',
206
- [target]: Partial<TaskResource>[] = []
207
- ) {
208
- const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
209
- const { body } = id
210
- ? await this.client.patch<LarkData<{ section: TaskListSection }>>(
211
- path,
212
- { section, update_fields: Object.keys(section) }
213
- )
214
- : await this.client.post<LarkData<{ section: TaskListSection }>>(
215
- path,
216
- { name: section.name, ...target }
217
- );
218
- return body!.data!.section;
219
- }
220
- }
221
-
222
- export type TaskFieldFilter = Filter<TaskField> &
223
- Partial<TaskListSectionFilter>;
224
-
225
- export abstract class TaskFieldModel extends Stream<TaskField, TaskFieldFilter>(
226
- ListModel
227
- ) {
228
- baseURI = 'task/v2/custom_fields';
229
-
230
- /**
231
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/list}
232
- */
233
- async *openStream({
234
- resource_type = 'tasklist',
235
- resource_id,
236
- user_id_type = 'union_id'
237
- }: TaskFieldFilter) {
238
- for await (const item of createPageStream<TaskField>(
239
- this.client,
240
- this.baseURI,
241
- total => (this.totalCount = total),
242
- { resource_type, resource_id, user_id_type }
243
- ))
244
- yield item;
245
- }
246
-
247
- /**
248
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/create}
249
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/patch}
250
- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/add}
251
- */
252
- @toggle('uploading')
253
- async updateOne(
254
- data: NewData<TaskField>,
255
- id?: string,
256
- user_id_type: UserIdType = 'union_id',
257
- [target, ...targets]: TaskResource[] = []
258
- ) {
259
- const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
260
- const { body } = id
261
- ? await this.client.patch<LarkData<{ custom_field: TaskField }>>(
262
- path,
263
- { custom_field: data, update_fields: Object.keys(data) }
264
- )
265
- : await this.client.post<LarkData<{ custom_field: TaskField }>>(
266
- path,
267
- { ...data, ...target }
268
- );
269
- const { custom_field } = body!.data!;
270
-
271
- for (const body of targets)
272
- await this.client.post(
273
- `${this.baseURI}/${custom_field.guid}/add`,
274
- body
275
- );
276
- return custom_field;
277
- }
278
- }
1
+ import {
2
+ Filter,
3
+ IDType,
4
+ ListModel,
5
+ NewData,
6
+ Stream,
7
+ toggle
8
+ } from 'mobx-restful';
9
+ import { buildURLData } from 'web-utility';
10
+
11
+ import { LarkData, UserIdType } from '../../type';
12
+ import { createPageStream } from '../base';
13
+ import {
14
+ Task,
15
+ TaskField,
16
+ TaskList,
17
+ TaskListSection,
18
+ TaskResource,
19
+ TaskSummary
20
+ } from './type';
21
+
22
+ export * from './type';
23
+
24
+ export interface BaseTaskFilter {
25
+ user_id_type?: UserIdType;
26
+ }
27
+
28
+ export interface TaskFilter
29
+ extends Filter<Task>,
30
+ BaseTaskFilter,
31
+ Partial<TaskResource>,
32
+ Record<`created_${'from' | 'to'}`, string> {
33
+ completed?: boolean;
34
+ }
35
+
36
+ export abstract class TaskModel extends Stream<Task, TaskFilter>(ListModel) {
37
+ baseURI = 'task/v2/tasks';
38
+
39
+ /**
40
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/list}
41
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/tasks}
42
+ */
43
+ async *openStream({
44
+ resource_type = 'my_tasks',
45
+ resource_id,
46
+ user_id_type = 'union_id',
47
+ ...rest
48
+ }: TaskFilter) {
49
+ if (resource_type === 'my_tasks') {
50
+ const stream = createPageStream<Task>(
51
+ this.client,
52
+ this.baseURI,
53
+ total => (this.totalCount = total),
54
+ { type: resource_type, user_id_type, ...rest }
55
+ );
56
+ for await (const item of stream) yield item;
57
+ } else {
58
+ const stream = createPageStream<TaskSummary>(
59
+ this.client,
60
+ `task/v2/tasklists/${resource_id}/tasks`,
61
+ total => (this.totalCount = total),
62
+ { user_id_type, ...rest }
63
+ );
64
+ for await (const { guid } of stream)
65
+ yield await this.getOne(guid, user_id_type);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/get}
71
+ */
72
+ @toggle('downloading')
73
+ async getOne(id: string, user_id_type: UserIdType = 'union_id') {
74
+ const { body } = await this.client.get<LarkData<{ task: Task }>>(
75
+ `${this.baseURI}/${id}?${buildURLData({ user_id_type })}`
76
+ );
77
+ return (this.currentOne = body!.data!.task);
78
+ }
79
+
80
+ /**
81
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/create}
82
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/task/patch}
83
+ */
84
+ @toggle('uploading')
85
+ async updateOne(
86
+ task: NewData<Task>,
87
+ id?: string,
88
+ user_id_type: UserIdType = 'union_id'
89
+ ) {
90
+ const path = `task/v2/tasks?${buildURLData({ user_id_type })}`;
91
+ const { body } = id
92
+ ? await this.client.patch<LarkData<{ task: Task }>>(path, {
93
+ task,
94
+ update_fields: Object.keys(task)
95
+ })
96
+ : await this.client.post<LarkData<{ task: Task }>>(path, task);
97
+
98
+ return body!.data!.task;
99
+ }
100
+ }
101
+
102
+ export type TaskListFilter = Filter<TaskList> & BaseTaskFilter;
103
+
104
+ export abstract class TaskListModel extends Stream<TaskList, TaskListFilter>(
105
+ ListModel
106
+ ) {
107
+ baseURI = 'task/v2/tasklists';
108
+
109
+ /**
110
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/list}
111
+ */
112
+ async *openStream({ user_id_type = 'union_id' }: TaskListFilter) {
113
+ for await (const item of createPageStream<TaskList>(
114
+ this.client,
115
+ this.baseURI,
116
+ total => (this.totalCount = total),
117
+ { user_id_type }
118
+ ))
119
+ yield item;
120
+ }
121
+
122
+ /**
123
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/get}
124
+ */
125
+ @toggle('downloading')
126
+ async getOne(id: IDType, user_id_type: UserIdType = 'union_id') {
127
+ const { body } = await this.client.get<
128
+ LarkData<{ tasklist: TaskList }>
129
+ >(`${this.baseURI}/${id}?${buildURLData({ user_id_type })}`);
130
+
131
+ return (this.currentOne = body!.data!.tasklist);
132
+ }
133
+
134
+ /**
135
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/create}
136
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/tasklist/patch}
137
+ */
138
+ @toggle('uploading')
139
+ async updateOne(
140
+ tasklist: NewData<TaskList>,
141
+ id?: string,
142
+ user_id_type: UserIdType = 'union_id'
143
+ ) {
144
+ const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
145
+ const { body } = id
146
+ ? await this.client.post<LarkData<{ tasklist: TaskList }>>(path, {
147
+ tasklist,
148
+ update_fields: Object.keys(tasklist)
149
+ })
150
+ : await this.client.post<LarkData<{ tasklist: TaskList }>>(
151
+ path,
152
+ tasklist
153
+ );
154
+ return body!.data!.tasklist;
155
+ }
156
+ }
157
+
158
+ export type TaskListSectionFilter = Partial<TaskResource> & BaseTaskFilter;
159
+
160
+ export abstract class TaskListSectionModel extends Stream<
161
+ TaskListSection,
162
+ TaskListSectionFilter
163
+ >(ListModel) {
164
+ baseURI = 'task/v2/sections';
165
+
166
+ /**
167
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/list}
168
+ */
169
+ async *openStream({
170
+ resource_type = 'my_tasks',
171
+ resource_id,
172
+ user_id_type = 'union_id'
173
+ }: TaskListSectionFilter) {
174
+ const stream = createPageStream<
175
+ Pick<TaskListSection, 'guid' | 'name' | 'is_default'>
176
+ >(this.client, this.baseURI, total => (this.totalCount = total), {
177
+ resource_type,
178
+ resource_id,
179
+ user_id_type
180
+ });
181
+
182
+ for await (const { guid } of stream) yield await this.getOne(guid);
183
+ }
184
+
185
+ /**
186
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/get}
187
+ */
188
+ @toggle('downloading')
189
+ async getOne(id: string) {
190
+ const { body } = await this.client.get<
191
+ LarkData<{ section: TaskListSection }>
192
+ >(`${this.baseURI}/${id}`);
193
+
194
+ return (this.currentOne = body!.data!.section);
195
+ }
196
+
197
+ /**
198
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/create}
199
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/section/patch}
200
+ */
201
+ @toggle('uploading')
202
+ async updateOne(
203
+ section: NewData<TaskListSection>,
204
+ id?: string,
205
+ user_id_type: UserIdType = 'union_id',
206
+ [target]: Partial<TaskResource>[] = []
207
+ ) {
208
+ const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
209
+ const { body } = id
210
+ ? await this.client.patch<LarkData<{ section: TaskListSection }>>(
211
+ path,
212
+ { section, update_fields: Object.keys(section) }
213
+ )
214
+ : await this.client.post<LarkData<{ section: TaskListSection }>>(
215
+ path,
216
+ { name: section.name, ...target }
217
+ );
218
+ return body!.data!.section;
219
+ }
220
+ }
221
+
222
+ export type TaskFieldFilter = Filter<TaskField> &
223
+ Partial<TaskListSectionFilter>;
224
+
225
+ export abstract class TaskFieldModel extends Stream<TaskField, TaskFieldFilter>(
226
+ ListModel
227
+ ) {
228
+ baseURI = 'task/v2/custom_fields';
229
+
230
+ /**
231
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/list}
232
+ */
233
+ async *openStream({
234
+ resource_type = 'tasklist',
235
+ resource_id,
236
+ user_id_type = 'union_id'
237
+ }: TaskFieldFilter) {
238
+ for await (const item of createPageStream<TaskField>(
239
+ this.client,
240
+ this.baseURI,
241
+ total => (this.totalCount = total),
242
+ { resource_type, resource_id, user_id_type }
243
+ ))
244
+ yield item;
245
+ }
246
+
247
+ /**
248
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/create}
249
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/patch}
250
+ * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/task-v2/custom_field/add}
251
+ */
252
+ @toggle('uploading')
253
+ async updateOne(
254
+ data: NewData<TaskField>,
255
+ id?: string,
256
+ user_id_type: UserIdType = 'union_id',
257
+ [target, ...targets]: TaskResource[] = []
258
+ ) {
259
+ const path = `${this.baseURI}?${buildURLData({ user_id_type })}`;
260
+ const { body } = id
261
+ ? await this.client.patch<LarkData<{ custom_field: TaskField }>>(
262
+ path,
263
+ { custom_field: data, update_fields: Object.keys(data) }
264
+ )
265
+ : await this.client.post<LarkData<{ custom_field: TaskField }>>(
266
+ path,
267
+ { ...data, ...target }
268
+ );
269
+ const { custom_field } = body!.data!;
270
+
271
+ for (const body of targets)
272
+ await this.client.post(
273
+ `${this.baseURI}/${custom_field.guid}/add`,
274
+ body
275
+ );
276
+ return custom_field;
277
+ }
278
+ }