From c7b6de308785df5d7a5a672dcf5ece76cf0617bb Mon Sep 17 00:00:00 2001 From: Ryan Crichton Date: Mon, 4 Mar 2024 15:19:16 +0200 Subject: [PATCH] Fix content type and body handling for OpenHIM responses --- package-lock.json | 4 ++-- package.json | 2 +- src/middlewares/mpi-mdm-everything.ts | 2 +- src/middlewares/mpi-mdm-summary.ts | 2 +- src/middlewares/openhim-proxy-interceptor.ts | 2 ++ src/middlewares/validation.ts | 13 +++++++++---- src/routes/index.ts | 10 +++++----- src/types/response.ts | 2 +- src/utils/utils.ts | 4 ++-- 9 files changed, 24 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c4d545..cea1724 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mpi-mediator", - "version": "v2.0.0", + "version": "v2.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mpi-mediator", - "version": "v2.0.0", + "version": "v2.0.2", "license": "ISC", "dependencies": { "@types/sinon": "^10.0.13", diff --git a/package.json b/package.json index 021e3d2..e83c68b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpi-mediator", - "version": "v2.0.1", + "version": "v2.0.2", "description": "An OpenHIM mediator to handle all interactions with an MPI component", "main": "index.ts", "scripts": { diff --git a/src/middlewares/mpi-mdm-everything.ts b/src/middlewares/mpi-mdm-everything.ts index 883bd47..42bb519 100644 --- a/src/middlewares/mpi-mdm-everything.ts +++ b/src/middlewares/mpi-mdm-everything.ts @@ -69,6 +69,6 @@ export const mpiMdmEverythingMiddleware: RequestHandler = async (req, res, next) const { status, body } = await fetchAllLinkedPatientResources(req.params.patientId); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(status).send(body); }; diff --git a/src/middlewares/mpi-mdm-summary.ts b/src/middlewares/mpi-mdm-summary.ts index bb51d91..859c530 100644 --- a/src/middlewares/mpi-mdm-summary.ts +++ b/src/middlewares/mpi-mdm-summary.ts @@ -42,6 +42,6 @@ export const mpiMdmSummaryMiddleware: RequestHandler = async (req, res, next) => const { status, body } = await fetchAllLinkedPatientSummary(req.params.patientId); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(status).send(body); }; diff --git a/src/middlewares/openhim-proxy-interceptor.ts b/src/middlewares/openhim-proxy-interceptor.ts index f5da736..9e21f50 100644 --- a/src/middlewares/openhim-proxy-interceptor.ts +++ b/src/middlewares/openhim-proxy-interceptor.ts @@ -24,6 +24,8 @@ export const openhimProxyResponseInterceptor = responseInterceptor( const responseBody = buildOpenhimResponseObject(transactionStatus, statusCode, body); + _res.setHeader('Content-Type', 'application/json+openhim'); + return JSON.stringify(responseBody); } ); diff --git a/src/middlewares/validation.ts b/src/middlewares/validation.ts index 7b7e73d..e748877 100644 --- a/src/middlewares/validation.ts +++ b/src/middlewares/validation.ts @@ -4,8 +4,13 @@ import logger from '../logger'; import { ResponseObject } from '../types/response'; import { buildOpenhimResponseObject, isHttpStatusOk, postData } from '../utils/utils'; -const { fhirDatastoreProtocol, fhirDatastoreHost, fhirDatastorePort, contentType, disableValidation } = - getConfig(); +const { + fhirDatastoreProtocol, + fhirDatastoreHost, + fhirDatastorePort, + contentType, + disableValidation, +} = getConfig(); export const validationMiddleware: RequestHandler = async (req, res, next) => { logger.info('Validating Fhir Resources'); @@ -21,7 +26,7 @@ export const validationMiddleware: RequestHandler = async (req, res, next) => { status: 400, }; } else if (disableValidation) { - return next(); + return next(); } else { response = await postData( fhirDatastoreProtocol, @@ -56,6 +61,6 @@ export const validationMiddleware: RequestHandler = async (req, res, next) => { response.body ); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(response.status).send(responseBody); }; diff --git a/src/routes/index.ts b/src/routes/index.ts index da35f7c..91bc49b 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -26,7 +26,7 @@ routes.post( asyncHandler(async (req, res) => { const result = await matchSyncHandler(req.body); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(result.status).send(result.body); }) ); @@ -40,7 +40,7 @@ routes.post( const responseBody = buildOpenhimResponseObject(transactionStatus, status, body); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(status).send(responseBody); }) ); @@ -67,7 +67,7 @@ routes.get( asyncHandler(async (req, res) => { const { status, body } = await fetchEverythingByRef(`Patient/${req.params.patientId}`); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(status).send(body); }) ); @@ -78,7 +78,7 @@ routes.get( asyncHandler(async (req, res) => { const { status, body } = await fetchPatientSummaryByRef(`Patient/${req.params.patientId}`); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(status).send(body); }) ); @@ -90,7 +90,7 @@ routes.post( asyncHandler(async (req, res) => { const result = await matchAsyncHandler(req.body); - res.set('Content-Type', 'application/openhim+json'); + res.set('Content-Type', 'application/json+openhim'); res.status(result.status).send(result.body); }) ); diff --git a/src/types/response.ts b/src/types/response.ts index 88fac92..0fdb56c 100644 --- a/src/types/response.ts +++ b/src/types/response.ts @@ -1,6 +1,6 @@ export interface Response { status: number; - body: object; + body: string; timestamp: string; headers: HeadersInit; } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index a1b782b..cc60f14 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -95,12 +95,12 @@ export const buildOpenhimResponseObject = ( openhimTransactionStatus: string, httpResponseStatusCode: number, responseBody: object, - contentType = 'application/json' + contentType = 'application/fhir+json' ): OpenHimResponseObject => { const response: Response = { status: httpResponseStatusCode, headers: { 'Content-Type': contentType }, - body: responseBody, + body: JSON.stringify(responseBody), timestamp: format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"), };