Skip to content

Commit f6f3667

Browse files
committed
Added additional behavior
1 parent 451a5cb commit f6f3667

19 files changed

+711
-103
lines changed

package-lock.json

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"license": "MIT",
55
"private": true,
66
"dependencies": {
7+
"date-fns": "^1.29.0",
78
"history": "^4.7.2",
89
"prop-types": "^15.6.2",
910
"qs": "^6.5.2",
1011
"rc-pagination": "^1.16.5",
1112
"react": "^16.4.1",
1213
"react-dom": "^16.4.1",
1314
"react-scripts": "1.1.4",
14-
"swiftype-app-search-javascript": "^1.4.0"
15+
"swiftype-app-search-javascript": "^2.0.0"
1516
},
1617
"scripts": {
1718
"build-css": "node-sass-chokidar src/ -o src/",

src/App.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ import React, { Component } from "react";
33
import { Body, Header } from "./components";
44
import AppSearchProvider from "./app-search/AppSearchProvider";
55
import AppSearchDriver from "./app-search/AppSearchDriver";
6+
import AppSearchAPIConnector from "./app-search/AppSearchAPIConnector";
7+
68
import {
9+
buildFacetConfigFromConfig,
710
buildSearchOptionsFromConfig,
811
getConfig
912
} from "./config/config-helper";
1013

1114
function createDriver() {
1215
const { hostIdentifier, searchKey, endpointBase, engineName } = getConfig();
1316
return new AppSearchDriver({
14-
hostIdentifier,
15-
searchKey,
16-
endpointBase,
17-
engineName,
17+
apiConnector: new AppSearchAPIConnector({
18+
hostIdentifier,
19+
searchKey,
20+
endpointBase,
21+
engineName
22+
}),
23+
facetConfig: buildFacetConfigFromConfig(),
1824
searchOptions: buildSearchOptionsFromConfig()
1925
});
2026
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as SwiftypeAppSearch from "swiftype-app-search-javascript";
2+
3+
/*
4+
* We mainly just need to filter out unsupported configuration values, like
5+
* `disjunctive`
6+
*/
7+
function toAPIFacetSyntax(facetConfig = {}) {
8+
return Object.entries(facetConfig).reduce((acc, [key, value]) => {
9+
acc[key] = Object.entries(value).reduce((propAcc, [propKey, propValue]) => {
10+
if (["type", "size", "ranges"].includes(propKey)) {
11+
propAcc[propKey] = propValue;
12+
}
13+
return propAcc;
14+
}, {});
15+
return acc;
16+
}, {});
17+
}
18+
19+
function getListOfDisjunctiveFacets(facetConfig = {}) {
20+
return Object.entries(facetConfig).reduce((acc, [key, value]) => {
21+
if (value && value.disjunctive === true) {
22+
return acc.concat(key);
23+
}
24+
return acc;
25+
}, []);
26+
}
27+
export default class AppSearchAPIConnector {
28+
/**
29+
* @param options Object
30+
* engineName - Engine to query, found in your App Search Dashboard
31+
* hostIdentifier - Credential found in your App Search Dashboard
32+
* searchKey - Credential found in your App Search Dashboard
33+
* endpointBase - (optional) Overrides the base of the Swiftype API endpoint
34+
* completely. Useful when proxying the Swiftype API or developing against
35+
* a local API server.
36+
*/
37+
constructor({ searchKey, engineName, hostIdentifier, endpointBase = "" }) {
38+
if (!engineName || !hostIdentifier || !searchKey) {
39+
throw Error("engineName, hostIdentifier, and searchKey are required");
40+
}
41+
42+
this.client = SwiftypeAppSearch.createClient({
43+
endpointBase,
44+
hostIdentifier: hostIdentifier,
45+
apiKey: searchKey,
46+
engineName: engineName
47+
});
48+
}
49+
50+
click(props) {
51+
return this.client.click(props);
52+
}
53+
54+
search(searchTerm, searchOptions) {
55+
const disjunctiveFacets = getListOfDisjunctiveFacets(searchOptions.facets);
56+
if (searchOptions.facets) {
57+
searchOptions.facets = toAPIFacetSyntax(searchOptions.facets);
58+
}
59+
60+
return this.client.search(searchTerm, {
61+
...searchOptions,
62+
disjunctiveFacets
63+
});
64+
}
65+
}

0 commit comments

Comments
 (0)