Merge branch 'main' of github/Draconizations/pk-webs-svelte into feat/dashboard
This commit is contained in:
28
dashboard/src/api/errors.ts
Normal file
28
dashboard/src/api/errors.ts
Normal 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?: any): ApiError {
|
||||
var type = ErrorType[ErrorType[code]] ?? ErrorType.Unknown;
|
||||
if (code >= 500) type = ErrorType.InternalServerError;
|
||||
|
||||
var err: ApiError = { code, type };
|
||||
|
||||
if (data) {
|
||||
var d = data;
|
||||
err.message = d.message;
|
||||
err.data = d;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
58
dashboard/src/api/index.ts
Normal file
58
dashboard/src/api/index.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import axios from 'axios';
|
||||
import * as Sentry from '@sentry/browser';
|
||||
|
||||
const baseUrl = () => localStorage.isBeta ? "https://api.beta.pluralkit.me" : "https://api.pluralkit.me";
|
||||
|
||||
const methods = ['get', 'post', 'delete', 'patch', 'put'];
|
||||
const noop = () => {};
|
||||
|
||||
const scheduled = [];
|
||||
const runAPI = () => {
|
||||
if (scheduled.length == 0) return;
|
||||
const {axiosData, res, rej} = scheduled.shift();
|
||||
axios(axiosData)
|
||||
.then((resp) => res(parseData(resp.status, resp.data)))
|
||||
.catch((err) => {
|
||||
Sentry.captureException("Fetch error", err);
|
||||
rej(err);
|
||||
});
|
||||
}
|
||||
|
||||
setInterval(runAPI, 500);
|
||||
|
||||
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) => scheduled.push({ res, rej, axiosData: {
|
||||
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,
|
||||
}}));
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
88
dashboard/src/api/types.ts
Normal file
88
dashboard/src/api/types.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
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;
|
||||
}
|
||||
|
||||
export interface Config {
|
||||
timezone: string;
|
||||
pings_enabled: boolean;
|
||||
member_default_private?: boolean;
|
||||
group_default_private?: boolean;
|
||||
show_private_info?: boolean;
|
||||
member_limit: number;
|
||||
group_limit: number;
|
||||
description_templates: string[];
|
||||
}
|
||||
|
||||
export 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,
|
||||
name_privacy?: string,
|
||||
metadata_privacy?: 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[];
|
||||
}
|
Reference in New Issue
Block a user