159 lines
5.8 KiB
TypeScript
159 lines
5.8 KiB
TypeScript
import {
|
|
Project,
|
|
Package,
|
|
Tag,
|
|
TagDetail,
|
|
Artifact,
|
|
ArtifactDetail,
|
|
UploadResponse,
|
|
PaginatedResponse,
|
|
ListParams,
|
|
TagListParams,
|
|
PackageListParams,
|
|
ArtifactListParams,
|
|
ProjectListParams,
|
|
GlobalSearchResponse,
|
|
} from './types';
|
|
|
|
const API_BASE = '/api/v1';
|
|
|
|
async function handleResponse<T>(response: Response): Promise<T> {
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({ detail: 'Unknown error' }));
|
|
throw new Error(error.detail || `HTTP ${response.status}`);
|
|
}
|
|
return response.json();
|
|
}
|
|
|
|
function buildQueryString(params: Record<string, unknown>): string {
|
|
const searchParams = new URLSearchParams();
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
const query = searchParams.toString();
|
|
return query ? `?${query}` : '';
|
|
}
|
|
|
|
// Global Search API
|
|
export async function globalSearch(query: string, limit: number = 5): Promise<GlobalSearchResponse> {
|
|
const params = buildQueryString({ q: query, limit });
|
|
const response = await fetch(`${API_BASE}/search${params}`);
|
|
return handleResponse<GlobalSearchResponse>(response);
|
|
}
|
|
|
|
// Project API
|
|
export async function listProjects(params: ProjectListParams = {}): Promise<PaginatedResponse<Project>> {
|
|
const query = buildQueryString(params as Record<string, unknown>);
|
|
const response = await fetch(`${API_BASE}/projects${query}`);
|
|
return handleResponse<PaginatedResponse<Project>>(response);
|
|
}
|
|
|
|
export async function listProjectsSimple(params: ListParams = {}): Promise<Project[]> {
|
|
const data = await listProjects(params);
|
|
return data.items;
|
|
}
|
|
|
|
export async function createProject(data: { name: string; description?: string; is_public?: boolean }): Promise<Project> {
|
|
const response = await fetch(`${API_BASE}/projects`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return handleResponse<Project>(response);
|
|
}
|
|
|
|
export async function getProject(name: string): Promise<Project> {
|
|
const response = await fetch(`${API_BASE}/projects/${name}`);
|
|
return handleResponse<Project>(response);
|
|
}
|
|
|
|
// Package API
|
|
export async function listPackages(projectName: string, params: PackageListParams = {}): Promise<PaginatedResponse<Package>> {
|
|
const query = buildQueryString(params as Record<string, unknown>);
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/packages${query}`);
|
|
return handleResponse<PaginatedResponse<Package>>(response);
|
|
}
|
|
|
|
export async function listPackagesSimple(projectName: string, params: PackageListParams = {}): Promise<Package[]> {
|
|
const data = await listPackages(projectName, params);
|
|
return data.items;
|
|
}
|
|
|
|
export async function getPackage(projectName: string, packageName: string): Promise<Package> {
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/packages/${packageName}`);
|
|
return handleResponse<Package>(response);
|
|
}
|
|
|
|
export async function createPackage(projectName: string, data: { name: string; description?: string }): Promise<Package> {
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/packages`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return handleResponse<Package>(response);
|
|
}
|
|
|
|
// Tag API
|
|
export async function listTags(projectName: string, packageName: string, params: TagListParams = {}): Promise<PaginatedResponse<TagDetail>> {
|
|
const query = buildQueryString(params as Record<string, unknown>);
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/${packageName}/tags${query}`);
|
|
return handleResponse<PaginatedResponse<TagDetail>>(response);
|
|
}
|
|
|
|
export async function listTagsSimple(projectName: string, packageName: string, params: TagListParams = {}): Promise<TagDetail[]> {
|
|
const data = await listTags(projectName, packageName, params);
|
|
return data.items;
|
|
}
|
|
|
|
export async function getTag(projectName: string, packageName: string, tagName: string): Promise<TagDetail> {
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/${packageName}/tags/${tagName}`);
|
|
return handleResponse<TagDetail>(response);
|
|
}
|
|
|
|
export async function createTag(projectName: string, packageName: string, data: { name: string; artifact_id: string }): Promise<Tag> {
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/${packageName}/tags`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return handleResponse<Tag>(response);
|
|
}
|
|
|
|
// Artifact API
|
|
export async function getArtifact(artifactId: string): Promise<ArtifactDetail> {
|
|
const response = await fetch(`${API_BASE}/artifact/${artifactId}`);
|
|
return handleResponse<ArtifactDetail>(response);
|
|
}
|
|
|
|
export async function listPackageArtifacts(
|
|
projectName: string,
|
|
packageName: string,
|
|
params: ArtifactListParams = {}
|
|
): Promise<PaginatedResponse<Artifact & { tags: string[] }>> {
|
|
const query = buildQueryString(params as Record<string, unknown>);
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/${packageName}/artifacts${query}`);
|
|
return handleResponse<PaginatedResponse<Artifact & { tags: string[] }>>(response);
|
|
}
|
|
|
|
// Upload
|
|
export async function uploadArtifact(projectName: string, packageName: string, file: File, tag?: string): Promise<UploadResponse> {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
if (tag) {
|
|
formData.append('tag', tag);
|
|
}
|
|
|
|
const response = await fetch(`${API_BASE}/project/${projectName}/${packageName}/upload`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
});
|
|
return handleResponse<UploadResponse>(response);
|
|
}
|
|
|
|
// Download URL
|
|
export function getDownloadUrl(projectName: string, packageName: string, ref: string): string {
|
|
return `${API_BASE}/project/${projectName}/${packageName}/+/${ref}`;
|
|
}
|