@defra/lis-infra-ui-services 0.33.0 → 0.34.0-lreg-518.alpha.2
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
|
@@ -1,10 +1,175 @@
|
|
|
1
|
-
#
|
|
1
|
+
# lis-infra-ui-services
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Part of the Livestock Information Service (LIS), which lets livestock keepers, owners, agents and
|
|
4
|
+
delegates record and manage their animals.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
`@defra/lis-infra-ui-services` is a shared npm package with the UI building blocks that LIS hubs and
|
|
7
|
+
spokes share: the page layout, shared error pages, scheduled maintenance, Nunjucks setup, GOV.UK
|
|
8
|
+
Frontend scripts and styles, static files, security headers and session cache.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
The templates follow the [GOV.UK Design System](https://design-system.service.gov.uk/).
|
|
11
|
+
|
|
12
|
+
Used by:
|
|
13
|
+
|
|
14
|
+
- hubs: [lis-hubs-front-office](https://github.com/DEFRA/lis-hubs-front-office), [lis-hubs-back-office](https://github.com/DEFRA/lis-hubs-back-office)
|
|
15
|
+
- spokes: [lis-apps-cattle-home](https://github.com/DEFRA/lis-apps-cattle-home), [lis-apps-cattle-register](https://github.com/DEFRA/lis-apps-cattle-register), [lis-apps-cattle-move](https://github.com/DEFRA/lis-apps-cattle-move), [lis-apps-cattle-death](https://github.com/DEFRA/lis-apps-cattle-death), [lis-apps-sheep-home](https://github.com/DEFRA/lis-apps-sheep-home)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @defra/lis-infra-ui-services \
|
|
21
|
+
@defra/lis-hubs-infra-access @defra/lis-hubs-infra-core @defra/lis-hubs-infra-registry govuk-frontend
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
These are peer dependencies, so the consuming app must install them too:
|
|
25
|
+
|
|
26
|
+
- `@defra/lis-hubs-infra-access` `>=0.29.0 <1.0.0`
|
|
27
|
+
- `@defra/lis-hubs-infra-core` `>=0.4.0 <1.0.0`
|
|
28
|
+
- `@defra/lis-hubs-infra-registry` `>=0.6.0 <1.0.0`
|
|
29
|
+
- `govuk-frontend` `>=6.5.0 <7.0.0`
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Import from the subpath you need, for example `@defra/lis-infra-ui-services/errors`. The package
|
|
34
|
+
has no config of its own. Each function takes the values it needs from the consuming app's
|
|
35
|
+
[convict](https://github.com/mozilla/node-convict) `config`.
|
|
36
|
+
|
|
37
|
+
### Nunjucks
|
|
38
|
+
|
|
39
|
+
`createNunjucksConfig` returns a registration for `@hapi/vision`. It sets up Nunjucks with the
|
|
40
|
+
GOV.UK Frontend templates, this package's layout (`layouts/base.njk`) and components, and the shared
|
|
41
|
+
error pages. It also adds the filters and globals, and a view context with `assetPath`,
|
|
42
|
+
`serviceName`, `getAssetPath`, `isSignedIn`, `hasPermission` and `errorPages`.
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { createNunjucksConfig } from '@defra/lis-infra-ui-services/nunjucks/plugin'
|
|
46
|
+
|
|
47
|
+
const nunjucksConfig = createNunjucksConfig({
|
|
48
|
+
config, // reads root, assetPath, serviceName, isProduction, nunjucks.watch, nunjucks.noCache
|
|
49
|
+
logger,
|
|
50
|
+
getRequestBasePath,
|
|
51
|
+
moduleId: 'cattle-home' // spokes only
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
await server.register(nunjucksConfig)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The template components are `appHeading`, `appAutocomplete`, `lisSubNavigation`, plus the search form,
|
|
58
|
+
sortable paginated table and date templates. `createNunjucksContextBuilder` (also exported from
|
|
59
|
+
`/nunjucks-context`) builds just the view context. The filters `formatAge`, `formatDate`,
|
|
60
|
+
`formatEarTag`, `formatCurrency` and `queryString` are also exported from `/nunjucks/filters`, and the
|
|
61
|
+
individual `/nunjucks/format-currency`, `/nunjucks/format-date` and `/nunjucks/query-string`
|
|
62
|
+
subpaths. `/nunjucks/globals` exports `PERMISSIONS` and `govukRebrand` (always `true`, which turns
|
|
63
|
+
on the GOV.UK rebrand in templates).
|
|
64
|
+
|
|
65
|
+
### Base path
|
|
66
|
+
|
|
67
|
+
Hubs serve spokes under a path prefix. `createBasePathHelpersForConfig` gives the helpers
|
|
68
|
+
`getRequestBasePath`, `buildAppPath`, `isPrefixedRequest`, `getRouteVariants` and `getAssetPaths`. The
|
|
69
|
+
prefix comes from the module in `@defra/lis-hubs-infra-registry`.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { createBasePathHelpersForConfig } from '@defra/lis-infra-ui-services/base-path'
|
|
73
|
+
|
|
74
|
+
const { getRequestBasePath, getAssetPaths } = createBasePathHelpersForConfig({
|
|
75
|
+
moduleId: 'cattle-home', // leave out in a hub
|
|
76
|
+
assetPath: config.get('assetPath')
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The standalone functions of the same names are exported too.
|
|
81
|
+
|
|
82
|
+
### Static files
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
import { createStaticFilesPlugin } from '@defra/lis-infra-ui-services/static-files'
|
|
86
|
+
import { statusCodes } from '@defra/lis-infra-ui-services/status-codes'
|
|
87
|
+
|
|
88
|
+
const serveStaticFiles = createStaticFilesPlugin({
|
|
89
|
+
assetPaths: getAssetPaths(),
|
|
90
|
+
staticCacheTimeout: config.get('staticCacheTimeout'),
|
|
91
|
+
noContentStatusCode: statusCodes.noContent
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
It serves `/favicon.ico` and the files under each asset path. `/status-codes` exports `statusCodes`.
|
|
96
|
+
`/duration` exports the `milliseconds` and `seconds` lookups and `unixEpoch()`.
|
|
97
|
+
|
|
98
|
+
### Security headers
|
|
99
|
+
|
|
100
|
+
`securityHeaders` is a hapi plugin that adds HSTS, `x-content-type-options`, `referrer-policy` and
|
|
101
|
+
`permissions-policy` headers to every response, including error responses. Register it after
|
|
102
|
+
`catchAll`, so its `onPreResponse` runs last.
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
import { securityHeaders } from '@defra/lis-infra-ui-services/security-headers'
|
|
106
|
+
|
|
107
|
+
await server.register(securityHeaders)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Session cache
|
|
111
|
+
|
|
112
|
+
`getCacheEngine` picks Redis or in-memory Catbox. `createSessionCachePluginForConfig` registers
|
|
113
|
+
[yar](https://hapi.dev/module/yar/) from the `session` config (`cache.name`, `cache.ttl`,
|
|
114
|
+
`cookie.password`, `cookie.ttl`). Session cookies are always `Secure`.
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import { getCacheEngine } from '@defra/lis-infra-ui-services/session-cache/cache-engine'
|
|
118
|
+
import { createSessionCachePluginForConfig } from '@defra/lis-infra-ui-services/session-cache'
|
|
119
|
+
|
|
120
|
+
const server = hapi.server({
|
|
121
|
+
cache: [
|
|
122
|
+
{
|
|
123
|
+
name: config.get('session.cache.name'),
|
|
124
|
+
engine: getCacheEngine({
|
|
125
|
+
engine: config.get('session.cache.engine'), // 'redis' or anything else for memory
|
|
126
|
+
config, // reads redis when the engine is 'redis'
|
|
127
|
+
logger
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
await server.register(createSessionCachePluginForConfig(config))
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`/redis-client` exports `buildRedisClient`, which `getCacheEngine` uses. It takes the `redis` config
|
|
137
|
+
(`host`, `port`, `keyPrefix`, `username`, `password`, `useTLS`, `useSingleInstanceCache`).
|
|
138
|
+
|
|
139
|
+
### Outbound proxy
|
|
140
|
+
|
|
141
|
+
`setupProxy` sends `fetch` and other HTTP clients through the proxy when `proxyUrl` is set. It does
|
|
142
|
+
nothing when it is empty.
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { setupProxy } from '@defra/lis-infra-ui-services/proxy/setup-proxy'
|
|
146
|
+
|
|
147
|
+
setupProxy({ proxyUrl: config.get('httpProxy'), logger })
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Referrer link
|
|
151
|
+
|
|
152
|
+
`createReferrerLink` remembers where a user came from, so a page reached from a list can link back to
|
|
153
|
+
that list with its query. Use `getSameHostReferrerPath` to read the referrer.
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
import { createReferrerLink } from '@defra/lis-infra-ui-services/referrer-link'
|
|
157
|
+
|
|
158
|
+
const referrerLink = createReferrerLink(server, {
|
|
159
|
+
cacheName: 'session',
|
|
160
|
+
allow: (path) => path.startsWith('/cphs'),
|
|
161
|
+
fallbackHref: '/cphs'
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
const back = await referrerLink.resolve(request) // { id, href } or null
|
|
165
|
+
const href = referrerLink.link('/cphs/12/34/5678', back?.id ?? null)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Microsite paths
|
|
169
|
+
|
|
170
|
+
The main entry point (`@defra/lis-infra-ui-services`) exports `buildMicrositePath(taxonomy, species)`, which returns `/<species>/<taxonomy>`.
|
|
171
|
+
|
|
172
|
+
### Error pages
|
|
8
173
|
|
|
9
174
|
`catchAll` from `@defra/lis-infra-ui-services/errors`, registered as an `onPreResponse` extension, renders one of four shared pages from `src/nunjucks/routes/error/` by status code:
|
|
10
175
|
|
|
@@ -15,6 +180,12 @@ Install dependencies for this package locally with `npm install` from this direc
|
|
|
15
180
|
| 503 | `service-unavailable` |
|
|
16
181
|
| anything else | `problem-with-service` |
|
|
17
182
|
|
|
183
|
+
```js
|
|
184
|
+
import { catchAll } from '@defra/lis-infra-ui-services/errors'
|
|
185
|
+
|
|
186
|
+
server.ext('onPreResponse', catchAll)
|
|
187
|
+
```
|
|
188
|
+
|
|
18
189
|
The contact link on these pages comes from the `errorPages` option of `createNunjucksConfig` (or `createNunjucksContextBuilder`), exposed to templates as `errorPages`. The key is optional:
|
|
19
190
|
|
|
20
191
|
```js
|
|
@@ -29,7 +200,7 @@ createNunjucksConfig({
|
|
|
29
200
|
})
|
|
30
201
|
```
|
|
31
202
|
|
|
32
|
-
|
|
203
|
+
### Layout scripts
|
|
33
204
|
|
|
34
205
|
`layoutComponents` from `@defra/lis-infra-ui-services/components/layout` is the list of GOV.UK Frontend components the layout template renders: Service navigation and Skip link.
|
|
35
206
|
|
|
@@ -39,7 +210,7 @@ Start each with `createAll`, alongside any page-level components (see the full e
|
|
|
39
210
|
|
|
40
211
|
Wrap the combined list in a `Set`, so a component that appears in more than one list (or that the app also lists itself) is only initialised once: govuk's base class logs an InitError on double init.
|
|
41
212
|
|
|
42
|
-
|
|
213
|
+
#### Scrollable table
|
|
43
214
|
|
|
44
215
|
Apps that render the sortable paginated table macro also start `ScrollableTable` from `@defra/lis-infra-ui-services/components/scrollable-table`. It drops the tab stop (and the region role and label) from a table wrapper that fits, and restores them when the table overflows, re-checking whenever the wrapper or table changes size. Without JavaScript the wrapper stays focusable.
|
|
45
216
|
|
|
@@ -59,7 +230,27 @@ for (const component of new Set([
|
|
|
59
230
|
}
|
|
60
231
|
```
|
|
61
232
|
|
|
62
|
-
|
|
233
|
+
#### Autocomplete
|
|
234
|
+
|
|
235
|
+
For the `appAutocomplete` template component, start the script with `initAllAutocompletes`. It enhances each input with [accessible-autocomplete](https://github.com/alphagov/accessible-autocomplete).
|
|
236
|
+
|
|
237
|
+
```js
|
|
238
|
+
import { initAllAutocompletes } from '@defra/lis-infra-ui-services/components/autocomplete'
|
|
239
|
+
|
|
240
|
+
initAllAutocompletes()
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Styles
|
|
244
|
+
|
|
245
|
+
Import the Sass files you need from your app's stylesheet: `components/autocomplete.scss`,
|
|
246
|
+
`components/search-form.scss`, `components/service-navigation.scss`,
|
|
247
|
+
`components/sortable-paginated-table.scss` and `components/sub-navigation.scss`.
|
|
248
|
+
|
|
249
|
+
```scss
|
|
250
|
+
@use 'pkg:@defra/lis-infra-ui-services/components/service-navigation.scss';
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Scheduled maintenance
|
|
63
254
|
|
|
64
255
|
Each hub switches maintenance on with its own config value, `maintenance.availableFrom`: the UK local date-time the service is back, in ISO 8601 form without an offset, e.g. `2026-11-19T09:00` or `2026-11-19T09:00:00`. Values with a `Z` or `+hh:mm` offset, and dates without a time, throw at start-up. An empty value leaves the service running as normal.
|
|
65
256
|
|
|
@@ -87,3 +278,91 @@ await server.register(
|
|
|
87
278
|
```
|
|
88
279
|
|
|
89
280
|
While `availableFrom` is set, every request except the health check and assets under `assetPath` gets the service unavailable page with a 503, showing the time as "9am on Thursday 19 November 2026" and carrying it in a `Retry-After` header.
|
|
281
|
+
|
|
282
|
+
### Environment variables
|
|
283
|
+
|
|
284
|
+
The package reads no environment variables itself. It takes all its settings from the `config` object the consuming app passes in. In the example above, `MAINTENANCE_AVAILABLE_FROM` is a variable the hub defines in its own config.
|
|
285
|
+
|
|
286
|
+
## Prerequisites
|
|
287
|
+
|
|
288
|
+
- Node.js 24 (see `.nvmrc`, use `nvm use`)
|
|
289
|
+
- To check changes against a consuming app: a local clone of [lis-dev](https://github.com/DEFRA/lis-dev) with its one-time setup done
|
|
290
|
+
|
|
291
|
+
## Setup
|
|
292
|
+
|
|
293
|
+
### Development
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
nvm use
|
|
297
|
+
npm install
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Test
|
|
301
|
+
|
|
302
|
+
There are no tools to install beyond `npm install`. Tests use [Vitest](https://vitest.dev/).
|
|
303
|
+
|
|
304
|
+
## Running in development
|
|
305
|
+
|
|
306
|
+
This is a library, so it has no server of its own. To try a change in a consuming app without
|
|
307
|
+
publishing, link the local copy into the lis-dev stack:
|
|
308
|
+
|
|
309
|
+
```bash
|
|
310
|
+
cd lis-dev
|
|
311
|
+
.bin/sideload link infra/ui-services # every installed copy now points at this repo
|
|
312
|
+
.bin/sideload up front-office # optional: run the consumer locally with hot reload
|
|
313
|
+
.bin/sideload unlink infra/ui-services # when done
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Running tests
|
|
317
|
+
|
|
318
|
+
```bash
|
|
319
|
+
npm test # unit tests
|
|
320
|
+
npm run test:coverage # with coverage
|
|
321
|
+
npm run lint # ESLint
|
|
322
|
+
npm run format:check # Prettier
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Before releasing a change, also run the tests of each consuming repo against the linked package.
|
|
326
|
+
|
|
327
|
+
## Pipelines
|
|
328
|
+
|
|
329
|
+
Build, versioning and publishing run in GitHub Actions (`.github/workflows`) using Cake scripts (`*.cake`):
|
|
330
|
+
|
|
331
|
+
- **Pull request to `main`**: validate (format, lint, test), Sonar analysis, version check
|
|
332
|
+
- **Push to a `feature/` or `hotfix/` branch**: prerelease version and tag
|
|
333
|
+
- **Merge to `main`**: release tag, then publish to [npm](https://www.npmjs.com/package/@defra/lis-infra-ui-services)
|
|
334
|
+
|
|
335
|
+
Versions come from git tags (SemVer). After a merge, wait for the publish job to finish before bumping the package in consuming repos.
|
|
336
|
+
|
|
337
|
+
## Branching policy
|
|
338
|
+
|
|
339
|
+
The repo uses mainline development. Branch from `main` and name the branch:
|
|
340
|
+
|
|
341
|
+
- `feature/LREG-<ticket>-<short-kebab-description>`
|
|
342
|
+
- `hotfix/LREG-<ticket>-<short-kebab-description>`
|
|
343
|
+
|
|
344
|
+
`LREG` must be uppercase, because the GitHub ruleset rejects other branch names. Rebase onto `main`
|
|
345
|
+
rather than merging `main` in.
|
|
346
|
+
|
|
347
|
+
## Contributing to this project
|
|
348
|
+
|
|
349
|
+
- Follow the [Defra software development standards](https://github.com/DEFRA/software-development-standards).
|
|
350
|
+
- `npm install` sets up a pre-commit hook (Husky) that runs the security audit, format check and lint.
|
|
351
|
+
- Open a pull request against `main` using the PR template. CI must pass and the Sonar quality gate must be green before merge.
|
|
352
|
+
- Feature branches bump the minor version and hotfix branches the patch version. While the package is on 0.x, a breaking change to the public API ships as a minor version, so call it out in the PR so consumers can update.
|
|
353
|
+
|
|
354
|
+
## Licence
|
|
355
|
+
|
|
356
|
+
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
|
|
357
|
+
|
|
358
|
+
<http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>
|
|
359
|
+
|
|
360
|
+
The following attribution statement MUST be cited in your products and applications when using this information.
|
|
361
|
+
|
|
362
|
+
> Contains public sector information licensed under the Open Government licence v3
|
|
363
|
+
|
|
364
|
+
### About the licence
|
|
365
|
+
|
|
366
|
+
The Open Government Licence (OGL) was developed by the Controller of His Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.
|
|
367
|
+
|
|
368
|
+
It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/lis-infra-ui-services",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.34.0-lreg-518.alpha.2",
|
|
4
4
|
"description": "UI-Services shared definitions for the livestock taxonomy topology.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
"./duration": "./src/duration.js",
|
|
23
23
|
"./errors": "./src/errors/index.js",
|
|
24
24
|
"./maintenance": "./src/maintenance.js",
|
|
25
|
-
"./nunjucks/build-navigation": "./src/nunjucks/build-navigation.js",
|
|
26
25
|
"./nunjucks/context": "./src/nunjucks/context.js",
|
|
27
26
|
"./nunjucks/filters": "./src/nunjucks/filters.js",
|
|
28
27
|
"./nunjucks/format-currency": "./src/nunjucks/format-currency.js",
|
package/src/index.js
CHANGED
|
@@ -1,372 +1,3 @@
|
|
|
1
|
-
export const SUPPORTED_TAXONOMIES = [
|
|
2
|
-
{
|
|
3
|
-
id: 'register',
|
|
4
|
-
slug: 'register',
|
|
5
|
-
label: 'Register',
|
|
6
|
-
summary: 'Capture and manage livestock registration activity.'
|
|
7
|
-
},
|
|
8
|
-
{
|
|
9
|
-
id: 'move',
|
|
10
|
-
slug: 'move',
|
|
11
|
-
label: 'Move',
|
|
12
|
-
summary: 'Coordinate movement-related livestock journeys and traceability.'
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
id: 'death',
|
|
16
|
-
slug: 'death',
|
|
17
|
-
label: 'Death',
|
|
18
|
-
summary: 'Capture livestock death notifications and downstream handling.'
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
id: 'status',
|
|
22
|
-
slug: 'status',
|
|
23
|
-
label: 'Status',
|
|
24
|
-
summary: 'Surface status information and lifecycle checks for livestock.'
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
id: 'home',
|
|
28
|
-
slug: 'home',
|
|
29
|
-
label: 'Home',
|
|
30
|
-
summary: 'Provide the default entry experience for each livestock species.'
|
|
31
|
-
}
|
|
32
|
-
]
|
|
33
|
-
|
|
34
|
-
export const SUPPORTED_SPECIES = [
|
|
35
|
-
{
|
|
36
|
-
id: 'ctt',
|
|
37
|
-
slug: 'cattle',
|
|
38
|
-
label: 'Cattle',
|
|
39
|
-
summary: 'Shared behaviour and wording for cattle journeys.'
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
id: 'cml',
|
|
43
|
-
slug: 'camlid',
|
|
44
|
-
label: 'Camlid',
|
|
45
|
-
summary: 'Shared behaviour and wording for camlid journeys.'
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
id: 'chk',
|
|
49
|
-
slug: 'chicken',
|
|
50
|
-
label: 'Chicken',
|
|
51
|
-
summary: 'Shared behaviour and wording for chicken journeys.'
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
id: 'shp',
|
|
55
|
-
slug: 'sheep',
|
|
56
|
-
label: 'Sheep',
|
|
57
|
-
summary: 'Shared behaviour and wording for sheep journeys.'
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
id: 'gt',
|
|
61
|
-
slug: 'goat',
|
|
62
|
-
label: 'Goat',
|
|
63
|
-
summary: 'Shared behaviour and wording for goat journeys.'
|
|
64
|
-
}
|
|
65
|
-
]
|
|
66
|
-
|
|
67
|
-
export const SPOKES = [
|
|
68
|
-
{
|
|
69
|
-
id: 'cattle-register',
|
|
70
|
-
label: 'Register for Cattle',
|
|
71
|
-
taxonomy: {
|
|
72
|
-
id: 'register',
|
|
73
|
-
label: 'Register'
|
|
74
|
-
},
|
|
75
|
-
species: {
|
|
76
|
-
id: 'cattle',
|
|
77
|
-
label: 'Cattle'
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
id: 'camlid-register',
|
|
82
|
-
label: 'Register for Camlid',
|
|
83
|
-
taxonomy: {
|
|
84
|
-
id: 'register',
|
|
85
|
-
label: 'Register'
|
|
86
|
-
},
|
|
87
|
-
species: {
|
|
88
|
-
id: 'camlid',
|
|
89
|
-
label: 'Camlid'
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
id: 'chicken-register',
|
|
94
|
-
label: 'Register for Chicken',
|
|
95
|
-
taxonomy: {
|
|
96
|
-
id: 'register',
|
|
97
|
-
label: 'Register'
|
|
98
|
-
},
|
|
99
|
-
species: {
|
|
100
|
-
id: 'chicken',
|
|
101
|
-
label: 'Chicken'
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
id: 'sheep-register',
|
|
106
|
-
label: 'Register for Sheep',
|
|
107
|
-
taxonomy: {
|
|
108
|
-
id: 'register',
|
|
109
|
-
label: 'Register'
|
|
110
|
-
},
|
|
111
|
-
species: {
|
|
112
|
-
id: 'sheep',
|
|
113
|
-
label: 'Sheep'
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
id: 'goat-register',
|
|
118
|
-
label: 'Register for Goat',
|
|
119
|
-
taxonomy: {
|
|
120
|
-
id: 'register',
|
|
121
|
-
label: 'Register'
|
|
122
|
-
},
|
|
123
|
-
species: {
|
|
124
|
-
id: 'goat',
|
|
125
|
-
label: 'Goat'
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
id: 'cattle-move',
|
|
130
|
-
label: 'Move for Cattle',
|
|
131
|
-
taxonomy: {
|
|
132
|
-
id: 'move',
|
|
133
|
-
label: 'Move'
|
|
134
|
-
},
|
|
135
|
-
species: {
|
|
136
|
-
id: 'cattle',
|
|
137
|
-
label: 'Cattle'
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
id: 'camlid-move',
|
|
142
|
-
label: 'Move for Camlid',
|
|
143
|
-
taxonomy: {
|
|
144
|
-
id: 'move',
|
|
145
|
-
label: 'Move'
|
|
146
|
-
},
|
|
147
|
-
species: {
|
|
148
|
-
id: 'camlid',
|
|
149
|
-
label: 'Camlid'
|
|
150
|
-
}
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
id: 'chicken-move',
|
|
154
|
-
label: 'Move for Chicken',
|
|
155
|
-
taxonomy: {
|
|
156
|
-
id: 'move',
|
|
157
|
-
label: 'Move'
|
|
158
|
-
},
|
|
159
|
-
species: {
|
|
160
|
-
id: 'chicken',
|
|
161
|
-
label: 'Chicken'
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
{
|
|
165
|
-
id: 'sheep-move',
|
|
166
|
-
label: 'Move for Sheep',
|
|
167
|
-
taxonomy: {
|
|
168
|
-
id: 'move',
|
|
169
|
-
label: 'Move'
|
|
170
|
-
},
|
|
171
|
-
species: {
|
|
172
|
-
id: 'sheep',
|
|
173
|
-
label: 'Sheep'
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
id: 'goat-move',
|
|
178
|
-
label: 'Move for Goat',
|
|
179
|
-
taxonomy: {
|
|
180
|
-
id: 'move',
|
|
181
|
-
label: 'Move'
|
|
182
|
-
},
|
|
183
|
-
species: {
|
|
184
|
-
id: 'goat',
|
|
185
|
-
label: 'Goat'
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
{
|
|
189
|
-
id: 'cattle-death',
|
|
190
|
-
label: 'Death for Cattle',
|
|
191
|
-
taxonomy: {
|
|
192
|
-
id: 'death',
|
|
193
|
-
label: 'Death'
|
|
194
|
-
},
|
|
195
|
-
species: {
|
|
196
|
-
id: 'cattle',
|
|
197
|
-
label: 'Cattle'
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
id: 'camlid-death',
|
|
202
|
-
label: 'Death for Camlid',
|
|
203
|
-
taxonomy: {
|
|
204
|
-
id: 'death',
|
|
205
|
-
label: 'Death'
|
|
206
|
-
},
|
|
207
|
-
species: {
|
|
208
|
-
id: 'camlid',
|
|
209
|
-
label: 'Camlid'
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
id: 'chicken-death',
|
|
214
|
-
label: 'Death for Chicken',
|
|
215
|
-
taxonomy: {
|
|
216
|
-
id: 'death',
|
|
217
|
-
label: 'Death'
|
|
218
|
-
},
|
|
219
|
-
species: {
|
|
220
|
-
id: 'chicken',
|
|
221
|
-
label: 'Chicken'
|
|
222
|
-
}
|
|
223
|
-
},
|
|
224
|
-
{
|
|
225
|
-
id: 'sheep-death',
|
|
226
|
-
label: 'Death for Sheep',
|
|
227
|
-
taxonomy: {
|
|
228
|
-
id: 'death',
|
|
229
|
-
label: 'Death'
|
|
230
|
-
},
|
|
231
|
-
species: {
|
|
232
|
-
id: 'sheep',
|
|
233
|
-
label: 'Sheep'
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
{
|
|
237
|
-
id: 'goat-death',
|
|
238
|
-
label: 'Death for Goat',
|
|
239
|
-
taxonomy: {
|
|
240
|
-
id: 'death',
|
|
241
|
-
label: 'Death'
|
|
242
|
-
},
|
|
243
|
-
species: {
|
|
244
|
-
id: 'goat',
|
|
245
|
-
label: 'Goat'
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
{
|
|
249
|
-
id: 'cattle-status',
|
|
250
|
-
label: 'Status for Cattle',
|
|
251
|
-
taxonomy: {
|
|
252
|
-
id: 'status',
|
|
253
|
-
label: 'Status'
|
|
254
|
-
},
|
|
255
|
-
species: {
|
|
256
|
-
id: 'cattle',
|
|
257
|
-
label: 'Cattle'
|
|
258
|
-
}
|
|
259
|
-
},
|
|
260
|
-
{
|
|
261
|
-
id: 'camlid-status',
|
|
262
|
-
label: 'Status for Camlid',
|
|
263
|
-
taxonomy: {
|
|
264
|
-
id: 'status',
|
|
265
|
-
label: 'Status'
|
|
266
|
-
},
|
|
267
|
-
species: {
|
|
268
|
-
id: 'camlid',
|
|
269
|
-
label: 'Camlid'
|
|
270
|
-
}
|
|
271
|
-
},
|
|
272
|
-
{
|
|
273
|
-
id: 'chicken-status',
|
|
274
|
-
label: 'Status for Chicken',
|
|
275
|
-
taxonomy: {
|
|
276
|
-
id: 'status',
|
|
277
|
-
label: 'Status'
|
|
278
|
-
},
|
|
279
|
-
species: {
|
|
280
|
-
id: 'chicken',
|
|
281
|
-
label: 'Chicken'
|
|
282
|
-
}
|
|
283
|
-
},
|
|
284
|
-
{
|
|
285
|
-
id: 'sheep-status',
|
|
286
|
-
label: 'Status for Sheep',
|
|
287
|
-
taxonomy: {
|
|
288
|
-
id: 'status',
|
|
289
|
-
label: 'Status'
|
|
290
|
-
},
|
|
291
|
-
species: {
|
|
292
|
-
id: 'sheep',
|
|
293
|
-
label: 'Sheep'
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
{
|
|
297
|
-
id: 'goat-status',
|
|
298
|
-
label: 'Status for Goat',
|
|
299
|
-
taxonomy: {
|
|
300
|
-
id: 'status',
|
|
301
|
-
label: 'Status'
|
|
302
|
-
},
|
|
303
|
-
species: {
|
|
304
|
-
id: 'goat',
|
|
305
|
-
label: 'Goat'
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
id: 'cattle-home',
|
|
310
|
-
label: 'Home for Cattle',
|
|
311
|
-
taxonomy: {
|
|
312
|
-
id: 'home',
|
|
313
|
-
label: 'Home'
|
|
314
|
-
},
|
|
315
|
-
species: {
|
|
316
|
-
id: 'cattle',
|
|
317
|
-
label: 'Cattle'
|
|
318
|
-
}
|
|
319
|
-
},
|
|
320
|
-
{
|
|
321
|
-
id: 'camlid-home',
|
|
322
|
-
label: 'Home for Camlid',
|
|
323
|
-
taxonomy: {
|
|
324
|
-
id: 'home',
|
|
325
|
-
label: 'Home'
|
|
326
|
-
},
|
|
327
|
-
species: {
|
|
328
|
-
id: 'camlid',
|
|
329
|
-
label: 'Camlid'
|
|
330
|
-
}
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
id: 'chicken-home',
|
|
334
|
-
label: 'Home for Chicken',
|
|
335
|
-
taxonomy: {
|
|
336
|
-
id: 'home',
|
|
337
|
-
label: 'Home'
|
|
338
|
-
},
|
|
339
|
-
species: {
|
|
340
|
-
id: 'chicken',
|
|
341
|
-
label: 'Chicken'
|
|
342
|
-
}
|
|
343
|
-
},
|
|
344
|
-
{
|
|
345
|
-
id: 'sheep-home',
|
|
346
|
-
label: 'Home for Sheep',
|
|
347
|
-
taxonomy: {
|
|
348
|
-
id: 'home',
|
|
349
|
-
label: 'Home'
|
|
350
|
-
},
|
|
351
|
-
species: {
|
|
352
|
-
id: 'sheep',
|
|
353
|
-
label: 'Sheep'
|
|
354
|
-
}
|
|
355
|
-
},
|
|
356
|
-
{
|
|
357
|
-
id: 'goat-home',
|
|
358
|
-
label: 'Home for Goat',
|
|
359
|
-
taxonomy: {
|
|
360
|
-
id: 'home',
|
|
361
|
-
label: 'Home'
|
|
362
|
-
},
|
|
363
|
-
species: {
|
|
364
|
-
id: 'goat',
|
|
365
|
-
label: 'Goat'
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
]
|
|
369
|
-
|
|
370
1
|
/**
|
|
371
2
|
* @param {string} taxonomy
|
|
372
3
|
* @param {string} species
|
|
@@ -375,5 +6,3 @@ export const SPOKES = [
|
|
|
375
6
|
export function buildMicrositePath(taxonomy, species) {
|
|
376
7
|
return `/${species}/${taxonomy}`
|
|
377
8
|
}
|
|
378
|
-
|
|
379
|
-
export { createHoldingService } from './services/holding-service/service.js'
|
|
@@ -27,8 +27,8 @@ Import and initialise the client module from the consuming application's entry
|
|
|
27
27
|
point, and include its stylesheet:
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
|
-
import { initAllAutocompletes } from '@
|
|
31
|
-
import '@
|
|
30
|
+
import { initAllAutocompletes } from '@defra/lis-infra-ui-services/components/autocomplete'
|
|
31
|
+
import '@defra/lis-infra-ui-services/components/autocomplete.scss'
|
|
32
32
|
|
|
33
33
|
initAllAutocompletes()
|
|
34
34
|
```
|
package/src/nunjucks/plugin.js
CHANGED
|
@@ -11,50 +11,6 @@ import * as globals from './globals.js'
|
|
|
11
11
|
|
|
12
12
|
const PACKAGE_JSON_FILENAME = 'package.json'
|
|
13
13
|
|
|
14
|
-
function findDependentPackageName(projectRoot, prefix) {
|
|
15
|
-
const packageJson = JSON.parse(
|
|
16
|
-
readFileSync(path.join(projectRoot, PACKAGE_JSON_FILENAME), 'utf8')
|
|
17
|
-
)
|
|
18
|
-
const dependencyNames = Object.keys({
|
|
19
|
-
...packageJson.dependencies,
|
|
20
|
-
...packageJson.devDependencies,
|
|
21
|
-
...packageJson.optionalDependencies,
|
|
22
|
-
...packageJson.peerDependencies
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
return dependencyNames.find((name) => name.startsWith(prefix)) ?? null
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function resolvePackageRoot(require, packageName) {
|
|
29
|
-
if (!packageName) {
|
|
30
|
-
return null
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let currentPath
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
currentPath = path.dirname(require.resolve(packageName))
|
|
37
|
-
} catch {
|
|
38
|
-
return null
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
while (true) {
|
|
42
|
-
const packageJsonPath = path.join(currentPath, PACKAGE_JSON_FILENAME)
|
|
43
|
-
|
|
44
|
-
if (existsSync(packageJsonPath)) {
|
|
45
|
-
return currentPath
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const parentPath = path.dirname(currentPath)
|
|
49
|
-
|
|
50
|
-
if (parentPath === currentPath) {
|
|
51
|
-
return null
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
currentPath = parentPath
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
14
|
function addIfExists(paths, candidatePath) {
|
|
59
15
|
if (candidatePath && existsSync(candidatePath)) {
|
|
60
16
|
paths.push(candidatePath)
|
|
@@ -111,35 +67,23 @@ function resolveTemplateRoots(projectRoot) {
|
|
|
111
67
|
const packageRequire = createRequire(
|
|
112
68
|
path.join(projectRoot, PACKAGE_JSON_FILENAME)
|
|
113
69
|
)
|
|
114
|
-
const speciesRoot = resolvePackageRoot(
|
|
115
|
-
packageRequire,
|
|
116
|
-
findDependentPackageName(projectRoot, '@livestock/species-')
|
|
117
|
-
)
|
|
118
|
-
const taxonomyRoot = resolvePackageRoot(
|
|
119
|
-
packageRequire,
|
|
120
|
-
findDependentPackageName(projectRoot, '@livestock/taxonomy-')
|
|
121
|
-
)
|
|
122
70
|
const govukFrontendRoot = path.dirname(
|
|
123
71
|
packageRequire.resolve('govuk-frontend/package.json')
|
|
124
72
|
)
|
|
125
73
|
|
|
126
|
-
return {
|
|
74
|
+
return { govukFrontendRoot }
|
|
127
75
|
}
|
|
128
76
|
|
|
129
77
|
function buildNunjucksEnvironment({
|
|
130
78
|
config,
|
|
131
79
|
projectRoot,
|
|
132
80
|
infrastructureRoot,
|
|
133
|
-
speciesRoot,
|
|
134
|
-
taxonomyRoot,
|
|
135
81
|
govukFrontendRoot
|
|
136
82
|
}) {
|
|
137
83
|
return nunjucks.configure(
|
|
138
84
|
[
|
|
139
85
|
path.join(govukFrontendRoot, 'dist'),
|
|
140
86
|
...buildProjectTemplatePaths(projectRoot),
|
|
141
|
-
...buildPackageTemplatePaths(speciesRoot),
|
|
142
|
-
...buildPackageTemplatePaths(taxonomyRoot),
|
|
143
87
|
...buildPackageTemplatePaths(infrastructureRoot)
|
|
144
88
|
],
|
|
145
89
|
{
|
|
@@ -170,14 +114,11 @@ export function createNunjucksConfig({
|
|
|
170
114
|
path.dirname(fileURLToPath(import.meta.url)),
|
|
171
115
|
'../..'
|
|
172
116
|
)
|
|
173
|
-
const {
|
|
174
|
-
resolveTemplateRoots(projectRoot)
|
|
117
|
+
const { govukFrontendRoot } = resolveTemplateRoots(projectRoot)
|
|
175
118
|
const nunjucksEnvironment = buildNunjucksEnvironment({
|
|
176
119
|
config,
|
|
177
120
|
projectRoot,
|
|
178
121
|
infrastructureRoot,
|
|
179
|
-
speciesRoot,
|
|
180
|
-
taxonomyRoot,
|
|
181
122
|
govukFrontendRoot
|
|
182
123
|
})
|
|
183
124
|
|
|
@@ -205,12 +146,7 @@ export function createNunjucksConfig({
|
|
|
205
146
|
environment: nunjucksEnvironment
|
|
206
147
|
},
|
|
207
148
|
relativeTo: projectRoot,
|
|
208
|
-
path: [
|
|
209
|
-
'src/server/routes',
|
|
210
|
-
...buildRoutePaths(speciesRoot),
|
|
211
|
-
...buildRoutePaths(taxonomyRoot),
|
|
212
|
-
...buildRoutePaths(infrastructureRoot)
|
|
213
|
-
],
|
|
149
|
+
path: ['src/server/routes', ...buildRoutePaths(infrastructureRoot)],
|
|
214
150
|
isCached: config.get('isProduction'),
|
|
215
151
|
context
|
|
216
152
|
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { logger } from '@defra/lis-hubs-infra-core'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {{ config: object, fetchImpl?: Function }} options
|
|
5
|
-
* @returns {Function}
|
|
6
|
-
*/
|
|
7
|
-
export function createHoldingService({ config, fetchImpl = globalThis.fetch }) {
|
|
8
|
-
if (!config?.get) {
|
|
9
|
-
throw new TypeError(
|
|
10
|
-
'Holding service requires a config object with a get method'
|
|
11
|
-
)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (typeof fetchImpl !== 'function') {
|
|
15
|
-
throw new TypeError('Holding service requires a fetch implementation')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return async function fetchHolding(cph, accessToken = null) {
|
|
19
|
-
const holdingService = getHoldingServiceConfig(config)
|
|
20
|
-
|
|
21
|
-
if (!holdingService.url) {
|
|
22
|
-
throw new TypeError('Holding service is enabled but not configured')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const headers = {
|
|
26
|
-
accept: 'application/json'
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (holdingService.apiKey) {
|
|
30
|
-
headers[holdingService.apiKeyHeader] = holdingService.apiKey
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (accessToken) {
|
|
34
|
-
headers.authorization = `Bearer ${accessToken}`
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const holdingUrl = new URL(holdingService.url)
|
|
38
|
-
holdingUrl.searchParams.set('holding', cph)
|
|
39
|
-
|
|
40
|
-
logger.info(`Getting holding ${cph}`)
|
|
41
|
-
const response = await fetchImpl(holdingUrl.toString(), {
|
|
42
|
-
method: 'GET',
|
|
43
|
-
headers
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
if (!response.ok) {
|
|
47
|
-
const responseText = await response.text()
|
|
48
|
-
logger.error(
|
|
49
|
-
`Holding service request failed with ${response.status}: ${responseText}`
|
|
50
|
-
)
|
|
51
|
-
throw new TypeError(
|
|
52
|
-
`Holding service request failed with ${response.status}: ${responseText}`
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const result = await response.json()
|
|
57
|
-
logger.info(`Got holding ${cph}: ${JSON.stringify(result)}`)
|
|
58
|
-
return buildHoldingResponse(result)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function buildHoldingResponse(holding = {}) {
|
|
63
|
-
return {
|
|
64
|
-
...holding
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function getHoldingServiceConfig(config) {
|
|
69
|
-
return {
|
|
70
|
-
url: config.get('holdingService.url'),
|
|
71
|
-
apiKey: config.get('holdingService.apiKey'),
|
|
72
|
-
apiKeyHeader: config.get('holdingService.apiKeyHeader')
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict'
|
|
2
|
-
import { beforeEach, test, vi } from 'vitest'
|
|
3
|
-
|
|
4
|
-
const { logger } = vi.hoisted(() => ({
|
|
5
|
-
logger: { info: vi.fn(), error: vi.fn() }
|
|
6
|
-
}))
|
|
7
|
-
|
|
8
|
-
vi.mock('@defra/lis-hubs-infra-core', () => ({
|
|
9
|
-
logger
|
|
10
|
-
}))
|
|
11
|
-
|
|
12
|
-
import { createHoldingService } from './service.js'
|
|
13
|
-
|
|
14
|
-
function createConfig(overrides = {}) {
|
|
15
|
-
const values = {
|
|
16
|
-
'holdingService.url': 'https://holding.example.test/holding',
|
|
17
|
-
'holdingService.apiKey': 'secret-key',
|
|
18
|
-
'holdingService.apiKeyHeader': 'x-api-key',
|
|
19
|
-
...overrides
|
|
20
|
-
}
|
|
21
|
-
return { get: (key) => values[key] }
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
logger.info.mockClear()
|
|
26
|
-
logger.error.mockClear()
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
test('fetches and returns a holding with API and bearer authentication', async () => {
|
|
30
|
-
const responseBody = { cph: '12/345/6789', name: 'Home Farm' }
|
|
31
|
-
const fetchImpl = vi.fn(async () => ({
|
|
32
|
-
ok: true,
|
|
33
|
-
json: async () => responseBody
|
|
34
|
-
}))
|
|
35
|
-
const fetchHolding = createHoldingService({
|
|
36
|
-
config: createConfig(),
|
|
37
|
-
fetchImpl
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
const result = await fetchHolding('12/345/6789', 'access-token')
|
|
41
|
-
|
|
42
|
-
const [url, options] = fetchImpl.mock.calls[0]
|
|
43
|
-
assert.equal(
|
|
44
|
-
url,
|
|
45
|
-
'https://holding.example.test/holding?holding=12%2F345%2F6789'
|
|
46
|
-
)
|
|
47
|
-
assert.deepEqual(options, {
|
|
48
|
-
method: 'GET',
|
|
49
|
-
headers: {
|
|
50
|
-
accept: 'application/json',
|
|
51
|
-
'x-api-key': 'secret-key',
|
|
52
|
-
authorization: 'Bearer access-token'
|
|
53
|
-
}
|
|
54
|
-
})
|
|
55
|
-
assert.deepEqual(result, responseBody)
|
|
56
|
-
assert.notEqual(result, responseBody)
|
|
57
|
-
assert.equal(logger.info.mock.calls.length, 2)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('omits optional authentication headers', async () => {
|
|
61
|
-
const fetchImpl = vi.fn(async () => ({ ok: true, json: async () => ({}) }))
|
|
62
|
-
const fetchHolding = createHoldingService({
|
|
63
|
-
config: createConfig({ 'holdingService.apiKey': '' }),
|
|
64
|
-
fetchImpl
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
await fetchHolding('123')
|
|
68
|
-
|
|
69
|
-
assert.deepEqual(fetchImpl.mock.calls[0][1].headers, {
|
|
70
|
-
accept: 'application/json'
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test('logs response details and throws when a request fails', async () => {
|
|
75
|
-
const fetchHolding = createHoldingService({
|
|
76
|
-
config: createConfig(),
|
|
77
|
-
fetchImpl: async () => ({
|
|
78
|
-
ok: false,
|
|
79
|
-
status: 503,
|
|
80
|
-
text: async () => 'Service unavailable'
|
|
81
|
-
})
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
await assert.rejects(
|
|
85
|
-
fetchHolding('123'),
|
|
86
|
-
/Holding service request failed with 503: Service unavailable/
|
|
87
|
-
)
|
|
88
|
-
assert.deepEqual(logger.error.mock.calls[0], [
|
|
89
|
-
'Holding service request failed with 503: Service unavailable'
|
|
90
|
-
])
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test('validates configuration and fetch dependencies', () => {
|
|
94
|
-
assert.throws(
|
|
95
|
-
() => createHoldingService({ config: null, fetchImpl: vi.fn() }),
|
|
96
|
-
/requires a config object/
|
|
97
|
-
)
|
|
98
|
-
assert.throws(
|
|
99
|
-
() => createHoldingService({ config: createConfig(), fetchImpl: null }),
|
|
100
|
-
/requires a fetch implementation/
|
|
101
|
-
)
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
test('rejects an enabled service without a URL', async () => {
|
|
105
|
-
const fetchHolding = createHoldingService({
|
|
106
|
-
config: createConfig({ 'holdingService.url': '' }),
|
|
107
|
-
fetchImpl: vi.fn()
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
await assert.rejects(fetchHolding('123'), /enabled but not configured/)
|
|
111
|
-
})
|