-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRepository.ts
More file actions
63 lines (52 loc) · 1.69 KB
/
Repository.ts
File metadata and controls
63 lines (52 loc) · 1.69 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
import { Repository, RepositoryModel, UserModel } from 'mobx-github';
import { Filter, ListModel, toggle } from 'mobx-restful';
import { buildURLData } from 'web-utility';
import {
githubClient,
githubRawClient,
GithubSearchData,
makeGithubSearchCondition,
} from './Base';
export class GitRepositoryModel extends RepositoryModel {
@toggle('downloading')
async downloadRaw(
path: string,
repository = this.currentOne.name,
ref = this.currentOne.default_branch,
) {
const owner = this.owner || (await userStore.getSession()).login;
const identity = `${owner}/${repository}`;
if (!ref) {
const { default_branch } = await this.getOne(identity);
ref = default_branch;
}
const { body } = await githubRawClient.get<ArrayBuffer>(
`${identity}/${ref}/${path}`,
);
return body!;
}
}
export const userStore = new UserModel();
export const repositoryStore = new GitRepositoryModel('Open-Source-Bazaar');
export type RepositoryFilter = Filter<Repository>;
export class RepositorySearchModel extends ListModel<
Repository,
RepositoryFilter
> {
baseURI = 'search/repositories';
client = githubClient;
async loadPage(
page = this.pageIndex,
per_page = this.pageSize,
{ full_name }: RepositoryFilter,
) {
const name = full_name?.split('/').at(-1);
const queryMap = { in: name ? 'name' : undefined },
keyword = name;
const condition = makeGithubSearchCondition(queryMap);
const { body } = await this.client.get<GithubSearchData<Repository>>(
`${this.baseURI}?${buildURLData({ page, per_page, q: `${condition} ${keyword}` })}`,
);
return { pageData: body!.items, totalCount: body!.total_count };
}
}