-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routes): add hl7v2-to-json route
- Loading branch information
Showing
7 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const fp = require("fastify-plugin"); | ||
const hl7v2 = require("@redoxengine/redox-hl7-v2"); | ||
|
||
/** | ||
* @author Frazer Smith | ||
* @description Pre-handler plugin that uses redox-hl7-v2 to convert string containing | ||
* HL7 v2.x in `req.body` to JSON. | ||
* `req` object is decorated with `conversionResults.body` holding the converted document. | ||
* @param {object} server - Fastify instance. | ||
*/ | ||
async function plugin(server) { | ||
const parser = new hl7v2.Parser(); | ||
|
||
server.addHook("onRequest", async (req) => { | ||
req.conversionResults = { body: undefined }; | ||
}); | ||
|
||
server.addHook("preHandler", async (req) => { | ||
/** | ||
* `htmlToText` function still attempts to parse empty bodies/input or invalid HTML | ||
* and produces results, so catch them here | ||
*/ | ||
// if (req.body === undefined || Object.keys(req.body).length === 0) { | ||
// throw server.httpErrors.badRequest(); | ||
// } | ||
|
||
try { | ||
const results = parser.parse(req.body); | ||
req.conversionResults.body = results; | ||
} catch { | ||
/** | ||
* redox-hl7-v2 will throw if the HL7 v2 message provided | ||
* by client is malformed or invalid, thus client error code | ||
*/ | ||
throw server.httpErrors.badRequest(); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = fp(plugin, { | ||
fastify: "4.x", | ||
name: "hl7v2ToJson", | ||
dependencies: ["@fastify/sensible"], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Import plugins | ||
const cors = require("@fastify/cors"); | ||
const hl7v2ToJson = require("../../../plugins/hl7v2-to-json"); | ||
|
||
const { hl7v2ToJsonPostSchema } = require("./schema"); | ||
|
||
const accepts = hl7v2ToJsonPostSchema.produces; | ||
|
||
/** | ||
* @author Frazer Smith | ||
* @description Sets routing options for server. | ||
* @param {object} server - Fastify instance. | ||
* @param {object} options - Route config values. | ||
* @param {*=} options.bearerTokenAuthKeys - Apply `bearerToken` security scheme to route if defined. | ||
* @param {object} options.cors - CORS settings. | ||
*/ | ||
async function route(server, options) { | ||
if (options.bearerTokenAuthKeys) { | ||
hl7v2ToJsonPostSchema.security = [{ bearerToken: [] }]; | ||
hl7v2ToJsonPostSchema.response[401] = { | ||
$ref: "responses#/properties/unauthorized", | ||
description: "Unauthorized", | ||
}; | ||
} | ||
|
||
server.addContentTypeParser( | ||
hl7v2ToJsonPostSchema.consumes, | ||
{ parseAs: "string" }, | ||
async (_req, payload) => { | ||
/** | ||
* The Content-Type header can be spoofed so is not trusted implicitly, | ||
* this checks the payload is an HL7 v2.x message | ||
*/ | ||
if (!payload.startsWith("MSH")) { | ||
throw server.httpErrors.unsupportedMediaType(); | ||
} | ||
|
||
return payload; | ||
} | ||
); | ||
|
||
// Register plugins | ||
await server | ||
// Enable CORS if options passed | ||
.register(cors, { | ||
...options.cors, | ||
methods: ["POST"], | ||
}) | ||
.register(hl7v2ToJson); | ||
|
||
server.route({ | ||
method: "POST", | ||
url: "/", | ||
schema: hl7v2ToJsonPostSchema, | ||
onRequest: async (req) => { | ||
if ( | ||
// Catch unsupported Accept header media types | ||
!req.accepts().type(accepts) | ||
) { | ||
throw server.httpErrors.notAcceptable(); | ||
} | ||
}, | ||
handler: (req, res) => { | ||
res.send(req.conversionResults.body); | ||
}, | ||
}); | ||
} | ||
|
||
module.exports = route; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const S = require("fluent-json-schema"); | ||
|
||
const tags = ["HL7 v2.x"]; | ||
|
||
/** | ||
* Fastify uses AJV for JSON Schema Validation, | ||
* see https://fastify.io/docs/latest/Reference/Validation-and-Serialization/ | ||
* | ||
* Input validation protects against XSS, HPP, prototype pollution, | ||
* and most other injection attacks. | ||
*/ | ||
const hl7v2ToJsonPostSchema = { | ||
tags, | ||
summary: "Convert HL7 v2.x message to JSON", | ||
description: | ||
"Returns the result of converting an HL7 v2.x message to JSON format.", | ||
operationId: "postHl7v2ToJson", | ||
consumes: ["text/hl7v2"], | ||
produces: ["application/json"], | ||
response: { | ||
200: S.object().additionalProperties(true), | ||
400: S.ref("responses#/properties/badRequest").description( | ||
"Bad Request" | ||
), | ||
406: S.ref("responses#/properties/notAcceptable").description( | ||
"Not Acceptable" | ||
), | ||
415: S.ref("responses#/properties/unsupportedMediaType").description( | ||
"Unsupported Media Type" | ||
), | ||
429: S.ref("responses#/properties/tooManyRequests").description( | ||
"Too Many Requests" | ||
), | ||
503: S.ref("responses#/properties/serviceUnavailable").description( | ||
"Service Unavailable" | ||
), | ||
}, | ||
}; | ||
|
||
module.exports = { hl7v2ToJsonPostSchema }; |