Skip to content

Commit

Permalink
feat(experiential): contains typescript specs for MSH
Browse files Browse the repository at this point in the history
#4 first draft of how MSH typescript checking would work. [ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent 8c2cc05 commit fb06663
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 298 deletions.
10 changes: 9 additions & 1 deletion __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Message} from "../src/message";
import {HL7_2_7_MSH} from "../src/specification/2.7";

describe('node hl7 client - builder tests', () => {

Expand All @@ -7,7 +8,14 @@ describe('node hl7 client - builder tests', () => {
test("blank message", async () => {

const message = new Message()
console.log(message)
await message.createHeader<HL7_2_7_MSH>({
msh_9: {
msh_9_1: "ADT",
msh_9_2: "A01",
msh_9_3: "ADT_A01"
},
msh_10: ""
})

})

Expand Down
16 changes: 1 addition & 15 deletions src/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {ClientBuilderOptions, normalizedClientBatchBuilderOptions} from "./norma
*/

export class Batch {

/* private _count: number = 0;
private _opt: ReturnType<typeof normalizedClientBatchBuilderOptions>;
private _header: string;
Expand All @@ -28,19 +27,6 @@ export class Batch {
this._tail = `FTS${this._opt.separatorField}`
}
}*/

buildHeader() {

}

/**
* Adds a new message segment to the batch.
* @since 1.0.0
*/
addMessage() {

}


}
}
1 change: 0 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ export class Client extends EventEmitter {
sendMessage (): void {
/* noop */
}

}
2 changes: 1 addition & 1 deletion src/delimiters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export enum Delimiters {
Repetition,
Escape,
SubComponent
}
}
6 changes: 3 additions & 3 deletions src/exception.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** Parent Cass of HL7 Error
* @since 1.0.0*/
* @since 1.0.0 */
class HL7Error extends Error {
code: number
/** @internal */
Expand All @@ -11,14 +11,14 @@ class HL7Error extends Error {
}

/** Used to indicate a fatal failure of a connection.
* @since 1.0.0*/
* @since 1.0.0 */
class HL7FatalError extends HL7Error {
/** @internal */
name = 'HL7FatalError'
}

/** Used to indicate a fatal failure of a connection.
* @since 1.0.0*/
* @since 1.0.0 */
class HL7ParserError extends HL7Error {
/** @internal */
name = 'HL7ParserError'
Expand Down
15 changes: 6 additions & 9 deletions src/field.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {Delimiters} from "./delimiters";
import {Node} from "./node";
import {ValueNode} from "./valueNode";
import { Delimiters } from './delimiters'
import { Node } from './node'
import { ValueNode } from './valueNode'

export class Field extends ValueNode {

constructor(parent: Node, key: string, text: string) {
super(parent, key, text, Delimiters.Field);
constructor (parent: Node, key: string, text: string) {
super(parent, key, text, Delimiters.Field)

console.log(parent, key, text)

}

}
}
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Client } from './client.js'
import { Listener } from './listener.js'
import { Message } from './message.js'
import { Node } from './node.js'
import { Parser, ParserPlan } from './parser.js'
/** HL7 Specs **/


export default Client
export { Client, Listener, Parser, ParserPlan}
export { Client, Listener, Parser, ParserPlan, Message, Node }

export type { HL7_2_7_MSH, HL7_2_7} from './specification/2.7.js'

export type { ClientOptions, ClientListenerOptions, ParserProcessRawData } from './normalize.js'
export type { HL7Error, HL7FatalError, HL7ParserError } from './exception.js'
93 changes: 56 additions & 37 deletions src/message.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,83 @@
import {Delimiters} from "./delimiters";
import {Node} from "./node";
import {ClientBuilderOptions, normalizedClientBuilderOptions} from "./normalize";
import {Segment} from "./segment";
import {createDateTime} from "./utils";

import { Delimiters } from './delimiters'
import { Node } from './node'
import { ClientBuilderOptions, normalizedClientBuilderOptions } from './normalize'
import { Segment } from './segment'
import { createDateTime } from './utils'

export class Message extends Node {

private _opt: ReturnType<typeof normalizedClientBuilderOptions>;
private readonly _delimiters: string;
private readonly _opt: ReturnType<typeof normalizedClientBuilderOptions>
private _delimiters: string

/**
* @param text If not provided, it will be MSH plus the default encoding characters.
* @param text If not provided you have to build a MSH header using (@see createHeader)
* @param props Override default encoding characters. (@default |^~\\&)
* @example
* If you are processing a full message, do this:
*
* const message = new Message("The HL7 Message Here")
* ... output cut ...
*
* If you are building out a message, do this:
*
* const message = new Message();
* await message.createHeader<HL7_2_7_MSH>() ({ ... required parameters ... })
* ... add additional segments, etc. Check out Unit Tests for complete examples ...
*
* which will generate the properly formatted MSH header for your HL7 Method.
*
* @since 1.0.0
*/
constructor(text: string = "", props: ClientBuilderOptions = normalizedClientBuilderOptions()) {
super( null,text, Delimiters.Segment)
constructor (text: string = '', props: ClientBuilderOptions = normalizedClientBuilderOptions()) {
super(null, text, Delimiters.Segment)

this._opt = normalizedClientBuilderOptions(props)
this._delimiters = `${this._opt.newLine}${this._opt.separatorField}${this._opt.separatorRepetition}${this._opt.separatorEscape}${this._opt.separatorSubComponent}`

if (!text) {
this.initialBuild()
}

}

addSegment(path: string): Segment {
if (!path) {
throw new Error("Missing segment path.");
addSegment (path: string): Segment {
if (typeof path === 'undefined') {
throw new Error('Missing segment path.')
}
let preparedPath = this.preparePath(path);
if (preparedPath.length != 1) {
throw new Error("Invalid segment path '" + path + "'.");
const preparedPath = this.preparePath(path)
if (preparedPath.length !== 1) {
throw new Error("Invalid segment path '" + path + "'.")
}
return <Segment>this.addChild(preparedPath[0]);
return this.addChild(preparedPath[0]) as Segment
}

initialBuild() {
async createHeader<T>(specification: T): Promise<void> {
try {
const specs = await this._opt.specification.checkMSH(specification)

if (typeof specs.msh_1 !== 'undefined') {
this._opt.separatorField = specs.msh_1
}

let segment = this.addSegment("MSH");
segment.set('MSH.1', this._opt.separatorField )
segment.set('MSH.2', `${this._opt.separatorRepetition}${this._opt.separatorEscape}${this._opt.separatorSubComponent}` )
segment.set('MSH.7', createDateTime())
if (typeof specs.msh_2 !== 'undefined') {
this._delimiters = `${this._opt.newLine}${specs.msh_2}`
}

const segment = this.addSegment('MSH')
segment.set('MSH.1', typeof specs.msh_1 !== 'undefined' ? specs.msh_1 : this._opt.separatorField)
segment.set('MSH.2', typeof specs.msh_2 !== 'undefined' ? specs.msh_2 : `${typeof specs.msh_1 !== 'undefined' ? specs.msh_1 : this._opt.separatorField}${this._opt.separatorRepetition}${this._opt.separatorEscape}${this._opt.separatorSubComponent}`)
segment.set('MSH.7', typeof specs.msh_7 !== 'undefined' ? specs.msh_7 : createDateTime())
segment.set('MSH.9').set('1', specs.msh_9_1).set('2', specs.msh_9_2).set('3', specs.msh_9_3)
} catch (e: any) {
throw new Error(e.message)
}
}

protected pathCore(): string[] {
protected pathCore (): string[] {
// the message has an empty path
return [];
return []
}

protected createChild(text: string, _index: number): Node {
protected createChild (text: string, _index: number): Node {
// make sure to remove any \n that might follow the \r
return new Segment(this, text.trim());
return new Segment(this, text.trim())
}

get delimiters(): string {
return this._delimiters;
get delimiters (): string {
return this._delimiters
}

}
}
Loading

0 comments on commit fb06663

Please sign in to comment.