Skip to content

Commit

Permalink
feat: hl7 startup message (#83)
Browse files Browse the repository at this point in the history
* clean up unit tests
* hl7server extends EventEmitter
* 100% coverage
  • Loading branch information
Bugs5382 authored Dec 9, 2024
2 parents c6e0e43 + 2c286c3 commit 9009631
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 47 deletions.
1 change: 1 addition & 0 deletions .github/workflows/action-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- name: Publish to npm
uses: JS-DevTools/npm-publish@v3
with:
provenance: true
token: ${{ secrets.NPM_TOKEN }}
tag: ${{ steps.set-npm-tag.outputs.NPM_TAG }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/action-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:

- name: Pre-Run
run: |
npm install --package-lock-only
npm install --ignore-scripts
npm install --package-lock-only --force
npm install --ignore-scripts --force
- name: Run Tests and Lint
run: |
Expand Down
9 changes: 9 additions & 0 deletions __tests__/__utils__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ export async function expectEvent<T = any>(
emitter.once(name, resolve);
});
}

export function getCurrentDateYYYYMMDD() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are 0-based
const day = String(date.getDate()).padStart(2, "0");

return `${year}${month}${day}`;
}
9 changes: 7 additions & 2 deletions __tests__/hl7.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "path";
import { describe, expect, test, beforeEach, afterEach } from "vitest";
import fastifyHL7 from "../src";
import { errors } from "../src/errors";
import { getCurrentDateYYYYMMDD } from "./__utils__/utils.js";

let app: FastifyInstance;

Expand Down Expand Up @@ -233,11 +234,15 @@ describe("plugin fastify-hl7 tests", () => {

test("...getClientByName", async () => {
const client = app.hl7.createClient("adt", { host: "0.0.0.0" });

const clientPullName = app.hl7.getClientByName("adt");

expect(clientPullName).toEqual(client);
});

test("...buildDate", async () => {
expect(app.hl7.buildDate(new Date(), "8")).toEqual(
getCurrentDateYYYYMMDD(),
);
});
});

