jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

clean up inflected dependency

Open hartzis opened this issue 9 years ago • 5 comments

clean ups for #129

This is an attempt to clean up inflected dependency.

  • serialize keyForAttribute can only take a function now and defaults to (k) => k
    • no longer accepts strings for casing
  • serialize pluralizeType can only take a function now and defaults to (t) => t
    • pluralizeType no longer defaults to true, 'pluralizing' types
  • deserialize keyForAttribute can only take a function now and defaults to (k) => k
    • no longer accepts strings for casing

These are definitely all major changes, v3->v4 probably, but i'd argue that they are steps in a good direction.

  • clean up dependencies, smaller footprint
  • keeps the same versatility for keyForAttribute and removes unneeded methods/tests to handle string casing
  • adds more versatility to pluralizeType by allowing a function that users can pass to handle their specific use cases

hartzis avatar Apr 04 '17 16:04 hartzis

Thank you so much @hartzis !

SeyZ avatar Apr 04 '17 19:04 SeyZ

yeah! take a look, let me know what you think.

I'll write a little migration readme here in the comments that you could possibly use in the wiki too.

hartzis avatar Apr 04 '17 22:04 hartzis

@SeyZ Attempt at migration readme info:

Migrate from 3.0 to 4.0

V4 updates a few options making them a little more flexible:

  • keyForAttribute(serialize and deserialize) takes only a function now that returns a string
    • defaults to: (key) => key
  • pluralizeType takes only a function now that returns a string
    • defaults to: (type) => type

We've eliminated inflected as a dependency. Doing this eliminates another dependency making for a smaller package size overall.

v3 keyForAttribute defaulted to 'dash-case':

var data = [{ id: 1, firstName: 'Sandro', lastName: 'Munda' }];

var UserSerializer = new JSONAPISerializer('users', {
  attributes: ['firstName', 'lastName']
});

var users = UserSerializer.serialize(data);

Produced(notice the attribute keys):

{
  "data": [{
    "type": "users",
    "id": "1",
    "attributes": {
      "first-name": "Sandro",
      "last-name": "Munda"
    }
  }
}

v4 attribute keys by default do not change now:

var data = [{ id: 1, firstName: 'Sandro', lastName: 'Munda' }];

var UserSerializer = new JSONAPISerializer('users', {
  attributes: ['firstName', 'lastName']
});

var users = UserSerializer.serialize(data);

Now produces:

{
  "data": [{
    "type": "users",
    "id": "1",
    "attributes": {
      "firstName": "Sandro",
      "lastName": "Munda"
    }
  }]
}

NOTE To migrate and achieve v3 default behavior is easy! You can add inflected to your libraries dependencies and do:

import Inflector from 'inflected';

function dasherize(key) {
  key = inflected.underscore(key);
  return inflected.dasherize(key);
}

var UserSerializer = new JSONAPISerializer('users', {
  attributes: ['firstName', 'lastName'],
  keyForAttribute: dasherize,
  pluralizeType: Inflector.pluralize
});

hartzis avatar Apr 17 '17 22:04 hartzis

#142 seems pretty important, and I can update this PR after that gets in :)

hartzis avatar May 26 '17 18:05 hartzis

seems there are definitely a few other important PRs that should probably get merged in before this.

I can get this updated after those other PRs get merged in!

hartzis avatar Jul 13 '17 20:07 hartzis