hl7v2 1.1.6 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HL7Message = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const node_process_1 = tslib_1.__importDefault(require("node:process"));
5
6
  const hl7v2_dictionary_1 = require("hl7v2-dictionary");
6
7
  const iconv_lite_1 = tslib_1.__importDefault(require("iconv-lite"));
7
8
  const uid_1 = require("uid");
@@ -195,50 +196,83 @@ class HL7Message {
195
196
  msa
196
197
  .field(hl7v2_dictionary_1.MSASegment.MessageControlID)
197
198
  .setValue(this.header.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).getValue());
198
- if (textMessage) {
199
- if (textMessage instanceof hl7_error_js_1.HL7Error) {
200
- msa.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(textMessage.message);
201
- const err = out.addSegment('ERR');
202
- err
203
- .field(hl7v2_dictionary_1.ERRSegment.HL7ErrorCode)
204
- .setValue(textMessage.hl7ErrorCode || 207);
205
- err.field(hl7v2_dictionary_1.ERRSegment.Severity).setValue(textMessage.severity);
206
- err
207
- .field(hl7v2_dictionary_1.ERRSegment.ApplicationErrorCode)
208
- .setValue(textMessage.appErrorCode);
209
- const errLocation = err.field(hl7v2_dictionary_1.ERRSegment.ErrorLocation);
210
- errLocation.comp(hl7v2_dictionary_1.ERLType.SegmentID).setValue(textMessage.segmentType);
211
- errLocation
212
- .comp(hl7v2_dictionary_1.ERLType.SegmentSequence)
213
- .setValue(textMessage.segmentSequence);
214
- errLocation
215
- .comp(hl7v2_dictionary_1.ERLType.FieldPosition)
216
- .setValue(textMessage.fieldPosition);
217
- errLocation
218
- .comp(hl7v2_dictionary_1.ERLType.ComponentNumber)
219
- .setValue(textMessage.componentPosition);
220
- errLocation
221
- .comp(hl7v2_dictionary_1.ERLType.FieldRepetition)
222
- .setValue(textMessage.repetitionIndex);
223
- errLocation
224
- .comp(hl7v2_dictionary_1.ERLType.SubComponentNumber)
225
- .setValue(textMessage.subComponentPosition);
199
+ if (textMessage)
200
+ msa.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(textMessage);
201
+ return out;
202
+ }
203
+ createNak(errors) {
204
+ const out = new HL7Message(this.version);
205
+ const msh = out.header;
206
+ // Sending Application
207
+ msh
208
+ .field(hl7v2_dictionary_1.MSHSegment.ReceivingApplication)
209
+ .setValue(msh.field(hl7v2_dictionary_1.MSHSegment.SendingFacility).getValue());
210
+ msh
211
+ .field(hl7v2_dictionary_1.MSHSegment.ReceivingFacility)
212
+ .setValue(this.header.field(hl7v2_dictionary_1.MSHSegment.SendingFacility).getValue());
213
+ msh.field(hl7v2_dictionary_1.MSHSegment.MessageType).setValue('ACK');
214
+ msh
215
+ .field(hl7v2_dictionary_1.MSHSegment.MessageType)
216
+ .comp(2)
217
+ .setValue(this.header.field(hl7v2_dictionary_1.MSHSegment.MessageType).getValue(2));
218
+ msh
219
+ .field(hl7v2_dictionary_1.MSHSegment.MessageControlID)
220
+ .setValue(String(Date.now() + (0, uid_1.uid)(5)));
221
+ const msaSegment = out.addSegment('MSA');
222
+ msaSegment.field(hl7v2_dictionary_1.MSASegment.AcknowledgmentCode).setValue('AE');
223
+ msaSegment
224
+ .field(hl7v2_dictionary_1.MSASegment.MessageControlID)
225
+ .setValue(this.header.field(hl7v2_dictionary_1.MSHSegment.MessageControlID).getValue());
226
+ for (const error of errors) {
227
+ this.addError(error);
228
+ }
229
+ return out;
230
+ }
231
+ addError(error) {
232
+ let msaSegment = this.getSegment('MSA');
233
+ if (!msaSegment)
234
+ msaSegment = this.addSegment('MSA');
235
+ if (error instanceof Error) {
236
+ msaSegment.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(error.message);
237
+ }
238
+ else
239
+ msaSegment.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(String(error));
240
+ const errSegment = this.addSegment('ERR');
241
+ if (error instanceof hl7_error_js_1.HL7Error) {
242
+ errSegment
243
+ .field(hl7v2_dictionary_1.ERRSegment.HL7ErrorCode)
244
+ .setValue(error.hl7ErrorCode || 207);
245
+ errSegment.field(hl7v2_dictionary_1.ERRSegment.Severity).setValue(error.severity);
246
+ errSegment
247
+ .field(hl7v2_dictionary_1.ERRSegment.ApplicationErrorCode)
248
+ .setValue(error.appErrorCode);
249
+ const errLocation = errSegment.field(hl7v2_dictionary_1.ERRSegment.ErrorLocation);
250
+ errLocation.comp(hl7v2_dictionary_1.ERLType.SegmentID).setValue(error.segmentType);
251
+ errLocation.comp(hl7v2_dictionary_1.ERLType.SegmentSequence).setValue(error.segmentSequence);
252
+ errLocation.comp(hl7v2_dictionary_1.ERLType.FieldPosition).setValue(error.fieldPosition);
253
+ errLocation
254
+ .comp(hl7v2_dictionary_1.ERLType.ComponentNumber)
255
+ .setValue(error.componentPosition);
256
+ errLocation.comp(hl7v2_dictionary_1.ERLType.FieldRepetition).setValue(error.repetitionIndex);
257
+ errLocation
258
+ .comp(hl7v2_dictionary_1.ERLType.SubComponentNumber)
259
+ .setValue(error.subComponentPosition);
260
+ if ((node_process_1.default?.env.NODE_ENV || '').startsWith('dev')) {
261
+ errSegment
262
+ .field(hl7v2_dictionary_1.ERRSegment.DiagnosticInformation)
263
+ .setValue(error.stack);
226
264
  }
227
- else if (textMessage instanceof Error) {
228
- msa.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(textMessage.message);
229
- const err = out.addSegment('ERR');
230
- err.field(hl7v2_dictionary_1.ERRSegment.HL7ErrorCode).setValue(207);
231
- err.field(hl7v2_dictionary_1.ERRSegment.Severity).setValue('E');
232
- if (textMessage.code)
233
- err
265
+ }
266
+ else {
267
+ errSegment.field(hl7v2_dictionary_1.ERRSegment.HL7ErrorCode).setValue(207);
268
+ errSegment.field(hl7v2_dictionary_1.ERRSegment.Severity).setValue('E');
269
+ if (error instanceof Error) {
270
+ if (error.code)
271
+ errSegment
234
272
  .field(hl7v2_dictionary_1.ERRSegment.ApplicationErrorCode)
235
- .setValue(textMessage);
236
- }
237
- else {
238
- msa.field(hl7v2_dictionary_1.MSASegment.TextMessage).setValue(textMessage);
273
+ .setValue(error.code);
239
274
  }
240
275
  }
241
- return out;
242
276
  }
243
277
  /**
244
278
  *
@@ -1,3 +1,4 @@
1
+ import process from 'node:process';
1
2
  import { dictionaries as defaultDictionaries, ERLType, ERRSegment, findNearestHL7Version, HL7Version, MSASegment, MSHSegment, } from 'hl7v2-dictionary';
2
3
  import iconv from 'iconv-lite';
3
4
  import { uid } from 'uid';
@@ -191,50 +192,83 @@ export class HL7Message {
191
192
  msa
192
193
  .field(MSASegment.MessageControlID)
193
194
  .setValue(this.header.field(MSHSegment.MessageControlID).getValue());
194
- if (textMessage) {
195
- if (textMessage instanceof HL7Error) {
196
- msa.field(MSASegment.TextMessage).setValue(textMessage.message);
197
- const err = out.addSegment('ERR');
198
- err
199
- .field(ERRSegment.HL7ErrorCode)
200
- .setValue(textMessage.hl7ErrorCode || 207);
201
- err.field(ERRSegment.Severity).setValue(textMessage.severity);
202
- err
203
- .field(ERRSegment.ApplicationErrorCode)
204
- .setValue(textMessage.appErrorCode);
205
- const errLocation = err.field(ERRSegment.ErrorLocation);
206
- errLocation.comp(ERLType.SegmentID).setValue(textMessage.segmentType);
207
- errLocation
208
- .comp(ERLType.SegmentSequence)
209
- .setValue(textMessage.segmentSequence);
210
- errLocation
211
- .comp(ERLType.FieldPosition)
212
- .setValue(textMessage.fieldPosition);
213
- errLocation
214
- .comp(ERLType.ComponentNumber)
215
- .setValue(textMessage.componentPosition);
216
- errLocation
217
- .comp(ERLType.FieldRepetition)
218
- .setValue(textMessage.repetitionIndex);
219
- errLocation
220
- .comp(ERLType.SubComponentNumber)
221
- .setValue(textMessage.subComponentPosition);
195
+ if (textMessage)
196
+ msa.field(MSASegment.TextMessage).setValue(textMessage);
197
+ return out;
198
+ }
199
+ createNak(errors) {
200
+ const out = new HL7Message(this.version);
201
+ const msh = out.header;
202
+ // Sending Application
203
+ msh
204
+ .field(MSHSegment.ReceivingApplication)
205
+ .setValue(msh.field(MSHSegment.SendingFacility).getValue());
206
+ msh
207
+ .field(MSHSegment.ReceivingFacility)
208
+ .setValue(this.header.field(MSHSegment.SendingFacility).getValue());
209
+ msh.field(MSHSegment.MessageType).setValue('ACK');
210
+ msh
211
+ .field(MSHSegment.MessageType)
212
+ .comp(2)
213
+ .setValue(this.header.field(MSHSegment.MessageType).getValue(2));
214
+ msh
215
+ .field(MSHSegment.MessageControlID)
216
+ .setValue(String(Date.now() + uid(5)));
217
+ const msaSegment = out.addSegment('MSA');
218
+ msaSegment.field(MSASegment.AcknowledgmentCode).setValue('AE');
219
+ msaSegment
220
+ .field(MSASegment.MessageControlID)
221
+ .setValue(this.header.field(MSHSegment.MessageControlID).getValue());
222
+ for (const error of errors) {
223
+ this.addError(error);
224
+ }
225
+ return out;
226
+ }
227
+ addError(error) {
228
+ let msaSegment = this.getSegment('MSA');
229
+ if (!msaSegment)
230
+ msaSegment = this.addSegment('MSA');
231
+ if (error instanceof Error) {
232
+ msaSegment.field(MSASegment.TextMessage).setValue(error.message);
233
+ }
234
+ else
235
+ msaSegment.field(MSASegment.TextMessage).setValue(String(error));
236
+ const errSegment = this.addSegment('ERR');
237
+ if (error instanceof HL7Error) {
238
+ errSegment
239
+ .field(ERRSegment.HL7ErrorCode)
240
+ .setValue(error.hl7ErrorCode || 207);
241
+ errSegment.field(ERRSegment.Severity).setValue(error.severity);
242
+ errSegment
243
+ .field(ERRSegment.ApplicationErrorCode)
244
+ .setValue(error.appErrorCode);
245
+ const errLocation = errSegment.field(ERRSegment.ErrorLocation);
246
+ errLocation.comp(ERLType.SegmentID).setValue(error.segmentType);
247
+ errLocation.comp(ERLType.SegmentSequence).setValue(error.segmentSequence);
248
+ errLocation.comp(ERLType.FieldPosition).setValue(error.fieldPosition);
249
+ errLocation
250
+ .comp(ERLType.ComponentNumber)
251
+ .setValue(error.componentPosition);
252
+ errLocation.comp(ERLType.FieldRepetition).setValue(error.repetitionIndex);
253
+ errLocation
254
+ .comp(ERLType.SubComponentNumber)
255
+ .setValue(error.subComponentPosition);
256
+ if ((process?.env.NODE_ENV || '').startsWith('dev')) {
257
+ errSegment
258
+ .field(ERRSegment.DiagnosticInformation)
259
+ .setValue(error.stack);
222
260
  }
223
- else if (textMessage instanceof Error) {
224
- msa.field(MSASegment.TextMessage).setValue(textMessage.message);
225
- const err = out.addSegment('ERR');
226
- err.field(ERRSegment.HL7ErrorCode).setValue(207);
227
- err.field(ERRSegment.Severity).setValue('E');
228
- if (textMessage.code)
229
- err
261
+ }
262
+ else {
263
+ errSegment.field(ERRSegment.HL7ErrorCode).setValue(207);
264
+ errSegment.field(ERRSegment.Severity).setValue('E');
265
+ if (error instanceof Error) {
266
+ if (error.code)
267
+ errSegment
230
268
  .field(ERRSegment.ApplicationErrorCode)
231
- .setValue(textMessage);
232
- }
233
- else {
234
- msa.field(MSASegment.TextMessage).setValue(textMessage);
269
+ .setValue(error.code);
235
270
  }
236
271
  }
237
- return out;
238
272
  }
239
273
  /**
240
274
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hl7v2",
3
3
  "description": "HL7 v2 parser, serializer, validator for NodeJS",
4
- "version": "1.1.6",
4
+ "version": "1.2.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
@@ -15,7 +15,7 @@
15
15
  "uid": "^2.0.2"
16
16
  },
17
17
  "peerDependencies": {
18
- "hl7v2-dictionary": "^1.1.6"
18
+ "hl7v2-dictionary": "^1.2.0"
19
19
  },
20
20
  "type": "module",
21
21
  "exports": {
@@ -30,7 +30,9 @@ export declare class HL7Message {
30
30
  getSegmentFromLast(segmentType: string, index?: number): HL7Segment | undefined;
31
31
  toHL7String(options?: HL7MessageSerializeOptions): string;
32
32
  parse(input: string | Buffer): void;
33
- createAck(ackCode?: AcknowledgmentCode, textMessage?: string | Error): HL7Message;
33
+ createAck(ackCode?: AcknowledgmentCode, textMessage?: string): HL7Message;
34
+ createNak(errors: (Error | string)[]): HL7Message;
35
+ addError(error: Error | string): void;
34
36
  /**
35
37
  *
36
38
  * @static