Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit d702460

Browse files
committed
refactor(github): 重构 GitHubClient 类的初始化和请求逻辑
1 parent 9790882 commit d702460

File tree

13 files changed

+65
-25
lines changed

13 files changed

+65
-25
lines changed

src/models/platform/github/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export class App extends GitHubClient {
4848
constructor (base: GitHubClient) {
4949
super(base)
5050
this.userToken = base.userToken
51-
this.base_url = get_base_url(this.type, { proxyType: ProxyType.Original })
51+
this.base_url = base.base_url
52+
this.api_url = base.api_url
5253
}
5354

5455
/**

src/models/platform/github/auth.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ExpiredAccessTokenMsg,
55
FailedToFetchAccessTokenMsg,
66
FailedToRefreshAccessTokenMsg,
7-
format_date,
87
InvalidAccessTokenMsg,
98
MissingAccessCodeMsg,
109
MissingAccessTokenMsg,
@@ -13,15 +12,17 @@ import {
1312
PermissionDeniedMsg,
1413
RefreshAccessTokenSuccessMsg
1514
} from '@/common'
15+
import { get_base_url } from '@/models'
1616
import { GitHubClient } from '@/models/platform/github/client'
17-
import type {
18-
AccessCodeType,
19-
AccessTokenType,
20-
ApiResponseType,
21-
CheckTokenResponseType,
22-
RefreshTokenResponseType,
23-
RefreshTokenType,
24-
TokenResponseType
17+
import {
18+
type AccessCodeType,
19+
type AccessTokenType,
20+
type ApiResponseType,
21+
type CheckTokenResponseType,
22+
ProxyType,
23+
type RefreshTokenResponseType,
24+
type RefreshTokenType,
25+
type TokenResponseType
2526
} from '@/types'
2627

2728
/**
@@ -39,6 +40,7 @@ export class Auth extends GitHubClient {
3940
super(base)
4041
this.userToken = base.userToken
4142
this.api_url = base.api_url
43+
this.base_url = base.base_url
4244
}
4345

4446
/**

src/models/platform/github/client.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ export class GitHubClient {
358358
case ProxyType.Common:
359359
case ProxyType.Reverse: {
360360
const proxyType = proxy.type
361-
this.base_url = get_base_url(type, { proxyUrl: proxy.address, proxyType })
362361
this.api_url = get_api_base_url(type, { proxyUrl: proxy.address, proxyType })
362+
this.currentRequestConfig.url = this.api_url
363363
break
364364
}
365365
case ProxyProtocol.HTTP:
@@ -416,9 +416,20 @@ export class GitHubClient {
416416
return jwt.sign(payload, this.Private_Key!, { algorithm: 'RS256' })
417417
}
418418

419+
/**
420+
* 获取当前请求的配置
421+
* @returns 当前请求的配置对象
422+
*/
423+
private getCurrentRequestConfig (): RequestConfigType {
424+
return {
425+
...this.currentRequestConfig,
426+
url: this.api_url
427+
}
428+
}
429+
419430
/**
420431
* 设置当前请求的配置
421-
* @protected - 仅在类内部访问
432+
* @protected - 仅在父类与子类中方法中可访问
422433
* @param config - 配置对象,包含以下属性:
423434
* - url: 请求的URL
424435
* - token: 认证令牌
@@ -435,13 +446,13 @@ export class GitHubClient {
435446
* @returns 返回一个新的 Request 实例
436447
*/
437448
private createRequest (): Request {
438-
const { url, token, tokenType } = this.currentRequestConfig
449+
const { url, token, tokenType } = this.getCurrentRequestConfig()
439450
const proxyConfig = this.proxy?.type !== 'common' ? this.proxy : null
440451
const customHeaders = {
441452
'X-GitHub-Api-Version': '2022-11-28',
442453
Accept: 'application/vnd.github+json'
443454
}
444-
return new Request(url ?? this.api_url, tokenType, token, proxyConfig, customHeaders)
455+
return new Request(url!, tokenType, token, proxyConfig, customHeaders)
445456
}
446457

447458
/**
@@ -458,6 +469,9 @@ export class GitHubClient {
458469
): Promise<ApiResponseType> {
459470
try {
460471
if (!path) throw new Error(MissingRequestPathMsg)
472+
this.setRequestConfig({
473+
token: this.userToken
474+
})
461475
const request = this.createRequest()
462476
const req = await request.get(path, parms, customHeaders)
463477
if ((req.statusCode === 403 || req.statusCode === 429) && req.headers['x-ratelimit-remaining'] === '0') {
@@ -489,6 +503,9 @@ export class GitHubClient {
489503
): Promise<ApiResponseType> {
490504
try {
491505
if (!path) throw new Error(MissingRequestPathMsg)
506+
this.setRequestConfig({
507+
token: this.userToken
508+
})
492509
const request = this.createRequest()
493510
const req = await request.post(path, data, customHeaders)
494511
if ((req.statusCode === 403 || req.statusCode === 429) && req.headers['x-ratelimit-remaining'] === '0') {
@@ -522,6 +539,9 @@ export class GitHubClient {
522539
): Promise<ApiResponseType> {
523540
try {
524541
if (!path) throw new Error(MissingRequestPathMsg)
542+
this.setRequestConfig({
543+
token: this.userToken
544+
})
525545
const request = this.createRequest()
526546
const req = await request.patch(path, params, data, customHeaders)
527547
if ((req.statusCode === 403 || req.statusCode === 429) && req.headers['x-ratelimit-remaining'] === '0') {
@@ -553,6 +573,9 @@ export class GitHubClient {
553573
): Promise<ApiResponseType> {
554574
try {
555575
if (!path) throw new Error(MissingRequestPathMsg)
576+
this.setRequestConfig({
577+
token: this.userToken
578+
})
556579
const request = this.createRequest()
557580
const req = await request.put(path, data, customHeaders)
558581
if ((req.statusCode === 403 || req.statusCode === 429) && req.headers['x-ratelimit-remaining'] === '0') {
@@ -586,6 +609,9 @@ export class GitHubClient {
586609
): Promise<ApiResponseType> {
587610
try {
588611
if (!path) throw new Error(MissingRequestPathMsg)
612+
this.setRequestConfig({
613+
token: this.userToken
614+
})
589615
const request = this.createRequest()
590616
const req = await request.delete(path, params, data, customHeaders)
591617
if ((req.statusCode === 403 || req.statusCode === 429) && req.headers['x-ratelimit-remaining'] === '0') {

src/models/platform/github/commit.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class Commit extends GitHubClient {
2929
constructor (base: GitHubClient) {
3030
super(base)
3131
this.userToken = base.userToken
32+
this.api_url = base.api_url
33+
this.base_url = base.base_url
3234
}
3335

3436
/**

src/models/platform/github/issue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export class Issue extends GitHubClient {
8181
constructor (base: GitHubClient) {
8282
super(base)
8383
this.userToken = base.userToken
84+
this.api_url = base.api_url
85+
this.base_url = base.base_url
8486
}
8587

8688
/**
@@ -1399,7 +1401,7 @@ export class Issue extends GitHubClient {
13991401
])
14001402
const IssueData: CreteIssueCommentResponseType = {
14011403
id: res.data.id,
1402-
html_url: `${get_base_url(this.type, { proxyType: ProxyType.Original })}/${owner}/${repo}/issues/${issue_number}#${res.data.id}`,
1404+
html_url: `${this.base_url}/${owner}/${repo}/issues/${issue_number}#${res.data.id}`,
14031405
body: res.data.body,
14041406
user: {
14051407
id: res.data.user.id,

src/models/platform/github/org.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class Org extends GitHubClient {
2626
constructor (base: GitHubClient) {
2727
super(base)
2828
this.userToken = base.userToken
29+
this.api_url = base.api_url
30+
this.base_url = base.base_url
2931
}
3032

3133
/**
@@ -136,7 +138,7 @@ export class Org extends GitHubClient {
136138
id: res.data.inviter.id,
137139
login: res.data.inviterlogin,
138140
name: res.data.inviter.name,
139-
html_url: `${get_base_url(this.type)}/${org}`,
141+
html_url: `${this.Client_Secret}/${org}`,
140142
role: role as 'admin' | 'member'
141143
}
142144
res.data = OrgData

src/models/platform/github/pull_request.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export class Pull_Request extends GitHubClient {
6565
constructor (base: GitHubClient) {
6666
super(base)
6767
this.userToken = base.userToken
68+
this.api_url = base.api_url
69+
this.base_url = base.base_url
6870
}
6971

7072
/**

src/models/platform/github/release.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export class Release extends GitHubClient {
3838
constructor (base: GitHubClient) {
3939
super(base)
4040
this.userToken = base.userToken
41+
this.api_url = base.api_url
42+
this.base_url = base.base_url
4143
}
4244

4345
/**

src/models/platform/github/repo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export class Repo extends GitHubClient {
6464
constructor (base: GitHubClient) {
6565
super(base)
6666
this.userToken = base.userToken
67-
this.base_url = get_base_url(this.type, { proxyType: ProxyType.Original })
67+
this.api_url = base.api_url
68+
this.base_url = base.base_url
6869
}
6970

7071
/**
@@ -893,7 +894,7 @@ export class Repo extends GitHubClient {
893894
const { owner, repo } = options
894895
let default_branch
895896
try {
896-
const github_url = this.base_url + '/' + owner + '/' + repo
897+
const github_url = get_base_url(this.type, { proxyType: ProxyType.Original }) + '/' + owner + '/' + repo
897898
default_branch = await get_remote_repo_default_branch(github_url)
898899
} catch (error) {
899900
default_branch = (await this.get_repo_info({ owner, repo })).data.default_branch

src/models/platform/github/search.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class Search extends GitHubClient {
2424
constructor (base: GitHubClient) {
2525
super(base)
2626
this.userToken = base.userToken
27+
this.base_url = base.base_url
28+
this.api_url = base.api_url
2729
}
2830

2931
/**

0 commit comments

Comments
 (0)