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": { "dependencies": {
"camelize-ts": "^3.0.0", "camelize-ts": "^3.0.0",
"lodash-es": "^4.17.21",
"url-join": "^5.0.0", "url-join": "^5.0.0",
"url-template": "^3.1.1" "url-template": "^3.1.1"
}, },
@ -49,6 +48,7 @@
"@types/mocha": "^10.0.6", "@types/mocha": "^10.0.6",
"@types/node": "^20.11.0", "@types/node": "^20.11.0",
"chai": "^5.0.0", "chai": "^5.0.0",
"lodash-es": "^4.17.21",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"ts-node": "^10.9.2" "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 urlJoin from "url-join";
import { parseTemplate } from "url-template"; import { parseTemplate } from "url-template";
import type { KeycloakAdminClient } from "../client.js"; import type { KeycloakAdminClient } from "../client.js";
@ -40,6 +39,16 @@ export interface RequestArgs {
headers?: HeadersInit; 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 { export class Agent {
#client: KeycloakAdminClient; #client: KeycloakAdminClient;
#basePath: string; #basePath: string;
@ -83,7 +92,9 @@ export class Agent {
// Filter query parameters by queryParamKeys // Filter query parameters by queryParamKeys
const queryParams = 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 // Add filtered payload parameters to base parameters
const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys]; const allUrlParamKeys = [...Object.keys(baseParams), ...urlParamKeys];
@ -138,7 +149,7 @@ export class Agent {
// Filter query parameters by queryParamKeys // Filter query parameters by queryParamKeys
const queryParams = queryParamKeys const queryParams = queryParamKeys
? pick(query, queryParamKeys) ? (pick(query, queryParamKeys) as any)
: undefined; : undefined;
// Add filtered query parameters to base parameters // 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) { if (!resourceId) {
// throw an error to let users know the response is not expected // throw an error to let users know the response is not expected
throw new Error( throw new Error(
@ -291,8 +302,7 @@ export class Agent {
} }
Object.keys(keyMapping).some((key) => { Object.keys(keyMapping).some((key) => {
if (isUndefined(payload[key])) { if (typeof payload[key] === "undefined") {
// Skip if undefined
return false; return false;
} }
const newKey = keyMapping[key]; const newKey = keyMapping[key];

View file

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