Api

api. Api

new Api(fetchImplopt)

Source:

Used for interaction with the dhis2 api.

This class is used as the backbone for d2 and handles all the interaction with the server. There is a singleton available to be reused across your applications. The singleton can be grabbed from the d2 instance. The api methods all handle URL-encoding for you, so you can just pass them unencoded strings

import { getInstance } from 'd2/lib/d2';

getInstance()
 .then(d2 => {
     const api = d2.Api.getApi() // Returns the Api singleton.

     api.get('resources');
 });

Uses Fetch to do network requests.

Parameters:
Name Type Attributes Description
fetchImpl Fetch <optional>

The fetch implementation to use. Can be used to pass a different implementation similar to the fetch Api. Will default to window.fetch in a browser context.

Methods

get(url, data, options) → {Promise.<*>}

Source:

Performs a GET request.

Parameters:
Name Type Description
url string

The url for the request, should be unencoded. Will return a rejected promise for malformed urls and urls that contain encoded query strings.

data *

Any data that should be sent with the request. For a GET request these are encoded and turned into query parameters. For POST and PUT requests it becomes the body.

options Object.<string, any>

The request options are passed as options to the fetch request. These options are passed as the init parameter to the fetch request.

Returns:

The response body.

Type
Promise.<*>

patch(url, data) → {Promise.<*>}

Source:

Perform a PATCH request.

Parameters:
Name Type Description
url string

The url for the request

data *

Any data that should be send with the request. This becomes the body of the PATCH request.

Returns:

The response body.

Type
Promise.<*>

post(url, data, options) → {Promise.<*>}

Source:

Performs a POST request.

Parameters:
Name Type Description
url string

The url for the request

data *

Any data that should be send with the request this becomes the body for the POST request

options Object.<string, any>

The request options are passed as options to the fetch request. These options are passed as the init parameter to the fetch request.

Returns:

The response body.

Type
Promise.<*>

setBaseUrl(baseUrl) → {this}

Source:

Sets the baseUrl that should be used for the api.

When working against the dhis2 demo instance at https://play.dhis2.org/demo the baseUrl would be set as https://play.dhis2.org/demo/api.

This method is used when calling the d2.init method with the baseUrl config property to configure the Api singleton.

Parameters:
Name Type Description
baseUrl string

The base url to be used for the API.

Returns:

Itself for chaining purposes

Type
this

setDefaultHeaders(headers)

Source:

Used for setting default headers that should be send with every request.

Example
const api = Api.getApi();

api.setDefaultHeaders({
 'x-requested-with': 'XMLHttpRequest', // Make sure the Api does not redirect when authorization is expired.
});
Parameters:
Name Type Description
headers Object.<string, string>

Default headers that should be set on every request.

setUnauthorizedCallback(cb)

Source:

When any request encounters a 401 - Unauthorized. This callback is called. Useful for when you want an session expiration-handler API-wide.

Parameters:
Name Type Description
cb *

Function to call when any request recieves a 401. Called with the response from the server.

update(url, data, useMergeStrategyopt) → {Promise.<*>}

Source:

Perform a PUT request.

Parameters:
Name Type Attributes Default Description
url string

The url for the request

data *

Any data that should be send with the request. This becomes the body of the PUT request.

useMergeStrategy boolean <optional>
false
Returns:

The response body.

Type
Promise.<*>