fastify-hl7 3.2.0 → 4.0.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +388 -60
  3. package/dist/api.d.ts +5 -0
  4. package/{lib/types → dist}/class/hL7Client.d.ts +49 -13
  5. package/{lib/esm → dist}/class/hL7Server.d.ts +2 -2
  6. package/dist/index.cjs +403 -0
  7. package/dist/index.cjs.map +1 -0
  8. package/{lib/types → dist}/index.d.ts +3 -3
  9. package/dist/index.mjs +377 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/{lib/esm → dist}/types.d.ts +30 -21
  12. package/dist/validation.d.ts +6 -0
  13. package/package.json +43 -48
  14. package/lib/cjs/api.js +0 -13
  15. package/lib/cjs/class/hL7Client.js +0 -238
  16. package/lib/cjs/class/hL7Server.js +0 -81
  17. package/lib/cjs/decorate.js +0 -2
  18. package/lib/cjs/errors.js +0 -13
  19. package/lib/cjs/index.js +0 -144
  20. package/lib/cjs/package.json +0 -3
  21. package/lib/cjs/types.js +0 -2
  22. package/lib/cjs/validation.js +0 -15
  23. package/lib/esm/api.d.ts +0 -7
  24. package/lib/esm/api.js +0 -5
  25. package/lib/esm/class/hL7Client.d.ts +0 -99
  26. package/lib/esm/class/hL7Client.js +0 -201
  27. package/lib/esm/class/hL7Server.js +0 -77
  28. package/lib/esm/decorate.js +0 -1
  29. package/lib/esm/errors.js +0 -7
  30. package/lib/esm/index.d.ts +0 -5
  31. package/lib/esm/index.js +0 -125
  32. package/lib/esm/package.json +0 -3
  33. package/lib/esm/types.js +0 -1
  34. package/lib/esm/validation.d.ts +0 -6
  35. package/lib/esm/validation.js +0 -11
  36. package/lib/types/api.d.ts +0 -7
  37. package/lib/types/class/hL7Server.d.ts +0 -38
  38. package/lib/types/decorate.d.ts +0 -38
  39. package/lib/types/errors.d.ts +0 -10
  40. package/lib/types/types.d.ts +0 -66
  41. package/lib/types/validation.d.ts +0 -6
  42. package/{lib/esm → dist}/decorate.d.ts +16 -16
  43. /package/{lib/esm → dist}/errors.d.ts +0 -0
