-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OC-93] - Send supported templates as http header #8
Conversation
To check for the warmup/init - it seems to me that we don't do any options sanitizing (neither here or here - only config sanit here. @mattiaerre I want therefore to add options sanitization within the warmup as well, what you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
over to you @nickbalestra
@@ -25,7 +25,13 @@ const getDefaultUserAgent = function() { | |||
); | |||
}; | |||
|
|||
const sanitiseDefaultOptions = function(options) { | |||
const getTemplatesInfo = templates => | |||
templates.reduce((templatesHash, template) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
me ❤️ this
return templatesHash; | ||
}, {}); | ||
|
||
const sanitiseDefaultOptions = function(options, config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to avoid param reassign; shall we Object.assign({}, options)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more about something like this:
const sanitiseDefaultOptions = (options, config) => {
let copy = Object.assign({}, options);
if (_.isFunction(copy)) {
copy = {};
}
copy.headers = lowerHeaderKeys(copy.headers);
copy.headers['user-agent'] =
copy.headers['user-agent'] || getDefaultUserAgent();
copy.headers.templates =
copy.headers.templates || getTemplatesInfo(config.templates);
copy.timeout = copy.timeout || 5;
return copy;
};
src/sanitiser.js
Outdated
conf = conf || {}; | ||
conf.components = conf.components || {}; | ||
conf.cache = conf.cache || {}; | ||
conf.templates = conf.templates | ||
? _.uniq(conf.templates.concat(baseTemplates)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to have a test that covers the uniq
method 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally agree, btw I've added coverage for the uniq helper but we don't have any test at all for all the rest of the utils, to be address in an had hoc PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added another comment for you @nickbalestra
return templatesHash; | ||
}, {}); | ||
|
||
const sanitiseDefaultOptions = function(options, config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more about something like this:
const sanitiseDefaultOptions = (options, config) => {
let copy = Object.assign({}, options);
if (_.isFunction(copy)) {
copy = {};
}
copy.headers = lowerHeaderKeys(copy.headers);
copy.headers['user-agent'] =
copy.headers['user-agent'] || getDefaultUserAgent();
copy.headers.templates =
copy.headers.templates || getTemplatesInfo(config.templates);
copy.timeout = copy.timeout || 5;
return copy;
};
@mattiaerre yes, much better so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this LGTM I also added some additional comments for you @nickbalestra but very minor stuff. you can automerge if you like 😉
return templatesHash; | ||
}, {}); | ||
|
||
const sanitiseDefaultOptions = function(options, config) { | ||
if (_.isFunction(options)) { | ||
options = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be part of the "copy" path as we are changing input if we do that,
but we can iterate later
src/utils/helpers.js
Outdated
uniques = uniques.concat(element); | ||
} | ||
} | ||
return uniques; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about this alternative solution?
const uniq = (input) => {
const obj = input.reduce((accumulator, element) => {
const copy = Object.assign({}, accumulator);
copy[element] = element;
return copy;
}, {});
return Object.keys(obj).map(key => obj[key]);
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho there is no point in copying the accumulator each time.
But the Object.keys works, I'll take that :)
@mattiaerre everything should be addresses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good thanks for that @nickbalestra
'5', | ||
'6', | ||
'a', | ||
'b' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the nice side effect is that is is also "sorting" 😊
Context: this will allow for example for React SSR to delegate to the Registry for the rendering, in case the template is not supported by the client.
https://opentable.atlassian.net/browse/OC-93