Skip to content

Commit

Permalink
FIX api service manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetkuslular committed May 27, 2022
1 parent 75a8df4 commit c8b5ced
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 91 deletions.
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 };
42 changes: 14 additions & 28 deletions src/renderMultiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,24 @@ 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);
});
}

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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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;
},
{}
Expand Down
11 changes: 0 additions & 11 deletions src/universal/core/api/ClientApiManager.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/universal/core/api/ClientApiManagerCache.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/universal/core/api/ServerApiManagerCache.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/universal/core/api/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -40,4 +40,4 @@ class ApiManager {
}
}

export default ApiManager;
export default BaseApiManager;
11 changes: 11 additions & 0 deletions src/universal/core/apiService/apiManager/ClientApiManager.js
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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)
});
Expand Down
3 changes: 3 additions & 0 deletions src/universal/core/apiService/apiManager/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as BaseApiManager } from './BaseApiManager';
export { default as ClientApiManager } from './ClientApiManager';
export { default as ServerApiManager } from './ServerApiManager';
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/universal/core/apiService/apiManagerCache/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as ServerApiManagerCache } from './ServerApiManagerCache';
export { default as ClientApiManagerCache } from './ClientApiManagerCache';
10 changes: 10 additions & 0 deletions src/universal/core/apiService/apiService.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/universal/core/apiService/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default } from './apiService';
export { default as ClientApiManager } from './apiManagerCache/ClientApiManagerCache';
export { default as ServerApiManager } from './apiManagerCache/ServerApiManagerCache';
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -101,5 +101,4 @@ function createApiClient(apiManager) {
};
}

// eslint-disable-next-line import/prefer-default-export
export { createApiClient };
export default createApiClient;
15 changes: 15 additions & 0 deletions src/universal/core/apiService/utils/createCache.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions src/universal/core/apiService/utils/freezeServices.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions src/universal/model/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 0 additions & 8 deletions src/universal/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -41,6 +34,5 @@ export {
JSON_CONTENT_TYPE,
CONTENT_TYPE_HEADER,
REQUEST_TYPES_WITH_BODY,
SERVICES,
BLACKLIST_OUTPUT
};

0 comments on commit c8b5ced

Please sign in to comment.