51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import { SearchResults, Tag, ContentsResponse } from "./gitea_api.d.ts";
|
|
|
|
const ENDPOINT_URL = "https://git.prelude.duckdns.org/api/v1/";
|
|
|
|
export interface GiteaOption{
|
|
token: string;
|
|
}
|
|
|
|
async function fetchFromGitea(url: string | URL, giteaOption? :GiteaOption): Promise<any> {
|
|
const headers = new Headers();
|
|
if (giteaOption) {
|
|
headers.append("Authorization", "token " + giteaOption.token);
|
|
}
|
|
const response = await fetch(url, {
|
|
headers: headers
|
|
}
|
|
);
|
|
const data = await response.json();
|
|
return data;
|
|
}
|
|
|
|
export async function searchRepositoryWithTopic(topic: string, giteaOption? :GiteaOption): Promise<SearchResults> {
|
|
const url = new URL(ENDPOINT_URL+ "repos/search");
|
|
url.searchParams.append("q", topic);
|
|
url.searchParams.append("topic", "true");
|
|
const data = await fetchFromGitea(url,giteaOption);
|
|
return data;
|
|
}
|
|
|
|
export async function getRepositoryTags(owner:string,
|
|
repo:string, giteaOption? :GiteaOption): Promise<Tag[]>{
|
|
const url = new URL(ENDPOINT_URL+ "repos/"+owner+"/"+repo+"/tags");
|
|
const data = await fetchFromGitea(url,giteaOption);
|
|
return data;
|
|
}
|
|
|
|
export async function getRepositoryContent(owner:string,
|
|
repo:string, path:string, ref:string, giteaOption? :GiteaOption): Promise<ContentsResponse[] | ContentsResponse>{
|
|
const url = new URL(ENDPOINT_URL+ "repos/"+owner+"/"+repo+"/contents/"+path);
|
|
url.searchParams.append("ref", ref);
|
|
const data = await fetchFromGitea(url,giteaOption);
|
|
return data;
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
const results = await searchRepositoryWithTopic("denolib");
|
|
console.log(results.data?.map((repo) => repo.full_name));
|
|
const s = await getRepositoryContent("monoid", "script", "", "");
|
|
console.log((s as ContentsResponse[]).map((x) => x.name));
|
|
}
|