Skip to content

Commit 50d27b5

Browse files
author
ahmetkuslular
committed
FIX code review issues
1 parent f6a2827 commit 50d27b5

File tree

5 files changed

+34
-78
lines changed

5 files changed

+34
-78
lines changed

src/render.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import xss from 'xss';
22

33
import { matchUrlInRouteConfigs } from './universal/core/route/routeUtils';
44
import Preview from './universal/components/Preview';
5-
import { HTTP_STATUS_CODES } from './universal/utils/constants';
5+
import { BLACKLIST_OUTPUT, HTTP_STATUS_CODES } from './universal/utils/constants';
66
import metrics from './metrics';
77
import {
88
renderComponent,
@@ -13,6 +13,7 @@ import {
1313
} from './universal/service/RenderService';
1414
import Component from './universal/model/Component';
1515
import logger from './universal/utils/logger';
16+
import omit from 'lodash/omit';
1617

1718
const appConfig = require('__APP_CONFIG__');
1819

@@ -60,9 +61,9 @@ const render = async (req, res) => {
6061
scripts,
6162
activeComponent,
6263
componentName,
63-
seoState,
6464
isPreviewQuery,
65-
responseOptions
65+
responseOptions,
66+
...responseData
6667
} = renderResponse;
6768

6869
const statusCode = responseOptions?.isPartialContent
@@ -71,8 +72,8 @@ const render = async (req, res) => {
7172

7273
if (!isPreview(context.query)) {
7374
const html = renderLinksAndScripts(output, '', '');
74-
75-
res.status(statusCode).json({ html, scripts, style: links, activeComponent, seoState });
75+
const otherParams = omit(responseData, BLACKLIST_OUTPUT);
76+
res.status(statusCode).json({ html, scripts, style: links, activeComponent, ...otherParams });
7677

7778
metrics.fragmentRequestDurationMicroseconds
7879
.labels(componentName, isWithoutHTML(context.query) ? '1' : '0')

src/universal/model/Renderer.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import omit from 'lodash/omit';
22
import { isPreview, renderComponent, renderLinksAndScripts } from '../service/RenderService';
3+
import { BLACKLIST_OUTPUT } from '../utils/constants';
34

4-
const blacklistOutput = [
5-
'componentName',
6-
'fullWidth',
7-
'isMobileComponent',
8-
'isPreviewQuery',
9-
'responseOptions'
10-
];
115
export default class Renderer {
126
constructor(component, context) {
137
this.component = component;
@@ -47,7 +41,7 @@ export default class Renderer {
4741
return new Promise(resolve => {
4842
renderComponent(this.component, this.context, this.initialState).then(response => {
4943
const { output, links, fullHtml, ...rest } = response;
50-
const otherParams = omit(rest, blacklistOutput);
44+
const otherParams = omit(rest, BLACKLIST_OUTPUT);
5145
const html = renderLinksAndScripts(output, '', '');
5246

5347
resolve({

src/universal/service/RenderService.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ const isWithoutHTML = query => {
5454
};
5555

5656
const isPreview = query => {
57-
return query.preview === '';
57+
if (query.preview || query.preview === '') {
58+
return true;
59+
}
60+
return false;
5861
};
5962

6063
const getPreviewLayout = query => {
61-
if (query?.previewLayout) {
62-
return query?.previewLayout;
64+
if (isPreview(query)) {
65+
return query?.preview;
6366
}
6467

6568
return '';

src/universal/service/getStates.js

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,31 @@
1-
import omit from 'lodash/omit';
2-
import camelCase from 'lodash/camelCase';
3-
4-
const blacklistFunctionName = [
5-
'getServerSideProps',
6-
'getServicesWithMultiple',
7-
'getInitialStateWithMultiple',
8-
'setDependencies',
9-
'setSeoState',
10-
'setRedirection',
11-
'setPageData'
12-
];
13-
14-
const getCustomSetters = (component, context, data) => {
15-
const pattern = new RegExp(`^set`);
16-
const functions = omit(component, blacklistFunctionName);
1+
const getResponseData = (component, context, data) => {
172
let result = {};
183

19-
Object.entries(functions).forEach(entity => {
20-
const [name, method] = entity;
21-
const isValidName = pattern.test(name);
22-
if (isValidName) {
23-
const propertyName = camelCase(name.replace(pattern, ''));
24-
let value = null;
25-
if (typeof method === 'function') {
26-
value = method(context, data);
27-
} else {
28-
value = method;
29-
}
30-
31-
if (value) {
32-
result = {
33-
...result,
34-
[propertyName]: value
35-
};
36-
}
4+
if (component.setResponseData) {
5+
if (typeof component.setResponseData === 'function') {
6+
result = component.setResponseData(context, data);
7+
} else {
8+
result = component.setResponseData;
379
}
38-
});
10+
}
3911

4012
return result;
4113
};
4214

4315
const getStates = async (component, context, predefinedInitialState) => {
4416
const initialState = predefinedInitialState || { data: {} };
4517
let subComponentFiles = [];
46-
let seoState = {};
4718
let responseOptions = {};
48-
let dependencies = [];
49-
let redirection = null;
50-
51-
if (component.setDependencies) {
52-
dependencies = component.setDependencies(context);
53-
}
19+
const responseData = getResponseData(component, context, initialState.data);
5420

5521
if (context.isWithoutState) {
56-
return { initialState, seoState, dependencies, subComponentFiles, responseOptions };
22+
return { initialState, subComponentFiles, responseOptions, ...responseData };
5723
}
5824

5925
if (!predefinedInitialState && component.getServerSideProps) {
6026
initialState.data = await component.getServerSideProps(context);
6127
}
6228

63-
if (component?.setSeoState) {
64-
seoState = component.setSeoState(initialState?.data) || {};
65-
}
66-
6729
if (initialState?.data?.subComponentFiles) {
6830
subComponentFiles = initialState?.data?.subComponentFiles || [];
6931
}
@@ -72,24 +34,11 @@ const getStates = async (component, context, predefinedInitialState) => {
7234
responseOptions = initialState?.data?.responseOptions || {};
7335
}
7436

75-
if (component.setRedirection) {
76-
redirection = component.setRedirection(context, initialState.data);
77-
}
78-
79-
if (component.setPageData) {
80-
redirection = component.setPageData(context, initialState.data);
81-
}
82-
83-
const setters = getCustomSetters(component, context, initialState.data);
84-
8537
return {
8638
initialState,
87-
seoState,
8839
subComponentFiles,
8940
responseOptions,
90-
dependencies,
91-
redirection,
92-
...setters
41+
...responseData
9342
};
9443
};
9544

src/universal/utils/constants.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ const SERVICES = Object.freeze(
2525
}, {})
2626
);
2727

28+
const BLACKLIST_OUTPUT = [
29+
'componentName',
30+
'fullWidth',
31+
'isMobileComponent',
32+
'isPreviewQuery',
33+
'responseOptions'
34+
];
35+
2836
export {
2937
HTTP_STATUS_CODES,
3038
WINDOW_GLOBAL_PARAMS,
3139
JSON_CONTENT_TYPE,
3240
CONTENT_TYPE_HEADER,
3341
REQUEST_TYPES_WITH_BODY,
34-
SERVICES
42+
SERVICES,
43+
BLACKLIST_OUTPUT
3544
};

0 commit comments

Comments
 (0)