Some Code Cleanup
This commit is contained in:
@@ -17,7 +17,8 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {fetchWithToken} from "./repo.ts";
|
||||
import {fetchWithToken, tokenStore} from "./repo.ts";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class AuthRepo {
|
||||
constructor(private token: string) {
|
||||
@@ -38,4 +39,6 @@ export class AuthRepo {
|
||||
method: "POST"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const authRepo = derived(tokenStore, ($token) => new AuthRepo($token));
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
import type {Player, Server} from "@type/data.ts";
|
||||
import {PlayerSchema, ServerSchema} from "@type/data.ts";
|
||||
import {fetchWithToken} from "./repo.ts";
|
||||
import {fetchWithToken, tokenStore} from "./repo.ts";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class DataRepo {
|
||||
constructor(private token: string) {}
|
||||
@@ -31,4 +32,6 @@ export class DataRepo {
|
||||
public async getMe(): Promise<Player> {
|
||||
return await fetchWithToken(this.token, "/data/me").then(value => value.json()).then(PlayerSchema.parse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const dataRepo = derived(tokenStore, ($token) => new DataRepo($token));
|
||||
@@ -18,10 +18,11 @@
|
||||
*/
|
||||
|
||||
import type {ExtendedEvent, ShortEvent, SWEvent} from "@type/event";
|
||||
import {fetchWithToken} from "./repo";
|
||||
import {fetchWithToken, tokenStore} from "./repo";
|
||||
import {ExtendedEventSchema, ShortEventSchema, SWEventSchema} from "@type/event.js";
|
||||
import {z} from "zod";
|
||||
import type {Dayjs} from "dayjs";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export interface CreateEvent {
|
||||
name: string
|
||||
@@ -95,3 +96,5 @@ export class EventRepo {
|
||||
return res.ok;
|
||||
}
|
||||
}
|
||||
|
||||
export const eventRepo = derived(tokenStore, ($token) => new EventRepo($token));
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
*/
|
||||
|
||||
import type {EventFight} from "@type/event.js";
|
||||
import {fetchWithToken} from "./repo";
|
||||
import {fetchWithToken, tokenStore} from "./repo";
|
||||
import {z} from "zod";
|
||||
import {EventFightSchema} from "@type/event.js";
|
||||
import type {Dayjs} from "dayjs";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export interface CreateFight {
|
||||
spielmodus: string
|
||||
@@ -86,12 +87,14 @@ export class FightRepo {
|
||||
}
|
||||
|
||||
public async deleteFight(fightId: number): Promise<void> {
|
||||
let res = await fetchWithToken(this.token, `/fights/${fightId}`, {
|
||||
const res = await fetchWithToken(this.token, `/fights/${fightId}`, {
|
||||
method: "DELETE"
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Could not delete fight: " + res.statusText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const fightRepo = derived(tokenStore, ($token) => new FightRepo($token));
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
*/
|
||||
|
||||
import type {Page, PageList} from "@type/page.ts";
|
||||
import {fetchWithToken} from "./repo.ts";
|
||||
import {fetchWithToken, tokenStore} from "./repo.ts";
|
||||
import {PageListSchema, PageSchema} from "@type/page.ts";
|
||||
import {bytesToBase64} from "../admin/util.ts";
|
||||
import {z} from "zod";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class PageRepo {
|
||||
constructor(private token: string) {}
|
||||
@@ -74,4 +75,6 @@ export class PageRepo {
|
||||
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})});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const pageRepo = derived(tokenStore, ($token) => new PageRepo($token));
|
||||
@@ -18,8 +18,9 @@
|
||||
*/
|
||||
|
||||
import type {Perms, UserPerms} from "@type/perms.js";
|
||||
import {fetchWithToken} from "./repo";
|
||||
import {fetchWithToken, tokenStore} from "./repo";
|
||||
import {PermsSchema, UserPermsSchema} from "@type/perms.js";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class PermsRepo {
|
||||
constructor(private token: string) {}
|
||||
@@ -68,3 +69,5 @@ export class PermsRepo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const permsRepo = derived(tokenStore, ($token) => new PermsRepo($token));
|
||||
|
||||
@@ -17,38 +17,16 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {derived, writable} from "svelte/store";
|
||||
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 {writable} from "svelte/store";
|
||||
|
||||
import { AES, enc } from "crypto-js";
|
||||
export const fetchWithToken = (token: string, url: string, params: RequestInit = {}) =>
|
||||
fetch(`${import.meta.env.PUBLIC_API_SERVER}${url}`, {...params, headers: {...(token !== "" ? {"Authorization": "Bearer " + (token)}:{}), "Content-Type": "application/json", ...params.headers}})
|
||||
.then(value => {
|
||||
if (value.status === 401) {
|
||||
tokenStore.set("");
|
||||
}
|
||||
return value;
|
||||
});
|
||||
|
||||
export const apiUrl = import.meta.env.PUBLIC_API_SERVER;
|
||||
const secret = import.meta.env.PUBLIC_SECRET;
|
||||
|
||||
function encryptToken(token: string): string {
|
||||
return AES.encrypt(token, secret).toString();
|
||||
}
|
||||
|
||||
function decryptToken(token: string): string {
|
||||
return AES.decrypt(token, secret).toString(enc.Utf8);
|
||||
}
|
||||
|
||||
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 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 tokenStore = writable((localStorage.getItem("sw-session") ?? ""));
|
||||
tokenStore.subscribe((value) => localStorage.setItem("sw-session", value));
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {fetchWithToken} from "./repo.ts";
|
||||
import {fetchWithToken, tokenStore} from "./repo.ts";
|
||||
import type {SchematicCode, SchematicInfo, SchematicList} from "@type/schem.ts";
|
||||
import {SchematicCodeSchema, SchematicInfoSchema, SchematicListSchema} from "@type/schem.ts";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class SchematicRepo {
|
||||
constructor(private token: string) {}
|
||||
@@ -49,4 +50,6 @@ export class SchematicRepo {
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const schemRepo = derived(tokenStore, ($token) => new SchematicRepo($token));
|
||||
@@ -18,8 +18,9 @@
|
||||
*/
|
||||
|
||||
import type {FightStats, Ranking, UserStats} from "@type/stats.ts";
|
||||
import {fetchWithToken} from "./repo.ts";
|
||||
import {fetchWithToken, tokenStore} from "./repo.ts";
|
||||
import {FightStatsSchema, RankingSchema, UserStatsSchema} from "@type/stats.ts";
|
||||
import {derived} from "svelte/store";
|
||||
|
||||
export class StatsRepo {
|
||||
|
||||
@@ -36,4 +37,6 @@ export class StatsRepo {
|
||||
public async getUserStats(id: number): Promise<UserStats> {
|
||||
return await fetchWithToken(this.token, `/stats/user/${id}`).then(value => value.json()).then(UserStatsSchema.parse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const statsRepo = derived(tokenStore, ($token) => new StatsRepo($token));
|
||||
Reference in New Issue
Block a user