lundi 26 septembre 2016

What is the best practice to serialize `User` model for guest and authrized user specified by JSONAPI?

I'm building API with express.js according to JSONAPI specification and have sequelize.js model. For example, User model with attributes such as name, email, balance, last_login_time and etc.

If user on client application request data about third-party user API needs to respond only with public attributes such as name and last_login_time.

If user on client application request data about himself API needs to respond with public and private attributes.

As a serializer I use jsonapi-serializer lib. I have serializer like that:

var JSONAPISerializer = require('jsonapi-serializer').Serializer;

module.exports = new JSONAPISerializer('users', {
  attributes: ['name']
});

Sould I create 2 different serializer? Will not it lead to troubles because almost all models will have 2 serializers and it could lead to confusion of references?




Aucun commentaire:

Enregistrer un commentaire