Skip to content

mhiner/fhir.js

 
 

Repository files navigation

fhir.js

Build Status

Gitter chat

JavaScript client for FHIR

Goals:

  • Support FHIR CRUD operations
  • Friendly and expressive query syntax
  • Support for adapters that provide idiomatic interfaces in angular, jQuery, extjs, etc
  • Support for access control (HTTP basic, OAuth2)
  • ...

Development

Node.js is required for build.

We recommend installling Node.js using nvm

Build & test:

git clone https://github.com/FHIR/fhir.js
cd fhir.js
npm install

# buld fhir.js
npm run-script build

# run all tests once
npm run-script spec

# watch tests while development
npm run-script spec-watch

# run integration tests
npm run-script integrate

API

Create instance of the FHIR client

To communicate with concrete FHIR server, you can create instance of the FHIR client, passing a configuration object & adapter object. Adapters are implemented for concrete frameworks/libs (see below).

var cfg = {
  // FHIR server base url
  baseUrl: 'http://myfhirserver.com',
  auth: {
     bearer: 'token',
     // OR for basic auth
     user: 'user',
     pass: 'secret'
  }
}

myClient = fhir(cfg, adapter)

Adapter implementation

Currently each adapter must implement an http(requestObj) function:

Structure of requestObj:

  • method - http method (GET|POST|PUT|DELETE)
  • url - url for request
  • headers - object with headers (i.e. {'Category': 'term; scheme="sch"; label="lbl"'}
  • 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:

Conformance & Profiles

Resource's CRUD

To create a FHIR resource, call myClient.create(entry, callback, errback), passing an object that contains the following propperties:

  • content (required) - resource in FHIR json
  • tags (opttional) - 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 resource
  • content - resource json
  • category - list of tags
var entry = {
  category: [{term: 'TAG term', schema: 'TAG schema', label: 'TAG label'}, ...]
  content: {
    resourceType: 'Patient',
    //...
  }
}

myClient.create(entry,
 function(entry){
    console.log(entry.id)
 },
 function(error){
   console.error(error)
 }
)

Tags Operations

Search

fhir.search('Patient', queryObject, callback, errback) function is used for FHIR resource's search.

If success callback will be called with resulting bundle.

For 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=>1970&birthDate=<=1980

{subject: {$type: 'Patient', name: 'maud', birthDate: {$gt: '1970'}}}
//=> subject:Patient.name=maud&subject:Patient.birthDate=>1970

{'subject.name': {$exact: 'maud'}}
//=> subject.name:exact=maud

For more information see tests

AngularJS adapter: ng-fhir

AngularJS adapter after npm run-script build can be found at dist/ngFhir.js

Usage:

angular.module('app', ['ng-fhir'])
  .config ($fhirProvider)->
     $fhirProvider.baseUrl = 'http://try-fhirplace.hospital-systems.com'
  .controller 'mainCtrl', ($scope, $fhir)->
     $fhir.search
       type: 'Patient'
       query: {name: {$exact: 'Maud'}}
       error: (error)-> $scope.error = error
       success: (bundle)-> $scope.patients = bundle.entry

jQuery adapter: jqFhir

jQuery build can be found at dist/jqFhir.js

Example app

Usage:

<script src="./jquery-???.min.js"> </script>
<script src="./jqFhir.js"> </script>
// create fhir instance
var fhir = jqFhir({
    baseUrl: 'https://ci-api.fhir.me',
    auth: {user: 'client', pass: 'secret'}
})

fhir.search({type: 'Patient', query: {name: 'maud'}})
.then(function(bundle){
  console.log('Search patients', bundle)
})

Node.js adapter: npm install fhir.ns

Via NPM you can npm install fhir.js. (If you want to work on the source code, you can compile coffee to js via npm install, and use ./lib/adapters/node as an entrypoint.)

var mkFhir = require('fhir.js');

var client = mkFhir({
  baseUrl: 'http://try-fhirplace.hospital-systems.com'
});

client.search( {type: 'Patient', query: { 'birthdate': '1974' }}, function(err, bundle) {
  var count = (bundle.entry && bundle.entry.length) || 0;
  console.log("# Patients born in 1974: ", count);
});

YUI adapter: yuiFhir

YUI build can be found at dist/yuiFhir.js

NOTE: The current implementation creates a YUI sandbox per request which is expensive.

Usage:

<script src="./yui-???.min.js"> </script>
<script src="./yuiFhir.js"> </script>
// create fhir instance
var fhir = jqFhir({
    baseUrl: 'https://ci-api.fhir.me',
    auth: {user: 'client', pass: 'secret'}
})

fhir.search(type: 'Patient', query: {name: 'maud'}, success: function(bundle) {}, error: function() {})

TODO

  • npm package
  • bower package

Contribute

Join us by github issues or pull-requests

License

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.

About

JavaScript client for FHIR

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • CoffeeScript 76.5%
  • JavaScript 20.2%
  • Shell 3.3%