|
| 1 | +import { components, operations } from "@octokit/openapi-types"; |
| 2 | +import { githubClient, RepositoryModel } from "mobx-github"; |
| 3 | +import { TranslationModel } from "mobx-i18n"; |
| 4 | +import { ListModel, Filter } from "mobx-restful"; |
| 5 | +import { buildURLData } from "web-utility"; |
| 6 | + |
| 7 | +export const i18n = new TranslationModel({ |
| 8 | + en_US: { |
| 9 | + load_more: "Load more", |
| 10 | + no_more: "No more", |
| 11 | + submit: "Submit", |
| 12 | + cancel: "Cancel", |
| 13 | + }, |
| 14 | +}); |
| 15 | + |
| 16 | +type Topic = components["schemas"]["topic-search-result-item"]; |
| 17 | + |
| 18 | +type TopicSearchResponse = |
| 19 | + operations["search/topics"]["responses"]["200"]["content"]["application/json"]; |
| 20 | + |
| 21 | +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; |
| 22 | + |
| 23 | +githubClient.use(({ request }, next) => { |
| 24 | + if (GITHUB_TOKEN) |
| 25 | + request.headers = { |
| 26 | + ...request.headers, |
| 27 | + Authorization: `Bearer ${GITHUB_TOKEN}`, |
| 28 | + }; |
| 29 | + return next(); |
| 30 | +}); |
| 31 | + |
| 32 | +class GitHubTopicModel extends ListModel<Topic> { |
| 33 | + baseURI = "search/topics"; |
| 34 | + client = githubClient; |
| 35 | + |
| 36 | + async loadPage(pageIndex: number, pageSize: number, { name }: Filter<Topic>) { |
| 37 | + const { body } = await this.client.get<TopicSearchResponse>( |
| 38 | + `${this.baseURI}?${buildURLData({ |
| 39 | + q: name, |
| 40 | + page: pageIndex, |
| 41 | + per_page: pageSize, |
| 42 | + })}` |
| 43 | + ); |
| 44 | + return { pageData: body!.items, totalCount: body!.total_count }; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export const repositoryStore = new RepositoryModel("idea2app"), |
| 49 | + topicStore = new GitHubTopicModel(); |
0 commit comments