1
+ import { Injectable } from "@nestjs/common" ;
1
2
import axios from "axios" ;
2
3
3
- import { GitHubEmailApiResponseDto } from "_types/GitHub/email-api-response.dto" ;
4
- import { GitHubUserDataApiResponseDto } from "_types/GitHub/user-data-api-response.dto" ;
4
+ import { GithubEmailApiResponseDto } from "_types/Github/email-api-response.dto" ;
5
+ import { GithubUserDataApiResponseDto } from "_types/Github/user-data-api-response.dto" ;
6
+ import { GithubAccountDataDto } from "_types/github-account-data.dto" ;
5
7
6
8
7
9
const GITHUB_ACCESS_TOKEN_API = "https://github.com/login/oauth/access_token" ;
8
10
const GITHUB_USER_DATA_API = "https://api.github.com/user" ;
9
11
const GITHUB_EMAIL_DATA_API = "https://api.github.com/user/emails" ;
10
12
11
- export async function getGitHubAccountData ( code : string ) {
12
- try {
13
- const accessToken = await getAccessToken ( code ) ;
14
- if ( ! accessToken ) {
15
- throw new Error ( "Error while getting access token" ) ;
16
- }
13
+ @Injectable ( )
14
+ export class GithubService {
15
+ async getGitHubAccountData (
16
+ code : string ,
17
+ ) : Promise < GithubAccountDataDto | null > {
18
+ try {
19
+ const accessToken = await getAccessToken ( code ) ;
20
+ const accountData = await getAccountData ( accessToken ) ;
17
21
18
- const accountData = await getAccountData ( accessToken ) ;
19
- return accountData ;
20
- }
21
- catch ( error ) {
22
- console . error ( error . message ) ;
23
- return null ;
22
+ return accountData ;
23
+ }
24
+ catch ( error ) {
25
+ throw new Error ( "Error while getting GitHub account data" ) ;
26
+ }
24
27
}
25
28
}
26
29
@@ -48,19 +51,20 @@ async function getAccessToken(code: string): Promise<string | null> {
48
51
return accessToken ;
49
52
}
50
53
catch ( error ) {
51
- console . error ( error . message ) ;
52
- return null ;
54
+ throw new Error ( "Error while getting access token" ) ;
53
55
}
54
56
}
55
57
56
- async function getAccountData ( accessToken : string ) : Promise < any | null > {
58
+ async function getAccountData (
59
+ accessToken : string ,
60
+ ) : Promise < GithubAccountDataDto | null > {
57
61
try {
58
62
const userDataResponse = await axios . get ( GITHUB_USER_DATA_API , {
59
63
headers : {
60
64
Authorization : `Bearer ${ accessToken } `
61
65
}
62
66
} ) ;
63
- const userData : GitHubUserDataApiResponseDto | null =
67
+ const userData : GithubUserDataApiResponseDto | null =
64
68
await userDataResponse . data ;
65
69
if ( ! userData ) {
66
70
throw new Error ( "User data not found" ) ;
@@ -71,17 +75,20 @@ async function getAccountData(accessToken: string): Promise<any | null> {
71
75
Authorization : `Bearer ${ accessToken } `
72
76
}
73
77
} ) ;
74
- const userEmails : [ GitHubEmailApiResponseDto ] | null =
78
+ const userEmails : [ GithubEmailApiResponseDto ] | null =
75
79
await userEmailsResponse . data ;
76
80
if ( ! userEmails ) {
77
81
throw new Error ( "User emails not found" ) ;
78
82
}
79
- const primaryEmail = userEmails . filter ( ( email ) => email . primary ) [ 0 ] ;
83
+
84
+ const primaryEmail = userEmails . find ( ( email ) => email . primary ) ;
85
+ if ( ! primaryEmail ) {
86
+ throw new Error ( "Primary email not found" ) ;
87
+ }
80
88
81
89
return { ...userData , primary_email : primaryEmail } ;
82
90
}
83
91
catch ( error ) {
84
- console . error ( error . message ) ;
85
- return null ;
92
+ throw new Error ( "Error while getting account data" ) ;
86
93
}
87
94
}
0 commit comments