Skip to content

Commit 03d10c2

Browse files
committed
feat(provide-origin-from-env): introduce origin provider function for ssr
1 parent 453b6e1 commit 03d10c2

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { PLATFORM_ID } from '@angular/core'
2+
import { provideOriginFromEnv } from './provide-origin-from-env.function'
3+
import { ORIGIN } from './origin.token'
4+
import { TestBed } from '@angular/core/testing'
5+
6+
describe('provideOriginFromEnv', () => {
7+
const ENV_KEY = 'TEST_ORIGIN'
8+
9+
let originalEnv: NodeJS.ProcessEnv
10+
11+
beforeEach(() => {
12+
originalEnv = { ...process.env }
13+
})
14+
15+
afterEach(() => {
16+
process.env = originalEnv
17+
})
18+
19+
function setup(platformId: 'browser' | 'server') {
20+
TestBed.configureTestingModule({
21+
providers: [{ provide: PLATFORM_ID, useValue: platformId }, provideOriginFromEnv(ENV_KEY)],
22+
})
23+
}
24+
25+
it('should provide the ORIGIN token with a valid origin from env', () => {
26+
setup('server')
27+
28+
const validOrigin = 'https://example.valid.com'
29+
30+
process.env[ENV_KEY] = validOrigin
31+
32+
expect(TestBed.inject(ORIGIN)).toBe(validOrigin)
33+
})
34+
35+
it('should throw if not running on the server', () => {
36+
setup('browser')
37+
38+
process.env[ENV_KEY] = 'https://example.valid.com'
39+
40+
expect(() => TestBed.inject(ORIGIN)).toThrowError(`provideOriginFromEnv can only be used on the server`)
41+
})
42+
43+
it('should throw if env var is not defined', () => {
44+
setup('server')
45+
delete process.env[ENV_KEY]
46+
expect(() => TestBed.inject(ORIGIN)).toThrowError(`Env var ${ENV_KEY} needs to be defined`)
47+
})
48+
49+
it('should throw if env var is not a valid origin', () => {
50+
setup('server')
51+
process.env[ENV_KEY] = 'invalid-origin'
52+
expect(() => TestBed.inject(ORIGIN)).toThrowError(`Env var ${ENV_KEY} is not a valid origin: invalid-origin`)
53+
})
54+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { inject, PLATFORM_ID, Provider } from '@angular/core'
2+
import { ORIGIN } from './origin.token'
3+
import { isPlatformServer } from '@angular/common'
4+
5+
declare const process: { env: Record<string, undefined | string> }
6+
7+
/**
8+
* Provides the {@link ORIGIN} token from the environment variable.
9+
*/
10+
export function provideOriginFromEnv(key: string): Provider[] {
11+
return [
12+
{
13+
provide: ORIGIN,
14+
useFactory: () => {
15+
if (!isPlatformServer(inject(PLATFORM_ID))) {
16+
throw new Error(`${provideOriginFromEnv.name} can only be used on the server`)
17+
}
18+
19+
const value = process.env[key]?.trim()
20+
if (!value) {
21+
throw new Error(`Env var ${key} needs to be defined`)
22+
}
23+
try {
24+
new URL(value)
25+
return value
26+
} catch (error) {
27+
throw new Error(`Env var ${key} is not a valid origin: ${value}`, { cause: error })
28+
}
29+
},
30+
},
31+
]
32+
}

libs/core/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export * from './lib/logger/remote/with-remote-transport.function'
4848
// origin
4949
export * from './lib/origin/origin.token'
5050
export * from './lib/origin/ensure-origin.interceptor'
51+
export * from './lib/origin/provide-origin-from-env.function'
5152

5253
// resize
5354
export * from './lib/resize/resize.service'

0 commit comments

Comments
 (0)