Merge pull request #4 from spiralw/refactor/api

refactor: generalize API library
This commit is contained in:
Jake Fulmine 2022-03-01 10:37:52 +01:00 committed by GitHub
commit 33ee82efa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 229 additions and 487 deletions

28
src/api/errors.ts Normal file
View File

@ -0,0 +1,28 @@
enum ErrorType {
Unknown = 0,
InvalidToken = 401,
NotFound = 404,
InternalServerError = 500,
}
interface ApiError {
code: number,
type: ErrorType,
message?: string,
data?: any,
}
export function parse(code: number, data?: string): ApiError {
var type = ErrorType[ErrorType[code]] ?? ErrorType.Unknown;
if (code >= 500) type = ErrorType.InternalServerError;
var err: ApiError = { code, type };
if (data) {
var d = JSON.parse(data);
err.message = d.message;
err.data = d;
}
return err;
}

View File

@ -1,40 +0,0 @@
interface GroupPrivacy {
description_privacy?: string,
icon_privacy?: string,
list_privacy?: string,
visibility?: string
}
export default class Group {
id?: string;
uuid?: string;
name?: string;
display_name?: string;
description?: string;
icon?: string;
banner?: string;
color?: string;
privacy?: GroupPrivacy;
created?: string;
members?: string[];
constructor(data: Group) {
this.id = data.id;
this.uuid = data.uuid;
this.name = data.name;
this.display_name = data.display_name;
this.description = data.description;
this.icon = data.icon;
this.banner = data.banner;
this.color = data.color;
this.created = data.created;
this.members = data.members;
if (data.privacy) {
this.privacy = {}
this.privacy.description_privacy = data.privacy.description_privacy;
this.privacy.icon_privacy = data.privacy.icon_privacy;
this.privacy.list_privacy = data.privacy.list_privacy;
this.privacy.visibility = data.privacy.visibility;
}
}
}

View File

