-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiscussion.ts
More file actions
78 lines (59 loc) · 2.27 KB
/
Copy pathDiscussion.ts
File metadata and controls
78 lines (59 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
import { components } from '@octokit/openapi-types';
import { Filter, ListModel, Stream } from 'mobx-restful';
import { buildURLData } from 'web-utility';
import { BaseFilter, githubClient } from './client';
export type Discussion = components['schemas']['discussion'];
export type DiscussionComment = components['schemas']['webhooks_answer'];
export type DiscussionFilter = Filter<Discussion> & BaseFilter;
export type DiscussionCommentFilter = Filter<DiscussionComment> & BaseFilter;
export class DiscussionModel extends Stream<Discussion, DiscussionFilter>(ListModel) {
client = githubClient;
constructor(
public owner: string,
public repository: string
) {
super();
this.baseURI = `repos/${owner}/${repository}/discussions`;
}
async *openStream(filter: DiscussionFilter) {
const { client, baseURI, pageSize: per_page } = this;
var count = 0;
for (let page = 1; ; page++) {
const { body } = await client.get<Discussion[]>(
`${baseURI}?${buildURLData({ per_page, page, ...filter })}`
);
if (!body![0]) break;
count += body!.length;
yield* body!;
if (body!.length < this.pageSize) break;
}
this.totalCount = count;
}
}
export class DiscussionCommentModel extends Stream<DiscussionComment, DiscussionCommentFilter>(
ListModel
) {
client = githubClient;
constructor(
public owner: string,
public repository: string,
public discussion: number
) {
super();
this.baseURI = `repos/${owner}/${repository}/discussions/${discussion}/comments`;
}
async *openStream(filter: DiscussionCommentFilter) {
const { client, baseURI, pageSize: per_page } = this;
const { body } = await client.get<Discussion>(baseURI.split('/').slice(0, -1).join('/'));
this.totalCount = body!.comments;
if (!this.totalCount) return;
for (let page = 1; ; page++) {
const { body } = await client.get<DiscussionComment[]>(
`${baseURI}?${buildURLData({ per_page, page, ...filter })}`
);
if (!body![0]) break;
yield* body!;
if (body!.length < this.pageSize) break;
}
}
}