describe("...hl7", () => {
Expand Down
16 changes: 8 additions & 8 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginPrettier from 'eslint-plugin-prettier';
import pluginPrettier from "eslint-plugin-prettier";

export default [
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
files: ["**/*.{js,mjs,cjs,ts}"],
plugins: {
'prettier': pluginPrettier
}
prettier: pluginPrettier,
},
},
{
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off'
}
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
},
},
{
ignores: [".node_modules/*", "docs/*", "lib/*", "__tests__/*"]
}
ignores: [".node_modules/*", "docs/*", "lib/*", "__tests__/*"],
},
];
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build:watch": "tsc -p src/tsconfig.esm.json -w",
"build:watch:cjs": "tsc -p src/tsconfig.cjs.json -w",
"npm:lint": "npmPkgJsonLint .",
"format": "prettier --write 'README.md' 'src/**/*.ts' '__tests__/**/*.ts'",
"format": "prettier --write 'README.md' 'src/**/*.ts' '__tests__/**/*.ts' '*.{mts,mjs}'",
"lint": "npm run npm:lint && eslint | snazzy",
"lint:fix": "npm run npm:lint && eslint --fix | snazzy",
"pack": "npm pack",
Expand Down Expand Up @@ -65,16 +65,16 @@
"node-hl7-server": "^3.1.0"
},
"devDependencies": {
"@eslint/js": "^9.15.0",
"@eslint/js": "^9.16.0",
"@shipgirl/typedoc-plugin-versions": "^0.2.8",
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.9.0",
"@types/node": "^22.10.1",
"@types/tcp-port-used": "^1.0.4",
"@typescript-eslint/eslint-plugin": "^8.14.0",
"@typescript-eslint/parser": "^8.14.0",
"@vitest/coverage-v8": "^2.1.5",
"@vitest/ui": "^2.1.5",
"eslint": "^9.14.0",
"@typescript-eslint/eslint-plugin": "^8.17.0",
"@typescript-eslint/parser": "^8.17.0",
"@vitest/coverage-v8": "^2.1.8",
"@vitest/ui": "^2.1.8",
"eslint": "^9.16.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"fastify": "^5.1.0",
Expand All @@ -86,12 +86,13 @@
"tcp-port-used": "^1.0.2",
"ts-node": "^10.9.2",
"tsd": "^0.31.2",
"typedoc": "^0.26.11",
"typescript": "^5.6.3",
"typescript-eslint": "^8.14.0",
"vitest": "^2.1.5"
"typedoc": "^0.27.4",
"typescript": "^5.7.2",
"typescript-eslint": "^8.17.0",
"vitest": "^2.1.8"
},
"precommit": [
"format",
"lint:fix",
"build"
]
Expand Down
16 changes: 8 additions & 8 deletions src/class/hL7Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class HL7Client {

/**
* Build a HL7 Batch
*@remarks Create a properly formatted HL7 Batch.
* @remarks Create a properly formatted HL7 Batch.
* @since 1.0.0
* @param props
*/
Expand All @@ -53,7 +53,7 @@ export class HL7Client {

/**
* Build Date
*@remarks Build a date string based off HL7 Standards
* @remarks Build a date string based off HL7 Standards
* @since 2.1.0
* @param date
* @param length Options are 8, 12, or 14 (default)
Expand All @@ -64,7 +64,7 @@ export class HL7Client {

/**
* Build a HL7 File Batch
*@remarks Create a properly formatted HL7 File Batch.
* @remarks Create a properly formatted HL7 File Batch.
* @since 1.0.0
* @param props
*/
Expand All @@ -83,7 +83,7 @@ export class HL7Client {

/**
* Build a HL7 Message
*@remarks Create a properly formatted HL7 message.
* @remarks Create a properly formatted HL7 message.
* @since 1.0.0
* @param props
*/
Expand Down Expand Up @@ -135,7 +135,7 @@ export class HL7Client {

/**
* Create an HL7 Outbound Connection
*@remarks Connect to an HL7 Server/Broker
* @remarks Connect to an HL7 Server/Broker
* @since 1.0.0
* @param name The name stored within created client connections.
* @param props
Expand Down Expand Up @@ -219,7 +219,7 @@ export class HL7Client {

/**
* Process a HL7
*@remarks A HL7 message that could either be a Message (MSH) or Batch (BHS)
* @remarks A HL7 message that could either be a Message (MSH) or Batch (BHS)
* @since 1.0.0
* @param text Raw HL& String
*/
Expand All @@ -233,7 +233,7 @@ export class HL7Client {

/**
* Read File
*@remarks Pass the correct path of the file you want to read.
* @remarks Pass the correct path of the file you want to read.
* @since 1.0.0
* @param fullFilePath
* @returns FileBatch
Expand All @@ -248,7 +248,7 @@ export class HL7Client {

/**
* Read a File Buffer
*@remarks Translate an already Buffered HL7 FHS segment to decode it.
* @remarks Translate an already Buffered HL7 FHS segment to decode it.
* @since 1.0.0
* @param fileBuffer
* @returns FileBatch
Expand Down
7 changes: 6 additions & 1 deletion src/class/hL7Server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventEmitter } from "node:events";
import Server, {
Inbound,
InboundHandler,
Expand All @@ -6,11 +7,12 @@ import Server, {
import { AServers } from "../decorate.js";
import { errors } from "../errors.js";

export class HL7Server {
export class HL7Server extends EventEmitter {
private readonly _server: Server;
private readonly _serverInboundConnections: AServers[];

constructor(server: Server) {
super();
this._server = server;
this._serverInboundConnections = [];
}
Expand Down Expand Up @@ -69,6 +71,9 @@ export class HL7Server {
port: props.port.toString(),
server: inbound,
});

this.emit("inbound", props.port.toString());

return inbound;
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const fastifyHL7 = fp<FastifyHL7Options>(async (fastify, opts) => {
// Server Functions
server = new HL7Server(serverInstance);

server.on("inbound", (port: string) => {
fastify.log.info("HL7 Inbound Server Listening on Port %s", port);
});

// before we close fastify, make sure all server instances are closed
fastify.addHook("preClose", async () => {
if (typeof server !== "undefined") {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare module "fastify" {
* @since 1.0.0 */
closeServerAll: () => Promise<boolean>;
/** Create Client
*@remarks Connecting to a remote server/broker that accepts connections.
* @remarks Connecting to a remote server/broker that accepts connections.
* @since 1.0.0 */
createClient: (name: string, props: ClientOptions) => Client;
/** Create an incoming port connection on the server.
Expand All @@ -52,7 +52,7 @@ declare module "fastify" {
handler: InboundHandler,
) => Inbound;
/** Create Outgoing Client Port
*@remarks This is on the established client that we are already a part off.
* @remarks This is on the established client that we are already a part off.
* @since 1.0.0 */
createConnection: (
name: string,
Expand Down Expand Up @@ -84,7 +84,7 @@ declare module "fastify" {

export interface FastifyInstance {
/** Main Decorator for Fastify
*@remarks hl7 is the decorator that everything hangs off.
* @remarks hl7 is the decorator that everything hangs off.
* @since 1.0.0 **/
hl7: HL7;
}
Expand Down
25 changes: 14 additions & 11 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { defineConfig } from 'vitest/config'
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
provider: "v8",
exclude: [
'__tests__/__utils__/**',
'docker',
'bin',
'certs',
'docs',
'lib'
]
}
}
})
...configDefaults.exclude,
"__tests__/**/**",
"src/api.ts",
"docker",
"bin",
"certs",
"docs",
"lib",
],
},
},
});

0 comments on commit 9009631

Please sign in to comment.