Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass data and extra options to all functions in a schema #27

Closed
ianmcdaniel opened this issue May 29, 2017 · 1 comment
Closed

Pass data and extra options to all functions in a schema #27

ianmcdaniel opened this issue May 29, 2017 · 1 comment
Assignees

Comments

@ianmcdaniel
Copy link

I want to build links for a resource using the request object from express, but the links option in schemas does not get passed options like topLevelLinks does.

Seems like any function in a schema should get passed both data and options, like this:

const Serializer = new JSONAPISerializer();

Serializer.register('message', {
  whitelist: ['body','created_at'],

  // currently only accepts an options argument :(
  topLevelLinks: function(data, options) {
    let lastRecordId = data[data.length-1].id;
    return {
      next: `${options.req.get('host')}${options.req.baseUrl}/messages?older_than=${lastRecordId}`
    }
  },

  // currently only accepts a data argument :(
  links: function(data, options){
    return {
      self: `${options.req.get('host')}${options.req.baseUrl}/messages`
    }
  }
});

let messages = serializer.serialize('message', results, {req});

Perhaps even the entire schema could be a function

Serializer.register('message', function(data, options) {
  return {
    ...
  }
});
@mattiloh
Copy link
Collaborator

Pretty neat addition to this module! Thanks @danivek!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants