-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-api.service.ts
107 lines (86 loc) · 2.43 KB
/
github-api.service.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Injectable } from '@angular/core';
import { Octokit } from "octokit";
import { Observable, from } from 'rxjs';
export interface Project {
name: string;
url: string;
created_at: string;
id: number;
}
export interface User {
login: string;
url: string;
created_at: string;
avatar_url: string;
id: number;
}
const octokit = new Octokit({
auth: atob('Z2l0aHViX3BhdF8xMUFRWTQzQ0Ewd1NmOUpTNkpBN0tiX0RJSVJXdFdyTmtpY0ZzQUVLZ3lUdnZCVVJ3N0VOUU8wZ1hzRzI3Wmc5d05RN0tDMlRLQ3ZlSVIwaEZD')
});
@Injectable({
providedIn: 'root'
})
export class GithubApiService {
constructor() { }
private async fetchUserProjects(username: string): Promise<any>{
try {
const result = await octokit.request('GET /users/{username}/repos', {
username: username
});
const projects = result.data.map(project =>
({
name: project.name,
url: project.url,
created_at: project.created_at ? project.created_at : '',
id: project.id
})
)
return projects;
} catch (error: any) {
return new Error(error);
}
}
private async fetchProject(username: string, reponame: string): Promise<any>{
try {
const result = await octokit.request('GET /repos/{owner}/{repo}', {
owner: username,
repo: reponame
})
const project: Project = {
name: result.data.name,
url: result.data.url,
created_at: result.data.created_at ? result.data.created_at : '',
id: result.data.id
}
return project;
} catch (error: any) {
return new Error(error);
}
}
private async fetchUser(username: string): Promise<any>{
try {
const result = await octokit.request('GET /users/{username}', {
username: username
});
const user: User = {
login: result.data.login,
url: result.data.url,
created_at: result.data.created_at,
avatar_url: result.data.avatar_url,
id: result.data.id,
}
return user;
} catch (error: any) {
return new Error(error);
}
}
getUserProjects$(username: string): Observable<Project[]>{
return from(this.fetchUserProjects(username));
}
getProject$(username: string, reponame: string): Observable<Project>{
return from(this.fetchProject(username, reponame));
}
getUser$(username: string): Observable<User>{
return from(this.fetchUser(username));
}
}