From 560eb87ba3405e44433ddaa2ff153c8ee4c3259e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Mon, 17 Jul 2023 19:11:14 +0200 Subject: [PATCH 1/2] Replace axios with fetch --- src/components/Searchkit/ESSearchApi.jsx | 69 ++++++------------------ src/components/Views/FacetedSearch.jsx | 2 +- 2 files changed, 17 insertions(+), 54 deletions(-) diff --git a/src/components/Searchkit/ESSearchApi.jsx b/src/components/Searchkit/ESSearchApi.jsx index 59959321..5bcf960c 100644 --- a/src/components/Searchkit/ESSearchApi.jsx +++ b/src/components/Searchkit/ESSearchApi.jsx @@ -1,34 +1,24 @@ import _get from 'lodash/get'; import _hasIn from 'lodash/hasIn'; -import axios from 'axios'; -import { RequestCancelledError } from 'react-searchkit'; import { CustomESRequestSerializer } from './CustomESRequestSerializer'; import { CustomESResponseSerializer } from './CustomESResponseSerializer'; export class PloneSearchApi { constructor(config) { - this.axiosConfig = _get(config, 'axios', {}); - this.validateAxiosConfig(); + this.fetchConfig = _get(config, 'fetchPayload', {}); + this.validateFetchConfig(); this.initSerializers(config); - this.initInterceptors(config); - this.initAxios(); this.search = this.search.bind(this); - this.axiosCancelToken = axios.CancelToken; this.elastic_search_api_url = config.elastic_search_api_url; this.elastic_search_api_index = config.elastic_search_api_index; } - validateAxiosConfig() { - if (!_hasIn(this.axiosConfig, 'url')) { - throw new Error('PloneSearchApi config: `node` field is required.'); + validateFetchConfig() { + if (!_hasIn(this.fetchConfig, 'url')) { + throw new Error('PloneSearchApi config: `url` is required.'); } } - initInterceptors(config) { - this.requestInterceptor = _get(config, 'interceptors.request', undefined); - this.responseInterceptor = _get(config, 'interceptors.response', undefined); - } - initSerializers(config) { const requestSerializerCls = _get( config, @@ -54,57 +44,30 @@ export class PloneSearchApi { }); } - initAxios() { - this.http = axios.create(this.axiosConfig); - this.addInterceptors(); - } - - addInterceptors() { - if (this.requestInterceptor) { - this.http.interceptors.request.use( - this.requestInterceptor.resolve, - this.requestInterceptor.reject, - ); - } - if (this.responseInterceptor) { - // Bug fix: this.http.interceptors.request.use( - this.http.interceptors.response.use( - this.responseInterceptor.resolve, - this.responseInterceptor.reject, - ); - } - } - /** * Perform the backend request to search and return the serialized list of results for the app state `results`. * @param {string} stateQuery the `query` state with the user input */ async search(stateQuery) { - // cancel any previous request in case it is still happening - this.axiosCancel && this.axiosCancel.cancel(); - // generate a new cancel token for this request - this.axiosCancel = this.axiosCancelToken.source(); - const payload = this.requestSerializer.serialize(stateQuery); // Extend paylod with url and index to address elasticsearch server - try { - const response = await this.http.request({ - method: 'POST', - data: { + try { + const response = await fetch(this.fetchConfig.url, { + method: "POST", + headers: this.fetchConfig.headers, + body: JSON.stringify({ elasticsearch_payload: payload, elasticsearch_url: this.elastic_search_api_url, elasticsearch_index: this.elastic_search_api_index, - }, + }) }); - let results = await this.responseSerializer.serialize(response.data); + + // let results = await this.responseSerializer.serialize(response.data); + let results = await response.json(); + results = this.responseSerializer.serialize(results); return results; } catch (error) { - error.message = error.response.data?.error?.message; - if (axios.isCancel(error)) { - throw new RequestCancelledError(); - } else { - throw error; - } + throw error; } } } diff --git a/src/components/Views/FacetedSearch.jsx b/src/components/Views/FacetedSearch.jsx index c855e0c3..243bf5c0 100644 --- a/src/components/Views/FacetedSearch.jsx +++ b/src/components/Views/FacetedSearch.jsx @@ -61,7 +61,7 @@ export const ploneSearchApi = (data) => { const cookies = new Cookies(); const authToken = cookies.get('auth_token'); return new PloneSearchApi({ - axios: { + fetchPayload: { url: expandToBackendURL('/@kitsearch'), timeout: 5000, headers: { From 5d2014b53fb4b0abebfb5b9c1f303c8013eb1f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20Su=CC=88ss?= Date: Mon, 17 Jul 2023 19:17:15 +0200 Subject: [PATCH 2/2] prettier --- src/components/Searchkit/ESSearchApi.jsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/components/Searchkit/ESSearchApi.jsx b/src/components/Searchkit/ESSearchApi.jsx index 5bcf960c..9a5bc199 100644 --- a/src/components/Searchkit/ESSearchApi.jsx +++ b/src/components/Searchkit/ESSearchApi.jsx @@ -51,17 +51,16 @@ export class PloneSearchApi { async search(stateQuery) { const payload = this.requestSerializer.serialize(stateQuery); // Extend paylod with url and index to address elasticsearch server - try { + try { const response = await fetch(this.fetchConfig.url, { - method: "POST", + method: 'POST', headers: this.fetchConfig.headers, body: JSON.stringify({ elasticsearch_payload: payload, elasticsearch_url: this.elastic_search_api_url, elasticsearch_index: this.elastic_search_api_index, - }) + }), }); - // let results = await this.responseSerializer.serialize(response.data); let results = await response.json(); results = this.responseSerializer.serialize(results);