-
Notifications
You must be signed in to change notification settings - Fork 1
/
gridConfigGenerator.ts
39 lines (35 loc) · 1.18 KB
/
gridConfigGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import recommendedConfig from "./gridConfig.ts";
import { JWK } from "./jwk.ts";
export function extendGridConfig(
config: Record<string, unknown>,
authorizationKey: JWK,
): Record<string, unknown> {
merge(config, recommendedConfig as Record<string, unknown>);
merge(config, { authorization: { keys: [authorizationKey] } });
return config;
}
function merge(
object: Record<string, unknown>,
extension: Record<string, unknown>,
) {
for (const name in extension) {
const childObject = object[name];
const childExtension = extension[name];
if (Array.isArray(childObject) && Array.isArray(childExtension)) {
for (const item of childExtension) {
childObject.push(item);
}
} else if (isPlainObject(childObject) && isPlainObject(childExtension)) {
merge(childObject, childExtension);
} else if (childObject == null) {
object[name] = childExtension;
}
}
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
if (Object.prototype.toString.call(value) !== "[object Object]") {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}