Blame · Line-by-line history
repos.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 5c83ccc | 1 | import { fetchJSON, postJSON } from './client'; |
| 9a8fbed | 2 | import { useSettingsStore } from '../store/settingsStore'; |
| 5c83ccc | 3 | |
| 4 | export interface Repository { | |
| 5 | id: number; | |
| 6 | name: string; | |
| 7 | fullName: string; | |
| 8 | description: string | null; | |
| 9 | isPrivate: boolean; | |
| 10 | isArchived: boolean; | |
| 11 | isFork: boolean; | |
| 12 | defaultBranch: string; | |
| 13 | language: string | null; | |
| 14 | starCount: number; | |
| 15 | forkCount: number; | |
| 16 | openIssueCount: number; | |
| 17 | ownerUsername: string; | |
| 18 | updatedAt: string; | |
| 19 | createdAt: string; | |
| 20 | } | |
| 21 | ||
| 22 | export interface TreeEntry { | |
| 23 | name: string; | |
| 24 | type: 'blob' | 'tree'; | |
| 25 | mode: string; | |
| 26 | sha: string; | |
| 27 | path: string; | |
| 28 | } | |
| 29 | ||
| 30 | export interface FileContent { | |
| 31 | name: string; | |
| 32 | path: string; | |
| 33 | sha: string; | |
| 34 | content: string; | |
| 35 | encoding: string; | |
| 36 | size: number; | |
| 37 | } | |
| 38 | ||
| 39 | export interface CommitEntry { | |
| 40 | sha: string; | |
| 41 | message: string; | |
| 42 | authorName: string; | |
| 43 | authorEmail: string; | |
| 44 | authorDate: string; | |
| 45 | committerName: string; | |
| 46 | committerDate: string; | |
| 47 | parentShas: string[]; | |
| 48 | } | |
| 49 | ||
| 50 | export interface BranchInfo { | |
| 51 | name: string; | |
| 52 | sha: string; | |
| 53 | isDefault: boolean; | |
| 54 | isProtected: boolean; | |
| 55 | } | |
| 56 | ||
| 57 | export interface SearchResult { | |
| 58 | file: string; | |
| 59 | line: number; | |
| 60 | content: string; | |
| 61 | sha: string; | |
| 62 | } | |
| 63 | ||
| 64 | export interface RepoStats { | |
| 65 | commits: number; | |
| 66 | branches: number; | |
| 67 | tags: number; | |
| 68 | contributors: number; | |
| 69 | } | |
| 70 | ||
| 71 | /** List repositories for a user. */ | |
| 72 | export async function listUserRepos(username: string): Promise<Repository[]> { | |
| 73 | return fetchJSON<Repository[]>(`/api/users/${encodeURIComponent(username)}/repos`); | |
| 74 | } | |
| 75 | ||
| 76 | /** List all public repos (explore). */ | |
| 77 | export async function listPublicRepos(page = 1): Promise<Repository[]> { | |
| 78 | return fetchJSON<Repository[]>(`/api/repos?page=${page}`); | |
| 79 | } | |
| 80 | ||
| 81 | /** Get a single repository. */ | |
| 82 | export async function getRepo(owner: string, repo: string): Promise<Repository> { | |
| 83 | return fetchJSON<Repository>(`/api/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`); | |
| 84 | } | |
| 85 | ||
| 86 | /** List the file tree for a repo at a given ref and path. */ | |
| 87 | export async function getTree( | |
| 88 | owner: string, | |
| 89 | repo: string, | |
| 90 | ref = 'HEAD', | |
| 91 | path = '', | |
| 92 | ): Promise<TreeEntry[]> { | |
| 93 | const encodedPath = path ? `/${encodeURIComponent(path)}` : ''; | |
| 94 | return fetchJSON<TreeEntry[]>( | |
| 95 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/tree/${encodeURIComponent(ref)}${encodedPath}?json=1`, | |
| 96 | ); | |
| 97 | } | |
| 98 | ||
| 99 | /** Get raw file content. */ | |
| 100 | export async function getFileContent( | |
| 101 | owner: string, | |
| 102 | repo: string, | |
| 103 | ref: string, | |
| 104 | path: string, | |
| 105 | ): Promise<string> { | |
| 9a8fbed | 106 | const host = useSettingsStore.getState().host; |
| 5c83ccc | 107 | const response = await fetch( |
| 9a8fbed | 108 | `${host}/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(ref)}/${path}`, |
| 5c83ccc | 109 | { |
| 110 | headers: { Accept: 'text/plain' }, | |
| 111 | }, | |
| 112 | ); | |
| 113 | if (!response.ok) throw new Error(`HTTP ${response.status}`); | |
| 114 | return response.text(); | |
| 115 | } | |
| 116 | ||
| 117 | /** List commits on a branch. */ | |
| 118 | export async function listCommits( | |
| 119 | owner: string, | |
| 120 | repo: string, | |
| 121 | branch = 'HEAD', | |
| 122 | page = 1, | |
| 123 | ): Promise<CommitEntry[]> { | |
| 124 | return fetchJSON<CommitEntry[]>( | |
| 125 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/commits?branch=${encodeURIComponent(branch)}&page=${page}&json=1`, | |
| 126 | ); | |
| 127 | } | |
| 128 | ||
| 129 | /** List branches. */ | |
| 130 | export async function listBranches(owner: string, repo: string): Promise<BranchInfo[]> { | |
| 131 | return fetchJSON<BranchInfo[]>( | |
| 132 | `/api/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/branches`, | |
| 133 | ); | |
| 134 | } | |
| 135 | ||
| 136 | /** Star a repo. */ | |
| 137 | export async function starRepo(owner: string, repo: string): Promise<void> { | |
| 138 | await postJSON(`/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/star`, {}); | |
| 139 | } | |
| 140 | ||
| 141 | /** Search code in a repo. */ | |
| 142 | export async function searchCode( | |
| 143 | owner: string, | |
| 144 | repo: string, | |
| 145 | query: string, | |
| 146 | ): Promise<SearchResult[]> { | |
| 147 | return fetchJSON<SearchResult[]>( | |
| 148 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/search?q=${encodeURIComponent(query)}&json=1`, | |
| 149 | ); | |
| 150 | } |