mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-22 21:07:57 +00:00
fix(cli): add default timeout to forgejo api check fetch requests
This commit is contained in:
parent
d6b9677ca5
commit
a4ee1d95dc
1 changed files with 14 additions and 5 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
const TIMEOUT = 5000;
|
||||||
|
|
||||||
export type CheckResult = {
|
export type CheckResult = {
|
||||||
name: string;
|
name: string;
|
||||||
success: boolean;
|
success: boolean;
|
||||||
|
|
@ -16,11 +18,18 @@ export function createCheckResult(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchWithTimeout(
|
||||||
|
url: string,
|
||||||
|
init: RequestInit,
|
||||||
|
): Promise<Response> {
|
||||||
|
return fetch(url, { ...init, signal: AbortSignal.timeout(TIMEOUT) });
|
||||||
|
}
|
||||||
|
|
||||||
export async function checkForgejoReachability(
|
export async function checkForgejoReachability(
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
): Promise<CheckResult> {
|
): Promise<CheckResult> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(baseUrl, { method: "GET" });
|
const response = await fetchWithTimeout(baseUrl, { method: "GET" });
|
||||||
if (response.ok || response.status === 200) {
|
if (response.ok || response.status === 200) {
|
||||||
return createCheckResult("Forgejo reachability", true);
|
return createCheckResult("Forgejo reachability", true);
|
||||||
}
|
}
|
||||||
|
|
@ -41,7 +50,7 @@ export async function checkForgejoApi(
|
||||||
colorize = false,
|
colorize = false,
|
||||||
): Promise<CheckResult> {
|
): Promise<CheckResult> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${baseUrl}/api/v1/user`, {
|
const response = await fetchWithTimeout(`${baseUrl}/api/v1/user`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${pat}`,
|
Authorization: `Bearer ${pat}`,
|
||||||
|
|
@ -87,7 +96,7 @@ export async function checkRepoAccess(
|
||||||
username: string,
|
username: string,
|
||||||
): Promise<CheckResult> {
|
): Promise<CheckResult> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetchWithTimeout(
|
||||||
`${baseUrl}/api/v1/users/${username}/repos?limit=1`,
|
`${baseUrl}/api/v1/users/${username}/repos?limit=1`,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
|
@ -112,7 +121,7 @@ export async function checkRepoAccess(
|
||||||
|
|
||||||
return createCheckResult(
|
return createCheckResult(
|
||||||
"Repo access",
|
"Repo access",
|
||||||
true,
|
false,
|
||||||
"No repos found (service account may not have repo access)",
|
"No repos found (service account may not have repo access)",
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue