Implement project browsing

This commit is contained in:
Alexis Sellier
2021-06-09 20:53:35 +02:00
parent e7aa71edd1
commit 6d313a4d45
17 changed files with 596 additions and 51 deletions
+140
View File
@@ -0,0 +1,140 @@
import type { Config } from '@app/config';
export interface Project {
id: string
anchor: {
stateHash: string
stateHashFormat: string
}
}
export interface Person {
urn: string
avatar: { emoji: string, background: { r: number, g: number, b: number } }
}
export interface Meta {
name: string
description: string
maintainers: Person[]
}
export interface Author {
avatar: string;
email: string;
name: string;
}
export enum ObjectType {
Blob = "BLOB",
Tree = "TREE",
}
export interface CommitHeader {
author: Author;
committer: Author;
committerTime: number;
description: string;
sha1: string;
summary: string;
}
export interface Info {
name: string;
objectType: ObjectType;
lastCommit: CommitHeader;
}
export interface Entry {
path: string;
info: Info;
}
export interface Blob {
binary?: boolean;
html?: boolean;
content: string;
path: string;
info: Info;
}
export interface Tree {
entries: Entry[];
info: Info;
path: string;
}
export async function getMetadata(urn: string, config: Config): Promise<Meta | null> {
const api = import.meta.env.RADICLE_HTTP_API;
const response = await fetch(`${api}/v1/projects/${urn}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return await response.json();
}
export async function getTree(
urn: string,
commit: string,
path: string,
config: Config
): Promise<any | null> {
if (! config.seed.api) return null;
const response = await fetch(`${config.seed.api}/v1/projects/${urn}/tree/${commit}/${path}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return response.json();
}
export async function getBlob(
urn: string,
commit: string,
path: string,
config: Config
): Promise<Blob | null> {
if (! config.seed.api) return null;
const response = await fetch(`${config.seed.api}/v1/projects/${urn}/blob/${commit}/${path}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return response.json();
}
export async function getReadme(
urn: string,
commit: string,
config: Config
): Promise<Blob | null> {
if (! config.seed.api) return null;
const response = await fetch(`${config.seed.api}/v1/projects/${urn}/readme/${commit}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return response.json();
}