Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 620cd29

Browse files
committed
refactor: update API endpoints to use base URL function
Signed-off-by: 诺墨 <[email protected]>
1 parent 9c67b21 commit 620cd29

File tree

7 files changed

+45
-32
lines changed

7 files changed

+45
-32
lines changed

common/utils.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { getUserAgent } from "universal-user-agent";
22
import { createGiteeError } from "./errors.js";
33
import { VERSION } from "./version.js";
44

5+
// Default Gitee API base URL
6+
const DEFAULT_GITEE_API_BASE_URL = "https://gitee.com/api/v5";
7+
8+
/**
9+
* Get the Gitee API base URL from environment variables or use the default
10+
* @returns The Gitee API base URL
11+
*/
12+
export function getGiteeApiBaseUrl(): string {
13+
return process.env.GITEE_API_BASE_URL || DEFAULT_GITEE_API_BASE_URL;
14+
}
15+
516
type RequestOptions = {
617
method?: string;
718
body?: unknown;
@@ -55,11 +66,13 @@ export function debug(message: string, data?: unknown): void {
5566
}
5667

5768
export async function giteeRequest(
58-
url: string,
69+
urlPath: string,
5970
method: string = "GET",
6071
body?: unknown,
6172
headers?: Record<string, string>
6273
): Promise<unknown> {
74+
// Check if the URL is already a full URL or a path
75+
const url = urlPath.startsWith("http") ? urlPath : `${getGiteeApiBaseUrl()}${urlPath.startsWith("/") ? urlPath : `/${urlPath}`}`;
6376
const requestHeaders: Record<string, string> = {
6477
"Accept": "application/json",
6578
"Content-Type": "application/json",
@@ -167,7 +180,7 @@ export async function checkBranchExists(
167180
branch: string
168181
): Promise<boolean> {
169182
try {
170-
await giteeRequest(`https://gitee.com/api/v5/repos/${owner}/${repo}/branches/${branch}`, "GET");
183+
await giteeRequest(`/repos/${owner}/${repo}/branches/${branch}`, "GET");
171184
return true;
172185
} catch (error) {
173186
if (error && typeof error === "object" && "name" in error && error.name === "GiteeResourceNotFoundError") {
@@ -179,7 +192,7 @@ export async function checkBranchExists(
179192

180193
export async function checkUserExists(username: string): Promise<boolean> {
181194
try {
182-
await giteeRequest(`https://gitee.com/api/v5/users/${username}`, "GET");
195+
await giteeRequest(`/users/${username}`, "GET");
183196
return true;
184197
} catch (error) {
185198
if (error && typeof error === "object" && "name" in error && error.name === "GiteeResourceNotFoundError") {

operations/branches.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateBranchName, validateOwnerName, validateRepositoryName } from "../common/utils.js";
2+
import { giteeRequest, validateBranchName, validateOwnerName, validateRepositoryName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteeCompleteBranchSchema, GiteeBranchSchema } from "../common/types.js";
44

55
// Schema definitions
@@ -54,12 +54,12 @@ export async function createBranchFromRef(
5454
repo = validateRepositoryName(repo);
5555
branchName = validateBranchName(branchName);
5656

57-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/branches`;
57+
const url = `/repos/${owner}/${repo}/branches`;
5858
const body = {
5959
branch_name: branchName,
6060
refs: refs,
6161
};
62-
62+
6363
const response = await giteeRequest(url, "POST", body);
6464
return GiteeBranchSchema.parse(response);
6565
}
@@ -75,8 +75,8 @@ export async function listBranches(
7575
owner = validateOwnerName(owner);
7676
repo = validateRepositoryName(repo);
7777

78-
const url = new URL(`https://gitee.com/api/v5/repos/${owner}/${repo}/branches`);
79-
78+
const url = new URL(`${getGiteeApiBaseUrl()}/repos/${owner}/${repo}/branches`);
79+
8080
if (sort) {
8181
url.searchParams.append("sort", sort);
8282
}
@@ -99,8 +99,8 @@ export async function getBranch(owner: string, repo: string, branch: string) {
9999
repo = validateRepositoryName(repo);
100100
branch = validateBranchName(branch);
101101

102-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/branches/${branch}`;
102+
const url = `/repos/${owner}/${repo}/branches/${branch}`;
103103
const response = await giteeRequest(url);
104-
104+
105105
return GiteeCompleteBranchSchema.parse(response);
106106
}

operations/files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateOwnerName, validateRepositoryName } from "../common/utils.js";
2+
import { giteeRequest, validateOwnerName, validateRepositoryName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteeDirectoryContentSchema, GiteeFileContentSchema, GiteeFileOperationResultSchema } from "../common/types.js";
44

55
// Schema definitions
@@ -64,7 +64,7 @@ export async function getFileContents(
6464
owner = validateOwnerName(owner);
6565
repo = validateRepositoryName(repo);
6666

67-
const url = new URL(`https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path}`);
67+
const url = new URL(`${getGiteeApiBaseUrl()}/repos/${owner}/${repo}/contents/${path}`);
6868
if (branch) {
6969
url.searchParams.append("ref", branch);
7070
}
@@ -94,7 +94,7 @@ export async function createOrUpdateFile(
9494
// Base64 encode the content
9595
const contentBase64 = Buffer.from(content).toString("base64");
9696

97-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path}`;
97+
const url = `/repos/${owner}/${repo}/contents/${path}`;
9898
const body: Record<string, string> = {
9999
content: contentBase64,
100100
message,

operations/issues.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateOwnerName, validateRepositoryName } from "../common/utils.js";
2+
import { giteeRequest, validateOwnerName, validateRepositoryName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteeIssueCommentSchema, GiteeIssueSchema } from "../common/types.js";
44

55
// Schema definitions
@@ -128,7 +128,7 @@ export async function createIssue(
128128
delete body.labels;
129129
}
130130

131-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/issues`;
131+
const url = `/repos/${owner}/${repo}/issues`;
132132
const response = await giteeRequest(url, "POST", body);
133133

134134
return GiteeIssueSchema.parse(response);
@@ -142,7 +142,7 @@ export async function listIssues(
142142
owner = validateOwnerName(owner);
143143
repo = validateRepositoryName(repo);
144144

145-
const url = new URL(`https://gitee.com/api/v5/repos/${owner}/${repo}/issues`);
145+
const url = new URL(`${getGiteeApiBaseUrl()}/repos/${owner}/${repo}/issues`);
146146

147147
// Add query parameters
148148
Object.entries(options).forEach(([key, value]) => {
@@ -164,7 +164,7 @@ export async function getIssue(
164164
owner = validateOwnerName(owner);
165165
repo = validateRepositoryName(repo);
166166

167-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/issues/${issueNumber}`;
167+
const url = `/repos/${owner}/${repo}/issues/${issueNumber}`;
168168
const response = await giteeRequest(url);
169169

170170
return GiteeIssueSchema.parse(response);
@@ -202,7 +202,7 @@ export async function updateIssue(
202202
}
203203

204204
// Note: In the Gitee API's update issue interface, `repo` is passed as a form parameter, not as a path parameter.
205-
const url = `https://gitee.com/api/v5/repos/${owner}/issues/${issueNumber}`;
205+
const url = `/repos/${owner}/issues/${issueNumber}`;
206206
const response = await giteeRequest(url, "PATCH", body);
207207

208208
return GiteeIssueSchema.parse(response);
@@ -217,7 +217,7 @@ export async function addIssueComment(
217217
owner = validateOwnerName(owner);
218218
repo = validateRepositoryName(repo);
219219

220-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/issues/${issueNumber}/comments`;
220+
const url = `/repos/${owner}/${repo}/issues/${issueNumber}/comments`;
221221
const response = await giteeRequest(url, "POST", { body });
222222

223223
return GiteeIssueCommentSchema.parse(response);

operations/pulls.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateOwnerName, validateRepositoryName, validateBranchName } from "../common/utils.js";
2+
import { giteeRequest, validateOwnerName, validateRepositoryName, validateBranchName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteePullRequestSchema } from "../common/types.js";
44

55
// Schema definitions
@@ -111,7 +111,7 @@ export async function createPullRequest(options: CreatePullRequestOptions) {
111111
const validatedHead = validateBranchName(rest.head);
112112
const validatedBase = validateBranchName(rest.base);
113113

114-
const url = `https://gitee.com/api/v5/repos/${validatedOwner}/${validatedRepo}/pulls`;
114+
const url = `/repos/${validatedOwner}/${validatedRepo}/pulls`;
115115
const body = {
116116
...rest,
117117
head: validatedHead,
@@ -131,7 +131,7 @@ export async function listPullRequests(
131131
owner = validateOwnerName(owner);
132132
repo = validateRepositoryName(repo);
133133

134-
const url = new URL(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls`);
134+
const url = new URL(`${getGiteeApiBaseUrl()}/repos/${owner}/${repo}/pulls`);
135135

136136
// Add query parameters
137137
Object.entries(options).forEach(([key, value]) => {
@@ -153,7 +153,7 @@ export async function getPullRequest(
153153
owner = validateOwnerName(owner);
154154
repo = validateRepositoryName(repo);
155155

156-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}`;
156+
const url = `/repos/${owner}/${repo}/pulls/${pullNumber}`;
157157
const response = await giteeRequest(url, "GET");
158158

159159
return GiteePullRequestSchema.parse(response);
@@ -168,7 +168,7 @@ export async function updatePullRequest(
168168
owner = validateOwnerName(owner);
169169
repo = validateRepositoryName(repo);
170170

171-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}`;
171+
const url = `/repos/${owner}/${repo}/pulls/${pullNumber}`;
172172
const response = await giteeRequest(url, "PATCH", options);
173173

174174
return GiteePullRequestSchema.parse(response);
@@ -183,7 +183,7 @@ export async function mergePullRequest(
183183
owner = validateOwnerName(owner);
184184
repo = validateRepositoryName(repo);
185185

186-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}/merge`;
186+
const url = `/repos/${owner}/${repo}/pulls/${pullNumber}/merge`;
187187
const response = await giteeRequest(url, "PUT", options);
188188

189189
return response;

operations/repos.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateOwnerName, validateRepositoryName } from "../common/utils.js";
2+
import { giteeRequest, validateOwnerName, validateRepositoryName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteeRepositorySchema } from "../common/types.js";
44

55
// Schema definitions
@@ -43,7 +43,7 @@ export type ForkRepositoryOptions = z.infer<typeof ForkRepositorySchema>;
4343
export async function createRepository(options: CreateRepositoryOptions) {
4444
try {
4545
console.log('Creating repository parameters:', JSON.stringify(options));
46-
const url = "https://gitee.com/api/v5/user/repos";
46+
const url = "/user/repos";
4747
const response = await giteeRequest(url, "POST", options);
4848
console.log('Create repository response:', JSON.stringify(response));
4949

@@ -69,7 +69,7 @@ export async function forkRepository(
6969
owner = validateOwnerName(owner);
7070
repo = validateRepositoryName(repo);
7171

72-
const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/forks`;
72+
const url = `/repos/${owner}/${repo}/forks`;
7373
const body: Record<string, string> = {};
7474

7575
if (organization) {

operations/users.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { giteeRequest, validateOwnerName } from "../common/utils.js";
2+
import { giteeRequest, validateOwnerName, getGiteeApiBaseUrl } from "../common/utils.js";
33
import { GiteeUserSchema } from "../common/types.js";
44

55
// Schema definitions
@@ -29,14 +29,14 @@ export type SearchUsersOptions = z.infer<typeof SearchUsersSchema>;
2929
export async function getUser(username: string) {
3030
username = validateOwnerName(username);
3131

32-
const url = `https://gitee.com/api/v5/users/${username}`;
32+
const url = `/users/${username}`;
3333
const response = await giteeRequest(url, "GET");
3434

3535
return GiteeUserSchema.parse(response);
3636
}
3737

3838
export async function getCurrentUser() {
39-
const url = "https://gitee.com/api/v5/user";
39+
const url = "/user";
4040
const response = await giteeRequest(url, "GET");
4141

4242
return GiteeUserSchema.parse(response);
@@ -45,7 +45,7 @@ export async function getCurrentUser() {
4545
export async function searchUsers(options: SearchUsersOptions) {
4646
const { q, page, per_page, sort, order } = options;
4747

48-
const url = new URL("https://gitee.com/api/v5/search/users");
48+
const url = new URL(`${getGiteeApiBaseUrl()}/search/users`);
4949
url.searchParams.append("q", q);
5050
if (page !== undefined) {
5151
url.searchParams.append("page", page.toString());

0 commit comments

Comments
 (0)