|
| 1 | +import { Repository, RepositoryModel, UserModel } from 'mobx-github'; |
| 2 | +import { Filter, ListModel, toggle } from 'mobx-restful'; |
| 3 | +import { buildURLData } from 'web-utility'; |
| 4 | + |
| 5 | +import { |
| 6 | + githubClient, |
| 7 | + githubRawClient, |
| 8 | + GithubSearchData, |
| 9 | + makeGithubSearchCondition, |
| 10 | +} from './Base'; |
| 11 | + |
| 12 | +export class GitRepositoryModel extends RepositoryModel { |
| 13 | + @toggle('downloading') |
| 14 | + async downloadRaw( |
| 15 | + path: string, |
| 16 | + repository = this.currentOne.name, |
| 17 | + ref = this.currentOne.default_branch, |
| 18 | + ) { |
| 19 | + const owner = this.owner || (await userStore.getSession()).login; |
| 20 | + const identity = `${owner}/${repository}`; |
| 21 | + |
| 22 | + if (!ref) { |
| 23 | + const { default_branch } = await this.getOne(identity); |
| 24 | + |
| 25 | + ref = default_branch; |
| 26 | + } |
| 27 | + const { body } = await githubRawClient.get<ArrayBuffer>( |
| 28 | + `${identity}/${ref}/${path}`, |
| 29 | + ); |
| 30 | + |
| 31 | + return body!; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export const userStore = new UserModel(); |
| 36 | +export const repositoryStore = new GitRepositoryModel('Open-Source-Bazaar'); |
| 37 | + |
| 38 | +export type RepositoryFilter = Filter<Repository>; |
| 39 | + |
| 40 | +export class RepositorySearchModel extends ListModel< |
| 41 | + Repository, |
| 42 | + RepositoryFilter |
| 43 | +> { |
| 44 | + baseURI = 'search/repositories'; |
| 45 | + client = githubClient; |
| 46 | + |
| 47 | + async loadPage( |
| 48 | + page = this.pageIndex, |
| 49 | + per_page = this.pageSize, |
| 50 | + { full_name }: RepositoryFilter, |
| 51 | + ) { |
| 52 | + const name = full_name?.split('/').at(-1); |
| 53 | + |
| 54 | + const queryMap = { in: name ? 'name' : undefined }, |
| 55 | + keyword = name; |
| 56 | + const condition = makeGithubSearchCondition(queryMap); |
| 57 | + |
| 58 | + const { body } = await this.client.get<GithubSearchData<Repository>>( |
| 59 | + `${this.baseURI}?${buildURLData({ page, per_page, q: `${condition} ${keyword}` })}`, |
| 60 | + ); |
| 61 | + return { pageData: body!.items, totalCount: body!.total_count }; |
| 62 | + } |
| 63 | +} |
0 commit comments