@defra/lis-infra-ui-services 0.46.0 → 0.47.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/README.md CHANGED
@@ -101,18 +101,30 @@ It serves `/favicon.ico` and the files under each asset path. `/status-codes` ex
101
101
 
102
102
  - `positiveInteger` (`'positive-integer'`): a whole number of at least 1, for timeouts, TTLs and circuit breaker thresholds, where 0 is a misconfiguration rather than "off". Values from environment variables are coerced from strings.
103
103
  - `duration({ max })` (`'duration'`, or `'duration-max-P7D'` when `max` is given): a length of time read as milliseconds. An environment variable may give an ISO 8601 duration of days, hours, minutes and seconds (`PT4H`, `P7D`, `P1DT12H`) or a whole number of milliseconds; both are coerced to milliseconds (the schema `default` must be a number). `max` is itself an ISO 8601 duration and rejects anything longer, for `SESSION_CACHE_TTL` where CDP requires sessions to expire within 7 days.
104
+ - `cdpEnvironment` (`'cdp-environment'`): one of the CDP environments the platform injects as `ENVIRONMENT` (`infra-dev`, `management`, `dev`, `test`, `ext-test`, `perf-test`, `prod`), or `local` or `docker_compose` outside CDP. One list for every service, so a new environment is added here rather than in each repository's own enum; `ENVIRONMENTS` exports the list.
104
105
  - `secret` (`'secret'`): a signing or encryption secret of at least 32 characters with no default, for `HUB_JWT_SECRET` and `SESSION_COOKIE_PASSWORD`. The setting must also be marked `sensitive: true`, or convict appends the value to the validation error.
105
106
 
106
107
  ```js
107
108
  import convict from 'convict'
108
- import { duration, positiveInteger, secret } from '@defra/lis-infra-ui-services/config-formats'
109
+ import {
110
+ cdpEnvironment,
111
+ duration,
112
+ positiveInteger,
113
+ secret
114
+ } from '@defra/lis-infra-ui-services/config-formats'
109
115
  import { milliseconds } from '@defra/lis-infra-ui-services/duration'
110
116
 
111
117
  const sessionTtl = duration({ max: 'P7D' })
118
+ convict.addFormat(cdpEnvironment)
112
119
  convict.addFormat(positiveInteger)
113
120
  convict.addFormat(sessionTtl)
114
121
  convict.addFormat(secret)
115
122
 
123
+ environment: {
124
+ format: 'cdp-environment',
125
+ default: 'local',
126
+ env: 'ENVIRONMENT'
127
+ },
116
128
  sessionCacheTtl: {
117
129
  format: sessionTtl.name,
118
130
  default: milliseconds.fourHours,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-infra-ui-services",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "UI-Services shared definitions for the livestock taxonomy topology.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -57,6 +57,36 @@ export const secret = {
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * The environments a LIS service can run in: the CDP environments, which
62
+ * the platform injects as `ENVIRONMENT`, plus `local` and `docker_compose`
63
+ * for running outside CDP. Register it with
64
+ * `convict.addFormat(cdpEnvironment)` and use `format: 'cdp-environment'`.
65
+ */
66
+ export const ENVIRONMENTS = Object.freeze([
67
+ 'local',
68
+ 'docker_compose',
69
+ 'infra-dev',
70
+ 'management',
71
+ 'dev',
72
+ 'test',
73
+ 'ext-test',
74
+ 'perf-test',
75
+ 'prod'
76
+ ])
77
+
78
+ export const cdpEnvironment = {
79
+ name: 'cdp-environment',
80
+ /**
81
+ * @param {unknown} value
82
+ */
83
+ validate(value) {
84
+ if (!ENVIRONMENTS.includes(value)) {
85
+ throw new Error(`must be one of ${ENVIRONMENTS.join(', ')}`)
86
+ }
87
+ }
88
+ }
89
+
60
90
  const ISO_DURATION_DATE = /^P(?:(\d+)D)?(?:T(.+))?$/
61
91
  const ISO_DURATION_TIME = /^(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?$/
62
92
  const MILLIS_IN_SECOND = 1000
@@ -2,12 +2,15 @@ import { describe, expect, test } from 'vitest'
2
2
  import convict from 'convict'
3
3
 
4
4
  import {
5
+ cdpEnvironment,
5
6
  duration,
7
+ ENVIRONMENTS,
6
8
  parseIsoDuration,
7
9
  positiveInteger,
8
10
  secret
9
11
  } from './config-formats.js'
10
12
 
13
+ convict.addFormat(cdpEnvironment)
11
14
  convict.addFormat(positiveInteger)
12
15
  convict.addFormat(secret)
13
16
 
@@ -310,3 +313,47 @@ describe('duration', () => {
310
313
  }
311
314
  )
312
315
  })
316
+
317
+ describe('cdpEnvironment', () => {
318
+ function createEnvironmentConfig(value) {
319
+ return convict({
320
+ environment: {
321
+ format: 'cdp-environment',
322
+ default: 'local'
323
+ }
324
+ }).load({ environment: value })
325
+ }
326
+
327
+ test('is registered as the cdp-environment format', () => {
328
+ // Assert
329
+ expect(cdpEnvironment.name).toBe('cdp-environment')
330
+ })
331
+
332
+ test.each(ENVIRONMENTS)('accepts %s', (value) => {
333
+ // Act
334
+ let error
335
+ try {
336
+ createEnvironmentConfig(value).validate({ allowed: 'strict' })
337
+ } catch (e) {
338
+ error = e
339
+ }
340
+
341
+ // Assert
342
+ expect(error).toBeUndefined()
343
+ })
344
+
345
+ test.each(['staging', 'Dev', '', null])('rejects %j', (value) => {
346
+ // Act
347
+ let error
348
+ try {
349
+ createEnvironmentConfig(value).validate({ allowed: 'strict' })
350
+ } catch (e) {
351
+ error = e
352
+ }
353
+
354
+ // Assert
355
+ expect(error.message).toContain(
356
+ 'environment: must be one of local, docker_compose, infra-dev, management, dev, test, ext-test, perf-test, prod'
357
+ )
358
+ })
359
+ })