refactor: generalize API library

This commit is contained in:
spiral
2022-02-01 15:24:45 -05:00
parent 6d2fa78767
commit e74b5e1c13
26 changed files with 229 additions and 487 deletions

View File

@@ -3,12 +3,13 @@
import { navigate, useLocation } from "svelte-navigator";
import { currentUser, loggedIn } from '../stores';
import System from '../lib/system/Main.svelte';
import PKAPI from '../api';
import Sys from '../api/system';
import SystemMain from '../lib/system/Main.svelte';
import MemberList from '../lib/member/List.svelte';
import GroupList from '../lib/group/List.svelte';
import { System } from '../api/types';
import api from '../api';
let isPublic = false;
// get the state from the navigator so that we know which tab to start on
@@ -30,7 +31,7 @@
});
// if there is no cached user, get the user from localstorage
let user = new Sys(current ? current : JSON.parse(localStorage.getItem("pk-user")));
let user: System = current ?? JSON.parse(localStorage.getItem("pk-user"));
// since the user in localstorage can be outdated, fetch the user from the api again
if (!current) {
login(localStorage.getItem("pk-token"));
@@ -45,12 +46,11 @@
// just the login function
async function login(token: string) {
const api = new PKAPI();
try {
if (!token) {
throw new Error("Token cannot be empty.")
}
const res: Sys = await api.getSystem({token: token});
const res: System = await api().systems("@me").get({ token });
localStorage.setItem("pk-token", token);
localStorage.setItem("pk-user", JSON.stringify(res));
loggedIn.update(() => true);
@@ -80,7 +80,7 @@
<Col class="mx-auto" xs={12} lg={11} xl={10}>
<TabContent class="mt-3">
<TabPane tabId="system" tab="System" active={tabPane === "system"}>
<System bind:user={user} bind:isPublic />
<SystemMain bind:user={user} bind:isPublic />
</TabPane>
<TabPane tabId="members" tab="Members" active={tabPane === "members"}>
<MemberList bind:groups={groups} bind:list={members} bind:isPublic />

View File

@@ -7,8 +7,8 @@
import twemoji from 'twemoji';
import { toHTML } from 'discord-markdown';
import PKAPI from '../api/index';
import type Sys from '../api/system';
import { System } from '../api/types';
import api from '../api';
let loading = false;
let err: string;
@@ -33,12 +33,11 @@
async function login(token: string) {
loading = true;
const api = new PKAPI();
try {
if (!token) {
throw new Error("Token cannot be empty.")
}
const res: Sys = await api.getSystem({token: token});
const res: System = await api().systems("@me").get({ token });
localStorage.setItem("pk-token", token);
localStorage.setItem("pk-user", JSON.stringify(res));
err = null;

View File

@@ -3,15 +3,16 @@
import { useParams } from "svelte-navigator";
import { onMount } from 'svelte';
import System from '../../lib/system/Main.svelte';
import PKAPI from '../../api';
import Sys from '../../api/system';
import SystemMain from '../../lib/system/Main.svelte';
import MemberList from '../../lib/member/List.svelte';
import GroupList from '../../lib/group/List.svelte';
import { System } from '../../api/types';
import api from '../../api';
let isPublic = true;
let user = new Sys({});
let user: System = {};
let settings = JSON.parse(localStorage.getItem("pk-settings"));
let members = [];
@@ -22,8 +23,6 @@
let err: string;
const api = new PKAPI();
let title = "system"
onMount(() => {
@@ -32,7 +31,7 @@
async function getSystem() {
try {
let res: Sys = await api.getSystem({id: id})
let res: System = await api().systems(id);
user = res;
title = user.name ? user.name : "system";
} catch (error) {
@@ -60,7 +59,7 @@
<Alert color="info">You are currently <b>viewing</b> a system.</Alert>
<TabContent class="mt-3">
<TabPane tabId="system" tab="System" active>
<System bind:user bind:isPublic />
<SystemMain bind:user bind:isPublic />
</TabPane>
<TabPane tabId="members" tab="Members">
<MemberList bind:list={members} bind:isPublic/>

View File

@@ -3,6 +3,8 @@
import FaInfoCircle from 'svelte-icons/fa/FaInfoCircle.svelte'
import ShardItem from '../lib/shard.svelte';
import api from '../api';
let hover = null;
let message = "Loading...";
@@ -24,7 +26,7 @@
let valid = false;
const get = async () => {
const pkdata = await fetch("https://api.pluralkit.me/private/meta").then(x => x.json());
const pkdata = await api().private.meta.get();
shards = pkdata.shards.sort((x, y) => (x.id > y.id) ? 1 : -1);
let pings = 0;
shards = shards.map(shard => {
@@ -36,7 +38,7 @@
pingAverage = Math.trunc(pings / shards.length).toString();
currentCommitMsg = `Current Git commit: <a href="https://github.com/xSke/PluralKit/commit/${pkdata.version}">${pkdata.version}</a>`;
currentCommitMsg = `Current Git commit: <a href="https://github.com/xSke/PluralKit/commit/${pkdata.version}">${pkdata.version.slice(0,7)}</a>`;
message = "";
};