@defra/fcp-audit-publisher

中文文档
1.0.14发布于 2026/11/02OGL-UK-3.0ESM + CJS构建来源已验证近 30 天下载 1.1万

A library to validate and/or publish FCP Audit events to the FCP Audit service.

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. ESM
  4. CommonJS
  5. validateAuditEvent(event)
  6. Return value
  7. publishAuditEvent(event, config)
  8. Config reference
  9. Default precedence
  10. eventSchema
  11. Event schema
  12. audit object
  13. security object
  14. Licence

Quality Gate Status Bugs Code Smells Duplicated Lines (%) Coverage Dependabot

FCP Audit Publisher

A library to validate and/or publish FCP Audit events to the FCP Audit service.

Installation

npm install fcp-audit-publisher

Usage

ESM

import { validateAuditEvent, publishAuditEvent } from '@defra/fcp-audit-publisher'

CommonJS

const { validateAuditEvent, publishAuditEvent } = require('@defra/fcp-audit-publisher')

validateAuditEvent(event)

Validates a JSON object against the FCP Audit event schema.

const result = validateAuditEvent({
  correlationid: 'abc-123',
  datetime: '2025-12-01T12:51:41.381Z',
  environment: 'prod',
  version: '1.0.0',
  application: 'FCP001',
  component: 'my-service',
  ip: '192.168.1.100',
  audit: {
    entities: [{ entity: 'application', action: 'created', entityid: 'APP-001' }]
  },
  security: null
})

Return value

On success:

{ valid: true, value: { /* normalised event */ } }

On failure:

{
  valid: false,
  errors: [
    '"datetime" is required',
    '"application" is required'
  ]
}

publishAuditEvent(event, config)

Applies defaults, validates the event, then publishes it to an AWS SNS topic.

The caller is responsible for constructing and reusing an SNSClient.

import { SNSClient } from '@aws-sdk/client-sns'
import { publishAuditEvent } from '@defra/fcp-audit-publisher'

const snsClient = new SNSClient({ region: 'eu-west-2' })

const result = await publishAuditEvent(
  {
    audit: {
      entities: [{ entity: 'application', action: 'created', entityid: 'APP-001' }]
    },
    security: null
  },
  {
    snsClient,
    sns: { topicArn: 'arn:aws:sns:eu-west-2:123456789012:your-sns' },
    application: 'FCP001',
    component: 'my-service',
    environment: 'prod',
    ip: '192.168.1.100',
    generateCorrelationId: true
  }
)

console.log(result.messageId)

Throws an Error if the config is invalid or if the event is invalid after defaults are applied.

Config reference

OptionTypeRequiredDescription
snsClientSNSClientYesA pre-constructed AWS SDK SNSClient instance. The caller owns its lifecycle
sns.topicArnstringYesARN of the SNS topic to publish to
versionstringNoDefault event schema version. Overridden by the event's own version field
applicationstringNoDefault application name. Overridden by the event's own application field
componentstringNoDefault component name. Overridden by the event's own component field
environmentstringNoDefault environment. Overridden by the event's own environment field
ipstringNoDefault IP address. Overridden by the event's own ip field
generateCorrelationIdbooleanNoWhen true, generates a UUID v4 as correlationid if the event has none

Default precedence

For fields that can be defaulted, the order of precedence from lowest to highest is:

library default → config value → event value

FieldLibrary defaultConfig overrideNotes
version'1.0.0'config.version
datetimenew Date().toISOString()—Applied only when absent from event
correlationid—generateCorrelationId: true generates a UUIDApplied only when absent from event
application—config.application
component—config.component
environment—config.environment
ip—config.ipShould only be used for services reporting their own IP address, ie Cron job or background service. Not when handling requests from users or other services.

eventSchema

The raw Joi schema used internally by validateAuditEvent, exported for the fcp-audit service only.

import { eventSchema } from '@defra/fcp-audit-publisher'

Other consumers should not import or depend on this directly — use validateAuditEvent/publishAuditEvent instead.


Event schema

All events must satisfy the following schema (validated via Joi):

FieldTypeRequiredConstraints
correlationidstringYesmax 50 chars
datetimeISO date stringYes
environmentstringYeslowercase, max 20 chars
versionstringYesmax 10 chars
applicationstringYesmax 30 chars
componentstringYesmax 30 chars
ipstringYesmax 20 chars
userstringNomax 50 chars
sessionidstringNomax 50 chars
auditobject or nullNo*See audit schema below
securityobject or nullNo*See security schema below

* At least one of audit or security must be present and non-null.

audit object

FieldTypeRequiredConstraints
entitiesarrayYesmin 1 item
entities[].entitystringYeslowercase, max 120 chars
entities[].actionstringYeslowercase, max 120 chars
entities[].entityidstringNomax 120 chars
accountsobjectNokeys: sbi, frn, vendor, trader, organisationId, crn, personId
statusstringNomax 120 chars
detailsobjectNofree-form

security object

FieldTypeRequiredConstraints
pmccodestringYesmax 4 chars (hyphens stripped)
priorityintegerNodefaults to 0
details.transactioncodestringNomax 4 chars
details.messagestringNomax 120 chars
details.additionalinfostringNomax 120 chars

Licence

THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at: http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3

版本与发布记录

@defra/fcp-audit-publisher 共发布 19 个版本,首次发布于 2026/05/05;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.14latest2026/11/029 天前
1.0.132026/10/2716 天前
1.0.122026/10/1626 天前
1.0.112026/09/301 个月前
1.0.102026/09/261 个月前
1.0.92026/09/112 个月前

关于 @defra/fcp-audit-publisher 的常见问题

@defra/fcp-audit-publisher 是什么?

A library to validate and/or publish FCP Audit events to the FCP Audit service.

如何安装 @defra/fcp-audit-publisher?

在项目目录运行 npm install @defra/fcp-audit-publisher,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@aws-sdk/client-sns(>=3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@defra/fcp-audit-publisher 支持 TypeScript 吗?

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

@defra/fcp-audit-publisher 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@defra/fcp-audit-publisher 可以商用吗?

@defra/fcp-audit-publisher 使用 OGL-UK-3.0 许可。声明的许可为 OGL-UK-3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@defra/fcp-audit-publisher 的下载量有多少?

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

@defra/fcp-audit-publisher 还在维护吗?

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