@defra/lis-hubs-infra-core 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-core",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Shared hub mechanics for livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -18,32 +18,20 @@ export function createProxyPlugin({ hubId, environment }) {
18
18
  await server.register(h2o2)
19
19
 
20
20
  for (const { id: moduleName, path, port } of modules) {
21
- let host, protocol
21
+ let baseUri = ''
22
22
 
23
- switch (environment) {
24
- case 'local':
25
- host = 'localhost'
26
- protocol = 'http'
27
- break
28
- case 'docker_compose':
29
- host = moduleName
30
- protocol = 'http'
31
- break
32
- case 'dev':
33
- case 'test':
34
- case 'perf-test':
35
- case 'prod':
36
- host = `lis-apps-${moduleName}.${environment}.cdp-int.defra.cloud`
37
- protocol = 'https'
38
- break
39
- default:
40
- throw new Error(`Unhandled environment: ${environment}`)
23
+ if (isLocal(environment)) {
24
+ baseUri = `http://localhost:${port}`
25
+ }
26
+ if (isCompose(environment)) {
27
+ baseUri = `http://${moduleName}:${port}`
28
+ }
29
+ if (isCdp(environment)) {
30
+ baseUri = `https://lis-apps-${moduleName}.${environment}.cdp-int.defra.cloud`
41
31
  }
42
32
 
43
- let baseUri = `${protocol}://${host}`
44
-
45
- if (environment === 'local' || environment === 'docker_compose') {
46
- baseUri = `${baseUri}:${port}`
33
+ if (!isSupportedEnv(environment) || baseUri === '') {
34
+ throw new Error(`Unsupported environment: ${environment}`)
47
35
  }
48
36
 
49
37
  server.route({
@@ -75,3 +63,29 @@ export function createProxyPlugin({ hubId, environment }) {
75
63
  }
76
64
  }
77
65
  }
66
+
67
+ function isSupportedEnv(env) {
68
+ const envs = [
69
+ 'local',
70
+ 'docker_compose',
71
+ 'dev',
72
+ 'test',
73
+ 'ext-test',
74
+ 'perf-test',
75
+ 'prod'
76
+ ]
77
+ return envs.includes(env)
78
+ }
79
+
80
+ function isLocal(env) {
81
+ return env === 'local'
82
+ }
83
+
84
+ function isCompose(env) {
85
+ return env === 'docker_compose'
86
+ }
87
+
88
+ function isCdp(env) {
89
+ const envs = ['dev', 'test', 'ext-test', 'perf-test', 'prod']
90
+ return envs.includes(env)
91
+ }
@@ -75,7 +75,7 @@ describe('createProxyPlugin()', () => {
75
75
 
76
76
  // Assert
77
77
  expect(error).toBeDefined()
78
- expect(error.message).toBe('Unhandled environment: unknown')
78
+ expect(error.message).toBe('Unsupported environment: unknown')
79
79
  })
80
80
 
81
81
  test('sorts modules with the longest path first so a shorter prefix does not swallow a longer one', async () => {