@govuk-pay/pay-js-commons

中文文档
7.0.111发布于 2026/11/13MITCommonJS构建来源已验证近 30 天下载 6027

Reusable js scripts for GOV.UK Pay Node.js projects

原始使用文档
文档目录9 节
  1. Browsered scripts
  2. Field Validation
  3. Utilities
  4. Nunjucks filters
  5. Axios base client
  6. Built in in retry functionality
  7. Releasing
  8. Licence
  9. Vulnerability Disclosure

GOV.UK Pay JS Commons

Reusable js scripts for GOV.UK Pay Node.js projects

Browsered scripts

This is a collection of client side scripts we use throughout GOV.UK Pay in the browser. We call it browsered because they are written in Node.js and browsered by Browserify to make them safe for all our browsers. We browserify within the microservice when it’s compiled.

List of scripts

Field Validation

This is a collection of validators that can be applied to inputs that will check the values and display errors using the GOV.UK elements styling.

Validators:

Required

This requires a value from a given input:

<form data-validate>
  <div class="govuk-form-group">
    <label for="name">Your name</label>
    <input name="name" data-validate="required" value="" />
  </div>
</form>

Currency

This requires the value is a valid currency amount i.e. “10” or ”9.99”.

<form data-validate>
  <div class="govuk-form-group">
    <label for="amount">Amount</label>
    <input name="amount" data-validate="required currency" value="" />
  </div>
</form>

Email

This requires the value is a valid email address with a TLD on the end (as technically an email doesn’t need one).

<form data-validate>
  <div class="govuk-form-group">
    <label for="email">Your email address</label>
    <input name="email" data-validate="email" value="" />
  </div>
</form>

Phone

This requires the value is a 11 digit phone number, it isn’t concerned with spacing, so 077 777 777 77 and 07777777777 are both valid.

<form data-validate>
  <div class="govuk-form-group">
    <label for="phone">Phone number</label>
    <input name="phone" data-validate="phone" value="" />
  </div>
</form>

HTTPS

This requires a link to begin with https://

<form data-validate>
  <div class="govuk-form-group">
    <label for="url">Return URL</label>
    <input name="url" data-validate="https" value="" />
  </div>
</form>

Number is less than maximum value

This requires the value is less than £100,000 as that has been deemed sensible…

<form data-validate>
  <div class="govuk-form-group">
    <label for="price">Amount</label>
    <input name="price" data-validate="belowMaxAmount" value="" />
  </div>
</form>

Password

This requires a password be at least 10 chars

<form data-validate>
  <div class="govuk-form-group">
    <label for="password">Password</label>
    <input name="password" data-validate="passwordLessThanTenChars" value="" />
  </div>
</form>

Maximum character limit

This requires a value be less than a certain number of characters. This limit is set within a data-attribute

<form data-validate>
  <div class="govuk-form-group">
    <label for="title">Title</label>
    <input name="title" data-validate="isFieldGreaterThanMaxLengthChars" data-validate-max-length="255" value="" />
  </div>
</form>

NAXSI

This checks whether a field contains characters than would cause NAXSI to get upset, meaning characters that look like code injection i.e. < > ; : ` ( ) " \' = | , ~ [ ]

<form data-validate>
  <div class="govuk-form-group">
    <label for="title">Title</label>
    <input name="title" data-validate="isNaxsiSafe" value="" />
  </div>
</form>

Utilities

Nunjucks filters

These are small functions that power the nunjucks filters but can also be used for server side stuff too.

These get loaded in to the Nunjucks environment and then can apply changes to variables in templates.

For example if a country comes in as ISO code EN it can be converted to it’s name like so

  <p>{{ countryCode | countryISOtoName }}</p>

Or a pence value can be converted to GBP

  <dl>
    <dt>Amount:</dt>
    <dd>{{ amount | penceToPounds }}</dd>
  </dl>

Axios base client

Used in our Node.js apps to call APIs such as connector or ledger.

Uses the NPM Axios library.

The base client provides the following configuration options:

  • onRequestStart - Callback function before starting a API request. Mainly used for logging.
  • onSuccessResponse - Callback function when request has completed successfully.
  • onFailureResponse - Callback function when request has failed.
  • acceptAllStatusCodes - boolean
    • By default all non 2xx responses are considered failed requests.
    • Set this to true and all status codes are considered a successful request. You code will then need to handle the response appropriately. Currently, only pay-frontend requires this to be set to true.

Built in in retry functionality

If a GET request provides a response with the error message ECONNRESET - the the Axios base client will automatically try that request another 2 times - 3 times in total.

By default, on each failed request, it will call the onFailureResponse callback.

If acceptAllStatusCodes=true, then it will call the onSuccessResponse callback.

Releasing

After a pull request is merged, Concourse will automatically create a new release pull request that increments the package version.

This pull request must be reviewed and merged by a developer.

Once the release pull request is merged, GitHub Actions will publish the new versioned package to NPM.

IMPORTANT: Other pull requests will be blocked from merging until the release pull request is merged or closed.

Licence

MIT License

Vulnerability Disclosure

GOV.UK Pay aims to stay secure for everyone. If you are a security researcher and have discovered a security vulnerability in this code, we appreciate your help in disclosing it to us in a responsible manner. Please refer to our vulnerability disclosure policy and our security.txt file for details.

版本与发布记录

@govuk-pay/pay-js-commons 共发布 281 个版本,首次发布于 2018/06/12;过去 12 个月发布了 81 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.0.111latest2026/11/134 天前
7.0.1102026/11/124 天前
7.0.1092026/11/097 天前
7.0.1082026/11/0710 天前
7.0.1072026/10/3017 天前
7.0.1062026/10/3017 天前

关于 @govuk-pay/pay-js-commons 的常见问题

@govuk-pay/pay-js-commons 是什么?

Reusable js scripts for GOV.UK Pay Node.js projects

如何安装 @govuk-pay/pay-js-commons?

在项目目录运行 npm install @govuk-pay/pay-js-commons,或使用 pnpm add / yarn add / bun add。

@govuk-pay/pay-js-commons 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/govuk-pay__pay-js-commons 是否存在。

@govuk-pay/pay-js-commons 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@govuk-pay/pay-js-commons 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^24.22.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@govuk-pay/pay-js-commons 可以商用吗?

@govuk-pay/pay-js-commons 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@govuk-pay/pay-js-commons 的下载量有多少?

根据 npm 官方下载统计,@govuk-pay/pay-js-commons 在 2026/10/17 – 2026/11/15 这 30 天内被下载 6,027 次,日均约 201 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@govuk-pay/pay-js-commons 还在维护吗?

最新版本 7.0.111 发布于 2026/11/13(4 天前);至今共发布 281 个版本,其中过去 12 个月发布了 81 个;首次发布于 2018/06/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。