
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@ministryofjustice/hmpps-connect-dps-components
Advanced tools
A package to allow the inclusion of connect dps micro frontend components within dps applications
hmpps-connect-dps-components is a Node.js client library to simplify the process of incorporating global components
within DPS applications. We welcome feedback on this README here
in order to improve it.
The package assumes adherance to the standard hmpps-template-typescript project. It requires:
res.locals containing a token, displayName, and authSource.COMPONENT_API_URLTo install the package, run the following command:
npm install @ministryofjustice/hmpps-connect-dps-components
Currently, the package provides the header and the footer component.
To incorporate use the middleware for appropriate routes within your Express application:
import dpsComponents from '@ministryofjustice/hmpps-connect-dps-components'
...
app.use(dpsComponents.getPageComponents({
dpsUrl: config.serviceUrls.digitalPrison,
logger,
})
)
However, please 🙏 consider carefully whether you need the components for EVERY request.
It may be sufficient for you app to only request components for GET requests for example, in which case
app.get('*', dpsComponents.getPageComponents({
dpsUrl: config.serviceUrls.digitalPrison,
logger,
})
)
may be more appropriate, especially if you use the PRG pattern to
handle form submission. This will help us to reduce the load on the micro frontend components API. You may wish to
go even further, for example avoiding routes that don't need components - the Prisoner Profile does
something like this to avoid the component API call for the following routes: /api (provides prisoner images) and /
(a redirect only route).
app.get(
/^(?!\/api|^\/$).*/,
dpsComponents.getPageComponents({
dpsUrl: config.serviceUrls.digitalPrison,
logger,
}),
(req, res) => {
res.render('prisonerProfile')
},
)
There are a number of options available depending on your requirements.
Add the hmpps-connect-dps-components path to the nunjucksSetup.ts file to enable css to be loaded:
const njkEnv = nunjucks.configure(
[
path.join(__dirname, '../../server/views'),
'node_modules/govuk-frontend/dist/',
'node_modules/govuk-frontend/dist/components/',
'node_modules/@ministryofjustice/frontend/',
'node_modules/@ministryofjustice/frontend/moj/components/',
'node_modules/@ministryofjustice/hmpps-connect-dps-components/dist/assets/',
],
{
autoescape: true,
express: app,
},
)
Include the package scss within the all.scss file
@import 'node_modules/@ministryofjustice/hmpps-connect-dps-components/dist/assets/footer';
@import 'node_modules/@ministryofjustice/hmpps-connect-dps-components/dist/assets/header-bar';
Include reference to the components in your layout.njk file:
{% for js in feComponents.jsIncludes %}
<script src="{{ js }}" nonce="{{ cspNonce }}"></script>
{% endfor %}
{% for css in feComponents.cssIncludes %}
<link href="{{ css }}" nonce="{{ cspNonce }}" rel="stylesheet" />
{% endfor %}
{% block header %}
{{ feComponents.header | safe }}
{% endblock %}
{% block footer %}
{{ feComponents.footer | safe }}
{% endblock %}
It may be that you need to add some extra requests for the page components for pages that do not fit the normal flow
of routes. e.g. in setUpAuthentication.ts on the /autherror path:
router.get(
'/autherror',
dpsComponents.getPageComponents({ dpsUrl: config.serviceUrls.digitalPrison }),
(req, res) => {
res.status(401)
return res.render('autherror')
},
)
This will provide a stripped down header for if there is no user object on res.locals.
The package updates the content-security-middleware to include references to the fe-components API. This package should be run after Helmet to prevent this being overwritten.
An optional parameter includeSharedData: true can be passed into the get method. Setting this will result in a
sharedData object being added to res.locals.feComponents containing data the components have collected to render.
This includes:
KEY_WORKER, meaning the user is a key worker and PERSONAL_OFFICER, meaning the user is a personal officer.)This can be useful e.g. for when your service needs access to activeCaseLoad information to prevent extra calls to the api and takes advantage of the caching that the micro frontend api does.
Many services typically add case load information to the user object on res.locals. This library provides some
optional middleware which populates:
res.locals.user.caseLoads with all case loads the user has access tores.locals.user.activeCaseLoad with the active case load of the userres.locals.user.activeCaseLoadId with the id of the active case loadIt uses the sharedData object if it is present and caches in req.session so that any subsequent routes that do not
use the component middleware can still use the data. If there is no data in the cache, it will fall back to making a
call to Prison API to retrieve the data using the user token.
To enable this, add the middleware after the component middleware as follows:
app.use(dpsComponents.retrieveCaseLoadData({ logger }))
Again there are a number of options available depending on your requirements.
This middleware checks the res.locals.user.authSource so ensure that any mock auth data used in tests includes
auth_source: 'nomis' in the response.
This library also provides an optional middleware which populates:
res.locals.user.allocationJobResponsibilities the allocation policy codes the user has the associated job responsibility for. Allocation policy codes are: KEY_WORKER, meaning the user is a key worker and PERSONAL_OFFICER, meaning the user is a personal officer.Similar to shared case load data, it uses the sharedData object if it is present and caches in req.session so that any subsequent routes that do not
use the component middleware can still use the data. If there is no data in the cache, it will fall back to making a
call to Allocations API to retrieve the data using the user token.
To enable this, add the middleware after the component middleware as follows:
app.use(dpsComponents.retrieveAllocationJobResponsibilities({ logger }))
This should go after dpsComponents.retrieveCaseLoadData so that res.locals.user.activeCaseLoadId will be populated.
It also requires ALLOCATIONS_API_URL to be configured in the environment variables.
Again there are a number of options available depending on your requirements.
This middleware checks the res.locals.user.authSource so ensure that any mock auth data used in tests includes
auth_source: 'nomis' in the response. It also checks the res.locals.user.activeCaseLoadId, which is required for retrieving allocation job responsibilities.
In the event of a failure to retrieve the components, the package will populate the html fields with fallback components.
The feComponents.sharedData will not be populated, but if you use the retrieveCaseLoadData middleware (see above) it
will either take case load data from the cache or make a call to the Prison API to retrieve it.
FAQs
A package to allow the inclusion of connect DPS micro frontend components within DPS applications
The npm package @ministryofjustice/hmpps-connect-dps-components receives a total of 544 weekly downloads. As such, @ministryofjustice/hmpps-connect-dps-components popularity was classified as not popular.
We found that @ministryofjustice/hmpps-connect-dps-components demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.