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(widgets): Implement queryRuleCustomData widget #222

Merged
merged 12 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ If a particular version of the above libraries don't work with the adapter, plea

This adapter works with all widgets in [this list](https://www.algolia.com/doc/api-reference/widgets/js/), _except_ for the following:

- `queryRuleCustomData`
- `queryRuleContext`

## Development
Expand Down
14,472 changes: 8,069 additions & 6,403 deletions dist/typesense-instantsearch-adapter.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.min.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/SearchResponseAdapter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/SearchResponseAdapter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"build:dist": "webpack",
"test:type": "tsd",
"build": "npm run build:lib && npm run build:dist",
"typesenseServer": "docker run -i -p 8108:8108 -v`pwd`/typesense-server-data/:/data typesense/typesense:0.25.2 --data-dir /data --api-key=xyz --listen-port 8108 --enable-cors",
"typesenseServer": "docker run -i -p 8108:8108 -v`pwd`/typesense-server-data/:/data typesense/typesense:27.1 --data-dir /data --api-key=xyz --listen-port 8108 --enable-cors",
"createServerNodesFile": "echo '172.17.0.2:8107:8108,172.17.0.3:7107:7108,172.17.0.4:9107:9108' > `pwd`/typesense-server-nodes",
"typesenseServer:0": "docker run -i -p 8108:8108 -p 8107:8107 -v/tmp/typesense-server-data-node-1/:/data -v`pwd`/typesense-server-nodes:/typesense-server-nodes typesense/typesense:0.19.0 --data-dir /data --api-key=xyz --listen-port 8108 --peering-port 8107 --enable-cors --nodes=/typesense-server-nodes",
"typesenseServer:1": "docker run -i -p 7108:7108 -p 7107:7107 -v/tmp/.typesense-server-data-node-2/:/data -v`pwd`/typesense-server-nodes:/typesense-server-nodes typesense/typesense:0.19.0 --data-dir /data --api-key=xyz --listen-port 7108 --peering-port 7107 --enable-cors --nodes=/typesense-server-nodes",
Expand Down
12 changes: 12 additions & 0 deletions src/SearchResponseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ export class SearchResponseAdapter {
return adaptedResult;
}

_adaptUserData(metadata) {
if (!metadata) return [];

return Array.isArray(metadata) ? metadata : [metadata];
}

adapt() {
const adaptedRenderingContent = this._adaptRenderingContent(this.typesenseResponse.facet_counts || []);
const adaptedResult = {
Expand All @@ -344,6 +350,12 @@ export class SearchResponseAdapter {
...(Object.keys(adaptedRenderingContent).length > 0 ? { renderingContent: adaptedRenderingContent } : null),
};

// Add appliedRules if metadata is present
if (this.typesenseResponse.metadata) {
adaptedResult.appliedRules = ["typesense-override"];
adaptedResult.userData = this._adaptUserData(this.typesenseResponse.metadata);
}

// If no results were found for the search, but there is still a conversation response,
// still send that as a hit so the conversation is accessible via Instantsearch
if (this.fullTypesenseResponse.conversation && adaptedResult.hits.length === 0) {
Expand Down
43 changes: 43 additions & 0 deletions test/SearchResponseAdpater.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,49 @@ describe("SearchResponseAdapter", () => {
});
});

describe("._adaptUserData", () => {
describe("When there's a metadata object in the response based on curation rules", () => {
it("formats Typesense's metadata object into userData", () => {
const typesenseResponse = require("./support/data/typesense-search-response-with-metadata.json");
const subject = new SearchResponseAdapter(
typesenseResponse["results"][0],
{
params: {
highlightPreTag: "<mark>",
highlightPostTag: "</mark>",
},
},
{},
);
const result = subject.adapt();
expect(result?.userData).toEqual([
{
promotion: "new_phones",
title: "Check out the latest holiday season phones",
},
]);
expect(result.appliedRules).toEqual(["typesense-override"]);
});
});
describe("When there's not a metadata object in the response based on curation rules", () => {
it("leaves the userData object empty", () => {
const typesenseResponse = require("./support/data/typesense-search-response.json");
const subject = new SearchResponseAdapter(
typesenseResponse["results"][0],
{
params: {
highlightPreTag: "<mark>",
highlightPostTag: "</mark>",
},
},
{},
);
const result = subject.adapt();
expect(result?.userData).toBeUndefined();
expect(result.appliedRules).toBeUndefined();
});
});
});
describe("._adaptRenderingContent", () => {
describe("when user does not specify any renderingContent", () => {
describe("when the search has results", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/searchExperience.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ describe("Search Experience", () => {

it("renders the grouped results", async () => {
await expect(page).toMatchElement("#brand-list", {
text: "AT&T",
text: "360fly",
});
await expect(page).toMatchElement("#stats", {
text: "250 results found",
text: "263 results found",
});
await expect(page).toMatchElement("#hits .ais-Hits-item:nth-of-type(1) .hit-name", {
text: "AT&T",
Expand Down
Loading
Loading