Closed Decipher closed 3 years ago
query = new DrupalJsonApiParams(query).addFields(...)
@Decipher I would like to know a little more context about this usage. When doesn't a developer have the knowledge about the type
of query
.
So the following is the usage AS IS,
// when query is DrupalJsonApiParams object.
query = query.addFields(...);
// when query is string
query = new DrupalJsonApiParams().initializeWithQueryString(query).addFields(...);
// when query is data object.
query = new DrupalJsonApiParams().initializeWithQueryObject(query).addFields(...);
// when query is undefined/false/null
query = new DrupalJsonApiParams().addFields(...);
And following is what is TO BE,
query = new DrupalJsonApiParams(query).addFields(...);
Technically it looks like this has to be handled outside DrupalJsonApiParams Class, so we can also handle the condition when query
is Falsy.
Maybe it can be a standalone method outside the class itself which is available for import. Like a pinch of Syntactic Salt.
The use case mostly comes about because the Druxt modules allow for the query parameter to be either:
The reason something is needed is that I often need for the module to be able to alter queries, to add explicitly required fields or filters.
Currently I have the following implementation in the DruxtStore: https://github.com/druxt/druxt.js/blob/develop/src/stores/druxt.js#L6-L16
Update: I tried to implement this in Typescript and it is not letting me do that. Such a bummer. Will give it a go again after consulting some Typescript Gurus.
Solved in a slightly different manner. Guess it should work... at least tests says so :)
Usage...
const api = new DrupalJsonApiParams();
api.addFilter('status', '1');
const newApi = new DrupalJsonApiParams();
newApi.initialize(api);
As a user I want the ability to instantiate and initialize a DrupalJsonApiParams object using a String, Object, or an existing DrupalJsonApiParams object, without the need to call a specific method.
Currently I have a
mergeQuery
method that will take aquery
String, Object or DrupalJsonApiParams object, and ensure I get a DrupalJsonApiParams object back, but it's a little clunky:That allows me to do something like:
It would be preferable if I could do something like: