Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetkuslular committed Jun 8, 2022
1 parent c8b5ced commit e99ee55
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 53 deletions.
4 changes: 2 additions & 2 deletions config/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ function replaceString() {
flags: 'g'
},
{
search: '__V_PREVIEW_PAGES__',
search: '__V_PREVIEW__',
replace: normalizeUrl(
voltranConfig.routing.previewPages || path.resolve(__dirname, './emptyModule.js')
voltranConfig.routing.preview || path.resolve(__dirname, './emptyModule.js')
),
flags: 'g'
},
Expand Down
18 changes: 4 additions & 14 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import xss from 'xss';

import { matchUrlInRouteConfigs } from './universal/core/route/routeUtils';
import Preview from './universal/components/Preview';
import { BLACKLIST_OUTPUT, HTTP_STATUS_CODES } from './universal/utils/constants';
import metrics from './metrics';
import {
Expand All @@ -14,20 +13,9 @@ import {
import Component from './universal/model/Component';
import logger from './universal/utils/logger';
import omit from 'lodash/omit';
import { getPreviewFile } from './universal/utils/previewHelper';

const appConfig = require('__APP_CONFIG__');
const previewPages = require('__V_PREVIEW_PAGES__');

function getPreview(output) {
const { layouts = {} } = previewPages?.default || {};
let PreviewFile = Preview;

if (layouts.default) {
PreviewFile = layouts.default;
}

return PreviewFile(output);
}

const render = async (req, res) => {
const isWithoutStateValue = isWithoutState(req.query);
Expand Down Expand Up @@ -102,8 +90,10 @@ const render = async (req, res) => {
context
);
const requestDispatcherFullHtml = requestDispatcherResponse.fullHtml;
const PreviewFile = getPreviewFile(context.query);
const body = [requestDispatcherFullHtml, fullHtml].join('\n');

const response = getPreview([requestDispatcherFullHtml, fullHtml].join('\n'));
const response = PreviewFile({ body, componentName });

res.status(statusCode).html(response);
} else {
Expand Down
22 changes: 9 additions & 13 deletions src/renderMultiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import async from 'async';
import { matchUrlInRouteConfigs } from './universal/core/route/routeUtils';
import Component from './universal/model/Component';
import Renderer from './universal/model/Renderer';
import Preview from './universal/components/Preview';
import {
isRequestDispatcher,
isPreview,
isWithoutHTML,
isWithoutState,
getPreviewLayout
isWithoutState
} from './universal/service/RenderService';
import metrics from './metrics';
import { HTTP_STATUS_CODES } from './universal/utils/constants';
import logger from './universal/utils/logger';

const previewPages = require('__V_PREVIEW_PAGES__');
import { getPreviewFile } from './universal/utils/previewHelper';

const getRenderOptions = req => {
const isPreviewValue = isPreview(req.query) || false;
Expand Down Expand Up @@ -192,21 +189,20 @@ async function getResponses(renderers) {
}

async function getPreview(responses, requestCount, req) {
const layoutName = getPreviewLayout(req.query);
const { layouts = {} } = previewPages?.default || {};
const componentNames = Object.keys(responses);
let PreviewFile = Preview;

if (layouts[layoutName]) {
PreviewFile = layouts[layoutName];
}
const PreviewFile = getPreviewFile(req.query);

const content = Object.keys(responses).map(name => {
const componentName = responses?.[name]?.activeComponent?.componentName ?? '';
return getLayoutWithClass(componentName, responses[name].fullHtml);
});
const body = [...content].join('\n');

return PreviewFile([...content].join('\n'), `${requestCount} request!`, componentNames);
return PreviewFile({
body,
requestCount,
componentNames
});
}

const DEFAULT_PARTIALS = ['RequestDispatcher'];
Expand Down
4 changes: 2 additions & 2 deletions src/universal/components/Preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const appConfig = require('__APP_CONFIG__');

export default (body, title = null) => {
const additionalTitle = title ? ` - ${title}` : '';
export default ({ body, componentName = '' }) => {
const additionalTitle = componentName ? ` - ${componentName}` : '';

function cr(condition, ok, cancel) {
return condition ? ok : cancel || '';
Expand Down
19 changes: 19 additions & 0 deletions src/universal/core/apiService/apiManager/BaseApiManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import axios from 'axios';
import Cookies from 'js-cookie';
import {
CONTENT_TYPE_HEADER,
JSON_CONTENT_TYPE,
REQUEST_TYPES_WITH_BODY
} from '../../../utils/constants';
import voltranConfig from '../../../../../voltran.config';
import CookieService from '../../../service/CookieService';

function createBaseConfig() {
return {};
Expand All @@ -22,6 +25,22 @@ class BaseApiManager {
headers['Accept-Encoding'] = 'gzip, deflate';
}

if (process.env.BROWSER && voltranConfig.setCookiesToHeader) {
const cookieMap = CookieService.getAllItems();

Object.keys(cookieMap).forEach(key => {
if (voltranConfig.setCookiesToHeaderKeys.length > 0) {
voltranConfig.setCookiesToHeaderKeys.map(item => {
if (key.indexOf(item) === 0) {
headers[key] = cookieMap[key];
}
});
} else {
headers[key] = cookieMap[key];
}
});
}

this.api = this.createInstance({
...createBaseConfig(),
...customConfig,
Expand Down
12 changes: 9 additions & 3 deletions src/universal/core/apiService/apiManager/ClientApiManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import createApiClient from '../utils/createApiClient';
import BaseApiManager from './BaseApiManager';

export default (config, timeout) => {
export default (entity, serviceConfigs) => {
const baseURL = entity.clientUrl || entity.url || entity.serverUrl || '/';
const config = {
...serviceConfigs,
...entity?.config
};

const apiManager = new BaseApiManager({
baseURL: config.clientUrl || config.url || config.serverUrl || '/',
timeout
baseURL,
...config
});

return createApiClient(apiManager);
Expand Down
6 changes: 3 additions & 3 deletions src/universal/core/apiService/apiManager/ServerApiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const BASE_HTTP_AGENT_CONFIG = {
rejectUnauthorized: false
};

export default (config, timeout) => {
export default (entity, serviceConfigs) => {
const apiManager = new BaseApiManager({
timeout,
baseURL: config.serverUrl || config.url || config.clientUrl || '/',
baseURL: entity.serverUrl || entity.url || entity.clientUrl || '/',
...serviceConfigs,
httpAgent: new http.Agent(BASE_HTTP_AGENT_CONFIG),
httpsAgent: new https.Agent(BASE_HTTP_AGENT_CONFIG)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import ClientApiManager from '../apiManager/ClientApiManager';
import createCache from '../utils/createCache';

const { services, timeouts } = require('__APP_CONFIG__');
const { services, serviceConfigs } = require('__APP_CONFIG__');

const cache = createCache(ClientApiManager, services, timeouts.clientApiManager);
const cache = createCache(ClientApiManager, services, serviceConfigs?.client);

export default cache;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import ServerApiManager from '../apiManager/ServerApiManager';
import createCache from '../utils/createCache';

const { services, timeouts } = require('__APP_CONFIG__');
const { services, serviceConfigs } = require('__APP_CONFIG__');

const cache = createCache(ServerApiManager, services, timeouts.serverApiManager);
const cache = createCache(ServerApiManager, services, serviceConfigs?.server);

export default cache;
4 changes: 2 additions & 2 deletions src/universal/partials/Welcome/partials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import components from '../../core/route/components';

const previewPages = require('__V_PREVIEW_PAGES__');
const preview = require('__V_PREVIEW__');

const partials = [];
const BLACKLIST = ['REQUEST_DISPATCHER'];
Expand All @@ -14,7 +14,7 @@ Object.keys(components).forEach(path => {
});
}
});
const pages = previewPages?.default?.pages || [];
const pages = preview?.default?.pages || [];
partials.push(...pages);

export default partials;
27 changes: 27 additions & 0 deletions src/universal/service/CookieService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Cookies from 'js-cookie';

const appConfig = require('__APP_CONFIG__');

const domain = appConfig.cookieStorageUrl;

export default class CookieService {
static setItem(key, value, options) {
Cookies.set(key, value, { domain, ...options });
}

static getItem(key) {
return Cookies.get(key);
}

static getAllItems() {
return Cookies.get();
}

static getJSON(key) {
return Cookies.getJSON(key);
}

static removeItem(key, options) {
Cookies.remove(key, { domain, ...options });
}
}
9 changes: 0 additions & 9 deletions src/universal/service/RenderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ const isPreview = query => {
return false;
};

const getPreviewLayout = query => {
if (isPreview(query)) {
return query?.preview;
}

return '';
};

const isWithoutState = query => {
return query.withoutState === '';
};
Expand Down Expand Up @@ -116,7 +108,6 @@ export {
getStates,
isWithoutHTML,
isPreview,
getPreviewLayout,
isRequestDispatcher,
isWithoutState,
renderComponent
Expand Down
1 change: 0 additions & 1 deletion src/universal/utils/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const appConfig = require('__APP_CONFIG__');
const voltranConfig = require('../../../voltran.config');

const WINDOW_GLOBAL_PARAMS = {
Expand Down
26 changes: 26 additions & 0 deletions src/universal/utils/previewHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Preview from '../components/Preview';
import { isPreview } from '../service/RenderService';

const previewPages = require('__V_PREVIEW__');

const getPreviewLayout = query => {
if (isPreview(query)) {
return query?.preview;
}

return '';
};

export const getPreviewFile = query => {
const layoutName = getPreviewLayout(query);
const { previewLayouts = {} } = previewPages?.default || {};
let PreviewFile = Preview;

if (previewLayouts[layoutName]) {
PreviewFile = previewLayouts[layoutName];
} else if (previewLayouts.default) {
PreviewFile = previewLayouts.default;
}

return PreviewFile;
};

0 comments on commit e99ee55

Please sign in to comment.