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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rethinkhealth/hl7v2-util-message-info

v0.4.2

Published

Extract HL7v2 message metadata (version, type, structure) from MSH segments

Readme

@rethinkhealth/hl7v2-util-message-info

Utility library to extract HL7v2 message metadata (version, type, structure) from MSH segments.

What is this?

@rethinkhealth/hl7v2-util-message-info provides utility functions to extract message metadata from the MSH segment of HL7v2 messages. It parses MSH-9 (message type, trigger event, structure) and MSH-12 (version) fields into a structured format.

This is a low-level utility. For automatic annotation of AST trees, see @rethinkhealth/hl7v2-annotate-message.

When should I use this?

Use this utility when you need to:

  • Extract message metadata on-demand from an HL7v2 AST
  • Build custom plugins that need message type/version information
  • Implement version-specific logic in transformers
  • Query message metadata without using the annotator plugin

If you're using unified pipelines, consider using @rethinkhealth/hl7v2-annotate-message instead, which pre-extracts and caches this information in Root.data.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install @rethinkhealth/hl7v2-util-message-info

Use

Basic Usage

import { parseHL7v2 } from '@rethinkhealth/hl7v2-parser';
import { getMessageInfo } from '@rethinkhealth/hl7v2-util-message-info';

const message = `MSH|^~\\&|SENDER||RECEIVER||20241201||ADT^A01^ADT_A01|MSG123|P|2.5`;
const tree = parseHL7v2(message);

const info = getMessageInfo(tree);
console.log(info);
// {
//   version: "2.5",
//   messageCode: "ADT",
//   triggerEvent: "A01",
//   messageStructure: "ADT_A01"
// }

Extract Specific Fields

import { 
  getVersion, 
  getMessageCode, 
  getTriggerEvent, 
  getMessageStructure 
} from '@rethinkhealth/hl7v2-util-message-info';

const version = getVersion(tree);           // "2.5"
const messageCode = getMessageCode(tree);   // "ADT"
const triggerEvent = getTriggerEvent(tree); // "A01"
const structure = getMessageStructure(tree); // "ADT_A01"

Check if Message Has Metadata

import { hasMessageInfo } from '@rethinkhealth/hl7v2-util-message-info';

if (hasMessageInfo(tree)) {
  // MSH segment exists with at least version or message code
  const info = getMessageInfo(tree);
}

Use in Custom Plugins

import type { Plugin } from 'unified';
import type { Root } from '@rethinkhealth/hl7v2-ast';
import { getMessageInfo } from '@rethinkhealth/hl7v2-util-message-info';

const myVersionSpecificPlugin: Plugin<[], Root, Root> = () => {
  return (tree: Root) => {
    const info = getMessageInfo(tree);
    
    if (info.version === '2.5') {
      // Apply 2.5-specific transformations
    } else if (info.version === '2.3.1') {
      // Apply 2.3.1-specific transformations
    }
    
    return tree;
  };
};

API

getMessageInfo(tree)

Extract all message metadata from the MSH segment.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

MessageInfo object containing:

  • version (string | undefined) — MSH-12 (e.g., "2.5")
  • messageCode (string | undefined) — MSH-9.1 (e.g., "ADT")
  • triggerEvent (string | undefined) — MSH-9.2 (e.g., "A01")
  • messageStructure (string | undefined) — MSH-9.3 (e.g., "ADT_A01")

getVersion(tree)

Extract message version from MSH-12.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

Version string (string | undefined)

getMessageCode(tree)

Extract message code from MSH-9.1.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

Message code (string | undefined)

getTriggerEvent(tree)

Extract trigger event from MSH-9.2.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

Trigger event (string | undefined)

getMessageStructure(tree)

Extract message structure from MSH-9.3.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

Message structure (string | undefined)

hasMessageInfo(tree)

Check if the tree has message metadata.

Parameters
  • tree (Root) — The HL7v2 AST root node
Returns

true if MSH segment exists with at least version or message code (boolean)

Types

interface MessageInfo {
  version?: string;           // MSH-12
  messageCode?: string;       // MSH-9.1
  triggerEvent?: string;      // MSH-9.2
  messageStructure?: string;  // MSH-9.3
}

Examples

Version-Based Routing

const info = getMessageInfo(tree);

switch (info.version) {
  case '2.3':
  case '2.3.1':
    routeToLegacySystem(tree);
    break;
  case '2.5':
  case '2.5.1':
    routeToModernSystem(tree);
    break;
  default:
    throw new Error(`Unsupported version: ${info.version}`);
}

Message Type Handling

const info = getMessageInfo(tree);

if (info.messageCode === 'ADT') {
  handleAdmitDischargeTransfer(tree);
} else if (info.messageCode === 'ORU') {
  handleObservationResults(tree);
} else if (info.messageCode === 'VXU') {
  handleVaccinationUpdate(tree);
}

Structure Validation

const info = getMessageInfo(tree);

const expectedStructure = `${info.messageCode}_${info.triggerEvent}`;
if (info.messageStructure !== expectedStructure) {
  console.warn(
    `Message structure mismatch: expected ${expectedStructure}, got ${info.messageStructure}`
  );
}

Performance

The utility functions query the AST on every call. For better performance in unified pipelines, use @rethinkhealth/hl7v2-annotate-message which extracts metadata once and caches it in Root.data.messageInfo.

Related

Contributing

We welcome contributions! Please see our Contributing Guide for more details.

License

Copyright 2025 Rethink Health, SUARL. All rights reserved.

This program is licensed to you under the terms of the MIT License.