@claudebernard/web-component-fhir-utils

中文文档
2.7.0发布于 2026/11/04TypeScriptES Module零依赖近 30 天下载 2879

Shared FHIR Bundle utilities (collection/transaction bundles, prior-prescription linking) for Claude Bernard web components and their integrators.

原始使用文档
文档目录11 节
  1. Installation
  2. Integrating an app
  3. Working with a prescription Bundle
  4. Removing medications
  5. Cleaning a Bundle
  6. Finding linked resources
  7. Building a component
  8. Supersession reasons
  9. API
  10. Versioning
  11. License

@claudebernard/web-component-fhir-utils

Shared FHIR Bundle helpers for Claude Bernard medication web components (bcb-medication-request-editor, bcb-therapeutic-alternative) and the applications that embed them.

These components take a small FHIR collection Bundle as input and emit a FHIR transaction Bundle describing exactly what changed — standard Bundle.entry.request.method semantics, no custom DTOs. This package gives you the two sides of that contract so you never write bespoke reconciliation code:

  • Integrating an app? Build the input Bundle, then apply the output Bundle back onto your own state.
  • Building a component? Use the same helpers to produce standards-compliant transaction diffs.

See the full guide on the Claude Bernard Developer Portal for the wider FHIR model

Status: this package defines the target Bundle-in/Bundle-out contract. Check each component's own changelog for the version that adopts it — until then, refer to that component's integration doc for its current API.

Installation

npm install @claudebernard/web-component-fhir-utils

Integrating an app

Build the input Bundle, pushing whatever resources the component needs:

import { createCollectionBundle } from "@claudebernard/web-component-fhir-utils";

const bundle = createCollectionBundle();
bundle.entry.push({ resource: medication });
bundle.entry.push({ resource: medicationRequest });

The Bundle gets a random id (UUID) by default; pass your own with createCollectionBundle(resources, { id }). Components that list several prescriptions (e.g. bcb-shared-medication-review) identify each one by its Bundle.id, so keep it stable if you rebuild the Bundle. Entries built from the resources you pass get a fullUrl (<resourceType>/<id>).

To build a whole prescription (with its date, patient and prescriber, which bcb-shared-medication-review uses to sort prescriptions by date), use createDocumentBundle:

const prescription = createDocumentBundle([medication, medicationRequest], {
  date: "2026-10-21",
  patientId: patient.id,
  practitionerName: "Dr Martin",
});

It returns a document Bundle whose first entry is a Composition (date, title, patient, author), followed by the Practitioner.

<bcb-medication-request-editor .medication="${bundle}"></bcb-medication-request-editor>

Apply the transaction Bundle the component emits back onto your own Bundle — this is the only integration code you need, and it never changes per component or per feature:

import { applyTransactionBundle } from "@claudebernard/web-component-fhir-utils";

editor.addEventListener("fhir-changes", ({ detail }) => {
  myPrescriptionBundle = applyTransactionBundle(myPrescriptionBundle, detail.bundle);
});

applyTransactionBundle understands POST (append), PUT (replace by resourceType + id), and DELETE (remove by resourceType + id). Options:

OptionDefaultEffect
onMissingTarget"append""throw" fails loudly instead of silently inserting a missing PUT / ignoring a missing DELETE
fullUrlFormat"urn:uuid"fullUrl given to the entries the call adds (POST, or a PUT appended for a missing target): "urn:uuid" → urn:uuid:<id>, "resource-type" → <resourceType>/<id> (what createCollectionBundle uses), or a function (resource) => string. Existing entries keep their fullUrl

To apply several transaction Bundles in a row, use applyTransactionBundles. They apply in array order, each one seeing the result of the previous one; the options are the same:

import { applyTransactionBundles } from "@claudebernard/web-component-fhir-utils";

myPrescriptionBundle = applyTransactionBundles(myPrescriptionBundle, [firstTransaction, secondTransaction], {
  fullUrlFormat: "resource-type",
});

Working with a prescription Bundle

Removing medications

removeMedications returns the transaction entries that delete medications from a prescription: each Medication, every MedicationRequest pointing at it (on-hold history included) and the matching references in Composition.section.entry (a PUT of the updated Composition). Apply them like any other diff, then use isPrescriptionEmpty to decide whether the whole prescription should go — deleting it stays your call, since you own its persistence:

