Response Transformation Functions

Response transformation (transform) functions are called right after a request returns successfully. They provide a hook for page authors to transform the response further for the consumption of the ServiceDataProvider or user interface.

The ServiceDataProvider supports a predefined list of response transformation function types, described in this section. Note that there are no guarantees of the order in which transform functions are called.

A response transformation function has the following signature: function (result). It can be defined on the service endpoint, but can also be overridden on the variable. The parameter to this function is:

  • result: an object that has the following properties:

    • response: the response object, an implementation of the Response interface of the Fetch web API.

    • body: The (response) body that corresponds to the requested content type.

Types of Response Transform Functions

paginate

The paginate response transform function is called with the response so this function can process it and return an object with the following properties set. The returned object is the primary way ServiceDataProvider obtains information about the paging state of the request:

  • totalSize: Optional. Used to inform ServiceDataProvider what the totalSize of the result is (the total count of the records in the backend service/endpoint).

  • hasMore: Usually required, because with the JET DataProvider API, the paginate response transform function is relied upon to inform the ServiceDataProvider when to stop requesting to fetch more data. It is a boolean that indicates whether there are more records to fetch. For example, in business object REST API use cases, this would map to the hasMore boolean property commonly returned in the response.

    An iterating component such as ServiceDataProvider requires this information in order to know when to stop iterating when fetching data. The reason for this requirement is that an iterating component will get the AsyncIterator from the dataProvider and continue iterating until there is no more data to fetch, until the component viewPort is filled, or until its current scrollPosition is reached, whichever comes first.

  • pagingState: Optional. This can be used to store any paging state specific to the paging capability supported by the endpoint. This property can be used in the response paginate transform function to set an additional paging state. This will then be passed as is to the request paginate transform function for the next fetch call.

// Variable Configuration
"transforms": {
  "response": {
    "paginate": " {{ $page.functions.paginateResponse }}"
  }
}
// paginate() Response Transform Function
PageModule.prototype.paginateResponse = function (result, context) {
  const ps = {}; const tr = {};
 
  if (result.body) {
    const rb = result.body;
    if (rb.totalCount) {
      tr.totalSize = rb.totalCount;
    }
    if (rb.totalCount > 0) {
      tr.hasMore = !!rb.hasMore;
    } else {
      tr.hasMore = false;
    }
  }
  return tr;
};

body

This transform function is called last, after all the other response transforms have been called. It is a hook for authors to transform the response body, or build an entirely new one, that the ServiceDataProvider or component expects.

Here is an example of a body transform function in the SDP configuration:

"transforms": {
  "response": {
    "body": " {{ $page.functions.bodyResponse }}"
  }
}

And here is an example of a body transform function:

/**
 * Called after response returns from a fetch call, this is a good place to post-process
 * response, fix up data and extract other info (like aggregations) and return a transformed result.
 * The object returned must have the body that SDP is configured for, in addition to any data
 * that might be needed.
 *
 * @param configuration - a Map containing the following properties
 * - headers: response header
 * - body: body of the response
 * - fetchConfiguration: the configuration that triggered this fetch call. If fetch was initiated by SDP this includes
 *   fetch capability, context, externalContext and fetchParameters property. Refer to the docs for the
 *   mergeTransformsOptions func callback for details.
 *
 * @param transformsContext transforms context
 *
 * @returns {{}}
 *
/PageModule.prototype.bodyResponse(configuration, transformsContext) {
  var tr = {};
  if (configuration.body) {
    // fix up result.body from REST if needed and set the new body in tr
    tr = configuration.body;
  }
  // you can also store additional data - example aggregations for Elastic endpoint responses.
  tr.aggregations = { foo: 4 };
  return tr;
}