2023-02-03 10:45:11 +00:00
|
|
|
import { isUndefined, last, omit, pick } from "lodash-es";
|
|
|
|
import urlJoin from "url-join";
|
|
|
|
import { parseTemplate } from "url-template";
|
|
|
|
import type { KeycloakAdminClient } from "../client.js";
|
|
|
|
import {
|
|
|
|
fetchWithError,
|
|
|
|
NetworkError,
|
|
|
|
parseResponse,
|
|
|
|
} from "../utils/fetchWithError.js";
|
|
|
|
import { stringifyQueryParams } from "../utils/stringifyQueryParams.js";
|
|
|
|
|
|
|
|
// constants
|
|
|
|
const SLASH = "/";
|
|
|
|
|
|
|
|
type Method = "GET" | "POST" | "PUT" | "DELETE";
|
|
|
|
|
|
|
|
// interface
|
|
|
|
export interface RequestArgs {
|
|
|
|
method: Method;
|
|
|
|
path?: string;
|
|
|
|
// Keys of url params to be applied
|
|
|
|
urlParamKeys?: string[];
|
|
|
|
// Keys of query parameters to be applied
|
|
|
|
queryParamKeys?: string[];
|
|
|
|
// Mapping of key transformations to be performed on the payload
|
|
|
|
keyTransform?: Record<string, string>;
|
|
|
|
// If responding with 404, catch it and return null instead
|
|
|
|
catchNotFound?: boolean;
|
|
|
|
// The key of the value to use from the payload of request. Only works for POST & PUT.
|
|
|
|
payloadKey?: string;
|
|
|
|
// Whether the response header have a location field with newly created resource id
|
|
|
|
// if this value is set, we return the field with format: {[field]: resourceId}
|
|
|
|
// to represent the newly created resource
|
|
|
|
// detail: keycloak/keycloak-nodejs-admin-client issue #11
|
|
|
|
returnResourceIdInLocationHeader?: { field: string };
|
|
|
|
/**
|
|
|
|
* Keys to be ignored, meaning that they will not be filtered out of the request payload even if they are a part of `urlParamKeys` or `queryParamKeys`,
|
|
|
|
*/
|
|
|
|
ignoredKeys?: string[];
|
|
|
|
headers?: HeadersInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Agent {
|
2023-10-23 18:12:55 +00:00
|
|
|
#client: KeycloakAdminClient;
|
|
|
|
#basePath: string;
|
|
|
|
#getBaseParams?: () => Record<string, any>;
|
|
|
|
#getBaseUrl?: () => string;
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
constructor({
|
|
|
|
client,
|
|
|
|
path = "/",
|
|
|
|
getUrlParams = () => ({}),
|
|
|
|
getBaseUrl = () => client.baseUrl,
|
|
|
|
}: {
|
|
|
|
client: KeycloakAdminClient;
|
|
|
|
path?: string;
|
|
|
|
getUrlParams?: () => Record<string, any>;
|
|
|
|
getBaseUrl?: () => string;
|
|
|
|
}) {
|
2023-10-23 18:12:55 +00:00
|
|
|
this.#client = client;
|
|
|
|
this.#getBaseParams = getUrlParams;
|
|
|
|
this.#getBaseUrl = getBaseUrl;
|
|
|
|
this.#basePath = path;
|
2023-02-03 10:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public request({
|
|
|
|
method,
|
|
|
|
path = "",
|
|
|
|
urlParamKeys = [],
|
|
|
|
queryParamKeys = [],
|
|
|
|
catchNotFound = false,
|
|
|
|
keyTransform,
|
|
|
|
payloadKey,
|
|
|
|
returnResourceIdInLocationHeader,
|
|
|
|
ignoredKeys,
|
|
|
|
headers,
|
|
|
|
}: RequestArgs) {
|
|
|
|
return async (
|
|
|
|
payload: any = {},
|
2023-07-11 14:03:21 +00:00
|
|
|
options?: Pick<RequestArgs, "catchNotFound">,
|
2023-02-03 10:45:11 +00:00
|
|
|
) => {
|
2023-10-23 18:12:55 +00:00
|
|
|
const baseParams = this.#getBaseParams?.() ?? {};
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
// Filter query parameters by queryParamKeys
|
2023-06-12 10:50:53 +00:00
|
|
|
const queryParams =
|
|
|
|
queryParamKeys.length > 0 ? pick(payload, queryParamKeys) : undefined;
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
// Add filtered payload parameters to base parameters
|
|
|
|
const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys];
|
|
|
|
const urlParams = { ...baseParams, ...pick(payload, allUrlParamKeys) };
|
|
|
|
|
2023-06-12 10:50:53 +00:00
|
|
|
if (!(payload instanceof FormData)) {
|
|
|
|
// Omit url parameters and query parameters from payload
|
|
|
|
const omittedKeys = ignoredKeys
|
|
|
|
? [...allUrlParamKeys, ...queryParamKeys].filter(
|
2023-07-11 14:03:21 +00:00
|
|
|
(key) => !ignoredKeys.includes(key),
|
2023-06-12 10:50:53 +00:00
|
|
|
)
|
|
|
|
: [...allUrlParamKeys, ...queryParamKeys];
|
2023-02-03 10:45:11 +00:00
|
|
|
|
2023-06-12 10:50:53 +00:00
|
|
|
payload = omit(payload, omittedKeys);
|
|
|
|
}
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
// Transform keys of both payload and queryParams
|
|
|
|
if (keyTransform) {
|
2023-10-23 18:12:55 +00:00
|
|
|
this.#transformKey(payload, keyTransform);
|
|
|
|
this.#transformKey(queryParams, keyTransform);
|
2023-02-03 10:45:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
return this.#requestWithParams({
|
2023-02-03 10:45:11 +00:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
payload,
|
|
|
|
urlParams,
|
|
|
|
queryParams,
|
|
|
|
// catchNotFound precedence: global > local > default
|
|
|
|
catchNotFound,
|
2023-10-23 18:12:55 +00:00
|
|
|
...(this.#client.getGlobalRequestArgOptions() ?? options ?? {}),
|
2023-02-03 10:45:11 +00:00
|
|
|
payloadKey,
|
|
|
|
returnResourceIdInLocationHeader,
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateRequest({
|
|
|
|
method,
|
|
|
|
path = "",
|
|
|
|
urlParamKeys = [],
|
|
|
|
queryParamKeys = [],
|
|
|
|
catchNotFound = false,
|
|
|
|
keyTransform,
|
|
|
|
payloadKey,
|
|
|
|
returnResourceIdInLocationHeader,
|
|
|
|
headers,
|
|
|
|
}: RequestArgs) {
|
|
|
|
return async (query: any = {}, payload: any = {}) => {
|
2023-10-23 18:12:55 +00:00
|
|
|
const baseParams = this.#getBaseParams?.() ?? {};
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
// Filter query parameters by queryParamKeys
|
|
|
|
const queryParams = queryParamKeys
|
|
|
|
? pick(query, queryParamKeys)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
// Add filtered query parameters to base parameters
|
|
|
|
const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys];
|
|
|
|
const urlParams = {
|
|
|
|
...baseParams,
|
|
|
|
...pick(query, allUrlParamKeys),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Transform keys of queryParams
|
|
|
|
if (keyTransform) {
|
2023-10-23 18:12:55 +00:00
|
|
|
this.#transformKey(queryParams, keyTransform);
|
2023-02-03 10:45:11 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
return this.#requestWithParams({
|
2023-02-03 10:45:11 +00:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
payload,
|
|
|
|
urlParams,
|
|
|
|
queryParams,
|
|
|
|
catchNotFound,
|
|
|
|
payloadKey,
|
|
|
|
returnResourceIdInLocationHeader,
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
async #requestWithParams({
|
2023-02-03 10:45:11 +00:00
|
|
|
method,
|
|
|
|
path,
|
|
|
|
payload,
|
|
|
|
urlParams,
|
|
|
|
queryParams,
|
|
|
|
catchNotFound,
|
|
|
|
payloadKey,
|
|
|
|
returnResourceIdInLocationHeader,
|
|
|
|
headers,
|
|
|
|
}: {
|
|
|
|
method: Method;
|
|
|
|
path: string;
|
|
|
|
payload: any;
|
|
|
|
urlParams: any;
|
|
|
|
queryParams?: Record<string, string>;
|
|
|
|
catchNotFound: boolean;
|
|
|
|
payloadKey?: string;
|
|
|
|
returnResourceIdInLocationHeader?: { field: string };
|
|
|
|
headers?: HeadersInit;
|
|
|
|
}) {
|
2023-10-23 18:12:55 +00:00
|
|
|
const newPath = urlJoin(this.#basePath, path);
|
2023-02-03 10:45:11 +00:00
|
|
|
|
|
|
|
// Parse template and replace with values from urlParams
|
|
|
|
const pathTemplate = parseTemplate(newPath);
|
|
|
|
const parsedPath = pathTemplate.expand(urlParams);
|
2023-10-23 18:12:55 +00:00
|
|
|
const url = new URL(`${this.#getBaseUrl?.() ?? ""}${parsedPath}`);
|
|
|
|
const requestOptions = { ...this.#client.getRequestOptions() };
|
2023-02-03 10:45:11 +00:00
|
|
|
const requestHeaders = new Headers([
|
|
|
|
...new Headers(requestOptions.headers).entries(),
|
2023-10-23 18:12:55 +00:00
|
|
|
["authorization", `Bearer ${await this.#client.getAccessToken()}`],
|
2023-02-03 10:45:11 +00:00
|
|
|
["accept", "application/json, text/plain, */*"],
|
|
|
|
...new Headers(headers).entries(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const searchParams: Record<string, string> = {};
|
|
|
|
|
|
|
|
// Add payload parameters to search params if method is 'GET'.
|
|
|
|
if (method === "GET") {
|
|
|
|
Object.assign(searchParams, payload);
|
|
|
|
} else if (requestHeaders.get("content-type") === "text/plain") {
|
|
|
|
// Pass the payload as a plain string if the content type is 'text/plain'.
|
|
|
|
requestOptions.body = payload as unknown as string;
|
2023-02-13 13:03:47 +00:00
|
|
|
} else if (payload instanceof FormData) {
|
|
|
|
requestOptions.body = payload;
|
2023-02-03 10:45:11 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise assume it's JSON and stringify it.
|
|
|
|
requestOptions.body = JSON.stringify(
|
2023-07-11 14:03:21 +00:00
|
|
|
payloadKey ? payload[payloadKey] : payload,
|
2023-02-03 10:45:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:03:47 +00:00
|
|
|
if (!requestHeaders.has("content-type") && !(payload instanceof FormData)) {
|
2023-02-03 10:45:11 +00:00
|
|
|
requestHeaders.set("content-type", "application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queryParams) {
|
|
|
|
Object.assign(searchParams, queryParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
url.search = stringifyQueryParams(searchParams);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetchWithError(url, {
|
|
|
|
...requestOptions,
|
|
|
|
headers: requestHeaders,
|
|
|
|
method,
|
|
|
|
});
|
|
|
|
|
|
|
|
// now we get the response of the http request
|
|
|
|
// if `resourceIdInLocationHeader` is true, we'll get the resourceId from the location header field
|
|
|
|
// todo: find a better way to find the id in path, maybe some kind of pattern matching
|
|
|
|
// for now, we simply split the last sub-path of the path returned in location header field
|
|
|
|
if (returnResourceIdInLocationHeader) {
|
|
|
|
const locationHeader = res.headers.get("location");
|
|
|
|
|
|
|
|
if (typeof locationHeader !== "string") {
|
|
|
|
throw new Error(
|
2023-07-11 14:03:21 +00:00
|
|
|
`location header is not found in request: ${res.url}`,
|
2023-02-03 10:45:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const resourceId = last(locationHeader.split(SLASH));
|
|
|
|
if (!resourceId) {
|
|
|
|
// throw an error to let users know the response is not expected
|
|
|
|
throw new Error(
|
2023-07-11 14:03:21 +00:00
|
|
|
`resourceId is not found in Location header from request: ${res.url}`,
|
2023-02-03 10:45:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return with format {[field]: string}
|
|
|
|
const { field } = returnResourceIdInLocationHeader;
|
|
|
|
return { [field]: resourceId };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
Object.entries(headers || []).find(
|
|
|
|
([key, value]) =>
|
|
|
|
key.toLowerCase() === "accept" &&
|
2023-07-11 14:03:21 +00:00
|
|
|
value === "application/octet-stream",
|
2023-02-03 10:45:11 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return res.arrayBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseResponse(res);
|
|
|
|
} catch (err) {
|
|
|
|
if (
|
|
|
|
err instanceof NetworkError &&
|
|
|
|
err.response.status === 404 &&
|
|
|
|
catchNotFound
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
#transformKey(payload: any, keyMapping: Record<string, string>) {
|
2023-02-03 10:45:11 +00:00
|
|
|
if (!payload) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(keyMapping).some((key) => {
|
|
|
|
if (isUndefined(payload[key])) {
|
|
|
|
// Skip if undefined
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const newKey = keyMapping[key];
|
|
|
|
payload[newKey] = payload[key];
|
|
|
|
delete payload[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|