Comments (5)
Hey @allistercsmith @bradbumbalough @mrkvon @sazzer
I've added the possibility to set a function for the meta key:
https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L198
If you need a dynamic meta for a relationship, it was already implemented:
https://github.com/SeyZ/jsonapi-serializer/blob/master/test/serializer.js#L1549
I think it's fixed this issue, right? @bradbumbalough Hope you will come back here 👍 💃
from jsonapi-serializer.
Hey @allistercsmith ! Thanks for your message, I appreciate!
Good idea for the dynamic meta. You're totally right, it's not good to recreate a serializer each time the meta change.
I like the idea to pass an opts
to the serialize method too. You should use the lodash merge method to merge the objects opts.meta
and that.opts.meta
:)
PR is of course welcomed!
from jsonapi-serializer.
Once we're passing options in to the serialize function, it could be useful to make them available to all of the various functions that are declared as part of the schemas as well. That way, we can - for example - make the request details available inside the functions that are used to generate URLs, instead of having to solely depend on the record data or else re-creating the schema every time.
from jsonapi-serializer.
The first (and possibly sufficient) step, as suggested in #67 could be: Allow the meta
in Serializer's opts
to be a function (or object of functions if one wishes to be more strict).
// meta as a function
// fully dynamic meta
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName'],
meta: (data) => { // or meta: (data) => data.meta
foo: 'bar',
baz: data.baz
}
});
// meta as an object of functions (optionally)
// which is more strict
// and similar to topLevelLinks and other ...Links options
var UserSerializer = new JSONAPISerializer('users', {
attributes: ['firstName', 'lastName'],
meta: {
foo: 'bar', // static meta property
baz: (data) => data.baz // dynamic meta property
}
});
var users = UserSerializer.serialize(data);
This would be the most intuitive solution imho.
The implementation of 2nd option may be identical to the way topLevelLinks
are done:
// lib/serializer.js
function getMeta(meta) {
return _.mapValues(meta, function (value) {
if (_.isFunction(value)) {
return value(records);
} else {
return value;
}
});
}
...
if (that.opts.meta) {
payload.meta = getMeta(that.opts.meta);
}
from jsonapi-serializer.
This was a requirement for my use case. Ended up switching to https://github.com/kutlerskaggs/json-api-ify as they provide a somewhat dynamic meta option.
from jsonapi-serializer.
Related Issues (20)
- feat: On deserialization it would be nice if the meta, links, etc, are available as a Symbol in the data
- How can I build deep relationships with my serializer HOT 1
- Automatically calculate attribute list
- 3.6.6 breaks deep relations HOT 4
- What is the difference between `ref` and `id`? HOT 2
- Strange issue with nested relationship deserializing HOT 3
- Support has died. See description. HOT 2
- deserialize does not deserializer links inside relationships objects HOT 3
- Express inside examples is set to version 0.0.0.0 which detects it as vulnerable by scanning tools like Twistlock
- falsy JavaScript attribute values are getting set to null if nullIfMissing flag is set
- How to deserialize with different type?
- Can't Make Relationship Work
- Ability to prefix 'meta' and 'links' key on deserialization HOT 1
- Deserializer [relationships] without [included] field wont work HOT 1
- would not be nice to have a generic valueForRelationship? HOT 1
- How to show all atributte relationships?
- Slow deserialization for specific input HOT 1
- Is it possible to customize the value of the type key? HOT 1
- meta data for Resource Identifier Objects
- Express name throws vulnerability error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-serializer.