elastic/search-ui

Custom Post-Processing of Request Body doesn't work with async function

Closed this issue · 2 comments

Describe the bug
When calling new ElasticsearchAPIConnector({...}, postProcessRequestBody) where postProcessRequestBody is the function described here to customize the Elasticsearch Request, passing in an async function seems to break everything, with a cryptic error.

To Reproduce
I haven't created a minimal case, but I think this search-ui API endpoint would be enough to reproduce the bug:

import ElasticsearchAPIConnector from '@elastic/search-ui-elasticsearch-connector'

async function postProcessRequestBody(requestBody, _state, _config) {
  return requestBody
}

const connector = new ElasticSearchApiConnector(connectionConfig, postProcessRequestBody)

return async (req, res) => {
    const { requestState, queryConfig } = req.body
    const response = await connector.onSearch(requestState, queryConfig)
    res.json(response)
}

Expected behavior
I would expect this to just send whatever request search-ui generated on the front-end

What happens
I believe this will actually cause an error, because the "response" is an unfulfilled promise that has no "aggregations" object (at least that's the way it fails for me, in my slightly more complex use case)

The error I got was "Cannot convert undefined or null to object", on this line.

Which backends and packages are you using:
Backend: [Elasticsearch]
Packages: [react-search-ui, search-ui-elasticsearch-connector]

FWIW: The reason I was using async, was that I'm taking a "Company ID" from the generated request, and looking up some company information, to enrich the request with additional related queries. I'm now looking for a way to do this outside the postProcessRequestBody function, but figured this issue was worth raising anyway, if for no other reason than to document that async isn't supported, and/or to possibly get a more helpful error message back when someone passes in an async function. But if possible, it would obviously be great if async functions were supported! 😄

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Is this issue still important to you? If so, please leave a comment and let us know. As always, thank you for your contributions.