From ff2a6d038ddce209de66f61ae3dc3bc247841844 Mon Sep 17 00:00:00 2001 From: Spectralitree <72747870+Spectralitree@users.noreply.github.com> Date: Fri, 31 Dec 2021 16:23:10 +0100 Subject: [PATCH] feat(api): adding member(s) to group(s) --- src/api/index.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index d69ef763..acf826d0 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,4 @@ -import axios, { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig } from 'axios'; +import axios, { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig, Axios } from 'axios'; import Sys from './system'; import Member from './member'; import Group from './group'; @@ -20,7 +20,9 @@ export default class PKAPI { PATCH_GROUP: (gid: string) => `/groups/${gid}`, PATCH_MEMBER: (mid: string) => `/members/${mid}`, - POST_MEMBER: () => `/members` + 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`, } baseUrl: string; @@ -131,6 +133,16 @@ export default class PKAPI { 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 getGroupList(options: {token?: string, id?: any, members?: boolean}) { if (!options.token && !options.id) { throw new Error("Must pass a token or id."); @@ -169,6 +181,16 @@ export default class PKAPI { 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); + } + } + 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.");