Move conversion of snake_case to camelCase to model to make recursive models still work

This commit is contained in:
kolaente 2020-04-14 22:46:27 +02:00
parent a77b4253cb
commit 7587821927
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 37 additions and 37 deletions

28
src/helpers/case.js Normal file
View file

@ -0,0 +1,28 @@
import {camelCase} from 'camel-case'
import {snakeCase} from 'snake-case'
/**
* Transforms field names to camel case.
* @param object
* @returns {*}
*/
export function objectToCamelCase(object) {
let parsedObject = {}
for (const m in object) {
parsedObject[camelCase(m)] = object[m]
}
return parsedObject
}
/**
* Transforms field names to snake case - used before making an api request.
* @param object
* @returns {*}
*/
export function objectToSnakeCase(object) {
let parsedObject = {}
for (const m in object) {
parsedObject[snakeCase(m)] = object[m]
}
return parsedObject
}

View file

@ -1,4 +1,5 @@
import {defaults, omitBy, isNil} from 'lodash'
import {objectToCamelCase} from '../helpers/case'
export default class AbstractModel {
@ -7,6 +8,9 @@ export default class AbstractModel {
* @param data
*/
constructor(data) {
data = objectToCamelCase(data)
// Put all data in our model while overriding those with a value of null or undefined with their defaults
defaults(this, omitBy(data, isNil), this.defaults())
}

View file

@ -10,7 +10,7 @@ export default class TaskModel extends AbstractModel {
this.id = Number(this.id)
this.listId = Number(this.listId)
// Make date objects from timestamps
this.dueDate = this.dueDate ? new Date(this.dueDate) : null
this.startDate = this.startDate ? new Date(this.startDate) : null

View file

@ -1,7 +1,6 @@
import axios from 'axios'
import {reduce, replace} from 'lodash'
import {camelCase} from 'camel-case'
import {snakeCase} from 'snake-case'
import { objectToSnakeCase } from '../helpers/case'
let config = require('../../public/config.json')
@ -48,19 +47,19 @@ export default class AbstractService {
if (this.useUpdateInterceptor()) {
config.data = self.beforeUpdate(config.data)
}
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
config.data = JSON.stringify(objectToSnakeCase(config.data))
break
case 'put':
if (this.useCreateInterceptor()) {
config.data = self.beforeCreate(config.data)
}
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
config.data = JSON.stringify(objectToSnakeCase(config.data))
break
case 'delete':
if (this.useDeleteInterceptor()) {
config.data = self.beforeDelete(config.data)
}
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
config.data = JSON.stringify(objectToSnakeCase(config.data))
break
}
return config
@ -239,32 +238,6 @@ export default class AbstractService {
// Preprocessors
////////////
/**
* Transforms field names to camel case.
* @param model
* @returns {*}
*/
modelToCamelCase(model) {
let parsedModel = {}
for (const m in model) {
parsedModel[camelCase(m)] = model[m]
}
return parsedModel
}
/**
* Transforms field names to snake case - used before making an api request.
* @param model
* @returns {*}
*/
modelToSnakeCase(model) {
let parsedModel = {}
for (const m in model) {
parsedModel[snakeCase(m)] = model[m]
}
return parsedModel
}
/**
* Default preprocessor for get requests
* @param model
@ -338,7 +311,6 @@ export default class AbstractService {
return this.errorHandler(error)
})
.then(response => {
response.data = this.modelToCamelCase(response.data)
return Promise.resolve(this.modelGetFactory(response.data))
})
.finally(() => {
@ -381,7 +353,6 @@ export default class AbstractService {
if (response.data === null) {
return Promise.resolve([])
}
response.data = this.modelToCamelCase(response.data)
return Promise.resolve(this.modelGetAllFactory(response.data))
})
.finally(() => {
@ -407,7 +378,6 @@ export default class AbstractService {
return this.errorHandler(error)
})
.then(response => {
response.data = this.modelToCamelCase(response.data)
return Promise.resolve(this.modelCreateFactory(response.data))
})
.finally(() => {
@ -433,7 +403,6 @@ export default class AbstractService {
return this.errorHandler(error)
})
.then(response => {
response.data = this.modelToCamelCase(response.data)
return Promise.resolve(this.modelUpdateFactory(response.data))
})
.finally(() => {
@ -459,7 +428,6 @@ export default class AbstractService {
return this.errorHandler(error)
})
.then(response => {
response.data = this.modelToCamelCase(response.data)
return Promise.resolve(response.data)
})
.finally(() => {