From d8a449e2edd31ca1e28ebcc4be5ed16c12d35a95 Mon Sep 17 00:00:00 2001 From: Simone Taeggi Date: Tue, 15 Nov 2022 12:48:32 +0100 Subject: [PATCH] refactor the fetchClient to the getFetchClient utils --- packages/core/admin/admin/src/StrapiApp.js | 4 +- .../PluginsInitializer/tests/index.test.js | 2 +- .../admin/src/components/Providers/index.js | 6 +-- .../pages/SettingsPage/tests/index.test.js | 2 +- .../core/admin/admin/src/utils/fetchClient.js | 48 ------------------- .../admin/admin/src/utils/getFetchClient.js | 45 +++++++++++++++++ packages/core/admin/admin/src/utils/index.js | 2 +- .../components/DataManagerProvider/index.js | 10 ++-- .../admin/src/utils/axiosInstance.js | 2 +- .../lib/src/hooks/useFetchClient/index.js | 4 +- .../src/providers/StrapiAppProvider/index.js | 2 +- 11 files changed, 62 insertions(+), 65 deletions(-) delete mode 100644 packages/core/admin/admin/src/utils/fetchClient.js create mode 100644 packages/core/admin/admin/src/utils/getFetchClient.js diff --git a/packages/core/admin/admin/src/StrapiApp.js b/packages/core/admin/admin/src/StrapiApp.js index 0511e39ee3..4f2201b347 100644 --- a/packages/core/admin/admin/src/StrapiApp.js +++ b/packages/core/admin/admin/src/StrapiApp.js @@ -22,7 +22,7 @@ import { import injectionZones from './injectionZones'; import favicon from './favicon.png'; import localStorageKey from './components/LanguageProvider/utils/localStorageKey'; -import { fetchClient } from './utils'; +import { getFetchClient } from './utils'; class StrapiApp { constructor({ adminConfig, appPlugins, library, middlewares, reducers }) { @@ -452,7 +452,7 @@ class StrapiApp { showTutorials={this.configurations.tutorials} showReleaseNotification={this.configurations.notifications.releases} store={store} - fetchClient={fetchClient} + getFetchClient={getFetchClient} > <> { runHookSeries={jest.fn()} menu={[]} settings={{}} - fetchClient={{}} + getFetchClient={jest.fn()} > diff --git a/packages/core/admin/admin/src/components/Providers/index.js b/packages/core/admin/admin/src/components/Providers/index.js index d29841afa1..d740074b7a 100644 --- a/packages/core/admin/admin/src/components/Providers/index.js +++ b/packages/core/admin/admin/src/components/Providers/index.js @@ -42,7 +42,7 @@ const Providers = ({ showTutorials, store, themes, - fetchClient, + getFetchClient, }) => { return ( @@ -64,7 +64,7 @@ const Providers = ({ runHookWaterfall={runHookWaterfall} runHookSeries={runHookSeries} settings={settings} - fetchClient={fetchClient} + getFetchClient={getFetchClient} > @@ -147,7 +147,7 @@ Providers.propTypes = { }).isRequired, custom: PropTypes.object, }).isRequired, - fetchClient: PropTypes.object.isRequired, + getFetchClient: PropTypes.func.isRequired, }; export default Providers; diff --git a/packages/core/admin/admin/src/pages/SettingsPage/tests/index.test.js b/packages/core/admin/admin/src/pages/SettingsPage/tests/index.test.js index b774141712..d9386ea744 100644 --- a/packages/core/admin/admin/src/pages/SettingsPage/tests/index.test.js +++ b/packages/core/admin/admin/src/pages/SettingsPage/tests/index.test.js @@ -43,7 +43,7 @@ const makeApp = (history, settings) => ( runHookWaterfall={jest.fn()} runHookSeries={jest.fn()} menu={[]} - fetchClient={{}} + getFetchClient={jest.fn()} > diff --git a/packages/core/admin/admin/src/utils/fetchClient.js b/packages/core/admin/admin/src/utils/fetchClient.js deleted file mode 100644 index 6d5c05e6b6..0000000000 --- a/packages/core/admin/admin/src/utils/fetchClient.js +++ /dev/null @@ -1,48 +0,0 @@ -import axios from 'axios'; -import { auth } from '@strapi/helper-plugin'; - -function getFetchClient() { - const instance = axios.create({ - baseURL: process.env.STRAPI_ADMIN_BACKEND_URL, - }); - - instance.interceptors.request.use( - async (config) => { - config.headers = { - Authorization: `Bearer ${auth.getToken()}`, - Accept: 'application/json', - 'Content-Type': 'application/json', - }; - - return config; - }, - (error) => { - Promise.reject(error); - } - ); - - instance.interceptors.response.use( - (response) => response, - (error) => { - // whatever you want to do with the error - if (error?.response?.status === 401) { - auth.clearAppStorage(); - window.location.reload(); - } - - throw error; - } - ); - - return instance; -} - -const instance = getFetchClient(); - -export default { - get: instance.get, - put: instance.put, - post: instance.post, - delete: instance.delete, - getFetchClient, -}; diff --git a/packages/core/admin/admin/src/utils/getFetchClient.js b/packages/core/admin/admin/src/utils/getFetchClient.js new file mode 100644 index 0000000000..30f206823b --- /dev/null +++ b/packages/core/admin/admin/src/utils/getFetchClient.js @@ -0,0 +1,45 @@ +import axios from 'axios'; +import { auth } from '@strapi/helper-plugin'; + +const instance = axios.create({ + baseURL: process.env.STRAPI_ADMIN_BACKEND_URL, +}); + +instance.interceptors.request.use( + async (config) => { + config.headers = { + Authorization: `Bearer ${auth.getToken()}`, + Accept: 'application/json', + 'Content-Type': 'application/json', + }; + + return config; + }, + (error) => { + Promise.reject(error); + } +); + +instance.interceptors.response.use( + (response) => response, + (error) => { + // whatever you want to do with the error + if (error?.response?.status === 401) { + auth.clearAppStorage(); + window.location.reload(); + } + + throw error; + } +); + +const getFetchClient = () => { + return { + get: (url, config) => instance.get(url, config), + put: (url, data, config) => instance.put(url, data, config), + post: (url, data, config) => instance.post(url, data, config), + delete: (url, config) => instance.get(url, config), + }; +}; + +export default getFetchClient; diff --git a/packages/core/admin/admin/src/utils/index.js b/packages/core/admin/admin/src/utils/index.js index 55b7eaa351..ec094c0290 100644 --- a/packages/core/admin/admin/src/utils/index.js +++ b/packages/core/admin/admin/src/utils/index.js @@ -7,4 +7,4 @@ export { default as sortLinks } from './sortLinks'; export { default as getExistingActions } from './getExistingActions'; export { default as getRequestUrl } from './getRequestUrl'; export { default as getFullName } from './getFullName'; -export { default as fetchClient } from './fetchClient'; +export { default as getFetchClient } from './getFetchClient'; diff --git a/packages/core/content-type-builder/admin/src/components/DataManagerProvider/index.js b/packages/core/content-type-builder/admin/src/components/DataManagerProvider/index.js index 7ce08ddbef..bd030e2dc9 100644 --- a/packages/core/content-type-builder/admin/src/components/DataManagerProvider/index.js +++ b/packages/core/content-type-builder/admin/src/components/DataManagerProvider/index.js @@ -108,7 +108,7 @@ const DataManagerProvider = ({ { data: reservedNames }, ] = await Promise.all( ['components', 'content-types', 'reserved-names'].map((endPoint) => { - // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient + // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient return axiosInstance.get(endPoint); }) ); @@ -266,7 +266,7 @@ const DataManagerProvider = ({ if (userConfirm) { lockAppWithAutoreload(); - // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient + // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient await axiosInstance.delete(requestURL); // Make sure the server has restarted @@ -316,7 +316,7 @@ const DataManagerProvider = ({ } lockAppWithAutoreload(); - // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient + // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient await axiosInstance.delete(requestURL); // Make sure the server has restarted @@ -350,7 +350,7 @@ const DataManagerProvider = ({ lockAppWithAutoreload(); // Update the category - // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient + // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient await axiosInstance({ url: requestURL, method: 'PUT', data: body }); // Make sure the server has restarted @@ -508,7 +508,7 @@ const DataManagerProvider = ({ // Lock the app lockAppWithAutoreload(); - // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient + // TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient await axiosInstance({ url: requestURL, method, diff --git a/packages/core/content-type-builder/admin/src/utils/axiosInstance.js b/packages/core/content-type-builder/admin/src/utils/axiosInstance.js index 68a72432b1..6de08f4c44 100644 --- a/packages/core/content-type-builder/admin/src/utils/axiosInstance.js +++ b/packages/core/content-type-builder/admin/src/utils/axiosInstance.js @@ -1,6 +1,6 @@ import axios from 'axios'; import { auth, wrapAxiosInstance } from '@strapi/helper-plugin'; -// TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from fetchClient +// TODO: remember to pass also the pluginId when you use the new get, post, put, delete methods from getFetchClient import pluginId from '../pluginId'; const instance = axios.create({ diff --git a/packages/core/helper-plugin/lib/src/hooks/useFetchClient/index.js b/packages/core/helper-plugin/lib/src/hooks/useFetchClient/index.js index 8cc6c466e2..9d8713058e 100644 --- a/packages/core/helper-plugin/lib/src/hooks/useFetchClient/index.js +++ b/packages/core/helper-plugin/lib/src/hooks/useFetchClient/index.js @@ -2,9 +2,9 @@ import { useContext } from 'react'; import StrapiAppContext from '../../contexts/StrapiAppContext'; const useFetchClient = () => { - const { fetchClient } = useContext(StrapiAppContext); + const { getFetchClient } = useContext(StrapiAppContext); - return fetchClient; + return getFetchClient(); }; export default useFetchClient; diff --git a/packages/core/helper-plugin/lib/src/providers/StrapiAppProvider/index.js b/packages/core/helper-plugin/lib/src/providers/StrapiAppProvider/index.js index 19cb60373e..2fc6fee034 100644 --- a/packages/core/helper-plugin/lib/src/providers/StrapiAppProvider/index.js +++ b/packages/core/helper-plugin/lib/src/providers/StrapiAppProvider/index.js @@ -31,7 +31,7 @@ StrapiAppProvider.propTypes = { runHookWaterfall: PropTypes.func.isRequired, runHookSeries: PropTypes.func.isRequired, settings: PropTypes.object.isRequired, - fetchClient: PropTypes.object.isRequired, + getFetchClient: PropTypes.func.isRequired, }; export default StrapiAppProvider;