-
Notifications
You must be signed in to change notification settings - Fork 46
Conversation
*/ | ||
setQueryParameter : function setParameter( parameter, value ) { | ||
var k = keys( this ); | ||
if( k.indexOf( parameter ) === -1 ) return this; |
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 would not check if the parameter exists here. We may want to set something custom/hidden or not yet supported by the helper. Is that doable?
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.
For several reasons, it would not be consistent with the behavior of the SearchParameters and more broadly of the Helper :
- All setters rely on mutateMe which uses the constructor to create a new instance. The constructor is made so it explicitly take the correct parameters, it results that it also filters wrong parameters.
- I think we're better off with a system that explicitly describes the parameters of the queries. There are many of them and it's practical to have them with the code of the client/library we use. If some parameters can be left undocumented, that's not really good.
I'll make sure we keep the parameters in sync.
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.
even with something like:
if( k.indexOf( parameter ) === -1 ) {
this.unknown[parameter] = value;
} else {
// bla bla
}
Not a big deal right now, let's wait the first time we have the issue to re-think about it :)
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.
If we ever happen to have a problem with this approach, that would be sad but we would be able to simplify the code. In the mean time, I agree let's try that and see :)
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 would then throw an exception or something instead of the return
; otherwise we'll spend hours to figure out typos. What do 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.
👍 Silently failing will lead to incomprehension.
Throwing a new Error('Unknown query parameter')
smg like that seems fine?
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.
Looks good to me! Should we consider a less destroying solution like logging?
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.
If I was using this I would really want it to throw if I am passing an unknown argument. If the goal is to only accept known arguments then it should throw. Logging would be ok if it was not an error.
But the client will expect something to work and it will not so it seems like it's not a warning.
You could also emit an error and if he does not listen to the error event of an EventEmitter it will throw (default behavior).
But then just throwing seems better to me.
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.
Sounds right to me then :)
The SearchParameters does not have event emitters so throw everything!! :)
@bobylito - Looks great - I'm looking forward to replacing my mutateMe method calls with this :) |
LGTM 👍 |
…ature/mergeSearchParameters FIX algolia/algoliasearch-helper-js#76 #84 Modify search parameters easily
Before we needed to rely on using
SearchParameters.mutateMe
withAlgoliasearchHelper.setState
to modify any of the parameters, we can set through the constructor but don't have setters.Now we can either use one of the more friendly
SearchParameters
methods :setQueryParameter
(to set a single parameter) orsetQueryParameters
(to set multiple parameters at once)Even more more friendly,
setQueryParameter
can be set directly on the helper. This method relies internally on its SearchParameters counterpart, and adds the event handling for more convenience.