Skip to content

Commit

Permalink
Merge pull request #2 from afosto/Feature/search-request-options-and-…
Browse files Browse the repository at this point in the history
…base-url

Feature/search-request-options-and-base-url
  • Loading branch information
Rapid0o authored Jan 5, 2022
2 parents b73ee9c + 2f11388 commit f8ae092
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@afosto/instant-search-client",
"version": "0.2.0",
"version": "0.3.0",
"private": false,
"description": "The Afosto InstantSearch client",
"main": "./dist/afosto-instant-search.min.js",
Expand Down
2 changes: 1 addition & 1 deletion playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2>Products</h2>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@latest/dist/instantsearch.production.min.js" integrity="sha256-2IhR6D0EWonVYvGbuQADSjD9VROGINJw9A8RFZEHXAM=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js" crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchClient = afostoInstantSearch('prxxx');
Expand Down
16 changes: 12 additions & 4 deletions src/afostoInstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ const afostoInstantSearch = (proxyId, options) => {
const searchResponseAdapter = SearchResponseAdapter();

const searchRequest = async context => {
const searchResponse = await fetch(`https://api.afosto.io/cnt/instant/search/${proxyId}`, {
const url = clientOptions.baseUrl?.replace('{proxyId}', proxyId);
const requestOptions = clientOptions.requestOptions || {};
const hasContextFormatter = clientOptions.transformContext && typeof clientOptions.transformContext === 'function';
const hasResponseFormatter = clientOptions.transformResponse && typeof clientOptions.transformResponse === 'function';
const payload = hasContextFormatter ? clientOptions.transformContext(context) : context;
const searchResponse = await fetch(url, {
...requestOptions,
method: 'POST',
headers: {
Accept: 'application/vnd.instantsearch+json',
...(requestOptions.headers || {}),
},
method: 'POST',
body: JSON.stringify(context),
body: JSON.stringify(payload),
});
const response = searchResponse.json();

return searchResponse.json();
return hasResponseFormatter ? clientOptions.transformResponse(response) : response;
}

const search = async requests => {
Expand Down
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const DEFAULT_OPTIONS = {
allowEmptyQuery: true,
baseUrl: 'https://api.afosto.io/cnt/instant/search/{proxyId}',
hitsPerPage: 10,
requestOptions: {},
threshold: 1,
transformContext: data => data,
transformResponse: data => data,
};

export const MATCH_FILTER_REGEX = /(?<key>\w+)(?<operator>>=|>|<|<=|=)(?<value>\d+)/g;
Expand Down

0 comments on commit f8ae092

Please sign in to comment.