Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose raw request content under _rawContent #6544

Merged
merged 5 commits into from
Jan 24, 2025

Conversation

e-krebs
Copy link
Contributor

@e-krebs e-krebs commented Jan 23, 2025

Summary

In order to access some composition-related data (when getRankingInfo is requested), we need to access the root content of the API response, not just results (cf. last example on this doc).

Result

exposing the root content of the API response (excluding results that is already exposed) through a _rawContent non-documented & non-typed property on search results.

it can therefor be accessed like this:

const { results: { _rawContent } } = useInstantSearch();

@e-krebs e-krebs self-assigned this Jan 23, 2025
Copy link

codesandbox-ci bot commented Jan 23, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9bb93f4:

Sandbox Source
example-instantsearch-getting-started Configuration
example-react-instantsearch-getting-started Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-react-instantsearch-next-routing-example Configuration
example-vue-instantsearch-getting-started Configuration

@e-krebs e-krebs marked this pull request as ready for review January 23, 2025 14:57
@e-krebs e-krebs requested a review from Haroenv January 23, 2025 14:57
@Haroenv
Copy link
Contributor

Haroenv commented Jan 23, 2025

looks like it's "lucky" that we only override _state, not recreate a whole results object in places like

derivedHelper.lastResults._state = helper!.state;

Maybe in server side rendering it could not be applied?

and
if (indexInitialResults?.results) {
// We restore the shape of the results provided to the instance to respect
// the helper's structure.
const results = new algoliasearchHelper.SearchResults(
new algoliasearchHelper.SearchParameters(indexInitialResults.state),
indexInitialResults.results
);
derivedHelper.lastResults = results;
helper.lastResults = results;
}

@e-krebs
Copy link
Contributor Author

e-krebs commented Jan 23, 2025

@Haroenv the instantsearch-core package you're referencing above doesn't appear to exist on master 🤔

@Haroenv
Copy link
Contributor

Haroenv commented Jan 23, 2025

sorry was on the next branch, but this code exists in master too in InstantSearch.js

@e-krebs
Copy link
Contributor Author

e-krebs commented Jan 23, 2025

sorry was on the next branch, but this code exists in master too in InstantSearch.js

I did update my implementation 🙂

@e-krebs
Copy link
Contributor Author

e-krebs commented Jan 24, 2025

@Haroenv should I account for _rawContent here?

derivedHelper.on('result', ({ results }) => {
// The index does not render the results it schedules a new render
// to let all the other indices emit their own results. It allows us to
// run the render process in one pass.
instantSearchInstance.scheduleRender();
// the derived helper is the one which actually searches, but the helper
// which is exposed e.g. via instance.helper, doesn't search, and thus
// does not have access to lastResults, which it used to in pre-federated
// search behavior.
helper!.lastResults = results;
lastValidSearchParameters = results?._state;
});

to do it properly here I'll have to add it to the types, though :

      derivedHelper.on('result', ({ results, _rawContent }) => {
        // The index does not render the results it schedules a new render
        // to let all the other indices emit their own results. It allows us to
        // run the render process in one pass.
        instantSearchInstance.scheduleRender();

        // the derived helper is the one which actually searches, but the helper
        // which is exposed e.g. via instance.helper, doesn't search, and thus
        // does not have access to lastResults, which it used to in pre-federated
        // search behavior.
        helper!.lastResults = results;
        helper!.lastResults._rawContent = _rawContent;
        lastValidSearchParameters = results?._state;
      });

@Haroenv
Copy link
Contributor

Haroenv commented Jan 24, 2025

No, that part is just copying the results so already works without changes.

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we know this isn't permeated through the whole life cycle, but because of the limited use case, let's land it like this first. Thanks!

@Haroenv Haroenv merged commit 5883502 into master Jan 24, 2025
14 checks passed
@Haroenv Haroenv deleted the feat/raw-response-content branch January 24, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants