JavaScript client for FHIR
- Support FHIR CRUD operations
- Friendly and expressive query syntax
- Support for access control (HTTP basic, OAuth2, Cookies)
- ...
Node.js
is required for build.
We recommend installing Node.js using nvm
Build & test:
git clone https://github.com/FHIR/fhir.js
cd fhir.js
npm install
# build fhir.js
npm run-script build
# run tests in node
npm run-script test
# run tests in phantomjs
npm run-script integrate
To communicate with concrete FHIR server, you can create instance of the FHIR client, passing a configuration object & adapter object. Adapters must be provided by the client.
var config = {
// FHIR server base url
baseUrl: 'http://myfhirserver.com',
auth: {
bearer: 'token',
// OR for basic auth
user: 'user',
pass: 'secret'
},
// Valid Options are 'same-origin', 'include'
credentials: 'same-origin',
headers: {
'X-Custom-Header': 'Custom Value',
'X-Another-Custom': 'Another Value',
}
}
myClient = fhir(config, adapter)
The config object is an object that is passed through the middleware chain. Any values in the config object that are not mutated by middleware will be available to the adapter.
Because middleware mutates the config, it is strongly recommended when implementing an adapter to not directly rely on config passed in.
This is the full url to your FHIR server. Resources will be appended to the end of it.
This is an object representing your authentication requirements. Possible options include:
This is your Bearer token when provided, it will add an Authorization: Bearer <token>
header to your requests.
This is your Basic auth Username.
When you provide both user name and password, basic auth will be used.
This is your basic auth password.
When you provide both user name and password, basic auth will be used.
This option controls the behaviour of sending cookies to the remote server. Refer to the table below for how to configure the option for your desired adapter.
A key:value object that represents headers. This object is passed through to you configured adapter.
If you choose to add custom headers to your requests, you should ensure that the server that you are talking to supplies the appropriate headers. Further reading on Allowed Headers: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
const config = {
headers: {
'X-Custom-Header': 'Custom Value',
'X-Another-Custom': 'Another Value',
}
}
Currently each adapter must implement an
http(requestObj)
function:
Structure of requestObj:
method
- http method (GET|POST|PUT|DELETE)url
- url for requestheaders
- object with headers (i.e. {'Category': 'term; scheme="sch"; label="lbl"'}
and return promise (A+)
http(requestObj).then(success, error)
where:
success
- success callback, which should be called with (data, status, headersFn, config)
- data - parsed body of responce
- status - responce HTTP status
- headerFn - function to get header, i.e. headerFn('Content')
- config - initial requestObj passed to http
error
- error callback, which should be called with (data, status, headerFn, config)
Here are implementations for:
To create a FHIR resource, call
myClient.create(entry, callback, errback)
, passing
an object that contains the following properties:
resource
(required) - resource in FHIR jsontags
(optional) - list of categories (see below)
In case of success,the callback function will be invoked with an object that contains the following attributes:
id
- url of created resourcecontent
- resource jsoncategory
- list of tags
var entry = {
category: [{term: 'TAG term', schema: 'TAG schema', label: 'TAG label'}, ...]
resource: {
resourceType: 'Patient',
//...
}
}
myClient.create(entry,
function(entry){
console.log(entry.id)
},
function(error){
console.error(error)
}
)
To get one specific object from a resource (usually by id), call fhir.read({type: resourceType})
. To specify the patient identifier, call fhir.read({type: resourceType, patient: patientIdentifier})
Examples:
fhir.read({type: 'Patient', patient: '8673ee4f-e2ab-4077-ba55-4980f408773e'})
To search a resource, call fhir.search({type: resourceType, query: queryObject})
,
where queryObject syntax fhir.js
adopts
mongodb-like query syntax (see):
{name: 'maud'}
//=> name=maud
{name: {$exact: 'maud'}}
//=> name:exact=maud
{name: {$or: ['maud','dave']}}
//=> name=maud,dave
{name: {$and: ['maud',{$exact: 'dave'}]}}
//=> name=maud&name:exact=Dave
{birthDate: {$gt: '1970', $lte: '1980'}}
//=> birthDate=gt1970&birthDate=lte1980
{subject: {$type: 'Patient', name: 'maud', birthDate: {$gt: '1970'}}}
//=> subject:Patient.name=maud&subject:Patient.birthDate=gt1970
{'subject.name': {$exact: 'maud'}}
//=> subject.name:exact=maud
To update a resource, call fhir.update({type: resourceType, id: identifier, resource: resourceObject})
.
In case of success,the callback function will be invoked.
Example:
this.fhirClient.update({
type: "Patient",
id: 1,
resource: {
name: 'New Name'
}
}).catch(function(e){
console.log('An error happened while updating patient: \n' + JSON.stringify(e));
throw e;
}).then(function(bundle){
console.log('Updating patient successed');
return bundle;
});
To update a resource, call fhir.delete({type: resourceType, id: identifier})
.
For more information see tests
FHIR.js is built on top of middleware concept. What is middleware? This is a high order function of shape:
var mw = function(next){
return function(args){
if (...) // some logic{
return next(args); //next mw in chain
} else {
return promise; //short circuit chain
}
}
}
Using function Middleware(mw) you can get composable middle-ware (with .and(mw) method):
mwComposition = Middleware(mw).and(anotherMw).and(anotherMw);
Every API function is built as chain of middlewares with end handler in the end:
conformance = $GET.and(BaseUrl.slash("metadata")).end(http)
create = $POST.and($resourceTypePath).and($ReturnHeader).and($JsonData).end(http),
API changes history is split into 3 fns:
- fhir.history
- fhir.typeHistory
- fhir.resourceHistory
- npm package
- bower package
Join us by github issues or pull-requests
Released under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.