Remove lodash as a hard dependency from the Admin Client (#26136)

Signed-off-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Jon Koops 2024-01-12 10:41:38 +01:00 committed by GitHub
parent 2ee49fe5e9
commit 194c69975b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 11 deletions

View file

@ -38,7 +38,6 @@
},
"dependencies": {
"camelize-ts": "^3.0.0",
"lodash-es": "^4.17.21",
"url-join": "^5.0.0",
"url-template": "^3.1.1"
},
@ -49,6 +48,7 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.0",
"chai": "^5.0.0",
"lodash-es": "^4.17.21",
"mocha": "^10.2.0",
"ts-node": "^10.9.2"
},

View file

@ -1,4 +1,3 @@
import { isUndefined, last, omit, pick } from "lodash-es";
import urlJoin from "url-join";
import { parseTemplate } from "url-template";
import type { KeycloakAdminClient } from "../client.js";
@ -40,6 +39,16 @@ export interface RequestArgs {
headers?: HeadersInit;
}
const pick = (value: Record<string, unknown>, keys: string[]) =>
Object.fromEntries(
Object.entries(value).filter(([key]) => keys.includes(key)),
);
const omit = (value: Record<string, unknown>, keys: string[]) =>
Object.fromEntries(
Object.entries(value).filter(([key]) => !keys.includes(key)),
);
export class Agent {
#client: KeycloakAdminClient;
#basePath: string;
@ -83,7 +92,9 @@ export class Agent {
// Filter query parameters by queryParamKeys
const queryParams =
queryParamKeys.length > 0 ? pick(payload, queryParamKeys) : undefined;
queryParamKeys.length > 0
? (pick(payload, queryParamKeys) as any)
: undefined;
// Add filtered payload parameters to base parameters
const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys];
@ -138,7 +149,7 @@ export class Agent {
// Filter query parameters by queryParamKeys
const queryParams = queryParamKeys
? pick(query, queryParamKeys)
? (pick(query, queryParamKeys) as any)
: undefined;
// Add filtered query parameters to base parameters
@ -249,7 +260,7 @@ export class Agent {
);
}
const resourceId = last(locationHeader.split(SLASH));
const resourceId = locationHeader.split(SLASH).pop();
if (!resourceId) {
// throw an error to let users know the response is not expected
throw new Error(
@ -291,8 +302,7 @@ export class Agent {
}
Object.keys(keyMapping).some((key) => {
if (isUndefined(payload[key])) {
// Skip if undefined
if (typeof payload[key] === "undefined") {
return false;
}
const newKey = keyMapping[key];

View file

@ -313,9 +313,6 @@ importers:
camelize-ts:
specifier: ^3.0.0
version: 3.0.0
lodash-es:
specifier: ^4.17.21
version: 4.17.21
url-join:
specifier: ^5.0.0
version: 5.0.0
@ -341,6 +338,9 @@ importers:
chai:
specifier: ^5.0.0
version: 5.0.0
lodash-es:
specifier: ^4.17.21
version: 4.17.21
mocha:
specifier: ^10.2.0
version: 10.2.0
@ -5367,7 +5367,6 @@ packages:
/lodash-es@4.17.21:
resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==}
dev: false
/lodash.debounce@4.0.8:
resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==}