Open apsavin opened 7 years ago
Hi @apsavin,
Yes, you are correct. I would say it was designed in this way because of "historical reasons". In the beginning, there was no meta
. Now it is obvious that from design perspective, it would be better to separate data and metadata entities, and I would probably do that breaking change by the time we reach stable v1.0.
As for now, I would leave it as it is before I don't accumulate more breaking changes.
I think, it's possible to fix this behavior without a breaking change using an extra option.
normalize(json, { buildResult: true })
// { entities: {}, result: {} }
We can even log a warning message that normalize(json)
and normalize(json, { endpoint })
are deprecated ways of usage of the library.
I can send a PR.
Thoughts?
Sounds good to me.
What do you think about to split it in two independent functions (eg: normalizeEntities
and normalizeResults
)? So the library is more modular and don't impose the root keys entities
and results
.
@lvauvillier, @apsavin
Maybe we could leave normalize
as it is and do as @lvauvillier suggests?
Well, sounds ok to me, but usually programmer wants just put result of a request in and get normalized data out. Why do two calls instead of one?
Was anyone here successful in parsing meta data with a different package? Had been happily using this package but unfortunately we require meta data now. Going through the client-side packages, any recommendations would be great!
Thanks
@NathanCH Meta should be also available. Please create a separate issue and provide your BE response and what you are getting after normalize
call. Or you may read the source code of this package, which is ~100 LOC long, and look at how we are dealing with meta
I'm not totally sure of my work yet, but as I previously used normalizr
and now this project as we've got now a json:api server, I did a lil trick which is:
import nativeNormalize from "json-api-normalizer" // https://github.com/yury-dymov/json-api-normalizer
const normalize = (json, opts = {}) => {
const entities = nativeNormalize(json, opts)
const result = json.data.map(({ id }) => id)
const meta = json.meta
return { entities, result, meta }
}
export default normalize
Not perfect but works in my case for the moment.
@kud this looks great :) Might be helpful for some other folks
@kud this looks great :) Might be helpful for some other folks
Thanks. We're moving from HAL to JSON:API. It sounds to be more flexible and easier for me as frontend dev to ask the data. Plus I don't need to create any schema, it's already done by json:api convention/son-api-normalizer. Loving it!
It seems that
json-api-normalizer
can't work if I have an entity with typemeta
. Am I right?Why not use
entities
andresult
top-level keys, like originalnormalizr
does?