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

Commit 5cdfe7a

Browse files
committed
feat(github/user): 添加通过用户id获取用户信息方法
- 新增 get_user_info_by_user_id 方法,通过用户 ID 获取信息
1 parent 97dd3df commit 5cdfe7a

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/models/github/user.ts

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GitHub } from '@/models/github/github'
1212
import {
1313
ApiResponseType,
1414
ContributionResult,
15+
UserIdParamType,
1516
UserNameParamType,
1617
UserResponseType
1718
} from '@/types'
@@ -48,58 +49,87 @@ export class User {
4849
}
4950

5051
/**
51-
* 通过访问令牌获取用户信息
52+
* 获取指定的用户信息
53+
* 不止可以获取用户信息还可以获取组织信息
54+
* @param options - 用户参数
55+
* @param options.username - 用户名或组织名
5256
*/
53-
public async get_user_info_by_token ():
57+
public async get_user_info (options: UserNameParamType):
5458
Promise<ApiResponseType<UserResponseType>> {
59+
if (!options.username) {
60+
throw new Error(NotOrgOrUserParamMsg)
61+
}
5562
this.options.setRequestConfig({
5663
token: this.userToken
5764
})
5865
try {
59-
const req = await this.get('/user')
66+
const req = await this.get(`/users/${options.username}`)
6067
if (req.statusCode === 401) {
6168
throw new Error(NotPerrmissionMsg)
6269
} else if (req.statusCode === 404) {
63-
throw new Error(NotUserMsg)
70+
throw new Error(NotOrgOrRepoMsg)
6471
}
6572
if (req.data) {
6673
req.data.created_at = await formatDate(req.data.created_at)
6774
req.data.updated_at = await formatDate(req.data.updated_at)
6875
}
6976
return req
7077
} catch (error) {
71-
throw new Error(`获取授权用户信息失败: ${(error as Error).message}`)
78+
throw new Error(`获取用户或组织信息失败: ${(error as Error).message}`)
7279
}
7380
}
7481

7582
/**
76-
* 获取指定的用户信息
77-
* 不止可以获取用户信息还可以获取组织信息
83+
* 通过用户id获取用户信息
84+
* user_id 不是用户名, 而是用户的唯一标识符,
7885
* @param options - 用户参数
79-
* @param options.username - 用户名或组织名
86+
* @param options.user_id - 用户id
87+
* @returns 用户信息
8088
*/
81-
public async get_user_info (options: UserNameParamType):
89+
public async get_user_info_by_user_id (options: UserIdParamType):
8290
Promise<ApiResponseType<UserResponseType>> {
83-
if (!options.username) {
84-
throw new Error(NotOrgOrUserParamMsg)
91+
this.options.setRequestConfig({
92+
token: this.userToken
93+
})
94+
try {
95+
const req = await this.get(`/user/${options.user_id}`)
96+
if (req.statusCode === 401) {
97+
throw new Error(NotPerrmissionMsg)
98+
} else if (req.statusCode === 404) {
99+
throw new Error(NotUserMsg)
100+
}
101+
if (req.data) {
102+
req.data.created_at = await formatDate(req.data.created_at)
103+
req.data.updated_at = await formatDate(req.data.updated_at)
104+
}
105+
return req
106+
} catch (error) {
107+
throw new Error(`通过用户id获取用户${options.user_id}信息失败: ${(error as Error).message}`)
85108
}
109+
}
110+
111+
/**
112+
* 通过访问令牌获取用户信息
113+
*/
114+
public async get_user_info_by_token ():
115+
Promise<ApiResponseType<UserResponseType>> {
86116
this.options.setRequestConfig({
87117
token: this.userToken
88118
})
89119
try {
90-
const req = await this.get(`/users/${options.username}`)
120+
const req = await this.get('/user')
91121
if (req.statusCode === 401) {
92122
throw new Error(NotPerrmissionMsg)
93123
} else if (req.statusCode === 404) {
94-
throw new Error(NotOrgOrRepoMsg)
124+
throw new Error(NotUserMsg)
95125
}
96126
if (req.data) {
97127
req.data.created_at = await formatDate(req.data.created_at)
98128
req.data.updated_at = await formatDate(req.data.updated_at)
99129
}
100130
return req
101131
} catch (error) {
102-
throw new Error(`获取用户或组织信息失败: ${(error as Error).message}`)
132+
throw new Error(`获取授权用户信息失败: ${(error as Error).message}`)
103133
}
104134
}
105135

@@ -141,10 +171,23 @@ export class User {
141171
data: res
142172
}
143173
} catch (error) {
144-
throw new Error(`获取用户贡献信息失败: ${(error as Error).message}`)
174+
throw new Error(`获取用户${options.username}贡献信息失败: ${(error as Error).message}`)
145175
}
146176
}
147177

178+
/**
179+
* 快速获取获取用户id
180+
* 该方法会自动获取当前用户的id,需要传入token
181+
* @returns 用户id
182+
* @example
183+
* ```ts
184+
* const userId = await user.get_user_id()
185+
* console.log(userId)
186+
*/
187+
public async get_user_id (): Promise<number> {
188+
return (await this.get_user_info_by_token()).data.id
189+
}
190+
148191
/**
149192
* 快速获取获取用户名
150193
* 该方法会自动获取当前用户的用户名,需要传入token

src/types/github/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ export interface UserNameParamType {
22
/** 用户名 */
33
username: string;
44
}
5+
export interface UserIdParamType {
6+
/** 用户id */
7+
user_id: number;
8+
}
59

610
export interface RepoOwnerParamType {
711
/** 仓库的拥有者 */

0 commit comments

Comments
 (0)