@ -1,253 +1,43 @@
import axios, { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig, Axios } from 'axios';
import Sys from './system';
import Member from './member';
import Group from './group';
import axios from 'axios';
const baseUrl = () => localStorage.isBeta ? "https://api.beta.pluralkit.me" : "https://api.pluralkit.me";
type FieldError = {
message: string
const methods = ['get', 'post', 'delete', 'patch', 'put'];
const noop = () => {};
export default function() {
const route = [];
const handler = {
get(_, name) {
if (route.length == 0 && name != "private")
route.push("v2");
if (methods.includes(name)) {
return ({ data = undefined, auth = true, token = null, query = null } = {}) => new Promise((res, rej) => axios({
url: baseUrl() + "/" + route.join("/") + (query ? `?${Object.keys(query).map(x => `${x}=${query[x]}`).join("&")}` : ""),
method: name,
headers: {
authorization: token ?? (auth ? localStorage.getItem("pk-token") : undefined),
"content-type": name == "get" ? undefined : "application/json"
},
data: !!data ? JSON.stringify(data) : undefined,
validateStatus: () => true,
}).then((resp) => res(parseData(resp.status, resp.data))).catch(rej));
}
route.push(name);
return new Proxy(noop, handler);
},
apply(target, _, args) {
route.push(...args.filter(x => x != null));
return new Proxy(noop, handler);
}
}
return new Proxy(noop, handler);
}
export default class PKAPI {
ROUTES = {
GET_SYSTEM: (sid?: string) => sid ? `/systems/${sid}` : `/systems/@me`,
GET_MEMBER_LIST: (sid?: string) => sid ? `/systems/${sid}/members` : `/systems/@me/members`,
GET_MEMBER: (mid: string) => `/members/${mid}`,
GET_GROUP_LIST: (sid?: string, members?: boolean) => `${sid ? `/systems/${sid}/groups` : `/systems/@me/groups`}` + `${members ? `?with_members=true` : ""}`,
PATCH_SYSTEM: () => `/systems/@me`,
PATCH_GROUP: (gid: string) => `/groups/${gid}`,
PATCH_MEMBER: (mid: string) => `/members/${mid}`,
POST_MEMBER: () => `/members`,
POST_MEMBER_GROUP: (mid: string, removing: boolean) => !removing ? `/members/${mid}/groups/add` : `/members/${mid}/groups/remove`,
POST_GROUP_MEMBER: (gid: string, removing: boolean) => !removing ? `/groups/${gid}/members/add` : `/groups/${gid}/members/remove`,
DELETE_MEMBER: (mid: string) => `/members/${mid}`,
DELETE_GROUP: (gid: string) => `/groups/${gid}`
}
baseUrl: string;
instance: AxiosInstance
constructor(baseUrl?: string) {
this.baseUrl = baseUrl || 'https://api.pluralkit.me';
this.instance = axios.create({
baseURL: this.baseUrl + '/v2'
})
}
async getSystem(options: { token?: string, id?: any}) {
if (!options.token && !options.id) {
throw new Error("Must pass a token or id.")
}
var system: Sys;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.GET_SYSTEM(options.id ? options.id : ""), 'GET', {token: !options.id ? options.token : ""});
if (res.status === 200) system = new Sys(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return system;
}
async patchSystem(options: {token: string, data: any}) {
var body = new Sys(options.data);
var system: Sys;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.PATCH_SYSTEM(), 'PATCH', {token: options.token, body: body});
if (res.status === 200) system = new Sys(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return system;
}
async getMemberList(options: { token?: string, id?: any}) {
if (!options.token && !options.id) {
throw new Error("Must pass a token or id.")
}
var members: Member[] = [];
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.GET_MEMBER_LIST(options.id ? options.id : ""), 'GET', {token: !options.id ? options.token : ""});
if (res.status === 200) {
let resObject: any = res.data;
resObject.forEach(m => {
let member = new Member(m);
members.push(member);
})
}
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return members;
}
async getMember(options: {id: any}) {
if (!options.id) {
throw new Error("Must pass an id.")
}
var member: Member;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.GET_MEMBER(options.id), 'GET', {});
if (res.status === 200) member = new Member(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return member;
}
async patchMember(options: {token: string, id: any, data: any}) {
var body = new Member(options.data);
var member: Member;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.PATCH_MEMBER(options.id), 'PATCH', {token: options.token, body: body});
if (res.status === 200) member = new Member(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return member;
}
async postMember(options: {token: any, data: any}) {
if (!options.token) throw new Error("Must pass a token.");
var body = new Member(options.data);
var member: Member;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.POST_MEMBER(), 'POST', {token: options.token, body: body});
if (res.status === 200) member = new Member(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return member;
}
async postMemberGroups(options: {token: string, id: string, data: any, removing?: boolean}) {
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.POST_MEMBER_GROUP(options.id, options.removing), 'POST', {token: options.token, body: options.data});
if (res.status !== 204) this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
}
async deleteMember(options: {token: string, id: string}) {
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.DELETE_MEMBER(options.id), 'DELETE', {token: options.token});
if (res.status !== 204 ) this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
}
async getGroupList(options: {token?: string, id?: any, members?: boolean}) {
if (!options.token && !options.id) {
throw new Error("Must pass a token or id.");
}
if (!options.members) options.members = false;
var groups: Group[] = [];
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.GET_GROUP_LIST(options.id ? options.id : "", options.members), 'GET', {token: !options.id ? options.token : ""});
if (res.status === 200) {
let resObject: any = res.data;
resObject.forEach(g => {
let group = new Group(g);
groups.push(group);
})
}
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return groups;
}
async patchGroup(options: {token: string, id: any, data: any}) {
var body = new Group(options.data);
var group: Group;
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.PATCH_GROUP(options.id), 'PATCH', {token: options.token, body: body});
if (res.status === 200) group = new Group(res.data);
else this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
return group;
}
async postGroupMembers(options: {token: string, id: string, data: any, removing?: boolean}) {
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.POST_GROUP_MEMBER(options.id, options.removing), 'POST', {token: options.token, body: options.data});
if (res.status !== 204) this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
}
async deleteGroup(options: {token: string, id: string}) {
var res: AxiosResponse;
try {
res = await this.handle(this.ROUTES.DELETE_GROUP(options.id), 'DELETE', {token: options.token});
if (res.status !== 204 ) this.handleErrors(res);
} catch (error) {
throw new Error(error.message);
}
}
handleErrors(res: any) {
if (res.status === 500) throw new Error("500: Internal server error.");
else if (res.status === 401) throw new Error("401: Your token is invalid.");
else {
let errorObject: any = res.data
if (errorObject.code) {
if (errorObject.code === 40001) {
var message: string;
for (var key in errorObject.errors) {
var val = errorObject.errors[key];
}
} else {
throw new Error(errorObject.message);
}
}
}
}
async handle(url: string, method: Method, options: {token?: string, body?: object}) {
var headers = {}
var request: AxiosRequestConfig = {url, method, headers}
if(options.token) request.headers["Authorization"] = options.token;
if (options.body) {
request.headers["Content-Type"] = "application/json";
request.data = JSON.stringify(options.body);
}
try {
var res = await this.instance(request);
} catch (error) {
res = error.response;
}
return res;
}
import * as errors from './errors';
function parseData(code: number, data: any) {
if (code == 200) return data;
if (code == 204) return;
throw errors.parse(code, data);
}

