Skip to content

Commit

Permalink
feat(core): add mergeApplicationConfig method (#49253)
Browse files Browse the repository at this point in the history
This commits add a utility method to merge multiple `ApplicationConfiguration` into one from left to right.  This is useful for server rendering were an application might have several configurations.

Usage Example:
```ts
const config = mergeApplicationConfig(appConfig, appServerConfig);
```

PR Close #49253
  • Loading branch information
alan-agius4 authored and AndrewKushnir committed Mar 1, 2023
1 parent 3aa85a8 commit 4e9531f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions goldens/public-api/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,9 @@ export const LOCALE_ID: InjectionToken<string>;
// @public
export function makeEnvironmentProviders(providers: (Provider | EnvironmentProviders)[]): EnvironmentProviders;

// @public
export function mergeApplicationConfig(...configs: ApplicationConfig[]): ApplicationConfig;

// @public
export enum MissingTranslationStrategy {
// (undocumented)
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/application_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ export interface ApplicationConfig {
*/
providers: Array<Provider|EnvironmentProviders>;
}

/**
* Merge multiple application configurations from left to right.
*
* @param configs Two or more configurations to be merged.
* @returns A merged [ApplicationConfig](api/core/ApplicationConfig).
*
* @publicApi
*/
export function mergeApplicationConfig(...configs: ApplicationConfig[]): ApplicationConfig {
return configs.reduce((prev, curr) => {
return Object.assign(prev, curr, {providers: [...prev.providers, ...curr.providers]});
}, {providers: []});
}
2 changes: 1 addition & 1 deletion packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export {Sanitizer} from './sanitization/sanitizer';
export {createNgModule, createNgModuleRef, createEnvironmentInjector} from './render3/ng_module_ref';
export {createComponent, reflectComponentType, ComponentMirror} from './render3/component';
export {isStandalone} from './render3/definition';
export {ApplicationConfig} from './application_config';
export {ApplicationConfig, mergeApplicationConfig} from './application_config';

import {global} from './util/global';
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/application_config_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ApplicationConfig, InjectionToken, mergeApplicationConfig} from '@angular/core';

describe('ApplicationConfig', () => {
describe('mergeApplicationConfig', () => {
const FOO_TOKEN = new InjectionToken('foo');
const BAR_TOKEN = new InjectionToken('bar');
const BUZ_TOKEN = new InjectionToken('buz');

const BASE_CONFIG: ApplicationConfig = {
providers: [{provide: FOO_TOKEN, useValue: 'foo'}],
};

const APP_CONFIG_EXPECT: ApplicationConfig = {
providers: [
{provide: FOO_TOKEN, useValue: 'foo'},
{provide: BAR_TOKEN, useValue: 'bar'},
{provide: BUZ_TOKEN, useValue: 'buz'},
]
};

it('should merge 2 configs from left to right', () => {
const extraConfig: ApplicationConfig = {
providers: [
{provide: BAR_TOKEN, useValue: 'bar'},
{provide: BUZ_TOKEN, useValue: 'buz'},
],
};

const config = mergeApplicationConfig(BASE_CONFIG, extraConfig);
expect(config).toEqual(APP_CONFIG_EXPECT);
});

it('should merge more than 2 configs from left to right', () => {
const extraConfigOne: ApplicationConfig = {
providers: [
{provide: BAR_TOKEN, useValue: 'bar'},
],
};
const extraConfigTwo: ApplicationConfig = {
providers: [
{provide: BUZ_TOKEN, useValue: 'buz'},
],
};

const config = mergeApplicationConfig(BASE_CONFIG, extraConfigOne, extraConfigTwo);
expect(config).toEqual(APP_CONFIG_EXPECT);
});
});
});

0 comments on commit 4e9531f

Please sign in to comment.