SHARE
TWEET

Untitled

a guest Jul 17th, 2017 36 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
Top