View File

@ -1,60 +0,0 @@
interface MemberPrivacy {
visibility?: string,
description_privacy?: string,
name_privacy?: string,
birthday_privacy?: string,
pronoun_privacy?: string,
avatar_privacy?: string,
metadata_privacy?: string
}
type proxytag = {
prefix?: string,
suffix?: string
}
export default class Member {
id?: string;
uuid?: string;
name?: string;
display_name?: string;
color?: string;
birthday?: string;
pronouns?: string;
avatar_url?: string;
banner?: string;
description?: string;
created?: string;
keep_proxy?: boolean
system?: string;
proxy_tags?: Array<proxytag>;
privacy?: MemberPrivacy
constructor(data: Member) {
this.id = data.id;
this.uuid = data.uuid;
this.name = data.name;
this.display_name = data.display_name;
this.color = data.color;
this.birthday = data.birthday;
this.pronouns = data.pronouns;
this.avatar_url = data.avatar_url;
this.banner = data.banner;
this.description = data.description;
this.created = data.created;
this.system = data.system;
this.proxy_tags = data.proxy_tags;
this.keep_proxy = data.keep_proxy;
if (data.privacy) {
this.privacy = {
visibility: data.privacy.visibility,
description_privacy: data.privacy.description_privacy,
name_privacy: data.privacy.name_privacy,
birthday_privacy: data.privacy.birthday_privacy,
pronoun_privacy: data.privacy.pronoun_privacy,
avatar_privacy: data.privacy.avatar_privacy,
metadata_privacy: data.privacy.metadata_privacy
}
}
}
}

View File

@ -1,43 +0,0 @@
interface SystemPrivacy {
description_privacy?: string,
member_list_privacy?: string,
front_privacy?: string,
front_history_privacy?: string,
group_list_privacy?: string
}
export default class Sys {
id?: string;
uuid?: string;
name?: string;
description?: string;
tag?: string;
avatar_url?: string;
banner?: string;
timezone?: string;
created?: string;
privacy?: SystemPrivacy;
color?: string;
constructor(data: Sys) {
this.id = data.id;
this.uuid = data.uuid;
this.name = data.name;
this.description = data.description;
this.tag = data.tag;
this.avatar_url = data.avatar_url;
this.banner = data.banner;
this.timezone = data.timezone;
this.created = data.created;
this.color = data.color;
if (data.privacy) {
this.privacy = {
description_privacy: data.privacy.description_privacy,
member_list_privacy: data.privacy.member_list_privacy,
front_privacy: data.privacy.front_privacy,
front_history_privacy: data.privacy.front_history_privacy,
group_list_privacy: data.privacy.group_list_privacy
}
}
}
}

