parent
734ca7623d
commit
4452cb071d
|
|
@ -2,6 +2,8 @@ import {
|
||||||
ApiResponse,
|
ApiResponse,
|
||||||
LibreCcmApiClient,
|
LibreCcmApiClient,
|
||||||
ListView,
|
ListView,
|
||||||
|
ApiError,
|
||||||
|
ApiClientError,
|
||||||
} from "@libreccm/ccm-apiclient-commons";
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
|
import { buildIdentifierParam } from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
@ -32,14 +34,12 @@ export class GroupsApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroups(limit: number, offset: number): Promise<ListView<Group>> {
|
async getGroups(limit: number, offset: number): Promise<ListView<Group>> {
|
||||||
|
const url = `${this.#GROUPS_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
`${this.#GROUPS_API_PREFIX}`,
|
|
||||||
{
|
|
||||||
limit: limit.toString(),
|
limit: limit.toString(),
|
||||||
offset: offset.toString(),
|
offset: offset.toString(),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
string,
|
string,
|
||||||
|
|
@ -60,86 +60,139 @@ export class GroupsApiClient {
|
||||||
offset: result.offset as number,
|
offset: result.offset as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get groups:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get groups`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get groups: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(`Failed to get groups: ${err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroup(groupIdentifier: string | number): Promise<Group> {
|
async getGroup(groupIdentifier: string | number): Promise<Group> {
|
||||||
try {
|
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
|
||||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
|
||||||
groupIdentifier
|
groupIdentifier
|
||||||
)}`
|
)}`;
|
||||||
);
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(url);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return buildGroupFromRecord(
|
return buildGroupFromRecord(
|
||||||
(await response.json()) as Record<string, unknown>
|
(await response.json()) as Record<string, unknown>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get group ${groupIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get group ${groupIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addGroup(group: Group): Promise<string> {
|
async addGroup(group: Group): Promise<string> {
|
||||||
|
const url = `${this.#GROUPS_API_PREFIX}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.post(
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
`${this.#GROUPS_API_PREFIX}`,
|
url,
|
||||||
JSON.stringify(group)
|
JSON.stringify(group)
|
||||||
);
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return group.name;
|
return group.name;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add group ${group.name}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"post",
|
||||||
|
`Failed to add group ${group.name}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add group ${group.name}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add group ${group.name}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateGroup(groupIdentifier: string, group: Group): Promise<void> {
|
async updateGroup(groupIdentifier: string, group: Group): Promise<void> {
|
||||||
|
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
|
groupIdentifier
|
||||||
|
)}`;
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
const response: ApiResponse = await this.#apiClient.put(
|
||||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
url,
|
||||||
groupIdentifier
|
|
||||||
)}`,
|
|
||||||
JSON.stringify(group)
|
JSON.stringify(group)
|
||||||
);
|
);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to update group ${groupIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to update group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to update group ${groupIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to update group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteGroup(groupIdentifier: string): Promise<void> {
|
async deleteGroup(groupIdentifier: string): Promise<void> {
|
||||||
try {
|
const url = `${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
|
||||||
`${this.#GROUPS_API_PREFIX}/${buildIdentifierParam(
|
|
||||||
groupIdentifier
|
groupIdentifier
|
||||||
)}`
|
)}`;
|
||||||
);
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to delete group ${name}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to delete group ${name}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to delete group ${name}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to delete group ${name}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,16 +201,13 @@ export class GroupsApiClient {
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number
|
offset: number
|
||||||
): Promise<ListView<GroupUserMembership>> {
|
): Promise<ListView<GroupUserMembership>> {
|
||||||
try {
|
|
||||||
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
||||||
|
const url = `${this.#GROUPS_API_PREFIX}/${groupParam}/members`;
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
try {
|
||||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/members`,
|
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||||
{
|
|
||||||
limit: limit.toString(),
|
limit: limit.toString(),
|
||||||
offset: offset.toString(),
|
offset: offset.toString(),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
string,
|
string,
|
||||||
|
|
@ -178,11 +228,22 @@ export class GroupsApiClient {
|
||||||
offset: result.offset as number,
|
offset: result.offset as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get members of group ${groupIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get members of group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get members of group ${groupIdentifier}: ${err}`;
|
if (err instanceof err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get members of group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -190,24 +251,32 @@ export class GroupsApiClient {
|
||||||
groupIdentifier: string | number,
|
groupIdentifier: string | number,
|
||||||
userIdentifier: string | number
|
userIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
|
||||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||||
const memberParam = buildIdentifierParam(userIdentifier);
|
const memberParam = buildIdentifierParam(userIdentifier);
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
const url = `${
|
||||||
`${
|
|
||||||
this.#GROUPS_API_PREFIX
|
this.#GROUPS_API_PREFIX
|
||||||
}/${groupParam}/members/${memberParam}`
|
}/${groupParam}/members/${memberParam}`;
|
||||||
);
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.put(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add user ${userIdentifier} to
|
throw new ApiError(
|
||||||
group ${groupIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to add user ${userIdentifier} to group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add user ${userIdentifier} to
|
if (err instanceof ApiError) {
|
||||||
group ${groupIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add user ${userIdentifier} to group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -215,24 +284,32 @@ export class GroupsApiClient {
|
||||||
groupIdentifier: string | number,
|
groupIdentifier: string | number,
|
||||||
userIdentifier: string | number
|
userIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
|
||||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||||
const memberParam = buildIdentifierParam(userIdentifier);
|
const memberParam = buildIdentifierParam(userIdentifier);
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
const url = `${
|
||||||
`${
|
|
||||||
this.#GROUPS_API_PREFIX
|
this.#GROUPS_API_PREFIX
|
||||||
}/${groupParam}/members/${memberParam}`
|
}/${groupParam}/members/${memberParam}`;
|
||||||
);
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to remove user ${userIdentifier} from
|
throw new ApiError(
|
||||||
group ${groupIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to remove user ${userIdentifier} from group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to remote user ${userIdentifier} from
|
if (err instanceof ApiError) {
|
||||||
group ${groupIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to remote user ${userIdentifier} from group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,11 +317,10 @@ export class GroupsApiClient {
|
||||||
groupIdentifier: string
|
groupIdentifier: string
|
||||||
): Promise<ListView<PartyRoleMembership>> {
|
): Promise<ListView<PartyRoleMembership>> {
|
||||||
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
const groupParam: string = buildIdentifierParam(groupIdentifier);
|
||||||
|
const url = `${this.#GROUPS_API_PREFIX}/${groupParam}/roles`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response: ApiResponse = await this.#apiClient.get(
|
const response: ApiResponse = await this.#apiClient.get(url);
|
||||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles`
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result: Record<
|
const result: Record<
|
||||||
string,
|
string,
|
||||||
|
|
@ -265,11 +341,22 @@ export class GroupsApiClient {
|
||||||
offset: result.offset as number,
|
offset: result.offset as number,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to get roles of group ${groupIdentifier}:
|
throw new ApiError(
|
||||||
${response.status} ${response.statusText}`;
|
response.status,
|
||||||
|
response.statusText,
|
||||||
|
"get",
|
||||||
|
`Failed to get roles of group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to get roles of group ${groupIdentifier}: ${err}`;
|
if (err instanceof ApiError) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to get roles of group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -277,22 +364,32 @@ export class GroupsApiClient {
|
||||||
groupIdentifier: string | number,
|
groupIdentifier: string | number,
|
||||||
roleIdentifier: string | number
|
roleIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
|
||||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||||
const response: ApiResponse = await this.#apiClient.put(
|
const url = `${
|
||||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}`
|
this.#GROUPS_API_PREFIX
|
||||||
);
|
}/${groupParam}/roles/${roleParam}`;
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.put(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to add role ${roleIdentifier} to
|
throw new ApiError(
|
||||||
group ${groupIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"put",
|
||||||
|
`Failed to add role ${roleIdentifier} to group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to add role ${roleIdentifier} to
|
if (err instanceof ApiError) {
|
||||||
group ${groupIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to add role ${roleIdentifier} to group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -300,22 +397,32 @@ export class GroupsApiClient {
|
||||||
groupIdentifier: string | number,
|
groupIdentifier: string | number,
|
||||||
roleIdentifier: string | number
|
roleIdentifier: string | number
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
|
||||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||||
const response: ApiResponse = await this.#apiClient.delete(
|
const url = `${
|
||||||
`${this.#GROUPS_API_PREFIX}/${groupParam}/roles/${roleParam}`
|
this.#GROUPS_API_PREFIX
|
||||||
);
|
}/${groupParam}/roles/${roleParam}`;
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw `Failed to remove role ${roleIdentifier} from
|
throw new ApiError(
|
||||||
group ${groupIdentifier}:
|
response.status,
|
||||||
${response.status} ${response.statusText}`;
|
response.statusText,
|
||||||
|
"delete",
|
||||||
|
`Failed to remove role ${roleIdentifier} from group ${groupIdentifier}`,
|
||||||
|
url
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw `Failed to remote role ${roleIdentifier} from
|
if (err instanceof ApiError) {
|
||||||
group ${groupIdentifier}: ${err}`;
|
throw err;
|
||||||
|
} else {
|
||||||
|
throw new ApiClientError(
|
||||||
|
`Failed to remote role ${roleIdentifier} from group ${groupIdentifier}: ${err}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue