Function: buildMessage()
buildMessage(
init):Hl7Message
Construct an outbound Hl7Message from semantic MSH fields (SER-06).
Synthesises a complete MSH RawSegment per D-10/D-11 and hands to
new Hl7Message({...}). Callers chain .addSegment(name, fields)
(mutation method) to append PID, OBX, etc.
Defaults applied when fields are omitted (D-10):
controlId→generateControlId()(D-12)timestamp→formatHl7Timestamp(new Date())(D-13)version→"2.5"processingId→"P"sendingApp/sendingFacility/receivingApp/receivingFacility→ empty
Encoding characters are always DEFAULT_ENCODING_CHARACTERS (D-14); no
option to customise in v1.
Empty string vs. omitted field (W1): at the HL7 wire level, passing
sendingApp: "" and omitting sendingApp produce IDENTICAL output
(both emit as absent: || at the MSH-3 position). If you need to
emit an HL7 explicit null ("", the two-char literal) at a specific
position, build the message first, then use setField:
const msg = buildMessage({ type: "ADT^A01" });
msg.setField("MSH.3", '""'); // sets RawField.isNull = true
// msg.toString() now emits MSH-3 as `""` (2 chars), not as absent.
BuildMessageInit carries the same note on the input shape.
Parameters
init
Returns
Example
import { buildMessage, parseHL7 } from "@cosyte/hl7";
const msg = buildMessage({
type: "ADT^A01",
sendingApp: "CLINIC",
sendingFacility: "MAIN",
receivingApp: "LAB",
receivingFacility: "REF",
}).addSegment("PID", ["", "", "MRN123", "", "Doe^John"]);
// Spec-clean HL7 string round-trips through parseHL7:
const round = parseHL7(msg.toString());
console.log(round.meta.type); // "ADT^A01"