75
src/api/types.ts Normal file
View File

@ -0,0 +1,75 @@
interface SystemPrivacy {
description_privacy?: string,
member_list_privacy?: string,
front_privacy?: string,
front_history_privacy?: string,
group_list_privacy?: string
}
export interface System {
id?: string;
uuid?: string;
name?: string;
description?: string;
tag?: string;
avatar_url?: string;
banner?: string;
timezone?: string;
created?: string;
privacy?: SystemPrivacy;
color?: string;
}
interface MemberPrivacy {
visibility?: string,
description_privacy?: string,
name_privacy?: string,
birthday_privacy?: string,
pronoun_privacy?: string,
avatar_privacy?: string,
metadata_privacy?: string
}
interface proxytag {
prefix?: string,
suffix?: string
}
export interface Member {
id?: string;
uuid?: string;
name?: string;
display_name?: string;
color?: string;
birthday?: string;
pronouns?: string;
avatar_url?: string;
banner?: string;
description?: string;
created?: string;
keep_proxy?: boolean
system?: string;
proxy_tags?: Array<proxytag>;
privacy?: MemberPrivacy
}
export interface GroupPrivacy {
description_privacy?: string,
icon_privacy?: string,
list_privacy?: string,
visibility?: string
}
export interface Group {
id?: string;
uuid?: string;
name?: string;
display_name?: string;
description?: string;
icon?: string;
banner?: string;
color?: string;
privacy?: GroupPrivacy;
created?: string;
members?: string[];
}

View File

@ -2,12 +2,12 @@
import { Row, Col, Modal, Image, Button, CardBody, ModalHeader, ModalBody, ModalFooter, Spinner } from 'sveltestrap';
import moment from 'moment';
import { toHTML } from 'discord-markdown';
import type Group from '../../api/group';
import Edit from './Edit.svelte';
import twemoji from 'twemoji';
import Privacy from './Privacy.svelte';
import type Member from 'src/api/member';
import MemberEdit from './MemberEdit.svelte';
import { Member, Group } from '../../api/types';
export let group: Group;
let editMode: boolean = false;

View File

