Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import inflection from "inflection"
- import Jsona, { ModelPropertiesMapper, JsonPropertiesMapper } from "jsona";
- class CustomModelPropertiesMapper extends ModelPropertiesMapper {
- getAttributes(model) {
- const camelCasedAttributes = super.getAttributes(model)
- const underscoreAttributes = {}
- Object.keys(camelCasedAttributes).forEach((name) => {
- underscoreAttributes[inflection.underscore(name)] = camelCasedAttributes[name]
- })
- return underscoreAttributes
- }
- }
- class CustomJsonPropertiesMapper extends JsonPropertiesMapper {
- setAttributes(model, attributes) {
- Object.keys(attributes).forEach((propName) => {
- model[inflection.camelize(propName, true)] = attributes[propName]
- })
- }
- }
- const jsonApi = new Jsona({
- modelPropertiesMapper: new CustomModelPropertiesMapper,
- jsonPropertiesMapper: new CustomJsonPropertiesMapper,
- })
- export default jsonApi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement