From ab16e9f081aaa7853daf4947b195d75103b0954d Mon Sep 17 00:00:00 2001 From: Piotr Mankowski Date: Mon, 22 Jan 2024 21:14:41 -0800 Subject: [PATCH] Passthrough --- config/config_docker.json | 3 ++- src/lib/helpers.ts | 14 ++++++++++++++ src/routes/fhir.ts | 17 ++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config/config_docker.json b/config/config_docker.json index a491ec9..e714e6e 100644 --- a/config/config_docker.json +++ b/config/config_docker.json @@ -1,7 +1,8 @@ { "app": { "port": 3000, - "mllpPort": 3001 + "mllpPort": 3001, + "hapiPassthrough": true }, "mediator": { "api": { diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index bfcd1f6..f78e560 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -47,3 +47,17 @@ export function getMetadata(): any { } } } + +export function hapiPassthrough(req: Request): any { + const requestUrl = new URL(req.url) + const targetUri = config.get('fhirServer:baseURL') + requestUrl.pathname + requestUrl.search + + logger.info(`Getting ${targetUri}`) + + const options = { + username: config.get('fhirServer:username'), + password: config.get('fhirServer:password'), + } + + return got(targetUri, options) +} diff --git a/src/routes/fhir.ts b/src/routes/fhir.ts index 388bc61..7bbf402 100644 --- a/src/routes/fhir.ts +++ b/src/routes/fhir.ts @@ -3,7 +3,7 @@ import express, { Request, Response } from 'express' import got from 'got' import URI from 'urijs' import config from '../lib/config' -import { invalidBundle, invalidBundleMessage } from '../lib/helpers' +import { hapiPassthrough, invalidBundle, invalidBundleMessage } from '../lib/helpers' import logger from '../lib/winston' import { generateSimpleIpsBundle } from '../workflows/ipsWorkflows' import { getResourceTypeEnum, isValidResourceType } from '../lib/validate' @@ -22,21 +22,27 @@ router.get('/:resource/:id?/:operation?', async (req: Request, res: Response) => try { let uri = URI(config.get('fhirServer:baseURL')) - if(isValidResourceType(req.params.resource)) { + if (isValidResourceType(req.params.resource)) { uri = uri.segment(getResourceTypeEnum(req.params.resource).toString()) } else { - return res.status(400).json({ message: `Invalid resource type ${req.params.resource}` }) + if (config.get('app:hapiPassthrough')) + return hapiPassthrough(req) + else + return res.status(400).json({ message: `Invalid resource type ${req.params.resource}` }) } if (req.params.id && /^[a-zA-Z0-9\-_]+$/.test(req.params.id)) { uri = uri.segment(encodeURIComponent(req.params.id)) } else { - return res.status(400).json({ message: `Invalid resource id ${req.params.id}` }) + if (config.get('app:hapiPassthrough')) + return hapiPassthrough(req) + else + return res.status(400).json({ message: `Invalid resource id ${req.params.id}` }) } for (const param in req.query) { const value = req.query[param] - if(value && /^[a-zA-Z0-9\-_]+$/.test(value.toString())) { + if (value && /^[a-zA-Z0-9\-_]+$/.test(value.toString())) { uri.addQuery(param, encodeURIComponent(value.toString())) } else { return res.status(400).json({ message: `Invalid query parameter ${param}=${value}` }) @@ -79,6 +85,7 @@ router.get('/:resource/:id?/:operation?', async (req: Request, res: Response) => res.status(200).json(result) } catch (error) { + return res.status(500).json(error) } })