From ded1097c9e8f4934d4e07b9c3efe17f193abc53f Mon Sep 17 00:00:00 2001 From: Stan Rutten Date: Wed, 22 Dec 2021 19:04:55 +0100 Subject: [PATCH] ASUPF-239: Change adapter application interface (#32) * Change adapter application interface Gives the user more control over the index * Update README.md * Minor version upgrade package.json Via semantic versioning * Eslint fix :) * Update packag.json to Minor change * Backwards compatibility with v1.0.4 * Better readable code --- README.md | 4 ++-- __tests__/adapter.test.ts | 21 ++++++++++++++++++--- package.json | 2 +- src/adapter.ts | 12 ++++++++---- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f3f3a19..3c47847 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,9 @@ const options = { }, }; -createIndex(data, options); +const index = createIndex(data, options); -const searchClient = getSearchClient(); +const searchClient = getSearchClient(index); ``` `options` Options are from the ItemsJS API found here: [ItemsJS](https://github.com/itemsapi/itemsjs) diff --git a/__tests__/adapter.test.ts b/__tests__/adapter.test.ts index d7c6c0e..e9bbbad 100644 --- a/__tests__/adapter.test.ts +++ b/__tests__/adapter.test.ts @@ -83,20 +83,26 @@ describe("getSearchClient", () => { }, ]; + const index = createIndex(products, options); + expect(() => { - getSearchClient().searchForFacetValues(); + getSearchClient(index).searchForFacetValues(); }).toThrowError(new Error("Not implemented")); + expect(getSearchClient(index).search(queries)).toBeDefined(); + expect(() => { + getSearchClient().searchForFacetValues(); + }).toThrowError(new Error("Not implemented")); expect(getSearchClient().search(queries)).toBeDefined(); }); }); describe("performSearch", () => { it("Performs a search", async () => { - createIndex(products, options); + const index = createIndex(products, options); const response: Readonly> = - await performSearch(requests); + await performSearch(requests, index); expect(response.results[0].hits.length).toBe(per_page || products.length); expect(response.results[0].page).toBe(page - 1); @@ -144,4 +150,13 @@ describe("performSearch", () => { price: { min: 7, max: 999, avg: 161.45, sum: 3229 }, }); }); + + it("Performs no search, when there is no index", async () => { + const index = null; + + const response: Readonly> = + await performSearch(requests, index); + + expect(response).toBeNull(); + }); }); diff --git a/package.json b/package.json index d506e80..7254ec9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "instantsearch-itemsjs-adapter", - "version": "1.0.4", + "version": "1.1.4", "description": "An adapter to use ItemsJs based client-side search with an Algolia Instantsearch front-end.", "main": "./lib/adapter", "directories": { diff --git a/src/adapter.ts b/src/adapter.ts index 673e67c..3ce00e8 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -9,21 +9,24 @@ import { ItemsJsOptions, SearchClient } from "./itemsjsInterface"; let index; -export function getSearchClient(): SearchClient { +export function getSearchClient(newIndex?: any): SearchClient { return { - search: (queries: MultipleQueriesQuery[]) => performSearch(queries), + search: (queries: MultipleQueriesQuery[]) => + performSearch(queries, index || newIndex), searchForFacetValues: () => { throw new Error("Not implemented"); }, }; } -export function createIndex(data: object, options: ItemsJsOptions): void { +export function createIndex(data: object, options: ItemsJsOptions): any { index = itemsjs(data, options); + return index; } export function performSearch( - requests: MultipleQueriesQuery[] + requests: MultipleQueriesQuery[], + index: any ): Readonly>> { if (index) { let processingTimeMS = 0; @@ -52,5 +55,6 @@ export function performSearch( return Promise.resolve({ results: responses }); } + return null; }