Skip to content

Add option to specify fetch function per request #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class Client {
* @param {Object} options Object used for configuring the query suggest, like 'types' or 'size'
* @returns {Promise<ResultList>} a Promise that returns results, otherwise throws an Error.
*/
querySuggestion(query, options = {}) {
querySuggestion(query, options = {}, fetchFunction = fetch) {
const params = Object.assign({ query: query }, options);

return request(
Expand All @@ -72,7 +72,8 @@ export default class Client {
this.querySuggestionPath,
params,
this.cacheResponses,
{ additionalHeaders: this.additionalHeaders }
{ additionalHeaders: this.additionalHeaders },
fetchFunction,
).then(handleErrorResponse);
}

Expand All @@ -83,7 +84,7 @@ export default class Client {
* @param {Object} options Object used for configuring the search like search_fields and result_fields
* @returns {Promise<ResultList>} a Promise that returns a {ResultList} when resolved, otherwise throws an Error.
*/
search(query, options = {}) {
search(query, options = {}, fetchFunction = fetch) {
const {
disjunctiveFacets,
disjunctiveFacetsAnalyticsTags,
Expand All @@ -99,7 +100,7 @@ export default class Client {
disjunctiveFacetsAnalyticsTags
).then(formatResultsJSON);
}
return this._performSearch(params).then(formatResultsJSON);
return this._performSearch(params, SEARCH_TYPES.SEARCH, fetchFunction).then(formatResultsJSON);
}

/**
Expand All @@ -111,15 +112,16 @@ export default class Client {
* - options: Object (optional)
* @returns {Promise<[ResultList]>} a Promise that returns an array of {ResultList} when resolved, otherwise throws an Error.
*/
multiSearch(searches) {
multiSearch(searches, fetchFunction = fetch) {
const params = searches.map(search => ({
query: search.query,
...(search.options || {})
}));

return this._performSearch(
{ queries: params },
SEARCH_TYPES.MULTI_SEARCH
SEARCH_TYPES.MULTI_SEARCH,
fetchFunction,
).then(responses => responses.map(formatResultsJSON));
}

Expand All @@ -145,9 +147,10 @@ export default class Client {
_performDisjunctiveSearch(
params,
disjunctiveFacets,
disjunctiveFacetsAnalyticsTags = ["Facet-Only"]
disjunctiveFacetsAnalyticsTags = ["Facet-Only"],
fetchFunction = fetch,
) {
const baseQueryPromise = this._performSearch(params);
const baseQueryPromise = this._performSearch(params, SEARCH_TYPES.SEARCH, fetchFunction);

const filters = new Filters(params.filters);
const appliedFilers = filters.getListOfAppliedFilters();
Expand Down Expand Up @@ -182,7 +185,7 @@ export default class Client {
facets: {
[appliedDisjunctiveFilter]: params.facets[appliedDisjunctiveFilter]
}
});
}, SEARCH_TYPES.SEARCH, fetchFunction);
}
);

Expand All @@ -199,7 +202,7 @@ export default class Client {
);
}

_performSearch(params, searchType = SEARCH_TYPES.SEARCH) {
_performSearch(params, searchType = SEARCH_TYPES.SEARCH, fetchFunction = fetch) {
const searchPath =
searchType === SEARCH_TYPES.MULTI_SEARCH
? this.multiSearchPath
Expand All @@ -210,7 +213,8 @@ export default class Client {
`${searchPath}.json`,
params,
this.cacheResponses,
{ additionalHeaders: this.additionalHeaders }
{ additionalHeaders: this.additionalHeaders },
fetchFunction,
).then(handleErrorResponse);
}

Expand All @@ -223,7 +227,7 @@ export default class Client {
* @param {String[]} tags Tags to categorize this request in the Dashboard
* @returns {Promise} An empty Promise, otherwise throws an Error.
*/
click({ query, documentId, requestId, tags = [] }) {
click({ query, documentId, requestId, tags = [], fetchFunction = fetch }) {
const params = {
query,
document_id: documentId,
Expand All @@ -237,7 +241,8 @@ export default class Client {
`${this.clickPath}.json`,
params,
this.cacheResponses,
{ additionalHeaders: this.additionalHeaders }
{ additionalHeaders: this.additionalHeaders },
fetchFunction,
).then(handleErrorResponse);
}
}
20 changes: 14 additions & 6 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function request(
path,
params,
cacheResponses,
{ additionalHeaders } = {}
{ additionalHeaders } = {},
fetchFunction = fetch,
) {
const method = "POST";
const key = cache.getKey(method, apiEndpoint + path, params);
Expand All @@ -19,9 +20,15 @@ export function request(
}
}

return _request(method, searchKey, apiEndpoint, path, params, {
additionalHeaders
}).then(response => {
return _request(
method,
searchKey,
apiEndpoint,
path,
params,
{ additionalHeaders },
fetchFunction,
).then(response => {
return response
.json()
.then(json => {
Expand All @@ -41,7 +48,8 @@ function _request(
apiEndpoint,
path,
params,
{ additionalHeaders } = {}
{ additionalHeaders } = {},
fetchFunction = fetch,
) {
const headers = new Headers({
...(searchKey && { Authorization: `Bearer ${searchKey}` }),
Expand All @@ -51,7 +59,7 @@ function _request(
...additionalHeaders
});

return fetch(`${apiEndpoint}${path}`, {
return fetchFunction(`${apiEndpoint}${path}`, {
method,
headers,
body: JSON.stringify(params),
Expand Down