Skip to content

Commit

Permalink
Fix content type and body handling for OpenHIM responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrichton committed Mar 4, 2024
1 parent c15f93c commit c7b6de3
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/mpi-mdm-everything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion src/middlewares/mpi-mdm-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 2 additions & 0 deletions src/middlewares/openhim-proxy-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
Expand Down
13 changes: 9 additions & 4 deletions src/middlewares/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down Expand Up @@ -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);
};
10 changes: 5 additions & 5 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
Expand All @@ -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);
})
);
Expand All @@ -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);
})
);
Expand All @@ -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);
})
);
Expand All @@ -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);
})
);
Expand Down
2 changes: 1 addition & 1 deletion src/types/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface Response {
status: number;
body: object;
body: string;
timestamp: string;
headers: HeadersInit;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

Expand Down

0 comments on commit c7b6de3

Please sign in to comment.