npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ministryofjustice/hmpps-rest-client

v2.1.0

Published

Standardized, reusable REST client for use with HMPPS services

Downloads

18,038

Readme

@ministryofjustice/hmpps-rest-client

This package aims to standardize the way HMPPS TypeScript applications interact with external services by providing a reusable REST client.

Features

  • Standardized HTTP request handling for HMPPS services
  • Automatic request retries
  • Authentication token management (supports system and user tokens)
  • Streamed responses for large payloads
  • Request logging and error handling
  • Configurable timeouts and request headers

Status

This library is currently: ready to adopt.

Teams are encouraged to use this library. Please provide feedback via slack to the #typescript channel.

Usage

Usage is best demonstrated by the HMPPS typescript template as it is already included. New projects based on this template will automatically adopt this package.

RestClient

The package provides an abstract RestClient class that you can extend to create API clients tailored to your service needs.

import { ApiConfig, RestClient } from '@ministryofjustice/hmpps-rest-client'

class ExampleApiClient extends RestClient {
  constructor() {
    super(
      'example-api',
      {
        url: 'https://example.com/api',
        timeout: {
          response: 5000,
          deadline: 10000,
        },
        agent: { maxSockets: 100 },
      } as ApiConfig,
      console, // Replace with a proper logger in production
      {
        getToken: async () => 'your-system-token', // Replace with a token management strategy
      },
    )
  }

  async getExampleData(username: string) {
    return this.get({ path: '/example-data' }, asSystem(username))
  }

  async postJsonData(username: string, jsonRequestBody: object) {
    return this.post({ path: '/example-data', data: jsonRequestBody }, asSystem(username))
  }

  async postMultipartData(username: string, file: { buffer: Buffer; originalname: string }, multipartRequestBody: object) {
    return this.post({ path: '/example-data', files: { upload: file }, multipartData: multipartRequestBody }, asSystem(username))
  }
}

export default new ExampleApiClient()

When using hmpps-auth-clients and dependency injection this might look like:

import { ApiConfig, AuthenticationClient, RestClient } from '@ministryofjustice/hmpps-rest-client'
import config from '../config'
import logger from '../../logger'

export default class ExampleApiClient extends RestClient {
  constructor(authenticationClient: AuthenticationClient) {
    super('Example API Client', config.apis.exampleApi as ApiConfig, logger, authenticationClient)
  }
  ...
}
// data/index.ts
const tokenStore = config.redis.enabled ? new RedisTokenStore(createRedisClient()) : new InMemoryTokenStore()
const authClient = new AuthenticationClient(config.apis.hmppsAuth, logger, tokenStore)
const client = new ExampleApiClient(authClient)

Proxy-Aware Keepalive Agents

If you need outbound requests to use a proxy while still reusing keepalive sockets, pass proxy settings as part of the agent configuration.

const apiConfig: ApiConfig = {
  url: 'https://example.com/api',
  timeout: {
    response: 5000,
    deadline: 10000,
  },
  agent: {
    timeout: 5000,
    proxyEnv: {
      HTTPS_PROXY: process.env.HTTPS_PROXY,
      NO_PROXY: process.env.NO_PROXY,
    },
  },
}

Alternatively, the rest client supports the NODE_USE_ENV_PROXY environment variable to automatically read proxy variables (HTTPS_PROXY, HTTP_PROXY and NO_PROXY) from the environment.

Setting NODE_USE_ENV_PROXY will override any proxy configuration that is programatically configured.

Authentication

This library accepts an optional AuthenticationClient provider which implements a getToken endpoint, used for return system tokens.

Additionally, the library provides

  • asSystem - for generating authentication options for making a request with a system token.
  • asUser - for generating authentication options for making a request with a user token.
  • You may also use the raw JWT string by passing it in place of authOptions.

Error handling

When an API call fails, the superagent ResponseError contains information about the request and response including errors that we would not want to be exposed to logs. The client library copies a subset of the available response properties onto a new error object and makes this available to an ErrorHandler (or ErrorLogger when processing streams) strategy.

The default ErrorHandler strategy is to simply log the error at warning level and then rethrow but this can be overriden on a per call or per client basis.

The status code of the response is mapped to an responseStatus which can be used to conditionally alter the handling of errors.

There are examples in RestClient.test.ts on how to coerce 404 response into null return values and also how to propagate the api response status in a format that express and the default error handler understands.

Developing this package

This module uses rollup, to build:

npm run lint-fix && npm run build && npm run test

Testing changes to this library

  • cd to this directory and then link this library: npm link
  • Utilise the in-development library within a project by using: npm link @ministryofjustice/hmpps-rest-client