@ -1,8 +1,8 @@
<script lang="ts">
import { Row, Col, Input, Button, Label, Alert, Spinner, Modal, ModalHeader, ModalBody } from 'sveltestrap';
import { createEventDispatcher } from 'svelte';
import Group from '../../api/group';
import PKAPI from '../../api';
import { Group } from '../../api/types';
import api from '../../api';
import autosize from 'svelte-autosize';
let loading: boolean = false;
@ -11,7 +11,7 @@
let err: string[] = [];
let input = new Group({name: group.name, display_name: group.display_name, banner: group.banner, color: group.color, icon: group.icon, description: group.description});
let input: Group = {...group};
const dispatch = createEventDispatcher();
@ -39,9 +39,8 @@
if (err.length > 0) return;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchGroup({token: localStorage.getItem("pk-token"), id: group.id, data: data});
let res = await api().groups(group.id).patch({data});
group = {...group, ...res};
err = [];
update();
@ -74,9 +73,8 @@
return;
}
loading = true;
const api = new PKAPI();
try {
await api.deleteGroup({token: localStorage.getItem("pk-token"), id: group.id});
await api().groups(group.id).delete();
deleteErr = null;
toggleDeleteModal();
loading = false;

View File

@ -4,15 +4,15 @@
import { onMount } from 'svelte';
import FaSearch from 'svelte-icons/fa/FaSearch.svelte'
import { useParams } from 'svelte-navigator';
import type Group from '../../api/group'
import type Member from '../../api/member'
import PKAPI from '../../api';
import CardsHeader from '../CardsHeader.svelte';
import ListPagination from '../ListPagination.svelte';
import Body from './Body.svelte';
import Svelecte, { addFormatter } from 'svelecte';
import FaLock from 'svelte-icons/fa/FaLock.svelte';
import { Member, Group } from '../../api/types';
import api from '../../api';
export let isPublic: boolean;
export let list: Group[];
@ -43,12 +43,11 @@
if (token || isPublic) fetchGroups();
});
const api = new PKAPI();
async function fetchGroups() {
listLoading = true;
try {
const res: Group[] = await api.getGroupList({token: !isPublic && token, id: isPublic && id, members: !isPublic ? true : false});
const res: Group[] = await api().systems(isPublic ? id : "@me").groups.get({ auth: !isPublic, query: { with_members: !isPublic } });
list = res;
listLoading = false;
} catch (error) {

View File

@ -1,9 +1,6 @@
<script lang="ts">
import type Group from "../../api/group";
import type Member from "../../api/member";
import { Row, Col, Button, Alert, ListGroup, ListGroupItem, Spinner } from 'sveltestrap';
import { createEventDispatcher } from 'svelte';
import PKAPI from '../../api';
import ListPagination from "../ListPagination.svelte";
import twemoji from "twemoji";
import FaUserPlus from 'svelte-icons/fa/FaUserPlus.svelte'
@ -11,6 +8,9 @@
import FaUserMinus from 'svelte-icons/fa/FaUserMinus.svelte'
import Svelecte, { addFormatter } from 'svelecte';
import { Group, Member } from '../../api/types';
import api from '../../api';
let loading: boolean = false;
let err: string;
export let group: Group;
@ -67,14 +67,12 @@ function memberListRenderer(item: any) {
function update() {
dispatch("update", group)
}
const api = new PKAPI();
async function submitAdd() {
let data = membersToBeAdded;
try {
loading = true;
await api.postGroupMembers({token: localStorage.getItem("pk-token"), id: group.id, data: data, removing: false});
await api().groups(group.id).members.add.post({data});
data.forEach(member => group.members.push(member));
update();
err = null;
@ -91,7 +89,7 @@ function memberListRenderer(item: any) {
let data = membersToBeRemoved;
try {
loading = true;
await api.postGroupMembers({token: localStorage.getItem("pk-token"), id: group.id, data: data, removing: true});
await api().groups(group.id).members.remove.post({data});
group.members = group.members.filter(m => !data.includes(m));
update();
err = null;

View File

@ -1,9 +1,10 @@
<script lang="ts">
import Group from "../../api/group";
import PKAPI from "../../api";
import { createEventDispatcher } from "svelte";
import { ModalBody, ModalHeader, Col, Row, Input, Label, ModalFooter, Button, Spinner, Alert } from "sveltestrap";
import { Group } from '../../api/types';
import api from '../../api';
export let privacyOpen: boolean;
export let group: Group;
const togglePrivacyModal = () => (privacyOpen = !privacyOpen);
@ -30,16 +31,15 @@
dispatch('update', group);
}
let input = new Group({privacy: group.privacy});
let input: Group = {privacy: group.privacy};
async function submit() {
let data = input;
err = null;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchGroup({token: localStorage.getItem("pk-token"), id: group.id, data: data});
let res = await api().groups(group.id).patch({data});
group = res;
update();
loading = false;

View File

@ -4,13 +4,13 @@
import { toHTML } from 'discord-markdown';
import twemoji from 'twemoji';
import type Member from '../../api/member';
import type Group from '../../api/group';
import GroupEdit from './GroupEdit.svelte';
import Edit from './Edit.svelte';
import Privacy from './Privacy.svelte';
import ProxyTags from './ProxyTags.svelte';
import { Member, Group } from '../../api/types';
export let groups: Group[] = [];
export let member: Member;
export let isPublic: boolean = false;

View File

@ -4,8 +4,8 @@
import autosize from 'svelte-autosize';
import moment from 'moment';
import Member from '../../api/member';
import PKAPI from '../../api';
import { Member } from '../../api/types'
import api from '../../api';
let loading: boolean = false;
export let member: Member;
@ -13,7 +13,7 @@
let err: string[] = [];
let input = new Member({name: member.name, display_name: member.display_name, birthday: member.birthday, pronouns: member.pronouns, color: member.color, description: member.description, banner: member.banner, avatar_url: member.avatar_url});
let input: Member = {...member};
const dispatch = createEventDispatcher();
@ -54,9 +54,8 @@
if (err.length > 0) return;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchMember({token: localStorage.getItem("pk-token"), id: member.id, data: data});
let res = await api().members(member.id).patch({data});
member = res;
err = [];
update();
@ -89,9 +88,8 @@
return;
}
loading = true;
const api = new PKAPI();
try {
await api.deleteMember({token: localStorage.getItem("pk-token"), id: member.id});
await api().members(member.id).delete();
deleteErr = null;
toggleDeleteModal();
loading = false;

View File

@ -1,9 +1,6 @@
<script lang="ts">
import type Group from "../../api/group";
import type Member from "../../api/member";
import { Row, Col, Button, Alert, ListGroup, ListGroupItem, Spinner } from 'sveltestrap';
import { createEventDispatcher } from 'svelte';
import PKAPI from '../../api';
import ListPagination from "../ListPagination.svelte";
import twemoji from "twemoji";
import Svelecte, { addFormatter } from 'svelecte';
@ -13,6 +10,9 @@
import FaFolderPlus from 'svelte-icons/fa/FaFolderPlus.svelte'
import FaFolderMinus from 'svelte-icons/fa/FaFolderMinus.svelte'
import { Member, Group } from '../../api/types';
import api from '../../api';
export let member: Member;
export let groups: Group[] = [];
let loading: boolean = false;
@ -69,13 +69,11 @@
dispatch("updateGroups", groups);
}
const api = new PKAPI();
async function submitAdd() {
let data = groupsToBeAdded;
try {
loading = true;
await api.postMemberGroups({token: localStorage.getItem("pk-token"), id: member.id, data: data, removing: false});
await api().members(member.id).groups.add.post({data});
groups.forEach(group => data.includes(group.uuid) && group.members.push(member.uuid));
updateGroups();
err = null;
@ -92,7 +90,7 @@
let data = groupsToBeRemoved;
try {
loading = true;
await api.postMemberGroups({token: localStorage.getItem("pk-token"), id: member.id, data: data, removing: true});
await api().members(member.id).groups.remove.post({data});
groups.forEach(group => {if (data.includes(group.uuid)) group.members = group.members.filter(m => m !== member.uuid)});
updateGroups();
err = null;

View File

@ -4,15 +4,15 @@
import { onMount } from 'svelte';
import FaSearch from 'svelte-icons/fa/FaSearch.svelte'
import { useParams } from 'svelte-navigator';
import type Member from '../../api/member';
import type Group from '../../api/group'
import PKAPI from '../../api';
import CardsHeader from '../CardsHeader.svelte';
import ListPagination from '../ListPagination.svelte';
import Svelecte, { addFormatter } from 'svelecte';
import FaLock from 'svelte-icons/fa/FaLock.svelte';
import Body from './Body.svelte';
import { Member, Group } from '../../api/types';
import api from '../../api';
export let isPublic: boolean;
export let list: Member[] = [];
@ -43,12 +43,10 @@
if (token || isPublic) fetchMembers();
});
const api = new PKAPI();
async function fetchMembers() {
listLoading = true;
try {
const res: Member[] = await api.getMemberList({token: !isPublic && token, id: isPublic && id});
const res: Member[] = await api().systems(isPublic ? id : "@me").members.get({ auth: !isPublic });
list = res;
listLoading = false;
} catch (error) {

View File

@ -1,9 +1,10 @@
<script lang="ts">
import Member from "../../api/member";
import PKAPI from "../../api";
import { createEventDispatcher } from "svelte";
import { Col, Row, Input, Label, Button, Alert, Spinner } from "sveltestrap";
import { Member } from '../../api/types';
import api from '../../api';
let loading: boolean;
export let privacyOpen: boolean;
export let member: Member;
@ -33,16 +34,15 @@
dispatch('update', member);
}
let input = new Member({privacy: member.privacy});
let input: Member = {privacy: member.privacy};
async function submit() {
let data = input;
err = null;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchMember({token: localStorage.getItem("pk-token"), id: member.id, data: data});
let res = await api().members(member.id).patch({data});
member = res;
update();
loading = false;

View File

@ -1,9 +1,10 @@
<script lang="ts">
import Member from "../../api/member";
import PKAPI from "../../api";
import { createEventDispatcher } from "svelte";
import { Col, Row, Input, Label, Button, Alert, Spinner, InputGroup } from "sveltestrap";
import { Member } from '../../api/types';
import api from '../../api';
let loading: boolean;
export let proxyOpen: boolean;
export let member: Member;
@ -26,12 +27,11 @@
return;
}
let data = new Member({proxy_tags: input});
const api = new PKAPI();
let data: Member = {proxy_tags: input};
loading = true;
try {
let res = await api.patchMember({token: localStorage.getItem("pk-token"), data: data, id: member.id});
let res = await api().members(member.id).patch({data});
member = res;
err = null;
update();

View File

@ -2,10 +2,11 @@
import { Row, Col, Modal, Image, Button } from 'sveltestrap';
import moment from 'moment';
import { toHTML } from 'discord-markdown';
import type Sys from '../../api/system';
import twemoji from 'twemoji';
import { System } from '../../api/types';
export let user: Sys;
export let user: System;
export let editMode: boolean;
export let isPublic: boolean;

View File

@ -1,18 +1,19 @@
<script lang="ts">
import { Row, Col, Input, Button, Label, Alert } from 'sveltestrap';
import Sys from '../../api/system';
import PKAPI from '../../api';
import autosize from 'svelte-autosize';
// import moment from 'moment-timezone';
import { currentUser } from '../../stores';
import { System } from '../../api/types';
import api from '../../api';
export let editMode: boolean;
export let user: Sys;
export let user: System;
export let loading: boolean;
let err: string[] = [];
let input = new Sys({name: user.name, tag: user.tag, color: user.color, avatar_url: user.avatar_url, banner: user.banner, description: user.description});
let input: System = {...user};
async function submit() {
let data = input;
@ -34,9 +35,8 @@
if (err.length > 0) return;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchSystem({token: localStorage.getItem("pk-token"), data: data});
let res = await api().systems("@me").patch({data});
user = res;
currentUser.update(() => res);
err = [];

View File

@ -5,9 +5,10 @@
import Body from './Body.svelte';
import Privacy from './Privacy.svelte';
import Edit from './Edit.svelte';
import type Sys from '../../api/system';
export let user: Sys;
import { System } from '../../api/types';
export let user: System;
export let isPublic = true;
let loading = false;

View File

@ -1,10 +1,11 @@
<script lang="ts">
import { Card, CardHeader, CardBody, CardTitle, Row, Col, Button, Spinner } from 'sveltestrap';
import FaUserLock from 'svelte-icons/fa/FaUserLock.svelte';
import type Sys from '../../api/system';
import PrivacyEdit from './PrivacyEdit.svelte';
export let user: Sys;
import { System } from '../../api/types';
export let user: System;
let editMode = false;
let loading: boolean;

View File

@ -1,25 +1,25 @@
<script lang="ts">
import Sys from '../../api/system';
import { Input, Row, Col, Button, Label, Alert } from 'sveltestrap';
import { currentUser } from '../../stores';
import PKAPI from '../../api';
import { System } from '../../api/types';
import api from '../../api';
export let loading = false;
export let user: Sys;
export let user: System;
export let editMode: boolean;
let err: string;
let input = new Sys({privacy: user.privacy});
let input: System = {privacy: user.privacy};
async function submit() {
let data = input;
err = null;
loading = true;
const api = new PKAPI();
try {
let res = await api.patchSystem({token: localStorage.getItem("pk-token"), data: data});
let res = await api().systems("@me").patch({data});
user = res;
currentUser.update(() => res);
editMode = false;

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 = "";
};