Skip to content

Commit c0bd9fd

Browse files
fix: add universal environment module
1 parent 6653ebe commit c0bd9fd

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

src/modules/environment/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { InjectionToken } from '@angular/core'
2+
import { makeStateKey } from '@angular/platform-browser'
3+
4+
export const ENV_CONFIG = new InjectionToken<any>('cfg.env')
5+
export const ENV_CONFIG_TS_KEY = makeStateKey<any>('cfg.env.ts')
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NgModule } from '@angular/core'
2+
import { EnvironmentService } from './environment.service'
3+
import { ENV_CONFIG, ENV_CONFIG_TS_KEY } from './common'
4+
import { TransferState } from '@angular/platform-browser'
5+
6+
export function fuseBoxConfigFactory(ts: TransferState) {
7+
return ts.get(ENV_CONFIG_TS_KEY, {})
8+
}
9+
10+
// tslint:disable-next-line:no-class
11+
@NgModule({
12+
providers: [
13+
EnvironmentService,
14+
{
15+
provide: ENV_CONFIG,
16+
useFactory: fuseBoxConfigFactory,
17+
deps: [TransferState]
18+
}
19+
]
20+
})
21+
export class EnvironmentBrowserModule {}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { NgModule, APP_BOOTSTRAP_LISTENER, ApplicationRef } from '@angular/core'
2+
import { EnvironmentService, IBaseConfig } from './environment.service'
3+
import { ENV_CONFIG, ENV_CONFIG_TS_KEY } from './common'
4+
import { TransferState } from '@angular/platform-browser'
5+
import { filter, first, take } from 'rxjs/operators'
6+
import { REQUEST } from '@nguniversal/express-engine/tokens'
7+
8+
function getConfigFromProcess() {
9+
return JSON.parse(process.env.FUSING_ANGULAR || '{}')
10+
}
11+
12+
export function serverEnvConfigFactory() {
13+
return getConfigFromProcess()
14+
}
15+
16+
// IF ENV CONTAINS SERVER_, remove from object
17+
function removeServerSpecific(
18+
obj: { readonly [key: string]: string },
19+
filterKey = 'SERVER_'
20+
) {
21+
return Object.keys(obj)
22+
.filter(key => !key.includes(filterKey))
23+
.reduce((acc, curr) => {
24+
return {
25+
...acc,
26+
[curr]: obj[curr]
27+
}
28+
}, {})
29+
}
30+
31+
export function onBootstrap(
32+
appRef: ApplicationRef,
33+
transferState: TransferState,
34+
req: any
35+
) {
36+
return () => {
37+
appRef.isStable
38+
.pipe(
39+
filter(Boolean),
40+
first(),
41+
take(1)
42+
)
43+
.subscribe(() => {
44+
transferState.set<IBaseConfig>(
45+
ENV_CONFIG_TS_KEY,
46+
removeServerSpecific(getConfigFromProcess())
47+
)
48+
})
49+
}
50+
}
51+
52+
// tslint:disable-next-line:no-class
53+
@NgModule({
54+
providers: [
55+
EnvironmentService,
56+
{ provide: ENV_CONFIG, useFactory: serverEnvConfigFactory },
57+
{
58+
provide: APP_BOOTSTRAP_LISTENER,
59+
useFactory: onBootstrap,
60+
deps: [ApplicationRef, TransferState, REQUEST],
61+
multi: true
62+
}
63+
]
64+
})
65+
export class EnvironmentServerModule {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Inject, Injectable } from '@angular/core'
2+
import { ENV_CONFIG } from './common'
3+
4+
export interface IBaseConfig {
5+
readonly [key: string]: string | undefined
6+
readonly env?: string
7+
readonly port?: string
8+
}
9+
10+
export interface IEnvironmentService<TConfig extends IBaseConfig> {
11+
readonly config: TConfig
12+
}
13+
14+
// tslint:disable-next-line:no-class
15+
@Injectable()
16+
export class EnvironmentService<TConfig extends IBaseConfig = IBaseConfig>
17+
implements IEnvironmentService<TConfig> {
18+
constructor(@Inject(ENV_CONFIG) public config: TConfig = {} as TConfig) {}
19+
}

src/modules/environment/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { ENV_CONFIG_TS_KEY, ENV_CONFIG } from './common'
2+
export { EnvironmentService } from './environment.service'
3+
export { EnvironmentBrowserModule } from './environment.browser.module'
4+
export { EnvironmentServerModule } from './environment.server.module'

0 commit comments

Comments
 (0)