import {
  applyTransactionBundle, createTransactionBundle, isPrescriptionEmpty, removeMedications,
} from "@claudebernard/web-component-fhir-utils";

const entries = removeMedications(myPrescriptionBundle, [medicationId]);
myPrescriptionBundle = applyTransactionBundle(myPrescriptionBundle, createTransactionBundle(entries));

if (isPrescriptionEmpty(myPrescriptionBundle)) {
  // no active MedicationRequest left: delete the prescription on your side
}

Only MedicationRequests that reference the removed Medication directly are deleted. A request of another medication that merely has a removed one as priorPrescription (a therapeutic-alternative swap) is the live replacement and is kept. Unknown ids are ignored.

Cleaning a Bundle

cleanBundle returns a copy of a Bundle without its orphan resources (no link to, and no link from, any other resource of the Bundle) and without dangling Composition.section.entry references (sections left empty by that are dropped). It is pure and idempotent.

import { cleanBundle, getCleanupEntries } from "@claudebernard/web-component-fhir-utils";

myPrescriptionBundle = cleanBundle(myPrescriptionBundle);

// or, to emit the cleanup as part of a transaction diff (DELETE per orphan, PUT per updated Composition):
const entries = getCleanupEntries(myPrescriptionBundle);
  • Composition, Patient and Practitioner are never deleted, even when nothing links to them. Override with { keep: ["Composition", ...] }.
  • Inactive resources (on-hold history linked through priorPrescription) are not orphans and are kept.
  • Resources without an id are left alone, as a transaction cannot target them.

Finding linked resources

findResourceReferences lists the resources a Bundle resource references (references) and the ones referencing it (referencedBy), through every Reference.reference (medication, priorPrescription, subject, Composition.section.entry, ...). target is an id, a Type/id reference, or the resource itself.

import { findResourceReferences } from "@claudebernard/web-component-fhir-utils";

const { referencedBy } = findResourceReferences<MedicationRequest>(bundle, medication, {
  direction: "incoming",
  resourceType: "MedicationRequest",
});
OptionDefaultEffect
direction"both""outgoing" (what target references), "incoming" (what references it) or "both"
transitivefalseFollow links of links until the chain ends
includeInactivefalseAlso return, and walk through, resources no longer active (on-hold, stopped, cancelled, completed, entered-in-error, inactive, revoked, deprecated, or active: false)
resourceTypeallOnly return resources of this type

