Skip to content

Commit

Permalink
fix: fix bug involving custom baseUrls
Browse files Browse the repository at this point in the history
  • Loading branch information
zeevo committed Sep 29, 2020
1 parent 9ce9c66 commit d825ad3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
1 change: 0 additions & 1 deletion packages/fhir-response-util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function create(req, res, json, options) {
location = `${fhirVersion}/${options.type}/${json.id}`;
}

console.log(location);
if (json.resource_version) {
let pathname = path.posix.join(location, '_history', json.resource_version);
res.set('Content-Location', `${baseUrl}/${pathname}`);
Expand Down
16 changes: 6 additions & 10 deletions packages/node-fhir-server-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
## [2.1.3](https://github.com/Asymmetrik/node-fhir-server-core/compare/2.0.10...2.1.3) (2020-09-23)


### Bug Fixes

* add allergyintolerance ([a91bc21](https://github.com/Asymmetrik/node-fhir-server-core/commit/a91bc210bff103d3bb8a68a8ca89a0b028ace04e))
* add allergyintrolerance controller and default to r4 if no version is found ([85af3a2](https://github.com/Asymmetrik/node-fhir-server-core/commit/85af3a279bcd37132f9516400fb91acef90c8eba))
* just return an R4 operation outcome on lost URLs ([86451ec](https://github.com/Asymmetrik/node-fhir-server-core/commit/86451ec2a362b22e39674c99cff48f241f0a57d0))

- add allergyintolerance ([a91bc21](https://github.com/Asymmetrik/node-fhir-server-core/commit/a91bc210bff103d3bb8a68a8ca89a0b028ace04e))
- add allergyintrolerance controller and default to r4 if no version is found ([85af3a2](https://github.com/Asymmetrik/node-fhir-server-core/commit/85af3a279bcd37132f9516400fb91acef90c8eba))
- just return an R4 operation outcome on lost URLs ([86451ec](https://github.com/Asymmetrik/node-fhir-server-core/commit/86451ec2a362b22e39674c99cff48f241f0a57d0))

### Features

* add allergyintolerance schema ([268cb8a](https://github.com/Asymmetrik/node-fhir-server-core/commit/268cb8a8ca0a8659fbaa0c341d612f7cf8986d17))
* add index files for each resource ([7410e4a](https://github.com/Asymmetrik/node-fhir-server-core/commit/7410e4a1090a7718d4b4170c37409c044ba2b72f))
* improve package bundling ([a30db64](https://github.com/Asymmetrik/node-fhir-server-core/commit/a30db64f8f3f9e2015b0138f8c931a3c2039df52))


- add allergyintolerance schema ([268cb8a](https://github.com/Asymmetrik/node-fhir-server-core/commit/268cb8a8ca0a8659fbaa0c341d612f7cf8986d17))
- add index files for each resource ([7410e4a](https://github.com/Asymmetrik/node-fhir-server-core/commit/7410e4a1090a7718d4b4170c37409c044ba2b72f))
- improve package bundling ([a30db64](https://github.com/Asymmetrik/node-fhir-server-core/commit/a30db64f8f3f9e2015b0138f8c931a3c2039df52))

## [2.1.2](https://github.com/Asymmetrik/node-fhir-server-core/compare/2.0.10...2.1.2) (2020-09-23)

Expand Down
43 changes: 20 additions & 23 deletions packages/node-fhir-server-core/src/server/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolveSchema } = require('./utils/schema.utils');
const { resolveSchema, isValidVersion } = require('./utils/schema.utils');
const deprecate = require('./utils/deprecation.notice');
const ServerError = require('./utils/server.error');
const invariant = require('./utils/invariant');
Expand Down Expand Up @@ -29,8 +29,8 @@ function mergeDefaults(providedConfig) {
profiles: {},
server: {},
logging: {
level: 'debug'
}
level: 'debug',
},
};

return Object.assign(defaults, providedConfig);
Expand All @@ -48,7 +48,7 @@ function verifyAndLoadProfiles(profiles) {
// support for the given profile. We will do that in the when setting up routes
// This may change for future versions when we generate new resources and we
// can more easily perform the validation here
Object.getOwnPropertyNames(profiles).forEach(name => {
Object.getOwnPropertyNames(profiles).forEach((name) => {
let versions = profiles[name].versions;
let service = profiles[name].service;
let message;
Expand Down Expand Up @@ -127,7 +127,7 @@ class Server {

this.env = {
IS_PRODUCTION: !process.env.NODE_ENV || process.env.NODE_ENV === 'production',
USE_HTTPS: server.ssl && server.ssl.key && server.ssl.cert ? server.ssl : undefined
USE_HTTPS: server.ssl && server.ssl.key && server.ssl.cert ? server.ssl : undefined,
};
// return self for chaining
return this;
Expand Down Expand Up @@ -172,7 +172,7 @@ class Server {
helmet(
helmetConfig || {
// Needs https running first
hsts: this.env.USE_HTTPS
hsts: this.env.USE_HTTPS,
}
)
);
Expand Down Expand Up @@ -246,16 +246,13 @@ class Server {
// Errors should be thrown with next and passed through
this.app.use((err, req, res, next) => {
// get base from URL instead of params since it might not be forwarded
const base = req.url.split('/')[1] || VERSIONS['4_0_0'];
const base = req.url.split('/')[1];

// Get an operation outcome for this instance
let OperationOutcome;
if (Object.keys(VERSIONS).includes(base)) {
OperationOutcome = resolveSchema(base, 'operationoutcome');
} else {
// if it's a misplaced URL, just return an R4 OperationOutcome
OperationOutcome = resolveSchema('4_0_0', 'operationoutcome');
}
const OperationOutcome = resolveSchema(
isValidVersion(base) ? base : VERSIONS['4_0_0'],
'operationoutcome'
);

// If there is an error and it is an OperationOutcome
if (err && err.resourceType === OperationOutcome.resourceType) {
Expand All @@ -272,10 +269,10 @@ class Server {
severity: 'error',
code: 'internal',
details: {
text: `Unexpected: ${err.message}`
}
}
]
text: `Unexpected: ${err.message}`,
},
},
],
});

logger.error(error);
Expand Down Expand Up @@ -306,10 +303,10 @@ class Server {
severity: 'error',
code: 'not-found',
details: {
text: `Invalid url: ${req.path}`
}
}
]
text: `Invalid url: ${req.path}`,
},
},
],
});

logger.error(error);
Expand Down Expand Up @@ -349,7 +346,7 @@ class Server {
: https.createServer(
{
key: fs.readFileSync(server.ssl.key),
cert: fs.readFileSync(server.ssl.cert)
cert: fs.readFileSync(server.ssl.cert),
},
this.app
);
Expand Down
12 changes: 12 additions & 0 deletions packages/node-fhir-server-core/src/server/utils/schema.utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { VERSIONS } = require('../../constants');

const schemasR4 = require('../resources/4_0_0/schemas');
const schemas3 = require('../resources/3_0_1/schemas');
const schemas1 = require('../resources/1_0_2/schemas');
Expand All @@ -19,6 +21,16 @@ const resolveSchema = (version = '4_0_0', schema = '') => {
}
};

/**
* Utility helpful for checking if a given string is a valid FHIR version
* within node-fhir-server-core
* @param {String} version
*/
const isValidVersion = (version) => {
return Object.keys(VERSIONS).includes(version);
};

module.exports = {
resolveSchema,
isValidVersion,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolveSchema } = require('./schema.utils');
const { resolveSchema, isValidVersion } = require('./schema.utils');

describe('Schema Utils tests', () => {
test('should get R4 parameters', () => {
Expand All @@ -25,4 +25,12 @@ describe('Schema Utils tests', () => {
const schema = resolveSchema('1_0_2', 'OperationOutcome');
expect(schema).toBeTruthy();
});
test('should evaluate that a string is an invalid fhir version', () => {
const version = 'foobar';
expect(isValidVersion(version)).toEqual(false);
});
test('should evaluate that a string is an valid fhir version', () => {
const version = '4_0_0';
expect(isValidVersion(version)).toEqual(true);
});
});

0 comments on commit d825ad3

Please sign in to comment.