mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-11 08:10:10 +00:00
refactor: generalize API library
This commit is contained in:
parent
6d2fa78767
commit
e74b5e1c13
26 changed files with 229 additions and 487 deletions
284
src/api/index.ts
284
src/api/index.ts
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue