Skip to content

Commit 4e9531f

Browse files
alan-agius4AndrewKushnir
authored andcommittedMar 1, 2023
feat(core): add mergeApplicationConfig method (#49253)
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
1 parent 3aa85a8 commit 4e9531f

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed
 

‎goldens/public-api/core/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ export const LOCALE_ID: InjectionToken<string>;
912912
// @public
913913
export function makeEnvironmentProviders(providers: (Provider | EnvironmentProviders)[]): EnvironmentProviders;
914914

915+
// @public
916+
export function mergeApplicationConfig(...configs: ApplicationConfig[]): ApplicationConfig;
917+
915918
// @public
916919
export enum MissingTranslationStrategy {
917920
// (undocumented)

‎packages/core/src/application_config.ts

+14
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ export interface ApplicationConfig {
2020
*/
2121
providers: Array<Provider|EnvironmentProviders>;
2222
}
23+
24+
/**
25+
* Merge multiple application configurations from left to right.
26+
*
27+
* @param configs Two or more configurations to be merged.
28+
* @returns A merged [ApplicationConfig](api/core/ApplicationConfig).
29+
*
30+
* @publicApi
31+
*/
32+
export function mergeApplicationConfig(...configs: ApplicationConfig[]): ApplicationConfig {
33+
return configs.reduce((prev, curr) => {
34+
return Object.assign(prev, curr, {providers: [...prev.providers, ...curr.providers]});
35+
}, {providers: []});
36+
}

‎packages/core/src/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export {Sanitizer} from './sanitization/sanitizer';
4040
export {createNgModule, createNgModuleRef, createEnvironmentInjector} from './render3/ng_module_ref';
4141
export {createComponent, reflectComponentType, ComponentMirror} from './render3/component';
4242
export {isStandalone} from './render3/definition';
43-
export {ApplicationConfig} from './application_config';
43+
export {ApplicationConfig, mergeApplicationConfig} from './application_config';
4444

4545
import {global} from './util/global';
4646
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {ApplicationConfig, InjectionToken, mergeApplicationConfig} from '@angular/core';
9+
10+
describe('ApplicationConfig', () => {
11+
describe('mergeApplicationConfig', () => {
12+
const FOO_TOKEN = new InjectionToken('foo');
13+
const BAR_TOKEN = new InjectionToken('bar');
14+
const BUZ_TOKEN = new InjectionToken('buz');
15+
16+
const BASE_CONFIG: ApplicationConfig = {
17+
providers: [{provide: FOO_TOKEN, useValue: 'foo'}],
18+
};
19+
20+
const APP_CONFIG_EXPECT: ApplicationConfig = {
21+
providers: [
22+
{provide: FOO_TOKEN, useValue: 'foo'},
23+
{provide: BAR_TOKEN, useValue: 'bar'},
24+
{provide: BUZ_TOKEN, useValue: 'buz'},
25+
]
26+
};
27+
28+
it('should merge 2 configs from left to right', () => {
29+
const extraConfig: ApplicationConfig = {
30+
providers: [
31+
{provide: BAR_TOKEN, useValue: 'bar'},
32+
{provide: BUZ_TOKEN, useValue: 'buz'},
33+
],
34+
};
35+
36+
const config = mergeApplicationConfig(BASE_CONFIG, extraConfig);
37+
expect(config).toEqual(APP_CONFIG_EXPECT);
38+
});
39+
40+
it('should merge more than 2 configs from left to right', () => {
41+
const extraConfigOne: ApplicationConfig = {
42+
providers: [
43+
{provide: BAR_TOKEN, useValue: 'bar'},
44+
],
45+
};
46+
const extraConfigTwo: ApplicationConfig = {
47+
providers: [
48+
{provide: BUZ_TOKEN, useValue: 'buz'},
49+
],
50+
};
51+
52+
const config = mergeApplicationConfig(BASE_CONFIG, extraConfigOne, extraConfigTwo);
53+
expect(config).toEqual(APP_CONFIG_EXPECT);
54+
});
55+
});
56+
});

0 commit comments

Comments
 (0)
Please sign in to comment.