d34dman / drupal-jsonapi-params

A package to manage json-api params
ISC License
59 stars 8 forks source link

request: Initialize from anything on instantiation. #19

Closed Decipher closed 3 years ago

Decipher commented 3 years ago

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 a query String, Object or DrupalJsonApiParams object, and ensure I get a DrupalJsonApiParams object back, but it's a little clunky:

  mergeQuery(query) {
    if (!(query instanceof DrupalJsonApiParams)) {
      if (query) {
        query = typeof query === 'string'
          ? new DrupalJsonApiParams().initializeWithQueryString(query)
          : new DrupalJsonApiParams().initializeWithQueryObject(query)
      } else {
        query = new DrupalJsonApiParams()
      }
    }
    return query
  }

That allows me to do something like:

this.mergeQuery(query).addFields(...)

It would be preferable if I could do something like:

query = new DrupalJsonApiParams(query).addFields(...)
d34dman commented 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.

Decipher commented 3 years ago

The use case mostly comes about because the Druxt modules allow for the query parameter to be either:

  1. A querystring
  2. A queryobject
  3. A DrupalJsonApiParams object

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

d34dman commented 3 years ago

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.

index ts — drupal-jsonapi-params 2021-05-21 14-33-10
d34dman commented 3 years ago

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);