From b09c5477615c499dad6500acd3528042806c5b31 Mon Sep 17 00:00:00 2001 From: stefano bovio Date: Tue, 21 Dec 2021 16:25:04 +0100 Subject: [PATCH] clean up dev server variables and requests (#691) --- geonode_mapstore_client/client/.env.sample | 4 ++ geonode_mapstore_client/client/.gitignore | 1 + geonode_mapstore_client/client/devServer.js | 14 +++---- .../client/js/api/geonode/v2/index.js | 37 +++++-------------- .../client/js/utils/APIUtils.js | 9 +---- .../client/js/utils/AppUtils.js | 24 ++++++++++-- geonode_mapstore_client/client/package.json | 8 +--- 7 files changed, 42 insertions(+), 55 deletions(-) create mode 100644 geonode_mapstore_client/client/.env.sample diff --git a/geonode_mapstore_client/client/.env.sample b/geonode_mapstore_client/client/.env.sample new file mode 100644 index 0000000000..f2d059eb15 --- /dev/null +++ b/geonode_mapstore_client/client/.env.sample @@ -0,0 +1,4 @@ + +DEV_SERVER_PROTOCOL=http +DEV_SERVER_HOSTNAME=localhost +DEV_TARGET_GEONODE_HOST=localhost:8000 diff --git a/geonode_mapstore_client/client/.gitignore b/geonode_mapstore_client/client/.gitignore index 1c04c41c8c..710bcf5ad9 100644 --- a/geonode_mapstore_client/client/.gitignore +++ b/geonode_mapstore_client/client/.gitignore @@ -10,3 +10,4 @@ package-lock.json yarn.lock *.code-workspace docs/ +.env diff --git a/geonode_mapstore_client/client/devServer.js b/geonode_mapstore_client/client/devServer.js index f30012f484..e72297331b 100644 --- a/geonode_mapstore_client/client/devServer.js +++ b/geonode_mapstore_client/client/devServer.js @@ -1,17 +1,15 @@ const path = require('path'); const fs = require('fs'); +const envConfig = fs.existsSync(path.join(__dirname, '.env')) + ? require('dotenv').config().parsed + : {}; module.exports = (devServerDefault, projectConfig) => { const appDirectory = projectConfig.appDirectory; - const envPath = path.resolve(appDirectory, 'env.json'); - const envJson = fs.existsSync(envPath) ? require(envPath) : {}; - const packageJSON = require(path.resolve(appDirectory, 'package.json')) || {}; - const geoNodeProjectConfig = packageJSON.geonode || {}; - const devServerOptions = geoNodeProjectConfig.devServer || {}; - const devServerHost = devServerOptions.host || envJson.DEV_SERVER_HOST || 'localhost'; - const proxyTargetHost = devServerOptions.proxyTargetHost || envJson.DEV_SERVER_PROXY_TARGET_HOST || 'localhost:8000'; - const protocol = devServerOptions.protocol || envJson.DEV_SERVER_HOST_PROTOCOL || 'http'; + const devServerHost = envConfig.DEV_SERVER_HOSTNAME || 'localhost'; + const proxyTargetHost = envConfig.DEV_TARGET_GEONODE_HOST || 'localhost:8000'; + const protocol = envConfig.DEV_SERVER_PROTOCOL || 'http'; const proxyTargetURL = `${protocol}://${proxyTargetHost}`; diff --git a/geonode_mapstore_client/client/js/api/geonode/v2/index.js b/geonode_mapstore_client/client/js/api/geonode/v2/index.js index 907f7ece07..3aaa392dbb 100644 --- a/geonode_mapstore_client/client/js/api/geonode/v2/index.js +++ b/geonode_mapstore_client/client/js/api/geonode/v2/index.js @@ -8,9 +8,7 @@ import axios from '@mapstore/framework/libs/ajax'; import { - parseDevHostname, - setRequestOptions, - getRequestOptions + parseDevHostname } from '@js/utils/APIUtils'; import merge from 'lodash/merge'; import mergeWith from 'lodash/mergeWith'; @@ -62,23 +60,6 @@ function addCountToLabel(name, count) { return `${name} (${count || 0})`; } -const requestOptions = (name, requestFunc) => { - const options = getRequestOptions(name); - if (!options) { - return axios.options(parseDevHostname(endpoints[name])) - .then(({ data }) => { - setRequestOptions(name, data); - return requestFunc(data); - }) - .catch(() => { - const error = { error: true }; - setRequestOptions(name, error); - return requestFunc(error); - }); - } - return requestFunc(options); -}; - // some fields such as search_fields does not support the array notation `key[]=value1&key[]=value2` // this function will parse all values included array in the `key=value1&key=value2` format function addQueryString(requestUrl, params) { @@ -153,7 +134,7 @@ export const getResources = ({ .filter(({ id }) => castArray(f || []).indexOf(id) !== -1) .reduce((acc, filter) => mergeCustomQuery(acc, filter.query || {}), {}) || {}; - return requestOptions(RESOURCES, () => axios.get(parseDevHostname( + return axios.get(parseDevHostname( addQueryString(endpoints[RESOURCES], q && { search: q, search_fields: ['title', 'abstract'] @@ -176,7 +157,7 @@ export const getResources = ({ return resource; }) }; - })); + }); }; export const getMaps = ({ @@ -186,7 +167,7 @@ export const getMaps = ({ sort, ...params }) => { - return requestOptions(MAPS, () => axios + return axios .get( parseDevHostname( addQueryString(endpoints[MAPS], q && { @@ -211,7 +192,7 @@ export const getMaps = ({ return resource; }) }; - })); + }); }; export const getDatasets = ({ @@ -221,7 +202,7 @@ export const getDatasets = ({ sort, ...params }) => { - return requestOptions(DATASETS, () => axios + return axios .get( parseDevHostname( addQueryString(endpoints[DATASETS], q && { @@ -246,7 +227,7 @@ export const getDatasets = ({ return resource; }) }; - })); + }); }; export const getDocumentsByDocType = (docType = 'image', { @@ -257,7 +238,7 @@ export const getDocumentsByDocType = (docType = 'image', { ...params }) => { - return requestOptions(MAPS, () => axios + return axios .get( parseDevHostname( addQueryString(endpoints[DOCUMENTS], q && { @@ -283,7 +264,7 @@ export const getDocumentsByDocType = (docType = 'image', { return resource; }) }; - })); + }); }; export const setMapThumbnail = (pk, body) => { diff --git a/geonode_mapstore_client/client/js/utils/APIUtils.js b/geonode_mapstore_client/client/js/utils/APIUtils.js index 002b42d9ea..96e4600c1f 100644 --- a/geonode_mapstore_client/client/js/utils/APIUtils.js +++ b/geonode_mapstore_client/client/js/utils/APIUtils.js @@ -8,8 +8,6 @@ import url from 'url'; -let requestOptions = {}; - /** * Utilities for api requests * @module utils/APIUtils @@ -37,11 +35,6 @@ export const parseDevHostname = (requestUrl) => { return requestUrl; }; -export const setRequestOptions = (name, options) => { requestOptions[name] = options; }; -export const getRequestOptions = name => requestOptions[name]; - export default { - parseDevHostname, - setRequestOptions, - getRequestOptions + parseDevHostname }; diff --git a/geonode_mapstore_client/client/js/utils/AppUtils.js b/geonode_mapstore_client/client/js/utils/AppUtils.js index 231caaa478..9b19fd4f6d 100644 --- a/geonode_mapstore_client/client/js/utils/AppUtils.js +++ b/geonode_mapstore_client/client/js/utils/AppUtils.js @@ -29,8 +29,23 @@ import url from 'url'; import axios from '@mapstore/framework/libs/ajax'; let actionListeners = {}; -// Add a taget url here to fix proxy issue -const targetURL = ''; +// Target url here to fix proxy issue +let targetURL = ''; +const getTargetUrl = () => { + if (!__DEVTOOLS__) { + return ''; + } + if (targetURL) { + return targetURL; + } + const geonodeUrl = getConfigProp('geoNodeSettings')?.geonodeUrl || ''; + if (!geonodeUrl) { + return ''; + } + const { host, protocol } = url.parse(geonodeUrl); + targetURL = `${protocol}//${host}`; + return targetURL; +}; export function getVersion() { if (!__DEVTOOLS__) { @@ -62,10 +77,11 @@ export function initializeApp() { } }; } - if (__DEVTOOLS__ && targetURL && config.url?.match(targetURL)?.[0]) { + const tUrl = getTargetUrl(); + if (tUrl && config.url?.match(tUrl)?.[0]) { return { ...config, - url: config.url.replace(targetURL, '') + url: config.url.replace(tUrl, '') }; } return config; diff --git a/geonode_mapstore_client/client/package.json b/geonode_mapstore_client/client/package.json index f7280e00cd..19b2dcb2c0 100644 --- a/geonode_mapstore_client/client/package.json +++ b/geonode_mapstore_client/client/package.json @@ -27,6 +27,7 @@ "license": "BSD-2-Clause", "devDependencies": { "@mapstore/project": "1.0.20", + "dotenv": "10.0.0", "jsdoc": "3.6.7" }, "dependencies": { @@ -35,13 +36,6 @@ "mapstore": "file:MapStore2", "react-helmet": "6.1.0" }, - "geonode": { - "devServer": { - "host": "localhost", - "proxyTargetHost": "localhost:8000", - "protocol": "http" - } - }, "mapstore": { "output": "dist", "publicPath": "/static/mapstore/dist/",