@@ -1,20 +1,33 @@
1
- import Client, { Batch, ClientBuilderFileOptions, ClientBuilderMessageOptions, ClientBuilderOptions, ClientListenerOptions, ClientOptions, FileBatch, Connection, Message, OutboundHandler } from "node-hl7-client";
1
+ import Client, { Batch, ClientBuilderFileOptions, ClientBuilderMessageOptions, ClientBuilderOptions, ClientListenerOptions, ClientOptions, Connection, createHL7Date, FileBatch, HL7_2_1, HL7_2_2, HL7_2_3, HL7_2_3_1, HL7_2_4, HL7_2_5, HL7_2_5_1, HL7_2_6, HL7_2_7, HL7_2_7_1, HL7_2_8, HL7Version, Message, OutboundHandler } from "node-hl7-client";
2
+ /** Accepted HL7 date-length values, derived from node-hl7-client's
3
+ * `createHL7Date` so it stays in sync with the upstream type. */
4
+ export type DateLength = Parameters<typeof createHL7Date>[1];
5
+ /** Maps an `HL7Version` string to the matching node-hl7-client builder class,
6
+ * so `createBuilder("2.7")` returns a fully typed `HL7_2_7`. */
7
+ export interface HL7VersionBuilders {
8
+ "2.1": HL7_2_1;
9
+ "2.2": HL7_2_2;
10
+ "2.3": HL7_2_3;
11
+ "2.3.1": HL7_2_3_1;
12
+ "2.4": HL7_2_4;
13
+ "2.5": HL7_2_5;
14
+ "2.5.1": HL7_2_5_1;
15
+ "2.6": HL7_2_6;
16
+ "2.7": HL7_2_7;
17
+ "2.7.1": HL7_2_7_1;
18
+ "2.8": HL7_2_8;
19
+ }
2
20
  export declare class HL7Client {
3
21
  /** @internal */
4
22
  private readonly _clientConnections;
5
23
  constructor();
6
- /**
7
- * Close All Connections for this Client
8
- * @since 1.0.0
9
- */
10
- closeAll(): Promise<boolean>;
11
24
  /**
12
25
  * Build a HL7 Batch
13
26
  * @remarks Create a properly formatted HL7 Batch.
14
27
  * @since 1.0.0
15
28
  * @param props
16
29
  */
17
- buildBatch(props?: ClientBuilderOptions): Batch;
30
+ buildBatch(properties?: ClientBuilderOptions): Batch;
18
31
  /**
19
32
  * Build Date
20
33
  * @remarks Build a date string based off HL7 Standards
@@ -22,27 +35,50 @@ export declare class HL7Client {
22
35
  * @param date
23
36
  * @param length Options are 8, 12, or 14 (default)
24
37
  */
25
- buildDate(date: Date, length?: string): string;
38
+ buildDate(date: Date, length?: DateLength): string;
26
39
  /**
27
40
  * Build a HL7 File Batch
28
41
  * @remarks Create a properly formatted HL7 File Batch.
29
42
  * @since 1.0.0
30
43
  * @param props
31
44
  */
32
- buildFileBatch(props?: ClientBuilderFileOptions): FileBatch;
45
+ buildFileBatch(properties?: ClientBuilderFileOptions): FileBatch;
33
46
  /**
34
47
  * Build a HL7 Message
35
48
  * @remarks Create a properly formatted HL7 message.
36
49
  * @since 1.0.0
37
50
  * @param props
38
51
  */
39
- buildMessage(props?: ClientBuilderMessageOptions): Message;
52
+ buildMessage(properties?: ClientBuilderMessageOptions): Message;
53
+ /**
54
+ * Close All Connections for this Client
55
+ * @since 1.0.0
56
+ */
57
+ closeAll(): Promise<boolean>;
58
+ /**
59
+ * Create a version-pinned HL7 message builder
60
+ * @remarks Returns the node-hl7-client builder for the given HL7 version, with
61
+ * per-version field validation (withdrawn fields throw, backward-compatibility
62
+ * fields warn, segments not in that version are rejected). Chain `build*`
63
+ * methods and finish with `toMessage()`.
64
+ * @since 4.0.0
65
+ * @param version One of the supported HL7 versions ("2.1" - "2.8").
66
+ * @param properties Optional builder options (date format, separators, hardError).
67
+ * @example
68
+ * ```ts
69
+ * const message = fastify.hl7.createBuilder("2.7")
70
+ * .buildMSH({ msh_9_1: "ADT", msh_9_2: "A01", msh_11_1: "P" })
71
+ * .buildPID({ pid_3: "MRN12345", pid_5: "DOE^JANE^A" })
72
+ * .toMessage();
73
+ * ```
74
+ */
75
+ createBuilder<V extends HL7Version>(version: V, properties?: ClientBuilderOptions): HL7VersionBuilders[V];
40
76
  /**
41
77
  *
42
78
  * @param name
43
79
  * @param props
44
80
  */
45
- createClient(name: string, props: ClientOptions): Client;
81
+ createClient(name: string, properties: ClientOptions): Client;
46
82
  /**
47
83
  * Create an HL7 Outbound Connection
48
84
  * @remarks Connect to an HL7 Server/Broker
@@ -51,7 +87,7 @@ export declare class HL7Client {
51
87
  * @param props
52
88
  * @param handler
53
89
  */
54
- createConnection(name: string, props: ClientListenerOptions, handler: OutboundHandler): Connection;
90
+ createConnection(name: string, properties: ClientListenerOptions, handler: OutboundHandler): Connection;
55
91
  /**
56
92
  * Get Client by the name
57
93
  * @since 1.0.0
@@ -70,7 +106,7 @@ export declare class HL7Client {
70
106
  * @since 1.0.0
71
107
  * @param text Raw HL& String
72
108
  */
73
- processHL7(text: string): Message | Batch;
109
+ processHL7(text: string): Batch | Message;
74
110
  /**
75
111
  * Read File
76
112
  * @remarks Pass the correct path of the file you want to read.
@@ -1,5 +1,5 @@
1
- import { EventEmitter } from "node:events";
2
1
  import Server, { Inbound, InboundHandler, ListenerOptions } from "node-hl7-server";
2
+ import { EventEmitter } from "node:events";
3
3
  export declare class HL7Server extends EventEmitter {
4
4
  private readonly _server;
5
5
  private readonly _serverInboundConnections;
@@ -22,7 +22,7 @@ export declare class HL7Server extends EventEmitter {
22
22
  * @param props
23
23
  * @param handler
24
24
  */
25
- createInbound(name: string, props: ListenerOptions, handler: InboundHandler): Inbound;
25
+ createInbound(name: string, properties: ListenerOptions, handler: InboundHandler): Inbound;
26
26
  /**
27
27
  * Get Server Inbound Connection by Name
28
28
  * @since 1.0.0
package/dist/index.cjs ADDED
@@ -0,0 +1,403 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ let fastify_plugin = require("fastify-plugin");
24
+ fastify_plugin = __toESM(fastify_plugin, 1);
25
+ let node_hl7_server = require("node-hl7-server");
26
+ node_hl7_server = __toESM(node_hl7_server, 1);
27
+ let node_hl7_client = require("node-hl7-client");
28
+ node_hl7_client = __toESM(node_hl7_client, 1);
29
+ let _fastify_error = require("@fastify/error");
30
+ _fastify_error = __toESM(_fastify_error, 1);
31
+ let node_events = require("node:events");
32
+ //#region src/errors.ts
33
+ const errors = {
34
+ /** Error if there is an setup error of the plugin itself. */
35
+ FASTIFY_HL7_ERR_SETUP_ERRORS: (0, _fastify_error.default)("FASTIFY_HL7_ERR_SETUP_ERRORS", "Setup error: %s"),
36
+ /** If an invalid usage error was done, this error would pop up. */
37
+ FASTIFY_HL7_ERR_USAGE: (0, _fastify_error.default)("FASTIFY_HL7_ERR_USAGE", "Usage error: %s")
38
+ };
39
+ //#endregion
40
+ //#region src/class/hL7Client.ts
41
+ /** Runtime version -> builder-class lookup backing `createBuilder`. */
42
+ const HL7_BUILDERS = {
43
+ "2.1": node_hl7_client.HL7_2_1,
44
+ "2.2": node_hl7_client.HL7_2_2,
45
+ "2.3": node_hl7_client.HL7_2_3,
46
+ "2.3.1": node_hl7_client.HL7_2_3_1,
47
+ "2.4": node_hl7_client.HL7_2_4,
48
+ "2.5": node_hl7_client.HL7_2_5,
49
+ "2.5.1": node_hl7_client.HL7_2_5_1,
50
+ "2.6": node_hl7_client.HL7_2_6,
51
+ "2.7": node_hl7_client.HL7_2_7,
52
+ "2.7.1": node_hl7_client.HL7_2_7_1,
53
+ "2.8": node_hl7_client.HL7_2_8
54
+ };
55
+ var HL7Client = class {
56
+ /** @internal */
57
+ _clientConnections;
58
+ constructor() {
59
+ this._clientConnections = [];
60
+ }
61
+ /**
62
+ * Build a HL7 Batch
63
+ * @remarks Create a properly formatted HL7 Batch.
64
+ * @since 1.0.0
65
+ * @param props
66
+ */
67
+ buildBatch(properties) {
68
+ return new node_hl7_client.Batch({ ...properties });
69
+ }
70
+ /**
71
+ * Build Date
72
+ * @remarks Build a date string based off HL7 Standards
73
+ * @since 2.1.0
74
+ * @param date
75
+ * @param length Options are 8, 12, or 14 (default)
76
+ */
77
+ buildDate(date, length) {
78
+ return (0, node_hl7_client.createHL7Date)(date, length);
79
+ }
80
+ /**
81
+ * Build a HL7 File Batch
82
+ * @remarks Create a properly formatted HL7 File Batch.
83
+ * @since 1.0.0
84
+ * @param props
85
+ */
86
+ buildFileBatch(properties) {
87
+ if (properties !== void 0 && (properties.fullFilePath !== void 0 || properties.fileBuffer !== void 0)) throw new errors.FASTIFY_HL7_ERR_USAGE("Use readFile or readFileBuffer method. This is for building.");
88
+ return new node_hl7_client.FileBatch({ ...properties });
89
+ }
90
+ /**
91
+ * Build a HL7 Message
92
+ * @remarks Create a properly formatted HL7 message.
93
+ * @since 1.0.0
94
+ * @param props
95
+ */
96
+ buildMessage(properties) {
97
+ if (properties !== void 0 && properties.text !== void 0) throw new errors.FASTIFY_HL7_ERR_USAGE("Use processMessage method. This is for building.");
98
+ return new node_hl7_client.Message({ ...properties });
99
+ }
100
+ /**
101
+ * Close All Connections for this Client
102
+ * @since 1.0.0
103
+ */
104
+ async closeAll() {
105
+ for (const outbound of this._clientConnections) outbound.ports.map(async (port) => {
106
+ await port.connection.close();
107
+ });
108
+ return true;
109
+ }
110
+ /**
111
+ * Create a version-pinned HL7 message builder
112
+ * @remarks Returns the node-hl7-client builder for the given HL7 version, with
113
+ * per-version field validation (withdrawn fields throw, backward-compatibility
114
+ * fields warn, segments not in that version are rejected). Chain `build*`
115
+ * methods and finish with `toMessage()`.
116
+ * @since 4.0.0
117
+ * @param version One of the supported HL7 versions ("2.1" - "2.8").
118
+ * @param properties Optional builder options (date format, separators, hardError).
119
+ * @example
120
+ * ```ts
121
+ * const message = fastify.hl7.createBuilder("2.7")
122
+ * .buildMSH({ msh_9_1: "ADT", msh_9_2: "A01", msh_11_1: "P" })
123
+ * .buildPID({ pid_3: "MRN12345", pid_5: "DOE^JANE^A" })
124
+ * .toMessage();
125
+ * ```
126
+ */
127
+ createBuilder(version, properties) {
128
+ const Builder = HL7_BUILDERS[version];
129
+ return new Builder(properties);
130
+ }
131
+ /**
132
+ *
133
+ * @param name
134
+ * @param props
135
+ */
136
+ createClient(name, properties) {
137
+ if (/[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/.test(name)) throw new errors.FASTIFY_HL7_ERR_USAGE("name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~.");
138
+ for (const connections of this._clientConnections) if (connections.name === name) throw new errors.FASTIFY_HL7_ERR_USAGE("name must be unique.");
139
+ const client = new node_hl7_client.default(properties);
140
+ this._clientConnections.push({
141
+ client,
142
+ name,
143
+ ports: []
144
+ });
145
+ return client;
146
+ }
147
+ /**
148
+ * Create an HL7 Outbound Connection
149
+ * @remarks Connect to an HL7 Server/Broker
150
+ * @since 1.0.0
151
+ * @param name The name stored within created client connections.
152
+ * @param props
153
+ * @param handler
154
+ */
155
+ createConnection(name, properties, handler) {
156
+ if (/[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/.test(name)) throw new errors.FASTIFY_HL7_ERR_USAGE("name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~.");
157
+ const getConnection = this._clientConnections.find((client) => client.name === name);
158
+ if (getConnection !== void 0) {
159
+ for (const outbound of getConnection.ports) if (outbound.port === properties.port.toString()) throw new errors.FASTIFY_HL7_ERR_USAGE(`port ${properties.port} is already used with this client. Choose a new outgoing port.`);
160
+ const outbound = new node_hl7_client.Connection(getConnection.client, properties, handler);
161
+ getConnection.ports.push({
162
+ connection: outbound,
163
+ port: properties.port.toString()
164
+ });
165
+ return outbound;
166
+ }
167
+ throw new errors.FASTIFY_HL7_ERR_USAGE("No valid client. Improper setup of a outbound connection.");
168
+ }
169
+ /**
170
+ * Get Client by the name
171
+ * @since 1.0.0
172
+ * @param name
173
+ */
174
+ getClientByName(name) {
175
+ const connection = this._clientConnections.find((connection) => connection.name === name);
176
+ if (connection !== void 0) return connection.client;
177
+ }
178
+ /**
179
+ * Get Connection by Port
180
+ * @since 1.0.0
181
+ * @param port
182
+ */
183
+ getClientConnectionByPort(port) {
184
+ let connection;
185
+ for (const outbound of this._clientConnections) for (const aPort of outbound.ports) if (aPort.port === port) connection = aPort.connection;
186
+ return connection;
187
+ }
188
+ /**
189
+ * Process a HL7
190
+ * @remarks A HL7 message that could either be a Message (MSH) or Batch (BHS)
191
+ * @since 1.0.0
192
+ * @param text Raw HL& String
193
+ */
194
+ processHL7(text) {
195
+ return (0, node_hl7_client.isBatch)(text) ? new node_hl7_client.Batch({ text }) : new node_hl7_client.Message({ text });
196
+ }
197
+ /**
198
+ * Read File
199
+ * @remarks Pass the correct path of the file you want to read.
200
+ * @since 1.0.0
201
+ * @param fullFilePath
202
+ * @returns FileBatch
203
+ * @example
204
+ * ```ts
205
+ * fastify.hl7.readFile( path.join('temp/', 'hl7.readTestBHS.20231208.hl7') )
206
+ * ```
207
+ */
208
+ readFile(fullFilePath) {
209
+ return new node_hl7_client.FileBatch({ fullFilePath });
210
+ }
211
+ /**
212
+ * Read a File Buffer
213
+ * @remarks Translate an already Buffered HL7 FHS segment to decode it.
214
+ * @since 1.0.0
215
+ * @param fileBuffer
216
+ * @returns FileBatch
217
+ * @example
218
+ * ```ts
219
+ * const fileBuffer = fs.readFileSync(path.join('temp/', 'hl7.readTestBHS.20231208.hl7'))
220
+ * fastify.hl7.readFileBuffer(fileBuffer)
221
+ * ```
222
+ */
223
+ readFileBuffer(fileBuffer) {
224
+ return new node_hl7_client.FileBatch({ fileBuffer });
225
+ }
226
+ };
227
+ //#endregion
228
+ //#region src/class/hL7Server.ts
229
+ var HL7Server = class extends node_events.EventEmitter {
230
+ _server;
231
+ _serverInboundConnections;
232
+ constructor(server) {
233
+ super();
234
+ this._server = server;
235
+ this._serverInboundConnections = [];
236
+ }
237
+ /**
238
+ * Close Inbound connection.
239
+ * @since 1.0.0
240
+ * @param port
241
+ */
242
+ async close(port) {
243
+ const inbound = this._serverInboundConnections.find((server) => server.port === port);
244
+ if (inbound !== void 0) return await inbound.server.close();
245
+ throw new errors.FASTIFY_HL7_ERR_USAGE(`No inbound server listening on port: ${port}`);
246
+ }
247
+ /**
248
+ * Close all Inbound connections.
249
+ * @since 1.0.0
250
+ */
251
+ async closeAll() {
252
+ this._serverInboundConnections.map(async (inbound) => {
253
+ await inbound.server.close();
254
+ });
255
+ return true;
256
+ }
257
+ /**
258
+ * Create Inbound connection.
259
+ * @since 1.0.0
260
+ * @param name
261
+ * @param props
262
+ * @param handler
263
+ */
264
+ createInbound(name, properties, handler) {
265
+ if (/[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/.test(name)) throw new errors.FASTIFY_HL7_ERR_USAGE("name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~.");
266
+ const inbound = new node_hl7_server.Inbound(this._server, properties, handler);
267
+ this._serverInboundConnections.push({
268
+ name,
269
+ port: properties.port.toString(),
270
+ server: inbound
271
+ });
272
+ this.emit("inbound", properties.port.toString());
273
+ return inbound;
274
+ }
275
+ /**
276
+ * Get Server Inbound Connection by Name
277
+ * @since 1.0.0
278
+ * @param name
279
+ */
280
+ getServerByName(name) {
281
+ const inbound = this._serverInboundConnections.find((inbound) => inbound.name === name);
282
+ if (inbound !== void 0) return inbound.server;
283
+ }
284
+ /**
285
+ * Get Server Inbound Connection by Port
286
+ * @since 1.0.0
287
+ * @param port
288
+ */
289
+ getServerByPort(port) {
290
+ const inbound = this._serverInboundConnections.find((inbound) => inbound.port === port);
291
+ if (inbound !== void 0) return inbound.server;
292
+ }
293
+ };
294
+ //#endregion
295
+ //#region src/validation.ts
296
+ /**
297
+ * @since 1.0.0
298
+ * @param opts
299
+ */
300
+ const validateOpts = async (options) => {
301
+ if (options.enableServer === void 0) options.enableServer = true;
302
+ return options;
303
+ };
304
+ //#endregion
305
+ //#region src/index.ts
306
+ /**
307
+ * @since 1.0.0
308
+ * @param fastify
309
+ * @param _opts
310
+ * @param connection
311
+ */
312
+ const decorateFastifyInstance = (fastify, _options, connection) => {
313
+ if (fastify.hl7 !== void 0) throw new errors.FASTIFY_HL7_ERR_SETUP_ERRORS("Already registered.");
314
+ if (fastify.hl7 === void 0) {
315
+ fastify.log.trace("[fastify-hl7] Decorate Fastify");
316
+ fastify.decorate("hl7", connection);
317
+ }
318
+ };
319
+ const fastifyHL7 = (0, fastify_plugin.default)(async (fastify, options) => {
320
+ const generatedOptions = await validateOpts(options);
321
+ options = {
322
+ ...options,
323
+ ...generatedOptions
324
+ };
325
+ const serverInstance = options.enableServer !== void 0 && options.enableServer ? new node_hl7_server.default(options.serverOptions) : void 0;
326
+ let server;
327
+ if (serverInstance !== void 0) {
328
+ server = new HL7Server(serverInstance);
329
+ server.on("inbound", (port) => {
330
+ fastify.log.info("HL7 Inbound Server Listening on Port %s", port);
331
+ });
332
+ fastify.addHook("preClose", async () => {
333
+ if (server !== void 0) await server.closeAll();
334
+ });
335
+ }
336
+ const client = new HL7Client();
337
+ fastify.addHook("preClose", async () => {
338
+ if (client !== void 0) await client.closeAll();
339
+ });
340
+ decorateFastifyInstance(fastify, options, {
341
+ _serverInstance: serverInstance,
342
+ buildBatch: function(properties) {
343
+ return client.buildBatch(properties);
344
+ },
345
+ buildDate: function(date, length) {
346
+ return client.buildDate(date, length);
347
+ },
348
+ buildFileBatch: function(properties) {
349
+ return client.buildFileBatch(properties);
350
+ },
351
+ buildMessage: function(properties) {
352
+ return client.buildMessage(properties);
353
+ },
354
+ closeServer: async function(port) {
355
+ if (server !== void 0) return await server.close(port);
356
+ throw new errors.FASTIFY_HL7_ERR_USAGE("server was not started. re-register plugin with enableServer set to true.");
357
+ },
358
+ closeServerAll: async () => {
359
+ if (server !== void 0) return await server.closeAll();
360
+ throw new errors.FASTIFY_HL7_ERR_USAGE("server was not started. re-register plugin with enableServer set to true.");
361
+ },
362
+ createBuilder: function(version, properties) {
363
+ return client.createBuilder(version, properties);
364
+ },
365
+ createClient: function(name, properties) {
366
+ return client.createClient(name, properties);
367
+ },
368
+ createConnection: function(name, properties, handler) {
369
+ return client.createConnection(name, properties, handler);
370
+ },
371
+ createInbound: function(name, properties, handler) {
372
+ if (server !== void 0) return server.createInbound(name, properties, handler);
373
+ throw new errors.FASTIFY_HL7_ERR_USAGE("server was not started. re-register plugin with enableServer set to true.");
374
+ },
375
+ getClientByName: function(name) {
376
+ return client.getClientByName(name);
377
+ },
378
+ getClientConnectionByPort: function(port) {
379
+ return client.getClientConnectionByPort(port);
380
+ },
381
+ getServerByName: function(name) {
382
+ if (server !== void 0) return server.getServerByName(name);
383
+ throw new errors.FASTIFY_HL7_ERR_USAGE("server was not started. re-register plugin with enableServer set to true.");
384
+ },
385
+ getServerByPort: function(port) {
386
+ if (server !== void 0) return server.getServerByPort(port);
387
+ throw new errors.FASTIFY_HL7_ERR_USAGE("server was not started. re-register plugin with enableServer set to true.");
388
+ },
389
+ processHL7: function(text) {
390
+ return client.processHL7(text);
391
+ },
392
+ readFile: function(fullFilePath) {
393
+ return client.readFile(fullFilePath);
394
+ },
395
+ readFileBuffer: function(fileBuffer) {
396
+ return client.readFileBuffer(fileBuffer);
397
+ }
398
+ });
399
+ });
400
+ //#endregion
401
+ module.exports = fastifyHL7;
402
+
403
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["HL7_2_1","HL7_2_2","HL7_2_3","HL7_2_3_1","HL7_2_4","HL7_2_5","HL7_2_5_1","HL7_2_6","HL7_2_7","HL7_2_7_1","HL7_2_8","Batch","FileBatch","Message","Client","Connection","EventEmitter","Inbound","validateOptions","Server"],"sources":["../src/errors.ts","../src/class/hL7Client.ts","../src/class/hL7Server.ts","../src/validation.ts","../src/index.ts"],"sourcesContent":["/*\nMIT License\n\nCopyright (c) 2026 Shane Froebel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\nimport createError from \"@fastify/error\";\n\nexport const errors = {\n /** Error if there is an setup error of the plugin itself. */\n FASTIFY_HL7_ERR_SETUP_ERRORS: createError(\n \"FASTIFY_HL7_ERR_SETUP_ERRORS\",\n \"Setup error: %s\",\n ),\n /** If an invalid usage error was done, this error would pop up. */\n FASTIFY_HL7_ERR_USAGE: createError(\n \"FASTIFY_HL7_ERR_USAGE\",\n \"Usage error: %s\",\n ),\n};\n","/*\nMIT License\n\nCopyright (c) 2026 Shane Froebel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\nimport Client, {\n Batch,\n ClientBuilderFileOptions,\n ClientBuilderMessageOptions,\n ClientBuilderOptions,\n ClientListenerOptions,\n ClientOptions,\n Connection,\n createHL7Date,\n FileBatch,\n HL7_2_1,\n HL7_2_2,\n HL7_2_3,\n HL7_2_3_1,\n HL7_2_4,\n HL7_2_5,\n HL7_2_5_1,\n HL7_2_6,\n HL7_2_7,\n HL7_2_7_1,\n HL7_2_8,\n HL7Version,\n isBatch,\n Message,\n OutboundHandler,\n} from \"node-hl7-client\";\n\nimport { AClients } from \"../decorate.js\";\nimport { errors } from \"../errors.js\";\n\n/** Accepted HL7 date-length values, derived from node-hl7-client's\n * `createHL7Date` so it stays in sync with the upstream type. */\nexport type DateLength = Parameters<typeof createHL7Date>[1];\n\n/** Maps an `HL7Version` string to the matching node-hl7-client builder class,\n * so `createBuilder(\"2.7\")` returns a fully typed `HL7_2_7`. */\nexport interface HL7VersionBuilders {\n \"2.1\": HL7_2_1;\n \"2.2\": HL7_2_2;\n \"2.3\": HL7_2_3;\n \"2.3.1\": HL7_2_3_1;\n \"2.4\": HL7_2_4;\n \"2.5\": HL7_2_5;\n \"2.5.1\": HL7_2_5_1;\n \"2.6\": HL7_2_6;\n \"2.7\": HL7_2_7;\n \"2.7.1\": HL7_2_7_1;\n \"2.8\": HL7_2_8;\n}\n\n/** Runtime version -> builder-class lookup backing `createBuilder`. */\nconst HL7_BUILDERS = {\n \"2.1\": HL7_2_1,\n \"2.2\": HL7_2_2,\n \"2.3\": HL7_2_3,\n \"2.3.1\": HL7_2_3_1,\n \"2.4\": HL7_2_4,\n \"2.5\": HL7_2_5,\n \"2.5.1\": HL7_2_5_1,\n \"2.6\": HL7_2_6,\n \"2.7\": HL7_2_7,\n \"2.7.1\": HL7_2_7_1,\n \"2.8\": HL7_2_8,\n} as const;\n\nexport class HL7Client {\n /** @internal */\n private readonly _clientConnections: AClients[];\n\n constructor() {\n this._clientConnections = [];\n }\n\n /**\n * Build a HL7 Batch\n * @remarks Create a properly formatted HL7 Batch.\n * @since 1.0.0\n * @param props\n */\n buildBatch(properties?: ClientBuilderOptions): Batch {\n return new Batch({ ...properties });\n }\n\n /**\n * Build Date\n * @remarks Build a date string based off HL7 Standards\n * @since 2.1.0\n * @param date\n * @param length Options are 8, 12, or 14 (default)\n */\n buildDate(date: Date, length?: DateLength): string {\n return createHL7Date(date, length);\n }\n\n /**\n * Build a HL7 File Batch\n * @remarks Create a properly formatted HL7 File Batch.\n * @since 1.0.0\n * @param props\n */\n buildFileBatch(properties?: ClientBuilderFileOptions): FileBatch {\n if (\n properties !== undefined &&\n (properties.fullFilePath !== undefined ||\n properties.fileBuffer !== undefined)\n ) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"Use readFile or readFileBuffer method. This is for building.\",\n );\n }\n return new FileBatch({ ...properties });\n }\n\n /**\n * Build a HL7 Message\n * @remarks Create a properly formatted HL7 message.\n * @since 1.0.0\n * @param props\n */\n buildMessage(properties?: ClientBuilderMessageOptions): Message {\n if (properties !== undefined && properties.text !== undefined) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"Use processMessage method. This is for building.\",\n );\n }\n return new Message({ ...properties });\n }\n\n /**\n * Close All Connections for this Client\n * @since 1.0.0\n */\n async closeAll(): Promise<boolean> {\n for (const outbound of this._clientConnections) {\n outbound.ports.map(async (port) => {\n await port.connection.close();\n });\n }\n return true;\n }\n\n /**\n * Create a version-pinned HL7 message builder\n * @remarks Returns the node-hl7-client builder for the given HL7 version, with\n * per-version field validation (withdrawn fields throw, backward-compatibility\n * fields warn, segments not in that version are rejected). Chain `build*`\n * methods and finish with `toMessage()`.\n * @since 4.0.0\n * @param version One of the supported HL7 versions (\"2.1\" - \"2.8\").\n * @param properties Optional builder options (date format, separators, hardError).\n * @example\n * ```ts\n * const message = fastify.hl7.createBuilder(\"2.7\")\n * .buildMSH({ msh_9_1: \"ADT\", msh_9_2: \"A01\", msh_11_1: \"P\" })\n * .buildPID({ pid_3: \"MRN12345\", pid_5: \"DOE^JANE^A\" })\n * .toMessage();\n * ```\n */\n createBuilder<V extends HL7Version>(\n version: V,\n properties?: ClientBuilderOptions,\n ): HL7VersionBuilders[V] {\n const Builder = HL7_BUILDERS[version] as new (\n properties?: ClientBuilderOptions,\n ) => HL7VersionBuilders[V];\n return new Builder(properties);\n }\n\n /**\n *\n * @param name\n * @param props\n */\n createClient(name: string, properties: ClientOptions): Client {\n const nameFormat = /[ `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~]/;\n if (nameFormat.test(name)) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"name must not contain certain characters: `!@#$%^&*()+\\\\-=\\\\[\\\\]{};':\\\"\\\\\\\\|,.<>\\\\/?~.\",\n );\n }\n\n // make sure that this does not exist already...\n for (const connections of this._clientConnections) {\n if (connections.name === name) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\"name must be unique.\");\n }\n // nto in the Client class yet\n // if (client.getHost() === props.host) {\n // throw new errors.FASTIFY_HL7_ERR_USAGE(`host is already a pointer. Name is: ${connections.name}`)\n // }\n }\n\n // new client\n const client = new Client(properties);\n\n // add it to the collection\n this._clientConnections.push({\n client,\n name,\n ports: [],\n });\n\n return client;\n }\n\n /**\n * Create an HL7 Outbound Connection\n * @remarks Connect to an HL7 Server/Broker\n * @since 1.0.0\n * @param name The name stored within created client connections.\n * @param props\n * @param handler\n */\n createConnection(\n name: string,\n properties: ClientListenerOptions,\n handler: OutboundHandler,\n ): Connection {\n const nameFormat = /[ `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~]/;\n if (nameFormat.test(name)) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"name must not contain certain characters: `!@#$%^&*()+\\\\-=\\\\[\\\\]{};':\\\"\\\\\\\\|,.<>\\\\/?~.\",\n );\n }\n\n const getConnection = this._clientConnections.find(\n (client) => client.name === name,\n );\n\n if (getConnection !== undefined) {\n // make sure port is not used all ready\n for (const outbound of getConnection.ports) {\n if (outbound.port === properties.port.toString()) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n `port ${properties.port} is already used with this client. Choose a new outgoing port.`,\n );\n }\n }\n\n // create outbound port to the server in getConnection.client\n const outbound = new Connection(\n getConnection.client,\n properties,\n handler,\n );\n\n // add it to the array of known ports. need to know this, so we can get it later if needed.\n getConnection.ports.push({\n connection: outbound,\n port: properties.port.toString(),\n });\n\n // return it right away. the user might do something with it.\n return outbound;\n }\n\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"No valid client. Improper setup of a outbound connection.\",\n );\n }\n\n /**\n * Get Client by the name\n * @since 1.0.0\n * @param name\n */\n getClientByName(name: string): Client | undefined {\n const connection = this._clientConnections.find(\n (connection) => connection.name === name,\n );\n if (connection !== undefined) {\n return connection.client;\n }\n return undefined;\n }\n\n /**\n * Get Connection by Port\n * @since 1.0.0\n * @param port\n */\n getClientConnectionByPort(port: string): Connection | undefined {\n let connection: Connection | undefined;\n for (const outbound of this._clientConnections) {\n for (const aPort of outbound.ports) {\n if (aPort.port === port) {\n connection = aPort.connection;\n }\n }\n }\n return connection;\n }\n\n /**\n * Process a HL7\n * @remarks A HL7 message that could either be a Message (MSH) or Batch (BHS)\n * @since 1.0.0\n * @param text Raw HL& String\n */\n processHL7(text: string): Batch | Message {\n return isBatch(text) ? new Batch({ text }) : new Message({ text });\n }\n\n /**\n * Read File\n * @remarks Pass the correct path of the file you want to read.\n * @since 1.0.0\n * @param fullFilePath\n * @returns FileBatch\n * @example\n * ```ts\n * fastify.hl7.readFile( path.join('temp/', 'hl7.readTestBHS.20231208.hl7') )\n * ```\n */\n readFile(fullFilePath: string): FileBatch {\n return new FileBatch({ fullFilePath });\n }\n\n /**\n * Read a File Buffer\n * @remarks Translate an already Buffered HL7 FHS segment to decode it.\n * @since 1.0.0\n * @param fileBuffer\n * @returns FileBatch\n * @example\n * ```ts\n * const fileBuffer = fs.readFileSync(path.join('temp/', 'hl7.readTestBHS.20231208.hl7'))\n * fastify.hl7.readFileBuffer(fileBuffer)\n * ```\n */\n readFileBuffer(fileBuffer: Buffer): FileBatch {\n return new FileBatch({ fileBuffer });\n }\n}\n","/*\nMIT License\n\nCopyright (c) 2026 Shane Froebel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\nimport Server, {\n Inbound,\n InboundHandler,\n ListenerOptions,\n} from \"node-hl7-server\";\nimport { EventEmitter } from \"node:events\";\n\nimport { AServers } from \"../decorate.js\";\nimport { errors } from \"../errors.js\";\n\nexport class HL7Server extends EventEmitter {\n private readonly _server: Server;\n private readonly _serverInboundConnections: AServers[];\n\n constructor(server: Server) {\n super();\n this._server = server;\n this._serverInboundConnections = [];\n }\n\n /**\n * Close Inbound connection.\n * @since 1.0.0\n * @param port\n */\n async close(port: string): Promise<boolean> {\n const inbound = this._serverInboundConnections.find(\n (server) => server.port === port,\n );\n if (inbound !== undefined) {\n return await inbound.server.close(); // close the server for all inbound connections\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n `No inbound server listening on port: ${port}`,\n );\n }\n\n /**\n * Close all Inbound connections.\n * @since 1.0.0\n */\n async closeAll(): Promise<boolean> {\n this._serverInboundConnections.map(async (inbound) => {\n await inbound.server.close();\n });\n return true;\n }\n\n /**\n * Create Inbound connection.\n * @since 1.0.0\n * @param name\n * @param props\n * @param handler\n */\n createInbound(\n name: string,\n properties: ListenerOptions,\n handler: InboundHandler,\n ): Inbound {\n const nameFormat = /[ `!@#$%^&*()+\\-=\\[\\]{};':\"\\\\|,.<>\\/?~]/;\n if (nameFormat.test(name)) {\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"name must not contain certain characters: `!@#$%^&*()+\\\\-=\\\\[\\\\]{};':\\\"\\\\\\\\|,.<>\\\\/?~.\",\n );\n }\n\n const inbound = new Inbound(this._server, properties, handler);\n\n this._serverInboundConnections.push({\n name,\n port: properties.port.toString(),\n server: inbound,\n });\n\n this.emit(\"inbound\", properties.port.toString());\n\n return inbound;\n }\n\n /**\n * Get Server Inbound Connection by Name\n * @since 1.0.0\n * @param name\n */\n getServerByName(name: string): Inbound | undefined {\n const inbound = this._serverInboundConnections.find(\n (inbound) => inbound.name === name,\n );\n if (inbound !== undefined) {\n return inbound.server;\n }\n return undefined;\n }\n\n /**\n * Get Server Inbound Connection by Port\n * @since 1.0.0\n * @param port\n */\n getServerByPort(port: string): Inbound | undefined {\n const inbound = this._serverInboundConnections.find(\n (inbound) => inbound.port === port,\n );\n if (inbound !== undefined) {\n return inbound.server;\n }\n return undefined;\n }\n}\n","/*\nMIT License\n\nCopyright (c) 2026 Shane Froebel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\nimport { FastifyHL7Options } from \"./decorate.js\";\n\n/**\n * @since 1.0.0\n * @param opts\n */\nexport const validateOpts = async (\n options: FastifyHL7Options,\n): Promise<FastifyHL7Options> => {\n // Mandatory, Defaulted\n if (options.enableServer === undefined) {\n options.enableServer = true;\n }\n\n return options;\n};\n","/*\nMIT License\n\nCopyright (c) 2026 Shane Froebel\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\nimport { FastifyInstance, HL7 } from \"fastify\";\nimport fp from \"fastify-plugin\";\nimport Client, {\n Batch,\n ClientBuilderFileOptions,\n ClientBuilderMessageOptions,\n ClientBuilderOptions,\n Connection,\n FileBatch,\n HL7Version,\n Message,\n} from \"node-hl7-client\";\nimport Server, {\n Inbound,\n InboundHandler,\n ListenerOptions,\n} from \"node-hl7-server\";\n\nimport type { FastifyHL7Options } from \"./decorate.js\";\n\nimport {\n type DateLength,\n HL7Client,\n type HL7VersionBuilders,\n} from \"./class/hL7Client.js\";\nimport { HL7Server } from \"./class/hL7Server.js\";\nimport { errors } from \"./errors.js\";\nimport { validateOpts as validateOptions } from \"./validation.js\";\nexport { type FastifyHL7Options } from \"./decorate.js\";\n\n/**\n * @since 1.0.0\n * @param fastify\n * @param _opts\n * @param connection\n */\nconst decorateFastifyInstance = (\n fastify: FastifyInstance,\n _options: FastifyHL7Options,\n connection: HL7,\n): void => {\n if (fastify.hl7 !== undefined) {\n throw new errors.FASTIFY_HL7_ERR_SETUP_ERRORS(\"Already registered.\");\n }\n\n if (fastify.hl7 === undefined) {\n fastify.log.trace(\"[fastify-hl7] Decorate Fastify\");\n fastify.decorate(\"hl7\", connection);\n }\n};\n\nconst fastifyHL7 = fp<FastifyHL7Options>(async (fastify, options) => {\n const generatedOptions = await validateOptions(options);\n options = { ...options, ...generatedOptions };\n\n // Create server.\n // Since there can only be one server per IP address\n // (i.e., the host this is running on.) There can only be more than one.\n // A server can host many HL7 Inbound connections via different ports, this is fine.\n // Clients are different as an app could talk to different servers, on many ports.\n // So we need to do something different.\n const serverInstance =\n options.enableServer !== undefined && options.enableServer\n ? new Server(options.serverOptions)\n : undefined;\n\n // Server Functions\n let server: HL7Server | undefined;\n if (serverInstance !== undefined) {\n // Server Functions\n server = new HL7Server(serverInstance);\n\n server.on(\"inbound\", (port: string) => {\n fastify.log.info(\"HL7 Inbound Server Listening on Port %s\", port);\n });\n\n // before we close fastify, make sure all server instances are closed\n fastify.addHook(\"preClose\", async () => {\n if (server !== undefined) {\n await server.closeAll();\n }\n });\n }\n\n // Client Functions\n const client = new HL7Client();\n\n // run these before fastify closes\n fastify.addHook(\"preClose\", async () => {\n if (client !== undefined) {\n await client.closeAll();\n }\n });\n\n decorateFastifyInstance(fastify, options, {\n _serverInstance: serverInstance,\n buildBatch: function (properties: ClientBuilderOptions | undefined): Batch {\n return client.buildBatch(properties);\n },\n buildDate: function (date: Date, length?: DateLength): string {\n return client.buildDate(date, length);\n },\n buildFileBatch: function (\n properties: ClientBuilderFileOptions | undefined,\n ): FileBatch {\n return client.buildFileBatch(properties);\n },\n buildMessage: function (\n properties: ClientBuilderMessageOptions | undefined,\n ): Message {\n return client.buildMessage(properties);\n },\n closeServer: async function (port: string): Promise<boolean> {\n if (server !== undefined) {\n return await server.close(port);\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"server was not started. re-register plugin with enableServer set to true.\",\n );\n },\n closeServerAll: async (): Promise<boolean> => {\n if (server !== undefined) {\n return await server.closeAll();\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"server was not started. re-register plugin with enableServer set to true.\",\n );\n },\n createBuilder: function <V extends HL7Version>(\n version: V,\n properties?: ClientBuilderOptions,\n ): HL7VersionBuilders[V] {\n return client.createBuilder(version, properties);\n },\n createClient: function (name, properties): Client {\n return client.createClient(name, properties);\n },\n createConnection: function (name, properties, handler) {\n return client.createConnection(name, properties, handler);\n },\n createInbound: function (\n name: string,\n properties: ListenerOptions,\n handler: InboundHandler,\n ): Inbound {\n if (server !== undefined) {\n return server.createInbound(name, properties, handler);\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"server was not started. re-register plugin with enableServer set to true.\",\n );\n },\n getClientByName: function (name: string): Client | undefined {\n return client.getClientByName(name);\n },\n getClientConnectionByPort: function (port: string): Connection | undefined {\n return client.getClientConnectionByPort(port);\n },\n getServerByName: function (name: string): Inbound | undefined {\n if (server !== undefined) {\n return server.getServerByName(name);\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"server was not started. re-register plugin with enableServer set to true.\",\n );\n },\n getServerByPort: function (port: string): Inbound | undefined {\n if (server !== undefined) {\n return server.getServerByPort(port);\n }\n throw new errors.FASTIFY_HL7_ERR_USAGE(\n \"server was not started. re-register plugin with enableServer set to true.\",\n );\n },\n processHL7: function (text: string): Batch | Message {\n return client.processHL7(text);\n },\n readFile: function (fullFilePath: string): FileBatch {\n return client.readFile(fullFilePath);\n },\n readFileBuffer: function (fileBuffer: Buffer): FileBatch {\n return client.readFileBuffer(fileBuffer);\n },\n });\n});\n\nexport default fastifyHL7;\n\nexport * from \"./types.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,SAAS;;CAEpB,+BAAA,GAAA,eAAA,QAAA,CACE,gCACA,iBACF;;CAEA,wBAAA,GAAA,eAAA,QAAA,CACE,yBACA,iBACF;AACF;;;;ACsCA,MAAM,eAAe;CACnB,OAAOA,gBAAAA;CACP,OAAOC,gBAAAA;CACP,OAAOC,gBAAAA;CACP,SAASC,gBAAAA;CACT,OAAOC,gBAAAA;CACP,OAAOC,gBAAAA;C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,OAAO,MAAM,IAAI;GAEhC,MAAM,IAAI,OAAO,sBACf,2EACF;EACF;EACA,gBAAgB,YAA8B;GAC5C,IAAI,WAAW,KAAA,GACb,OAAO,MAAM,OAAO,SAAS;GAE/B,MAAM,IAAI,OAAO,sBACf,2EACF;EACF;EACA,eAAe,SACb,SACA,YACuB;GACvB,OAAO,OAAO,cAAc,SAAS,UAAU;EACjD;EACA,cAAc,SAAU,MAAM,YAAoB;GAChD,OAAO,OAAO,aAAa,MAAM,UAAU;EAC7C;EACA,kBAAkB,SAAU,MAAM,YAAY,SAAS;GACrD,OAAO,OAAO,iBAAiB,MAAM,YAAY,OAAO;EAC1D;EACA,eAAe,SACb,MACA,YACA,SACS;GACT,IAAI,WAAW,KAAA,GACb,OAAO,OAAO,cAAc,MAAM,YAAY,OAAO;GAEvD,MAAM,IAAI,OAAO,sBACf,2EACF;EACF;EACA,iBAAiB,SAAU,MAAkC;GAC3D,OAAO,OAAO,gBAAgB,IAAI;EACpC;EACA,2BAA2B,SAAU,MAAsC;GACzE,OAAO,OAAO,0BAA0B,IAAI;EAC9C;EACA,iBAAiB,SAAU,MAAmC;GAC5D,IAAI,WAAW,KAAA,GACb,OAAO,OAAO,gBAAgB,IAAI;GAEpC,MAAM,IAAI,OAAO,sBACf,2EACF;EACF;EACA,iBAAiB,SAAU,MAAmC;GAC5D,IAAI,WAAW,KAAA,GACb,OAAO,OAAO,gBAAgB,IAAI;GAEpC,MAAM,IAAI,OAAO,sBACf,2EACF;EACF;EACA,YAAY,SAAU,MAA+B;GACnD,OAAO,OAAO,WAAW,IAAI;EAC/B;EACA,UAAU,SAAU,cAAiC;GACnD,OAAO,OAAO,SAAS,YAAY;EACrC;EACA,gBAAgB,SAAU,YAA+B;GACvD,OAAO,OAAO,eAAe,UAAU;EACzC;CACF,CAAC;AACH,CAAC"}
@@ -1,5 +1,5 @@
1
- import { FastifyHL7Options } from "./decorate.js";
2
- export * from "./types.js";
1
+ import type { FastifyHL7Options } from "./decorate.js";
2
+ export { type FastifyHL7Options } from "./decorate.js";
3
3
  declare const fastifyHL7: import("fastify").FastifyPluginCallback<FastifyHL7Options, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
4
4
  export default fastifyHL7;
5
- export { FastifyHL7Options };
5
+ export * from "./types.js";