2024-02-01 15:46:11 +00:00
|
|
|
import { CallOptions } from "./api/methods";
|
2023-01-16 10:23:07 +00:00
|
|
|
import { Links, parseLinks } from "./api/parse-links";
|
2024-02-01 15:46:11 +00:00
|
|
|
import { parseResponse } from "./api/parse-response";
|
2023-01-16 10:23:07 +00:00
|
|
|
import { Permission, Resource, Scope } from "./api/representations";
|
2024-02-01 15:46:11 +00:00
|
|
|
import { request } from "./api/request";
|
|
|
|
import { KeycloakContext } from "./root/KeycloakContext";
|
2022-11-03 14:06:26 +00:00
|
|
|
|
2023-01-16 10:23:07 +00:00
|
|
|
export const fetchResources = async (
|
2024-02-01 15:46:11 +00:00
|
|
|
{ signal, context }: CallOptions,
|
2023-01-16 10:23:07 +00:00
|
|
|
requestParams: Record<string, string>,
|
2023-07-11 14:03:21 +00:00
|
|
|
shared: boolean | undefined = false,
|
2023-01-16 10:23:07 +00:00
|
|
|
): Promise<{ data: Resource[]; links: Links }> => {
|
2024-02-01 15:46:11 +00:00
|
|
|
const response = await request(
|
|
|
|
`/resources${shared ? "/shared-with-me?" : "?"}`,
|
|
|
|
context,
|
|
|
|
{ searchParams: shared ? requestParams : undefined, signal },
|
2023-01-16 10:23:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let links: Links;
|
|
|
|
|
|
|
|
try {
|
|
|
|
links = parseLinks(response);
|
|
|
|
} catch (error) {
|
|
|
|
links = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: checkResponse(await response.json()),
|
|
|
|
links,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchPermission = async (
|
2024-02-01 15:46:11 +00:00
|
|
|
{ signal, context }: CallOptions,
|
2023-07-11 14:03:21 +00:00
|
|
|
resourceId: string,
|
2023-01-16 10:23:07 +00:00
|
|
|
): Promise<Permission[]> => {
|
2024-02-01 15:46:11 +00:00
|
|
|
const response = await request(
|
2023-01-16 10:23:07 +00:00
|
|
|
`/resources/${resourceId}/permissions`,
|
2024-02-01 15:46:11 +00:00
|
|
|
context,
|
|
|
|
{ signal },
|
2023-01-16 10:23:07 +00:00
|
|
|
);
|
2024-02-01 15:46:11 +00:00
|
|
|
return parseResponse<Permission[]>(response);
|
2023-01-16 10:23:07 +00:00
|
|
|
};
|
2022-11-03 14:06:26 +00:00
|
|
|
|
2023-01-16 10:23:07 +00:00
|
|
|
export const updateRequest = (
|
2024-02-01 15:46:11 +00:00
|
|
|
context: KeycloakContext,
|
2023-01-16 10:23:07 +00:00
|
|
|
resourceId: string,
|
|
|
|
username: string,
|
2023-07-11 14:03:21 +00:00
|
|
|
scopes: Scope[] | string[],
|
2023-01-16 10:23:07 +00:00
|
|
|
) =>
|
2024-02-01 15:46:11 +00:00
|
|
|
request(`/resources/${resourceId}/permissions`, context, {
|
|
|
|
method: "PUT",
|
|
|
|
body: [{ username, scopes }],
|
2023-01-16 10:23:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const updatePermissions = (
|
2024-02-01 15:46:11 +00:00
|
|
|
context: KeycloakContext,
|
2023-01-16 10:23:07 +00:00
|
|
|
resourceId: string,
|
2023-07-11 14:03:21 +00:00
|
|
|
permissions: Permission[],
|
2023-01-16 10:23:07 +00:00
|
|
|
) =>
|
2024-02-01 15:46:11 +00:00
|
|
|
request(`/resources/${resourceId}/permissions`, context, {
|
|
|
|
method: "PUT",
|
|
|
|
body: permissions,
|
2023-01-16 10:23:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function checkResponse<T>(response: T) {
|
|
|
|
if (!response) throw new Error("Could not fetch");
|
|
|
|
return response;
|
|
|
|
}
|