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,8 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {Player, Server} from "../types/data.ts";
import {PlayerSchema, ServerSchema} from "../types/data.ts";
import type {Player, Server} from "@type/data.ts";
import {PlayerSchema, ServerSchema} from "@type/data.ts";
import {fetchWithToken} from "./repo.ts";
export class DataRepo {

View File

@@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {ExtendedEvent, ShortEvent, SWEvent} from "../types/event.js";
import type {ExtendedEvent, ShortEvent, SWEvent} from "@type/event";
import {fetchWithToken} from "./repo";
import {ExtendedEventSchema, ShortEventSchema, SWEventSchema} from "../types/event.js";
import {ExtendedEventSchema, ShortEventSchema, SWEventSchema} from "@type/event.js";
import {z} from "zod";
import type {Dayjs} from "dayjs";

View File

@@ -17,10 +17,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {EventFight} from "../types/event.js";
import type {EventFight} from "@type/event.js";
import {fetchWithToken} from "./repo";
import {z} from "zod";
import {EventFightSchema} from "../types/event.js";
import {EventFightSchema} from "@type/event.js";
import type {Dayjs} from "dayjs";
export interface CreateFight {

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})});
}
}

View File

@@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {Perms, UserPerms} from "../types/perms.js";
import type {Perms, UserPerms} from "@type/perms.js";
import {fetchWithToken} from "./repo";
import {PermsSchema, UserPermsSchema} from "../types/perms.js";
import {PermsSchema, UserPermsSchema} from "@type/perms.js";
export class PermsRepo {
constructor(private token: string) {}

View File

@@ -18,18 +18,16 @@
*/
import {derived, writable} from "svelte/store";
import {EventRepo} from "./event.js";
import {FightRepo} from "./fight.js";
import {PermsRepo} from "./perms.js";
import {PageRepo} from "./page.ts";
import {DataRepo} from "./data.ts";
import {EventRepo} from "./event";
import {FightRepo} from "./fight";
import {PermsRepo} from "./perms";
import {PageRepo} from "./page";
import {DataRepo} from "./data";
import {SchematicRepo} from "./schem";
import {StatsRepo} from "./stats";
import {AuthRepo} from "./auth";
import { AES, enc, format } from "crypto-js";
import {SchematicRepo} from "./schem.ts";
import {StatsRepo} from "./stats.ts";
import {AuthRepo} from "./auth.ts";
export { EventRepo } from "./event.js"
import { AES, enc } from "crypto-js";
export const apiUrl = import.meta.env.PUBLIC_API_SERVER;
const secret = import.meta.env.PUBLIC_SECRET;
@@ -43,14 +41,14 @@ function decryptToken(token: string): string {
}
export const fetchWithToken = (token: string, url: string, params: RequestInit = {}) => fetch(`${apiUrl}${url}`, {...params, headers: {...(token !== "" ? {"Authorization": "Bearer " + (token)}:{}), "Content-Type": "application/json", ...params.headers}});
export const tokenStore = writable(decryptToken(localStorage.getItem("sw-session") ?? ""))
tokenStore.subscribe((value) => localStorage.setItem("sw-session", encryptToken(value)))
export const tokenStore = writable(decryptToken(localStorage.getItem("sw-session") ?? ""));
tokenStore.subscribe((value) => localStorage.setItem("sw-session", encryptToken(value)));
export const eventRepo = derived(tokenStore, ($token) => new EventRepo($token))
export const fightRepo = derived(tokenStore, ($token) => new FightRepo($token))
export const permsRepo = derived(tokenStore, ($token) => new PermsRepo($token))
export const pageRepo = derived(tokenStore, ($token) => new PageRepo($token))
export const dataRepo = derived(tokenStore, ($token) => new DataRepo($token))
export const schemRepo = derived(tokenStore, ($token) => new SchematicRepo($token))
export const statsRepo = derived(tokenStore, ($token) => new StatsRepo($token))
export const authRepo = derived(tokenStore, ($token) => new AuthRepo($token))
export const eventRepo = derived(tokenStore, ($token) => new EventRepo($token));
export const fightRepo = derived(tokenStore, ($token) => new FightRepo($token));
export const permsRepo = derived(tokenStore, ($token) => new PermsRepo($token));
export const pageRepo = derived(tokenStore, ($token) => new PageRepo($token));
export const dataRepo = derived(tokenStore, ($token) => new DataRepo($token));
export const schemRepo = derived(tokenStore, ($token) => new SchematicRepo($token));
export const statsRepo = derived(tokenStore, ($token) => new StatsRepo($token));
export const authRepo = derived(tokenStore, ($token) => new AuthRepo($token));

View File

@@ -18,8 +18,8 @@
*/
import {fetchWithToken} from "./repo.ts";
import type {SchematicCode, SchematicInfo, SchematicList} from "../types/schem.ts";
import {SchematicCodeSchema, SchematicInfoSchema, SchematicListSchema} from "../types/schem.ts";
import type {SchematicCode, SchematicInfo, SchematicList} from "@type/schem.ts";
import {SchematicCodeSchema, SchematicInfoSchema, SchematicListSchema} from "@type/schem.ts";
export class SchematicRepo {
constructor(private token: string) {}
@@ -41,12 +41,12 @@ export class SchematicRepo {
}
public async uploadSchematic(name: string, content: string) {
return await fetchWithToken(this.token, `/schem`, {
return await fetchWithToken(this.token, "/schem", {
method: "POST",
body: JSON.stringify({
name,
content
})
})
});
}
}

View File

@@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {FightStats, Ranking, UserStats} from "../types/stats.ts";
import type {FightStats, Ranking, UserStats} from "@type/stats.ts";
import {fetchWithToken} from "./repo.ts";
import {FightStatsSchema, RankingSchema, UserStatsSchema} from "../types/stats.ts";
import {FightStatsSchema, RankingSchema, UserStatsSchema} from "@type/stats.ts";
export class StatsRepo {
@@ -30,7 +30,7 @@ export class StatsRepo {
}
public async getFightStats(): Promise<FightStats> {
return await fetchWithToken(this.token, `/stats/fights`).then(value => value.json()).then(FightStatsSchema.parse);
return await fetchWithToken(this.token, "/stats/fights").then(value => value.json()).then(FightStatsSchema.parse);
}
public async getUserStats(id: number): Promise<UserStats> {