Skip to content

Commit ded1097

Browse files
authored
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
1 parent b2b2b9a commit ded1097

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ const options = {
6161
},
6262
};
6363

64-
createIndex(data, options);
64+
const index = createIndex(data, options);
6565

66-
const searchClient = getSearchClient();
66+
const searchClient = getSearchClient(index);
6767
```
6868
`options` Options are from the ItemsJS API found here: [ItemsJS](https://github.com/itemsapi/itemsjs)
6969

__tests__/adapter.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,26 @@ describe("getSearchClient", () => {
8383
},
8484
];
8585

86+
const index = createIndex(products, options);
87+
8688
expect(() => {
87-
getSearchClient().searchForFacetValues();
89+
getSearchClient(index).searchForFacetValues();
8890
}).toThrowError(new Error("Not implemented"));
91+
expect(getSearchClient(index).search(queries)).toBeDefined();
8992

93+
expect(() => {
94+
getSearchClient().searchForFacetValues();
95+
}).toThrowError(new Error("Not implemented"));
9096
expect(getSearchClient().search(queries)).toBeDefined();
9197
});
9298
});
9399

94100
describe("performSearch", () => {
95101
it("Performs a search", async () => {
96-
createIndex(products, options);
102+
const index = createIndex(products, options);
97103

98104
const response: Readonly<MultipleQueriesResponse<object>> =
99-
await performSearch(requests);
105+
await performSearch(requests, index);
100106

101107
expect(response.results[0].hits.length).toBe(per_page || products.length);
102108
expect(response.results[0].page).toBe(page - 1);
@@ -144,4 +150,13 @@ describe("performSearch", () => {
144150
price: { min: 7, max: 999, avg: 161.45, sum: 3229 },
145151
});
146152
});
153+
154+
it("Performs no search, when there is no index", async () => {
155+
const index = null;
156+
157+
const response: Readonly<MultipleQueriesResponse<object>> =
158+
await performSearch(requests, index);
159+
160+
expect(response).toBeNull();
161+
});
147162
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "instantsearch-itemsjs-adapter",
3-
"version": "1.0.4",
3+
"version": "1.1.4",
44
"description": "An adapter to use ItemsJs based client-side search with an Algolia Instantsearch front-end.",
55
"main": "./lib/adapter",
66
"directories": {

src/adapter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ import { ItemsJsOptions, SearchClient } from "./itemsjsInterface";
99

1010
let index;
1111

12-
export function getSearchClient(): SearchClient {
12+
export function getSearchClient(newIndex?: any): SearchClient {
1313
return {
14-
search: (queries: MultipleQueriesQuery[]) => performSearch(queries),
14+
search: (queries: MultipleQueriesQuery[]) =>
15+
performSearch(queries, index || newIndex),
1516
searchForFacetValues: () => {
1617
throw new Error("Not implemented");
1718
},
1819
};
1920
}
2021

21-
export function createIndex(data: object, options: ItemsJsOptions): void {
22+
export function createIndex(data: object, options: ItemsJsOptions): any {
2223
index = itemsjs(data, options);
24+
return index;
2325
}
2426

2527
export function performSearch(
26-
requests: MultipleQueriesQuery[]
28+
requests: MultipleQueriesQuery[],
29+
index: any
2730
): Readonly<Promise<MultipleQueriesResponse<object>>> {
2831
if (index) {
2932
let processingTimeMS = 0;
@@ -52,5 +55,6 @@ export function performSearch(
5255

5356
return Promise.resolve({ results: responses });
5457
}
58+
5559
return null;
5660
}

0 commit comments

Comments
 (0)