Digipass S3 is now DigipassONE. This section is currently being updated to reflect our new name.

Custom REST Client

Prev Next

To customize the Server REST API requests and responses, follow these steps to provide your own implementation of the RestClient interface that includes the ajaxCall() function.

  1. Define your implementation of the RestClient interface as a RestClientCustom() function.

  2. Implement the ajaxCall() interface function inside class RestClientCustom. This is the function that can modify the request. For example, this function can set custom headers or add fields to the request payload before sending the request to the Server.

const RestClientCustom = function() {
    // Implement the RestClient interface function.
    this.ajaxCall = function(request, requestJson, restParams) {
        // Example of adding a custom property to the request.
        requestJson.exampleData = 'exampleData';
        // Example of adding a custom header.
        const headers = {};
        request.headers.forEach(function(value, key) {
            headers[key] = value;
        });
        headers['Custom-Header'] = 'RestClientCustom';
        // Example of setting the request timeout.
        const timeout = restParams.ajaxCallConnectionTimeout * 1000;
        // Create a new request based on the original one with modified parameters.
        const newRequest = new Request(request, {
            body: JSON.stringify(requestJson),
            headers: headers,
            signal: AbortSignal.timeout(timeout)
        });
        // Send the request and return the Promise.
        return fetch(newRequest);
    }
}

Note that the AJAX request timeout provided to AbortSignal.timeout() is in milliseconds and can be specified using the AppSdkConfig.ajaxCallConnectionTimeout property. AppSdkConfig.ajaxCallConnectionTimeout is in seconds and is accessible through restParams.ajaxCallConnectionTimeout. Instead of hard-coding the AJAX request timeout, the code above sets the value from restParams.ajaxCallConnectionTimeout.

  1. The final step is different depending on whether or not you changed the default behavior of the App SDK to communicate with API Server through an iframe.

    1. If your application is using the App SDK with an iframe, which is its default behavior, then the final step is to move your class RestClientCustom function into the iframe-custom.js file (if it is not already there) and move the iframe-custom.js file into the nnlappsdk-<version>/js folder.

    2. If your application is using the App SDK without iframe, then you have more work to do. Implement the createRestClient() function of the RestClientFactory base class.

const CustomRestClientFactory = function() {
    // Implement the base class function.
    this.createRestClient = function() {
        return new RestClientCustom();
    }
}

Provide the instance of your custom factory by calling RestClientFactory.setInstance() before starting any operation:

RestClientFactory.setInstance(new CustomRestClientFactory());

Note that since you are providing the App SDK with your instance of the RestClient interface from Step 2. above, you do not have to name your implementation RestClientCustom.

For more information, see the RestClient interface in the Client API Docs.

Working Example in Tutorial Web App

  • Refer to RestClientCustom in the file sampleRestClient/iframe-custom.js.