Skip to content

Commit

Permalink
feat(atomic): add middleware preprocessor for the recommendation engi…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme authored Jul 9, 2024
1 parent 26bf619 commit 4e0803d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/headless/src/api/search/search-api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ describe('search api client', () => {
const res = await searchAPIClient.facetSearch(req);
expect(res.moreValuesAvailable).toEqual(true);
});

describe('when calling SearchAPIClient.recommendations', () => {
it('should preprocess the response', async () => {
const recommendationState = createMockRecommendationState();
const processedRecommendations = buildMockSearchResponse();

buildSearchAPIClient({
postprocessSearchResponseMiddleware: (response) => {
return {
...response,
body: processedRecommendations,
};
},
});

const req = await buildRecommendationRequest(recommendationState);
const res = await searchAPIClient.recommendations(req);

if (!isErrorResponse(res)) {
expect(res.success).toEqual(processedRecommendations);
}
});
});
});

describe('noop middleware', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/headless/src/api/search/search-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@ export class SearchAPIClient implements FacetSearchAPIClient {
const body = await response.json();

if (isSuccessSearchResponse(body)) {
return {success: body};
const payload = {response, body};
payload.body = shimResponse(body);
const processedResponse =
await this.options.postprocessSearchResponseMiddleware(payload);
return {
success: processedResponse.body,
};
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Schema, StringValue} from '@coveo/bueno';
import {PostprocessSearchResponseMiddleware} from '../../api/search/search-api-client-middleware';
import {nonEmptyString} from '../../utils/validate-payload';
import {
EngineConfiguration,
Expand Down Expand Up @@ -40,6 +41,10 @@ export interface RecommendationEngineConfiguration extends EngineConfiguration {
* America/Montreal
*/
timezone?: string;
/**
* Allows for augmenting a search response before the state is updated.
*/
preprocessSearchResponseMiddleware?: PostprocessSearchResponseMiddleware;
}

export const recommendationEngineConfigurationSchema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function createSearchAPIClient(
logger,
preprocessRequest: configuration.preprocessRequest || NoopPreprocessRequest,
postprocessSearchResponseMiddleware:
configuration.preprocessSearchResponseMiddleware ||
NoopPostprocessSearchResponseMiddleware,
postprocessFacetSearchResponseMiddleware:
NoopPostprocessFacetSearchResponseMiddleware,
Expand Down

0 comments on commit 4e0803d

Please sign in to comment.