Skip to content

Commit

Permalink
Warning on the browser console when local config has extra properties…
Browse files Browse the repository at this point in the history
… vs. remote config
  • Loading branch information
EricWittmann committed Jan 30, 2024
1 parent 5ff2eba commit cd35ddd
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions ui/ui-app/src/services/useConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createEndpoint, httpGet } from "@utils/rest.utils.ts";
import { cloneObject } from "@utils/object.utils.ts";

export enum AlertVariant {
success = "success",
Expand Down Expand Up @@ -137,6 +138,37 @@ export function getRegistryConfig(): ApicurioRegistryConfig {
return config;
}


function difference(base: any, overrides: any | undefined): any {
const rval: any = cloneObject(overrides);

// Remove any properties that exist in base.
Object.getOwnPropertyNames(base).forEach(propertyName => {
if (typeof rval[propertyName] !== "object") {
delete rval[propertyName];
}
});

// Now diff any remaining props that are objects
Object.getOwnPropertyNames(rval).forEach(propertyName => {
const value: any = rval[propertyName];
const baseValue: any = base[propertyName];
if (typeof value === "object") {
rval[propertyName] = difference(baseValue, value);
}
});

// Now remove any properties with empty object values.
Object.getOwnPropertyNames(rval).forEach(propertyName => {
if (typeof rval[propertyName] === "object" && Object.keys(rval[propertyName]).length === 0) {
delete rval[propertyName];
}
});

return rval;
}


function overrideObject(base: any, overrides: any | undefined): any {
if (overrides === undefined) {
return {
Expand All @@ -161,11 +193,7 @@ function overrideObject(base: any, overrides: any | undefined): any {
}

function overrideConfig(base: ApicurioRegistryConfig, overrides: ApicurioRegistryConfig): ApicurioRegistryConfig {
const rval: ApicurioRegistryConfig = overrideObject(base, overrides);
// Make sure to use the local (overrides) artifacts property, since that has the
// REST API endpoint (which is pretty important).
rval.artifacts = overrides.artifacts;
return rval;
return overrideObject(base, overrides);
}

let registryConfig: ApicurioRegistryConfig = getRegistryConfig();
Expand Down Expand Up @@ -196,10 +224,22 @@ export class ConfigServiceImpl implements ConfigService {

public fetchAndMergeConfigs(): Promise<void> {
const endpoint: string = createEndpoint(this.artifactsUrl(), "/system/uiConfig");

const localConfig: ApicurioRegistryConfig = registryConfig;

console.info("[Config] Fetching UI configuration from: ", endpoint);
return httpGet<ApicurioRegistryConfig>(endpoint).then(config => {
console.info("[Config] UI configuration fetched successfully: ", config);
registryConfig = overrideConfig(config, registryConfig);
return httpGet<ApicurioRegistryConfig>(endpoint).then(remoteConfig => {
console.info("[Config] UI configuration fetched successfully: ", remoteConfig);
// Always use the local config's "artifacts" property (contains the REST API endpoint)
remoteConfig.artifacts = localConfig.artifacts;
// Override the remote config with anything in the local config. Then set the result
// as the new official app config.
registryConfig = overrideConfig(remoteConfig, localConfig);
// Check for extra/unknown local config and warn about it.
const diff: any = difference(remoteConfig, localConfig);
if (Object.keys(diff).length > 0) {
console.warn("[Config] Local config contains unexpected properties: ", diff);
}
}).catch(error => {
console.error("[Config] Error fetching UI configuration: ", error);
console.error("------------------------------------------");
Expand Down

0 comments on commit cd35ddd

Please sign in to comment.