Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import inflection from "inflection"
  2. import Jsona, { ModelPropertiesMapper, JsonPropertiesMapper } from "jsona";
  3.  
  4. class CustomModelPropertiesMapper extends ModelPropertiesMapper {
  5. getAttributes(model) {
  6. const camelCasedAttributes = super.getAttributes(model)
  7. const underscoreAttributes = {}
  8. Object.keys(camelCasedAttributes).forEach((name) => {
  9. underscoreAttributes[inflection.underscore(name)] = camelCasedAttributes[name]
  10. })
  11.  
  12. return underscoreAttributes
  13. }
  14. }
  15.  
  16. class CustomJsonPropertiesMapper extends JsonPropertiesMapper {
  17. setAttributes(model, attributes) {
  18. Object.keys(attributes).forEach((propName) => {
  19. model[inflection.camelize(propName, true)] = attributes[propName]
  20. })
  21. }
  22. }
  23.  
  24. const jsonApi = new Jsona({
  25. modelPropertiesMapper: new CustomModelPropertiesMapper,
  26. jsonPropertiesMapper: new CustomJsonPropertiesMapper,
  27. })
  28.  
  29. export default jsonApi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement