From 70da30434dfada20f66b78f17327b3fd6fe31b26 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/universal/core/api/ClientApiManager.js | 11 ----------- src/universal/core/api/ClientApiManagerCache.js | 13 ------------- src/universal/core/api/ServerApiManagerCache.js | 13 ------------- src/universal/core/api/index.js | 2 -- .../apiManager/BaseApiManager.js} | 6 +++--- .../apiService/apiManager/ClientApiManager.js | 11 +++++++++++ .../apiManager}/ServerApiManager.js | 6 +++--- src/universal/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/utils/constants.js | 8 -------- 18 files changed, 85 insertions(+), 60 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 (72%) 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/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..a4c12d3 --- /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, + timeout + }); + + return createApiClient(apiManager); +}; diff --git a/src/universal/core/api/ServerApiManager.js b/src/universal/core/apiService/apiManager/ServerApiManager.js similarity index 72% rename from src/universal/core/api/ServerApiManager.js rename to src/universal/core/apiService/apiManager/ServerApiManager.js index e181237..28bb154 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,7 +10,7 @@ const BASE_HTTP_AGENT_CONFIG = { }; export default (config, timeout) => { - const apiManager = new ApiManager({ + const apiManager = new BaseApiManager({ timeout, baseURL: config.serverUrl, httpAgent: new http.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/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 };