KevinAst / feature-u

Feature Based Project Organization for React
https://feature-u.js.org/
MIT License
91 stars 6 forks source link

@withKeys enhancement #33

Closed ScreamZ closed 3 years ago

ScreamZ commented 4 years ago

Hi kevin,

working on feature-u typescript implementation as I already discussed by e-mail with you :)

Currently, if you need to get assets with keys, you need to add a prefix with @withKeys. I guess it's too magic and it doesn't work well with types based solutions.

A better approach could be to pass an argument to the hooks or HOC.

const Element = useFassets("some.content",true) where true is an optional parameter with default false. That allows to get keys.

Also, we might consider useFasset and useFassets for wildcard support. That could allow to not cast result with typescript to consider it as an array.

What do you think about that?

Regards, Andréas

KevinAst commented 4 years ago

Thanks for your suggestion Andréas.

I think you make some valid points. With a proclivity to "static typing", your suggestions make a lot of sense.

With that said, it really comes down to a personal preference. Granted, your suggestions play better for static types.

However, I still prefer the current API with it's overloaded functionality, as it provides a very concise way to extract multiple aspects in a single invocation. Obviously, you are free to disagree.

the current useFassets() API:

The useFassets() API currently takes on 3 distinct forms. This may be an anti-pattern to some (and problematic in in strongly typed definitions), but I actually prefer the way it currently operates.

Per the the docs (https://feature-u.js.org/cur/api.html#useFassets):

 + useFassets(): fassets ... the full Fassets object
 + useFassets(fassetsKey): fassetResource | fassetResource[] (array used when wildcards are found in fassetsKey)
 + useFassets(mapFassetsToPropsStruct): {key1: fassetResource, key2: fassetResource ... }

The second case is identical to the get() method of the Fassets object ... https://feature-u.js.org/cur/api.html#Fassets_get

The third case allows you to retrieve multiple keys with one invocation.

The API in the docs give more examples.

SideBar

BTW: Just an FYI - I'm am not going to want to promote a formal TypeScript mapping directly in the feature-u project, for a variety of reasons.

The major issue is one of time. As I mentioned, I have never used TypeScript, and this is not something that is on my radar at the moment.