-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathservice.ts
107 lines (95 loc) · 3.36 KB
/
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 { ConfigService } from "src/config/service";
import { FetchService } from "src/fetch/service";
import { Service } from "typedi";
import {
GeneralGithubQuery,
GetRepositoryInput,
GetRepositoryIssuesResponse,
GetRepositoryResponse,
GetUserInput,
GitHubListRepositoryMilestonesInput,
GithubMilestone,
GitHubRateLimitApiResponse,
GitHubUserApiResponse,
ListRepositoryContributorsResponse,
} from "./types";
@Service()
export class GithubService {
constructor(
private readonly configService: ConfigService,
private readonly fetchService: FetchService,
) {}
public getUser = async ({ username }: GetUserInput): Promise<GitHubUserApiResponse> => {
const user = await this.fetchService.get<GitHubUserApiResponse>(
`${this.apiURL}/users/${username}`,
{ headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {} },
);
return user;
};
public listRepositoryIssues = async ({
owner,
repo,
}: GetRepositoryInput): Promise<GetRepositoryIssuesResponse> => {
const repoIssues = await this.fetchService.get<GetRepositoryIssuesResponse>(
`${this.apiURL}/repos/${owner}/${repo}/issues`,
{
headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {},
// @TODO-ZM: add pagination
params: { sort: "updated", per_page: 100 },
},
);
return repoIssues;
};
public getRepository = async ({
owner,
repo,
}: GetRepositoryInput): Promise<GetRepositoryResponse> => {
const repoInfo = await this.fetchService.get<GetRepositoryResponse>(
`${this.apiURL}/repos/${owner}/${repo}`,
{ headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {} },
);
return repoInfo;
};
public listRepositoryContributors = async ({
owner,
repository,
}: Omit<GeneralGithubQuery, "path">): Promise<ListRepositoryContributorsResponse> => {
const contributors = await this.fetchService.get<ListRepositoryContributorsResponse>(
`${this.apiURL}/repos/${owner}/${repository}/contributors`,
{
headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {},
params: { state: "all", per_page: 100 },
},
);
// @TODO-ZM: validate responses using DTOs, for all fetchService methods
if (!Array.isArray(contributors)) return [];
return contributors;
};
public getRateLimit = async (): Promise<{ limit: number; used: number; ratio: number }> => {
const rateLimitInfo = await this.fetchService.get<GitHubRateLimitApiResponse>(
`${this.apiURL}/rate_limit`,
{ headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {} },
);
const { limit, used } = rateLimitInfo.rate;
return {
limit,
used,
ratio: used / limit,
};
};
public listRepositoryMilestones = async ({
owner,
repository,
}: GitHubListRepositoryMilestonesInput): Promise<GithubMilestone[]> => {
const milestones = await this.fetchService.get<GithubMilestone[]>(
`${this.apiURL}/repos/${owner}/${repository}/milestones`,
{
headers: this.githubToken ? { Authorization: `Token ${this.githubToken}` } : {},
params: { state: "all", per_page: 100 },
},
);
return milestones;
};
private githubToken = this.configService.env().GITHUB_TOKEN;
private apiURL = "https://api.github.com";
}