Some Code Cleanup

This commit is contained in:
2023-12-25 21:54:40 +01:00
parent a2687083e0
commit 3108d9bf20
61 changed files with 305 additions and 247 deletions

View File

@@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {Page, PageList} from "../types/page.ts";
import type {Page, PageList} from "@type/page.ts";
import {fetchWithToken} from "./repo.ts";
import {PageListSchema, PageSchema} from "../types/page.ts";
import {PageListSchema, PageSchema} from "@type/page.ts";
import {bytesToBase64} from "../admin/util.ts";
import {z} from "zod";
@@ -30,13 +30,13 @@ export class PageRepo {
return await fetchWithToken(this.token, `/page?branch=${branch}`)
.then(value => value.json())
.then(PageListSchema.parse)
.then(value => value.map(value1 => ({...value1, path: value1.path.replace("src/content/", "")})))
.then(value => value.map(value1 => ({...value1, path: value1.path.replace("src/content/", "")})));
}
public async getPage(id: number, branch: string = "master"): Promise<Page> {
return await fetchWithToken(this.token, `/page/${id}?branch=${branch}`)
.then(value => value.json())
.then(PageSchema.parse)
.then(PageSchema.parse);
}
public async updatePage(id: number, content: string, sha: string, message: string, branch: string = "master"): Promise<void> {
@@ -46,32 +46,32 @@ export class PageRepo {
content: bytesToBase64(new TextEncoder().encode(content)),
sha, message
})
})
});
}
public async getBranches(): Promise<string[]> {
return await fetchWithToken(this.token, "/page/branch")
.then(value => value.json())
.then(value => z.array(z.string()).parse(value))
.then(value => z.array(z.string()).parse(value));
}
public async createBranch(branch: string): Promise<void> {
await fetchWithToken(this.token, `/page/branch`, {method: "POST", body: JSON.stringify({branch})})
await fetchWithToken(this.token, "/page/branch", {method: "POST", body: JSON.stringify({branch})});
}
public async deleteBranch(branch: string): Promise<void> {
await fetchWithToken(this.token, `/page/branch`, {method: "DELETE", body: JSON.stringify({branch})})
await fetchWithToken(this.token, "/page/branch", {method: "DELETE", body: JSON.stringify({branch})});
}
public async createFile(path: string, branch: string = "master"): Promise<void> {
await fetchWithToken(this.token, `/page?branch=${branch}`, {method: "POST", body: JSON.stringify({path})})
await fetchWithToken(this.token, `/page?branch=${branch}`, {method: "POST", body: JSON.stringify({path})});
}
public async merge(branch: string, message: string): Promise<void> {
await fetchWithToken(this.token, `/page/branch/merge`, {method: "POST", body: JSON.stringify({branch, message})})
await fetchWithToken(this.token, "/page/branch/merge", {method: "POST", body: JSON.stringify({branch, message})});
}
public async deletePage(id: number, message: string, sha: string, branch: string = "master"): Promise<void> {
await fetchWithToken(this.token, `/page/${id}?branch=${branch}`, {method: "DELETE", body: JSON.stringify({message, sha})})
await fetchWithToken(this.token, `/page/${id}?branch=${branch}`, {method: "DELETE", body: JSON.stringify({message, sha})});
}
}