From 976055f6a268b0dce34f7633bbbdc02c9fa3438b Mon Sep 17 00:00:00 2001 From: ahmetkuslular Date: Wed, 27 Apr 2022 23:53:41 +0300 Subject: [PATCH] FIX api service manager --- src/index.js | 5 +-- src/renderMultiple.js | 42 +++++++------------ src/universal/core/api/ClientApiManager.js | 11 ----- .../core/api/ClientApiManagerCache.js | 13 ------ .../core/api/ServerApiManagerCache.js | 13 ------ src/universal/core/api/index.js | 2 - .../apiManager/BaseApiManager.js} | 6 +-- .../apiService/apiManager/ClientApiManager.js | 11 +++++ .../apiManager}/ServerApiManager.js | 8 ++-- .../core/apiService/apiManager/index.js | 3 ++ .../apiManagerCache/ClientApiManagerCache.js | 9 ++++ .../apiManagerCache/ServerApiManagerCache.js | 9 ++++ .../core/apiService/apiManagerCache/index.js | 2 + src/universal/core/apiService/apiService.js | 10 +++++ src/universal/core/apiService/index.js | 3 ++ .../apiService/utils/createApiClient.js} | 8 ++-- .../core/apiService/utils/createCache.js | 15 +++++++ .../core/apiService/utils/freezeServices.js | 11 +++++ src/universal/model/Renderer.js | 3 +- src/universal/utils/constants.js | 8 ---- 20 files changed, 101 insertions(+), 91 deletions(-) delete mode 100644 src/universal/core/api/ClientApiManager.js delete mode 100644 src/universal/core/api/ClientApiManagerCache.js delete mode 100644 src/universal/core/api/ServerApiManagerCache.js delete mode 100644 src/universal/core/api/index.js rename src/universal/core/{api/ApiManager.js => apiService/apiManager/BaseApiManager.js} (89%) create mode 100644 src/universal/core/apiService/apiManager/ClientApiManager.js rename src/universal/core/{api => apiService/apiManager}/ServerApiManager.js (61%) create mode 100644 src/universal/core/apiService/apiManager/index.js create mode 100644 src/universal/core/apiService/apiManagerCache/ClientApiManagerCache.js create mode 100644 src/universal/core/apiService/apiManagerCache/ServerApiManagerCache.js create mode 100644 src/universal/core/apiService/apiManagerCache/index.js create mode 100644 src/universal/core/apiService/apiService.js create mode 100644 src/universal/core/apiService/index.js rename src/universal/{common/network/apiUtils.js => core/apiService/utils/createApiClient.js} (93%) create mode 100644 src/universal/core/apiService/utils/createCache.js create mode 100644 src/universal/core/apiService/utils/freezeServices.js diff --git a/src/index.js b/src/index.js index d217a12..a17b25e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,5 @@ import voltran from './universal/partials/withBaseComponent'; -import { SERVICES } from './universal/utils/constants'; -import { ClientApiManager, ServerApiManager } from './universal/core/api'; +import apiService, { ClientApiManager, ServerApiManager } from './universal/core/apiService'; export default voltran; -export { SERVICES, ClientApiManager, ServerApiManager }; +export { ClientApiManager, ServerApiManager, apiService }; diff --git a/src/renderMultiple.js b/src/renderMultiple.js index 92f21b1..cdb367f 100644 --- a/src/renderMultiple.js +++ b/src/renderMultiple.js @@ -63,22 +63,16 @@ function getRenderer(name, req) { } function iterateServicesMap(servicesMap, callback) { - Object.getOwnPropertySymbols(servicesMap).forEach(serviceName => { - const endPoints = servicesMap[serviceName]; + Object.getOwnPropertyNames(servicesMap).forEach(serviceName => { + const requests = servicesMap[serviceName]; - Object.keys(endPoints).forEach(endPointName => { - callback(serviceName, endPointName); - }); + callback(serviceName, requests); }); } -function reduceServicesMap(servicesMap, callback, initialValue) { - return Object.getOwnPropertySymbols(servicesMap).map(serviceName => { - const endPoints = servicesMap[serviceName]; - - return Object.keys(endPoints).reduce((obj, endPointName) => { - return callback(serviceName, endPointName, obj); - }, initialValue); +function reduceServicesMap(servicesMap, callback, obj) { + return Object.getOwnPropertyNames(servicesMap).map(serviceName => { + return callback(serviceName, obj); }); } @@ -86,9 +80,7 @@ function getHashes(renderers) { return renderers .filter(renderer => renderer.servicesMap) .reduce((hashes, renderer) => { - iterateServicesMap(renderer.servicesMap, (serviceName, endPointName) => { - const requests = renderer.servicesMap[serviceName][endPointName]; - + iterateServicesMap(renderer.servicesMap, (serviceName, requests) => { requests.forEach(request => { if (hashes[request.hash]) { hashes[request.hash].occurrence += 1; @@ -121,12 +113,8 @@ function incWinnerScore(winner, hashes) { hashes[winner.hash].score += 1; } -function putWinnerMap(serviceName, endPointName, winnerMap, winner) { - if (winnerMap[serviceName]) { - winnerMap[serviceName][endPointName] = winner; - } else { - winnerMap[serviceName] = { [endPointName]: winner }; - } +function putWinnerMap(serviceName, winnerMap, winner) { + winnerMap[serviceName] = winner; } async function setInitialStates(renderers) { @@ -135,12 +123,10 @@ async function setInitialStates(renderers) { const promises = renderers .filter(renderer => renderer.servicesMap) .reduce((promises, renderer) => { - iterateServicesMap(renderer.servicesMap, (serviceName, endPointName) => { - const requests = renderer.servicesMap[serviceName][endPointName]; - + iterateServicesMap(renderer.servicesMap, (serviceName, requests) => { const winner = getWinner(requests, hashes); incWinnerScore(winner, hashes); - putWinnerMap(serviceName, endPointName, renderer.winnerMap, winner); + putWinnerMap(serviceName, renderer.winnerMap, winner); if (!promises[winner.hash]) { promises[winner.hash] = callback => { @@ -176,9 +162,9 @@ async function setInitialStates(renderers) { renderer.setInitialState( reduceServicesMap( renderer.winnerMap, - (serviceName, endPointName, obj) => { - const request = renderer.winnerMap[serviceName][endPointName]; - obj[endPointName] = results[request.hash]; + (serviceName, obj) => { + const request = renderer.winnerMap[serviceName]; + obj[serviceName] = results[request.hash]; return obj; }, {} diff --git a/src/universal/core/api/ClientApiManager.js b/src/universal/core/api/ClientApiManager.js deleted file mode 100644 index 0513103..0000000 --- a/src/universal/core/api/ClientApiManager.js +++ /dev/null @@ -1,11 +0,0 @@ -import ApiManager from './ApiManager'; -import { createApiClient } from '../../common/network/apiUtils'; - -export default (config, timeout) => { - const apiManager = new ApiManager({ - baseURL: config.clientUrl, - timeout - }); - - return createApiClient(apiManager); -}; diff --git a/src/universal/core/api/ClientApiManagerCache.js b/src/universal/core/api/ClientApiManagerCache.js deleted file mode 100644 index 56ad45b..0000000 --- a/src/universal/core/api/ClientApiManagerCache.js +++ /dev/null @@ -1,13 +0,0 @@ -/* istanbul ignore file */ -import ClientApiManager from './ClientApiManager'; -import { SERVICES } from '../../utils/constants'; - -const { services, timeouts } = require('__APP_CONFIG__'); - -const cache = {}; - -Object.entries(services).forEach(entity => { - cache[SERVICES[entity[0]]] = ClientApiManager(entity[1], timeouts.clientApiManager); -}); - -export default cache; diff --git a/src/universal/core/api/ServerApiManagerCache.js b/src/universal/core/api/ServerApiManagerCache.js deleted file mode 100644 index b0e77a9..0000000 --- a/src/universal/core/api/ServerApiManagerCache.js +++ /dev/null @@ -1,13 +0,0 @@ -/* istanbul ignore file */ -import ServerApiManager from './ServerApiManager'; -import { SERVICES } from '../../utils/constants'; - -const { services, timeouts } = require('__APP_CONFIG__'); - -const cache = {}; - -Object.entries(services).forEach(entity => { - cache[SERVICES[entity[0]]] = ServerApiManager(entity[1], timeouts.serverApiManager); -}); - -export default cache; diff --git a/src/universal/core/api/index.js b/src/universal/core/api/index.js deleted file mode 100644 index 2990a60..0000000 --- a/src/universal/core/api/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export { default as ClientApiManager } from './ClientApiManagerCache'; -export { default as ServerApiManager } from './ServerApiManagerCache'; diff --git a/src/universal/core/api/ApiManager.js b/src/universal/core/apiService/apiManager/BaseApiManager.js similarity index 89% rename from src/universal/core/api/ApiManager.js rename to src/universal/core/apiService/apiManager/BaseApiManager.js index 778ea1e..80a621d 100644 --- a/src/universal/core/api/ApiManager.js +++ b/src/universal/core/apiService/apiManager/BaseApiManager.js @@ -3,13 +3,13 @@ import { CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE, REQUEST_TYPES_WITH_BODY -} from '../../utils/constants'; +} from '../../../utils/constants'; function createBaseConfig() { return {}; } -class ApiManager { +class BaseApiManager { constructor(customConfig) { const headers = { common: { @@ -40,4 +40,4 @@ class ApiManager { } } -export default ApiManager; +export default BaseApiManager; diff --git a/src/universal/core/apiService/apiManager/ClientApiManager.js b/src/universal/core/apiService/apiManager/ClientApiManager.js new file mode 100644 index 0000000..574fc0d --- /dev/null +++ b/src/universal/core/apiService/apiManager/ClientApiManager.js @@ -0,0 +1,11 @@ +import createApiClient from '../utils/createApiClient'; +import BaseApiManager from './BaseApiManager'; + +export default (config, timeout) => { + const apiManager = new BaseApiManager({ + baseURL: config.clientUrl || config.url || config.serverUrl || '/', + timeout + }); + + return createApiClient(apiManager); +}; diff --git a/src/universal/core/api/ServerApiManager.js b/src/universal/core/apiService/apiManager/ServerApiManager.js similarity index 61% rename from src/universal/core/api/ServerApiManager.js rename to src/universal/core/apiService/apiManager/ServerApiManager.js index e181237..acb7e5e 100644 --- a/src/universal/core/api/ServerApiManager.js +++ b/src/universal/core/apiService/apiManager/ServerApiManager.js @@ -1,5 +1,5 @@ -import ApiManager from './ApiManager'; -import { createApiClient } from '../../common/network/apiUtils'; +import createApiClient from '../utils/createApiClient'; +import BaseApiManager from './BaseApiManager'; import http from 'http'; import https from 'https'; @@ -10,9 +10,9 @@ const BASE_HTTP_AGENT_CONFIG = { }; export default (config, timeout) => { - const apiManager = new ApiManager({ + const apiManager = new BaseApiManager({ timeout, - baseURL: config.serverUrl, + baseURL: config.serverUrl || config.url || config.clientUrl || '/', httpAgent: new http.Agent(BASE_HTTP_AGENT_CONFIG), httpsAgent: new https.Agent(BASE_HTTP_AGENT_CONFIG) }); diff --git a/src/universal/core/apiService/apiManager/index.js b/src/universal/core/apiService/apiManager/index.js new file mode 100644 index 0000000..59fb173 --- /dev/null +++ b/src/universal/core/apiService/apiManager/index.js @@ -0,0 +1,3 @@ +export { default as BaseApiManager } from './BaseApiManager'; +export { default as ClientApiManager } from './ClientApiManager'; +export { default as ServerApiManager } from './ServerApiManager'; diff --git a/src/universal/core/apiService/apiManagerCache/ClientApiManagerCache.js b/src/universal/core/apiService/apiManagerCache/ClientApiManagerCache.js new file mode 100644 index 0000000..5dd1026 --- /dev/null +++ b/src/universal/core/apiService/apiManagerCache/ClientApiManagerCache.js @@ -0,0 +1,9 @@ +/* istanbul ignore file */ +import ClientApiManager from '../apiManager/ClientApiManager'; +import createCache from '../utils/createCache'; + +const { services, timeouts } = require('__APP_CONFIG__'); + +const cache = createCache(ClientApiManager, services, timeouts.clientApiManager); + +export default cache; diff --git a/src/universal/core/apiService/apiManagerCache/ServerApiManagerCache.js b/src/universal/core/apiService/apiManagerCache/ServerApiManagerCache.js new file mode 100644 index 0000000..2a56eeb --- /dev/null +++ b/src/universal/core/apiService/apiManagerCache/ServerApiManagerCache.js @@ -0,0 +1,9 @@ +/* istanbul ignore file */ +import ServerApiManager from '../apiManager/ServerApiManager'; +import createCache from '../utils/createCache'; + +const { services, timeouts } = require('__APP_CONFIG__'); + +const cache = createCache(ServerApiManager, services, timeouts.serverApiManager); + +export default cache; diff --git a/src/universal/core/apiService/apiManagerCache/index.js b/src/universal/core/apiService/apiManagerCache/index.js new file mode 100644 index 0000000..442c9ce --- /dev/null +++ b/src/universal/core/apiService/apiManagerCache/index.js @@ -0,0 +1,2 @@ +export { default as ServerApiManagerCache } from './ServerApiManagerCache'; +export { default as ClientApiManagerCache } from './ClientApiManagerCache'; diff --git a/src/universal/core/apiService/apiService.js b/src/universal/core/apiService/apiService.js new file mode 100644 index 0000000..c09c762 --- /dev/null +++ b/src/universal/core/apiService/apiService.js @@ -0,0 +1,10 @@ +import { ServerApiManagerCache, ClientApiManagerCache } from './apiManagerCache'; + +const getApiService = () => { + const isBrowser = typeof window !== 'undefined'; + + return isBrowser ? ClientApiManagerCache : ServerApiManagerCache; +}; + +const apiService = getApiService(); +export default apiService; diff --git a/src/universal/core/apiService/index.js b/src/universal/core/apiService/index.js new file mode 100644 index 0000000..d689927 --- /dev/null +++ b/src/universal/core/apiService/index.js @@ -0,0 +1,3 @@ +export { default } from './apiService'; +export { default as ClientApiManager } from './apiManagerCache/ClientApiManagerCache'; +export { default as ServerApiManager } from './apiManagerCache/ServerApiManagerCache'; diff --git a/src/universal/common/network/apiUtils.js b/src/universal/core/apiService/utils/createApiClient.js similarity index 93% rename from src/universal/common/network/apiUtils.js rename to src/universal/core/apiService/utils/createApiClient.js index d83a266..35d599e 100644 --- a/src/universal/common/network/apiUtils.js +++ b/src/universal/core/apiService/utils/createApiClient.js @@ -1,5 +1,5 @@ -import Request from '../../model/Request'; -import { createCacheManagerInstance } from '../../core/cache/cacheUtils'; +import Request from '../../../model/Request'; +import { createCacheManagerInstance } from '../../cache/cacheUtils'; function createApiClient(apiManager) { const cacheManager = createCacheManagerInstance(); @@ -35,6 +35,7 @@ function createApiClient(apiManager) { const payload = getPayload(url, method, params, configArgument); const uri = apiManager.api.getUri(payload); + console.log('URI:', uri); return new Request(apiManager.api, payload, uri, response); } @@ -101,5 +102,4 @@ function createApiClient(apiManager) { }; } -// eslint-disable-next-line import/prefer-default-export -export { createApiClient }; +export default createApiClient; diff --git a/src/universal/core/apiService/utils/createCache.js b/src/universal/core/apiService/utils/createCache.js new file mode 100644 index 0000000..2100ead --- /dev/null +++ b/src/universal/core/apiService/utils/createCache.js @@ -0,0 +1,15 @@ +import freezeServices from './freezeServices'; + +const createCache = (ApiManager, services, timeout) => { + const cache = {}; + const frozenServicesData = freezeServices(services); + + Object.entries(services).forEach(entity => { + const [serviceKey, serviceValues] = entity; + cache[frozenServicesData[serviceKey]] = ApiManager(serviceValues, timeout); + }); + + return cache; +}; + +export default createCache; diff --git a/src/universal/core/apiService/utils/freezeServices.js b/src/universal/core/apiService/utils/freezeServices.js new file mode 100644 index 0000000..ba6491b --- /dev/null +++ b/src/universal/core/apiService/utils/freezeServices.js @@ -0,0 +1,11 @@ +function freezeServices(services) { + return Object.freeze( + Object.keys(services).reduce((obj, val) => { + // eslint-disable-next-line no-param-reassign + obj[val] = val; + return obj; + }, {}) + ); +} + +export default freezeServices; diff --git a/src/universal/model/Renderer.js b/src/universal/model/Renderer.js index 731737e..bc98f3f 100644 --- a/src/universal/model/Renderer.js +++ b/src/universal/model/Renderer.js @@ -33,8 +33,7 @@ export default class Renderer { } getServicesWithMultiple() { - const options = { isServer: false }; - return this.component.object.getServicesWithMultiple(this.context, options); + return this.component.object.getServicesWithMultiple(this.context); } render() { diff --git a/src/universal/utils/constants.js b/src/universal/utils/constants.js index 684b0f9..5bc9c28 100644 --- a/src/universal/utils/constants.js +++ b/src/universal/utils/constants.js @@ -19,13 +19,6 @@ const HTTP_STATUS_CODES = { const JSON_CONTENT_TYPE = 'application/json'; const CONTENT_TYPE_HEADER = 'Content-Type'; const REQUEST_TYPES_WITH_BODY = ['post', 'put', 'patch']; -const SERVICES = Object.freeze( - Object.keys(appConfig.services).reduce((obj, val) => { - // eslint-disable-next-line no-param-reassign - obj[val] = Symbol(val); - return obj; - }, {}) -); const BLACKLIST_OUTPUT = [ 'componentName', @@ -41,6 +34,5 @@ export { JSON_CONTENT_TYPE, CONTENT_TYPE_HEADER, REQUEST_TYPES_WITH_BODY, - SERVICES, BLACKLIST_OUTPUT };