Skip to content

Commit

Permalink
ASUPF-239: Change adapter application interface (#32)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ruitjes authored Dec 22, 2021
1 parent b2b2b9a commit ded1097
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 18 additions & 3 deletions __tests__/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultipleQueriesResponse<object>> =
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);
Expand Down Expand Up @@ -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<MultipleQueriesResponse<object>> =
await performSearch(requests, index);

expect(response).toBeNull();
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 8 additions & 4 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<MultipleQueriesResponse<object>>> {
if (index) {
let processingTimeMS = 0;
Expand Down Expand Up @@ -52,5 +55,6 @@ export function performSearch(

return Promise.resolve({ results: responses });
}

return null;
}

0 comments on commit ded1097

Please sign in to comment.