168 lines
4.0 KiB
TypeScript
168 lines
4.0 KiB
TypeScript
|
|
import { getApiBaseUrl } from "./api-base";
|
||
|
|
|
||
|
|
// Forgejo Connection types
|
||
|
|
export interface ForgejoConnection {
|
||
|
|
id: string;
|
||
|
|
organization_id: string;
|
||
|
|
name: string;
|
||
|
|
base_url: string;
|
||
|
|
token: null; // Always null in responses
|
||
|
|
active: boolean;
|
||
|
|
has_token: boolean;
|
||
|
|
token_last_eight: string | null;
|
||
|
|
created_at: string;
|
||
|
|
updated_at: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ForgejoConnectionCreate {
|
||
|
|
name: string;
|
||
|
|
base_url: string;
|
||
|
|
token: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ForgejoConnectionUpdate {
|
||
|
|
name?: string;
|
||
|
|
base_url?: string;
|
||
|
|
token?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Forgejo Repository types
|
||
|
|
export interface ForgejoRepository {
|
||
|
|
id: string;
|
||
|
|
organization_id: string;
|
||
|
|
connection_id: string;
|
||
|
|
owner: string;
|
||
|
|
repo: string;
|
||
|
|
display_name: string;
|
||
|
|
default_branch: string;
|
||
|
|
active: boolean;
|
||
|
|
connection: ForgejoConnection;
|
||
|
|
last_sync_at: string | null;
|
||
|
|
last_sync_error: string | null;
|
||
|
|
created_at: string;
|
||
|
|
updated_at: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ForgejoRepositoryCreate {
|
||
|
|
connection_id: string;
|
||
|
|
owner: string;
|
||
|
|
repo: string;
|
||
|
|
display_name?: string;
|
||
|
|
default_branch?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ForgejoRepositoryUpdate {
|
||
|
|
display_name?: string;
|
||
|
|
default_branch?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
// API client
|
||
|
|
const API_BASE_URL = getApiBaseUrl();
|
||
|
|
|
||
|
|
async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
|
||
|
|
const response = await fetch(url, {
|
||
|
|
...init,
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
...(init?.headers || {}),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const errorData = await response.json().catch(() => ({}));
|
||
|
|
throw new Error(errorData.detail || `API error: ${response.statusText}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Forgejo Connection API
|
||
|
|
export async function getForgejoConnections(): Promise<ForgejoConnection[]> {
|
||
|
|
return fetchJson<ForgejoConnection[]>(`${API_BASE_URL}/api/v1/forgejo/connections`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function createForgejoConnection(
|
||
|
|
data: ForgejoConnectionCreate,
|
||
|
|
): Promise<ForgejoConnection> {
|
||
|
|
return fetchJson<ForgejoConnection>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/connections`,
|
||
|
|
{
|
||
|
|
method: "POST",
|
||
|
|
body: JSON.stringify(data),
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getForgejoConnection(
|
||
|
|
connectionId: string,
|
||
|
|
): Promise<ForgejoConnection> {
|
||
|
|
return fetchJson<ForgejoConnection>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/connections/${connectionId}`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function updateForgejoConnection(
|
||
|
|
connectionId: string,
|
||
|
|
data: ForgejoConnectionUpdate,
|
||
|
|
): Promise<ForgejoConnection> {
|
||
|
|
return fetchJson<ForgejoConnection>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/connections/${connectionId}`,
|
||
|
|
{
|
||
|
|
method: "PATCH",
|
||
|
|
body: JSON.stringify(data),
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteForgejoConnection(connectionId: string): Promise<void> {
|
||
|
|
await fetch(`${API_BASE_URL}/api/v1/forgejo/connections/${connectionId}`, {
|
||
|
|
method: "DELETE",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Forgejo Repository API
|
||
|
|
export async function getForgejoRepositories(): Promise<ForgejoRepository[]> {
|
||
|
|
return fetchJson<ForgejoRepository[]>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/repositories`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function createForgejoRepository(
|
||
|
|
data: ForgejoRepositoryCreate,
|
||
|
|
): Promise<ForgejoRepository> {
|
||
|
|
return fetchJson<ForgejoRepository>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/repositories`,
|
||
|
|
{
|
||
|
|
method: "POST",
|
||
|
|
body: JSON.stringify(data),
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getForgejoRepository(
|
||
|
|
repositoryId: string,
|
||
|
|
): Promise<ForgejoRepository> {
|
||
|
|
return fetchJson<ForgejoRepository>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/repositories/${repositoryId}`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function updateForgejoRepository(
|
||
|
|
repositoryId: string,
|
||
|
|
data: ForgejoRepositoryUpdate,
|
||
|
|
): Promise<ForgejoRepository> {
|
||
|
|
return fetchJson<ForgejoRepository>(
|
||
|
|
`${API_BASE_URL}/api/v1/forgejo/repositories/${repositoryId}`,
|
||
|
|
{
|
||
|
|
method: "PATCH",
|
||
|
|
body: JSON.stringify(data),
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteForgejoRepository(repositoryId: string): Promise<void> {
|
||
|
|
await fetch(`${API_BASE_URL}/api/v1/forgejo/repositories/${repositoryId}`, {
|
||
|
|
method: "DELETE",
|
||
|
|
});
|
||
|
|
}
|