Keycloak
69 строк · 1.7 Кб
1import { CallOptions } from "./api/methods";
2import { Links, parseLinks } from "./api/parse-links";
3import { parseResponse } from "./api/parse-response";
4import { Permission, Resource, Scope } from "./api/representations";
5import { request } from "./api/request";
6import { KeycloakContext } from "./root/KeycloakContext";
7
8export const fetchResources = async (
9{ signal, context }: CallOptions,
10requestParams: Record<string, string>,
11shared: boolean | undefined = false,
12): Promise<{ data: Resource[]; links: Links }> => {
13const response = await request(
14`/resources${shared ? "/shared-with-me?" : "?"}`,
15context,
16{ searchParams: shared ? requestParams : undefined, signal },
17);
18
19let links: Links;
20
21try {
22links = parseLinks(response);
23} catch (error) {
24links = {};
25}
26
27return {
28data: checkResponse(await response.json()),
29links,
30};
31};
32
33export const fetchPermission = async (
34{ signal, context }: CallOptions,
35resourceId: string,
36): Promise<Permission[]> => {
37const response = await request(
38`/resources/${resourceId}/permissions`,
39context,
40{ signal },
41);
42return parseResponse<Permission[]>(response);
43};
44
45export const updateRequest = (
46context: KeycloakContext,
47resourceId: string,
48username: string,
49scopes: Scope[] | string[],
50) =>
51request(`/resources/${resourceId}/permissions`, context, {
52method: "PUT",
53body: [{ username, scopes }],
54});
55
56export const updatePermissions = (
57context: KeycloakContext,
58resourceId: string,
59permissions: Permission[],
60) =>
61request(`/resources/${resourceId}/permissions`, context, {
62method: "PUT",
63body: permissions,
64});
65
66function checkResponse<T>(response: T) {
67if (!response) throw new Error("Could not fetch");
68return response;
69}
70