140 lines
3.1 KiB
TypeScript
140 lines
3.1 KiB
TypeScript
|
import { Observable, switchMap, map } from 'rxjs';
|
||
|
import { fromFetch } from 'rxjs/fetch';
|
||
|
|
||
|
export interface GithubRepo {
|
||
|
id: number;
|
||
|
node_id: string;
|
||
|
name: string;
|
||
|
full_name: string;
|
||
|
private: boolean;
|
||
|
owner: Owner;
|
||
|
html_url: string;
|
||
|
description: string;
|
||
|
fork: boolean;
|
||
|
url: string;
|
||
|
forks_url: string;
|
||
|
keys_url: string;
|
||
|
collaborators_url: string;
|
||
|
teams_url: string;
|
||
|
hooks_url: string;
|
||
|
issue_events_url: string;
|
||
|
events_url: string;
|
||
|
assignees_url: string;
|
||
|
branches_url: string;
|
||
|
tags_url: string;
|
||
|
blobs_url: string;
|
||
|
git_tags_url: string;
|
||
|
git_refs_url: string;
|
||
|
trees_url: string;
|
||
|
statuses_url: string;
|
||
|
languages_url: string;
|
||
|
stargazers_url: string;
|
||
|
contributors_url: string;
|
||
|
subscribers_url: string;
|
||
|
subscription_url: string;
|
||
|
commits_url: string;
|
||
|
git_commits_url: string;
|
||
|
comments_url: string;
|
||
|
issue_comment_url: string;
|
||
|
contents_url: string;
|
||
|
compare_url: string;
|
||
|
merges_url: string;
|
||
|
archive_url: string;
|
||
|
downloads_url: string;
|
||
|
issues_url: string;
|
||
|
pulls_url: string;
|
||
|
milestones_url: string;
|
||
|
notifications_url: string;
|
||
|
labels_url: string;
|
||
|
releases_url: string;
|
||
|
deployments_url: string;
|
||
|
created_at: Date;
|
||
|
updated_at: Date;
|
||
|
pushed_at: Date;
|
||
|
git_url: string;
|
||
|
ssh_url: string;
|
||
|
clone_url: string;
|
||
|
svn_url: string;
|
||
|
homepage: string;
|
||
|
size: number;
|
||
|
stargazers_count: number;
|
||
|
watchers_count: number;
|
||
|
language: string;
|
||
|
has_issues: boolean;
|
||
|
has_projects: boolean;
|
||
|
has_downloads: boolean;
|
||
|
has_wiki: boolean;
|
||
|
has_pages: boolean;
|
||
|
forks_count: number;
|
||
|
mirror_url: null;
|
||
|
archived: boolean;
|
||
|
disabled: boolean;
|
||
|
open_issues_count: number;
|
||
|
license: null;
|
||
|
allow_forking: boolean;
|
||
|
is_template: boolean;
|
||
|
web_commit_signoff_required: boolean;
|
||
|
topics: any[];
|
||
|
visibility: string;
|
||
|
forks: number;
|
||
|
open_issues: number;
|
||
|
watchers: number;
|
||
|
default_branch: string;
|
||
|
}
|
||
|
|
||
|
export interface Owner {
|
||
|
login: string;
|
||
|
id: number;
|
||
|
node_id: string;
|
||
|
avatar_url: string;
|
||
|
gravatar_id: string;
|
||
|
url: string;
|
||
|
html_url: string;
|
||
|
followers_url: string;
|
||
|
following_url: string;
|
||
|
gists_url: string;
|
||
|
starred_url: string;
|
||
|
subscriptions_url: string;
|
||
|
organizations_url: string;
|
||
|
repos_url: string;
|
||
|
events_url: string;
|
||
|
received_events_url: string;
|
||
|
type: string;
|
||
|
site_admin: boolean;
|
||
|
}
|
||
|
|
||
|
export interface GithubError {
|
||
|
message: string;
|
||
|
documentation_url: string;
|
||
|
}
|
||
|
|
||
|
export function getLatestRepositories(
|
||
|
user: string,
|
||
|
amount: number
|
||
|
): Observable<GithubRepo[]> {
|
||
|
return getRepositoriesOfUser(user).pipe(
|
||
|
map((repositories: GithubRepo[]) => {
|
||
|
return repositories
|
||
|
.sort(
|
||
|
(a: GithubRepo, b: GithubRepo) =>
|
||
|
+b.updated_at - +a.updated_at
|
||
|
)
|
||
|
.slice(0, amount);
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function getRepositoriesOfUser(user: string): Observable<GithubRepo[]> {
|
||
|
const fetchUrl = `https://api.github.com/users/${user}/repos`;
|
||
|
return fromFetch(fetchUrl).pipe(
|
||
|
switchMap((response: Response) => {
|
||
|
if (response.ok) {
|
||
|
return response.json();
|
||
|
} else {
|
||
|
console.error(`Error ${response.status}: ${JSON.stringify(response)}`);
|
||
|
return [];
|
||
|
}
|
||
|
}),
|
||
|
);
|
||
|
}
|