d2

Module that contains the entry points for working with the d2 instance. Most of the api related functionality will be available through this package.

The most important functions from the module are init and getInstance these are used to create and to get a hold of the initialized instance of d2. The initialized instance is the object that allows you access most of d2's functionality.

Source:
Example
import { init } from 'd2/lib/d2';

init({ baseUrl: 'https://play.dhis2.org/demo/api/27/' })
 .then(d2 => console.log(d2.currentUser.name));

Members

(static, constant) config :Config

Source:

Can be used to set config options before initialisation of d2.

Type:
  • Config
Example
import {config, init} from 'd2';

config.baseUrl = '/demo/api';
config.i18n.sources.add('i18n/systemsettingstranslations.properties');

init()
  .then(d2 => {
    d2.system.settings.all()
      .then(systemSettings => Object.keys())
      .then(systemSettingsKey => {
        d2.i18n.getTranslation(systemSettingsKey);
      });
  });
  

Methods

(static) getInstance() → {Promise.<D2>}

Source:

This function can be used to retrieve the singleton instance of d2. The instance is being created by calling the init method.

Example
import {init, getInstance} from 'd2';

init({baseUrl: '/dhis2/api/'});
getInstance()
  .then(d2 => {
     d2.models.dataElement.list();
     // and all your other d2 magic.
  });
Returns:

A promise to the initialized d2 instance.

Type
Promise.<D2>

(static) getManifest(url, ApiClassopt) → {Promise}

Source:

Utility function to load the app manifest.

The manifest well be enhanced with a getBaseUrl() utility function that will return the base url of the DHIS2 instance. This is a simple getter for the activities.dhis.href property on the manifest.

Example
import { getManifest } from 'd2/lib/d2';

getManifest()
  .then(manifest => {
     console.log(manifest.getBaseUrl());
  });
Parameters:
Name Type Attributes Description
url string

The location of the manifest. Generally this is located in the root of your app folder. (e.g. './manifest.webapp)

ApiClass Api <optional>

An implementation of the Api class that will be used to fetch the manifest.

Returns:

Returns a Promise to the DHIS2 app manifest with the added getBaseUrl method.

Type
Promise

(static) init(initConfig) → {Promise.<D2>}

Source:

Init function that used to initialise d2. This will load the schemas from the DHIS2 api and configure your d2 instance.

The config object that can be passed into init can have the following properties:

baseUrl: Set this when the url is something different then /api. If you are running your dhis instance in a subdirectory of the actual domain for example http://localhost/dhis/ you should set the base url to /dhis/api

unauthorizedCb: A callback function that is called whenever a API-request encounters a 401 - Unauthorized response. The function is called with (request, response) - the request object that failed, and the parsed response from the server.

Example
import init from 'd2';

init({baseUrl: '/dhis/api'})
  .then((d2) => {
    console.log(d2.model.dataElement.list());
  });
Parameters:
Name Type Description
initConfig Object

Configuration object that will be used to configure to define D2 Setting. See the description for more information on the available settings.

Returns:

A promise that resolves with the intialized d2 object.

Type
Promise.<D2>

(inner) getUserSettings() → {Promise}

Source:

The object that is the result of the promise will have the following properties

Example
import {getUserSettings} from 'd2/lib/d2';

getUserSettings()
 .then(userSettings => {
     console.log(userSettings);
 });
Returns:

A promise to the current user settings

Type
Promise