References are matched whatever their spelling (Type/id, urn:uuid:<id>, an entry's fullUrl, absolute URL); references to resources absent from the Bundle, contained (#id) and canonical references are ignored.

Building a component

Produce diff entries with createBundleEntry / createTransactionBundle:

import { createBundleEntry, createTransactionBundle } from "@claudebernard/web-component-fhir-utils";

const bundle = createTransactionBundle([
  createBundleEntry(updatedResource, "PUT"),
  createBundleEntry(newResource, "POST"),
]);

For the common "this MedicationRequest replaces that one" case (dosage override, therapeutic alternative, ...), use supersedeMedicationRequest — it produces the on-hold PUT + linked, tagged POST pair as house convention dictates, so every component represents supersession the same way:

import { PriorPrescriptionReason, supersedeMedicationRequest } from "@claudebernard/web-component-fhir-utils";

const entries = supersedeMedicationRequest(
  originalMedicationRequest,
  { dosageInstruction: newDosage },
  PriorPrescriptionReason.DOSAGE_OVERRIDE,
  { bundle: inputBundle }, // optional, see below
);

Pass the Bundle the original lives in as options.bundle and every Composition listing the original in section.entry also gets a PUT swapping that reference for the new request, so the document stops pointing at the on-hold one. Without bundle, only the two entries below are returned.

The two main entries are shaped like this a diff shaped like this — the original request goes on-hold via PUT, the new one is created active via POST and linked back through priorPrescription:

{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [
    {
      "resource": { "resourceType": "MedicationRequest", "id": "mr-123", "status": "on-hold", "...": "..." },
      "request": { "method": "PUT", "url": "MedicationRequest/mr-123" }
    },
    {
      "resource": {
        "resourceType": "MedicationRequest",
        "id": "mr-456",
        "status": "active",
        "dosageInstruction": ["..."],
        "priorPrescription": {
          "reference": "MedicationRequest/mr-123",
          "extension": [{ "url": "https://platform.claudebernard.fr/fhir/StructureDefinition/prior-prescription-reason", "valueCode": "dosage-override" }]
        }
      },
      "request": { "method": "POST", "url": "MedicationRequest" }
    }
  ]
}

Supersession reasons

PriorPrescriptionReason carries why the new request replaces the old one, as a valueCode on the priorPrescription extension. Pick the one that matches the user's intent — consumers filter on it with hasPriorPrescriptionReason:

ValueCodeWhen
PriorPrescriptionReason.DOSAGE_OVERRIDEdosage-overrideSame medication, dosage changed
PriorPrescriptionReason.MEDICATION_OVERRIDEmedication-overrideDifferent medication, same therapeutic intent
PriorPrescriptionReason.THERAPEUTIC_ALTERNATIVEtherapeutic-alternativeSubstitution proposed by bcb-therapeutic-alternative

More worked examples (including the therapeutic-alternative case) are in the Developer Portal guide.

API

ExportPurpose
createCollectionBundle(resources?, options?)Build the input collection Bundle (options.id, random UUID by default)
createDocumentBundle(resources?, { date, patientId, practitionerName?, id? })Build a prescription document Bundle (Composition + Practitioner first)
createBundleEntry(resource, method, url?)Build one transaction entry (POST/PUT/DELETE)
createTransactionBundle(entries)Wrap entries into the output transaction Bundle
applyTransactionBundle(target, transaction, options?)Apply a transaction Bundle onto a Bundle you hold (options.onMissingTarget, options.fullUrlFormat)
applyTransactionBundles(target, transactions, options?)Apply several transaction Bundles, in order, onto a Bundle you hold
findResourceInBundle(bundle, resourceType, id)Look up a resource by type + id
supersedeMedicationRequest(original, changes, reason, { bundle? })Build the on-hold/priorPrescription/extension pair for a supersession; with bundle, also a PUT swapping the reference in Composition.section.entry
removeMedications(bundle, medicationIds)Entries deleting medications, their MedicationRequests, and their Composition.section.entry references
isPrescriptionEmpty(bundle)True when no active MedicationRequest is left
cleanBundle(bundle, options?) / getCleanupEntries(bundle, options?)Drop orphan resources and dangling Composition.section.entry references (as a Bundle / as transaction entries)
findResourceReferences(bundle, target, options?)Active resources a resource references and is referenced by (direction, transitive, includeInactive, resourceType)
hasPriorPrescriptionReason(medicationRequest, reason)Check why a MedicationRequest supersedes another
PriorPrescriptionReason, PRIOR_PRESCRIPTION_REASON_URL, BCB_CODE_SYSTEMShared constants

Versioning

This package carries the version of the fhir-monorepo repository — one version, tag and changelog for the whole repo. Published versions are therefore sparse: a release only reaches npm when this package actually changed.

License

Copyright of Cegedim. See LICENSE for details.

版本与发布记录

@claudebernard/web-component-fhir-utils 共发布 19 个版本,首次发布于 2026/07/29;过去 12 个月发布了 19 个版本。

版本发布日期距今
2.7.0latest2026/11/045 天前
2.6.02026/10/2119 天前
2.5.32026/10/021 个月前
2.5.12026/10/021 个月前
2.5.02026/09/291 个月前
2.4.22026/09/141 个月前

关于 @claudebernard/web-component-fhir-utils 的常见问题

@claudebernard/web-component-fhir-utils 是什么?

Shared FHIR Bundle utilities (collection/transaction bundles, prior-prescription linking) for Claude Bernard web components and their integrators.

如何安装 @claudebernard/web-component-fhir-utils?

在项目目录运行 npm install @claudebernard/web-component-fhir-utils,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@types/fhir(^0.0.41)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@claudebernard/web-component-fhir-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@claudebernard/web-component-fhir-utils 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@claudebernard/web-component-fhir-utils 可以商用吗?

@claudebernard/web-component-fhir-utils 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@claudebernard/web-component-fhir-utils 的下载量有多少?

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

@claudebernard/web-component-fhir-utils 还在维护吗?

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