Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

d2

dhis224.3kBSD-3-Clause31.10.2

Javascript library for DHIS2

dhis2, api, library

readme

d2

build status greenkeeper badge npm version

The full api documentation is available here. To get started we suggest you read the overview. If you already know what you're looking for we suggest the reference material on what is available on the instance of d2.

For more information on how the models work the model module documentation is helpful reference material. For other questions see the FAQ.

Quickstart guide

Install

Start with adding d2 to your project:

yarn add d2 or npm install d2

After installing you will be able to import the library into your project by using the files in the lib folder:

// Using ES2015 imports
import d2 from 'd2';

// Using CommonJS imports
var d2 = require('d2');

Initialise the library

To be able to use d2 you will first need to initialise the library. This is required to let the library know where it should load its data from (e.g. the schemas, currentUser, authorities). The schemas are the definitions of the data model as used in DHIS2.

To do this you can provide d2 with a baseUrl (if you don't provide any the default of ../api will be used):

import { init } from 'd2';

init({ baseUrl: 'http://apps.dhis2.org/dev/api' })
  .then(d2 => {
    //Your d2 is initialised and ready to use.
  });

Get first page of users and print their names

d2.models.user.list()
  .then(userCollection => {
    userCollection.forEach(user => console.log(user.name)));
  });

That's it! See the documentation referenced above for further information.

Report an issue

The issue tracker can be found in DHIS2 JIRA under the LIBS project.

Deep links:

changelog

31.10.2 (2021-11-18)

Bug Fixes

  • noop: trigger release due to failed auth (a0626b9)

31.10.1 (2021-11-17)

Bug Fixes

  • spread options to requestOptions (8151af1)
  • api: allow override of content-type in update() when value is string (af2eccb)
  • datastore: explicitly set content-type when updating' (1a4892b)

31.10.0 (2021-04-07)

Features

  • json: treat access collection as non-model collections (67bc88d)

31.9.3 (2021-03-24)

Bug Fixes

  • only pass fixed dimensions as dimension for data value sets (DHIS2-9789) (f99e1e9), closes #776

31.9.2 (2021-03-22)

Bug Fixes

  • remove encoding of query string for analytics requests DHIS2-10722 (#284) (c810235)
  • remove unused import (#285) (6e1a408)

31.9.1 (2021-03-09)

Bug Fixes

  • current-user: add getUserGroupIds, allow custom fields in getUserGroups (DHIS2-10625) (#280) (d3e84a6)

31.9.0 (2020-12-11)

Bug Fixes

  • allow uid for org unit levels (18a86f8)

Features

D2 Changelog

31.6.0

March 7th 2019

Breaking changes:

31.5.0

February 28th 2019

Breaking changes:

  • Changed from babel es2015 and stage-2 presets to babel-preset-env, so for certain browsers support might have changed. Though currently the support is aligned with our supported browsers.

Bugfix:

  • Fixed usage of isomorphic-fetch, to allow for usage of d2 in node and the browser.

30.1.0

February 15th 2019

Breaking changes:

  • d2.Api Api methods will reject with an error when passing urls with an encoded query-string, or when passing urls with a malformed query string. The existing query string and anything that will be appended will be encoded for you by d2.Api's methods.

28.3.0

October 26th 2017

Breaking changes:

  • d2.dataStore API has changed in order to be more streamlined and easier to use:
    • d2.dataStore.create() has been added to be able to ensure a new (empty) namespace.
    • d2.dataStore.get() now rejects if the namespace does not exist.
    • d2.dataStore.getKeys() now always fetches from the server, use d2.dataStore.keys-member to get a list of internal-keys in a synchronous way.
Added
  • d2.currentUser.dataStore has been added, and is a wrapper around UserDataStore. The API shares most functionality with d2.dataStore.

28.0.0

September 19th 2017

Breaking changes:

  • d2.system.loadAppStore has changed in order to support the new central app store.
  • Support for dataType and contentType options on API requests have been removed. These were added for compatibility with jQuery, and have been deprecated since version 2.25. To migrate, manipulate the request headers directly instead:
    • dataType corresponds to the Accept header:
      • Before: api.get(url, { dataType: 'text' })
      • Now: api.get(url, { headers: { 'Accept': 'text/plain' }})
    • contentType corresponds to the Content-Type header:
      • Before: api.post(url, data, { contentType: 'text' })
      • Now: api.post(url, data, { headers: { 'Content-Type': 'text/plain' }})

27.0.0

February 20th 2016

Breaking changes:

  • d2.currentUser.uiLocale has been removed, d2.currentUser.userSettings.get should be used instead.
  • userSettings.get will now now always return a Promise (This therefore also applies to d2.currentUser.userSettings.get)
  • systemSettings.get will now always return a Promise.

25.2.0

November 18th 2016

Breaking change:

  • Calling save() on an instance of d2.Model or d2.ModelCollectionProperty that has no changes will now return a promise that immediately resolves to an empty object, in stead of a promise that's rejected with an error message

25.0.1

August 1st 2016
Added
  • [feat] clone() can now be used on a model instance to clone an object