simple-deno-import-gitea/gitea.ts

51 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2023-03-24 01:40:08 +09:00
import { SearchResults, Tag, ContentsResponse } from "./gitea_api.d.ts";
const ENDPOINT_URL = "https://git.prelude.duckdns.org/api/v1/";
2023-06-12 10:30:55 +09:00
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> {
2023-03-24 01:40:08 +09:00
const url = new URL(ENDPOINT_URL+ "repos/search");
url.searchParams.append("q", topic);
url.searchParams.append("topic", "true");
2023-06-12 10:30:55 +09:00
const data = await fetchFromGitea(url,giteaOption);
2023-03-24 01:40:08 +09:00
return data;
}
export async function getRepositoryTags(owner:string,
2023-06-12 10:30:55 +09:00
repo:string, giteaOption? :GiteaOption): Promise<Tag[]>{
2023-03-24 01:40:08 +09:00
const url = new URL(ENDPOINT_URL+ "repos/"+owner+"/"+repo+"/tags");
2023-06-12 10:30:55 +09:00
const data = await fetchFromGitea(url,giteaOption);
2023-03-24 01:40:08 +09:00
return data;
}
export async function getRepositoryContent(owner:string,
2023-06-12 10:30:55 +09:00
repo:string, path:string, ref:string, giteaOption? :GiteaOption): Promise<ContentsResponse[] | ContentsResponse>{
2023-03-24 01:40:08 +09:00
const url = new URL(ENDPOINT_URL+ "repos/"+owner+"/"+repo+"/contents/"+path);
url.searchParams.append("ref", ref);
2023-06-12 10:30:55 +09:00
const data = await fetchFromGitea(url,giteaOption);
2023-03-24 01:40:08 +09:00
return data;
}
if (import.meta.main) {
2023-06-12 10:35:43 +09:00
const results = await searchRepositoryWithTopic("denolib");
2023-03-24 01:40:08 +09:00
console.log(results.data?.map((repo) => repo.full_name));
const s = await getRepositoryContent("monoid", "script", "", "");
console.log((s as ContentsResponse[]).map((x) => x.name));
2023-06-12 10:35:43 +09:00
}