feat(api): adding member(s) to group(s)
This commit is contained in:
parent
6b24b362da
commit
ff2a6d038d
@ -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 Sys from './system';
|
||||||
import Member from './member';
|
import Member from './member';
|
||||||
import Group from './group';
|
import Group from './group';
|
||||||
@ -20,7 +20,9 @@ export default class PKAPI {
|
|||||||
PATCH_GROUP: (gid: string) => `/groups/${gid}`,
|
PATCH_GROUP: (gid: string) => `/groups/${gid}`,
|
||||||
PATCH_MEMBER: (mid: string) => `/members/${mid}`,
|
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;
|
baseUrl: string;
|
||||||
@ -131,6 +133,16 @@ export default class PKAPI {
|
|||||||
return member;
|
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}) {
|
async getGroupList(options: {token?: string, id?: any, members?: boolean}) {
|
||||||
if (!options.token && !options.id) {
|
if (!options.token && !options.id) {
|
||||||
throw new Error("Must pass a token or id.");
|
throw new Error("Must pass a token or id.");
|
||||||
@ -169,6 +181,16 @@ export default class PKAPI {
|
|||||||
return group;
|
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) {
|
handleErrors(res: any) {
|
||||||
if (res.status === 500) throw new Error("500: Internal server error.");
|
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 if (res.status === 401) throw new Error("401: Your token is invalid.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user