Leveraging Coveo Atomic Framework for Request and Response Manipulation
In the Coveo Atomic framework, we have powerful tools for handling requests and responses, even if we don't have the same event-handling methods available in JSUI. Two key tools for this purpose are the PreProcessRequestMiddleware and preprocessSearchResponseMiddleware.
PreProcessRequestMiddleware:
The PreProcessRequestMiddleware provides direct access to requests before responses are received and pages are loaded. This allows for various manipulations, such as modifying request parameters or intercepting actions before execution. For instance, you can log query information for debugging purposes and perform conditional actions based on the query content.
Example: preprocessRequest: (request, clientOrigin, metadata) => { const body = JSON.parse(request.body); request.body = JSON.stringify(body); // Reassign the modified body back to request
// Execute the first search after preprocessing searchInterface.executeFirstSearch();
return request; // Return the modified request }
preprocessSearchResponseMiddleware: The preprocessSearchResponseMiddleware operates after pages have loaded, providing access to search responses. Here, you can manipulate aspects such as total count and relevant data to meet specific requirements. This middleware allows for additional processing of search responses if necessary.
Example:
preprocessSearchResponseMiddleware: (response) => { // Additional processing of search response if needed return response; // Return the modified response } searchInterface.executeFirstSearch(); }();
By leveraging these middleware options, developers can effectively customize search experiences within the Coveo Atomic framework to align with unique business needs and user expectations.