keycloak-scim/js/apps/account-ui/src/api.ts

70 lines
1.7 KiB
TypeScript
Raw Normal View History

import { CallOptions } from "./api/methods";
2023-01-16 10:23:07 +00:00
import { Links, parseLinks } from "./api/parse-links";
import { parseResponse } from "./api/parse-response";
2023-01-16 10:23:07 +00:00
import { Permission, Resource, Scope } from "./api/representations";
import { request } from "./api/request";
import { KeycloakContext } from "./root/KeycloakContext";
2023-01-16 10:23:07 +00:00
export const fetchResources = async (
{ signal, context }: CallOptions,
2023-01-16 10:23:07 +00:00
requestParams: Record<string, string>,
shared: boolean | undefined = false,
2023-01-16 10:23:07 +00:00
): Promise<{ data: Resource[]; links: Links }> => {
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 (
{ signal, context }: CallOptions,
resourceId: string,
2023-01-16 10:23:07 +00:00
): Promise<Permission[]> => {
const response = await request(
2023-01-16 10:23:07 +00:00
`/resources/${resourceId}/permissions`,
context,
{ signal },
2023-01-16 10:23:07 +00:00
);
return parseResponse<Permission[]>(response);
2023-01-16 10:23:07 +00:00
};
2023-01-16 10:23:07 +00:00
export const updateRequest = (
context: KeycloakContext,
2023-01-16 10:23:07 +00:00
resourceId: string,
username: string,
scopes: Scope[] | string[],
2023-01-16 10:23:07 +00:00
) =>
request(`/resources/${resourceId}/permissions`, context, {
method: "PUT",
body: [{ username, scopes }],
2023-01-16 10:23:07 +00:00
});
export const updatePermissions = (
context: KeycloakContext,
2023-01-16 10:23:07 +00:00
resourceId: string,
permissions: Permission[],
2023-01-16 10:23:07 +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;
}