Move conversion of snake_case to camelCase to model to make recursive models still work
This commit is contained in:
parent
a77b4253cb
commit
7587821927
4 changed files with 37 additions and 37 deletions
28
src/helpers/case.js
Normal file
28
src/helpers/case.js
Normal 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
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import {defaults, omitBy, isNil} from 'lodash'
|
import {defaults, omitBy, isNil} from 'lodash'
|
||||||
|
import {objectToCamelCase} from '../helpers/case'
|
||||||
|
|
||||||
export default class AbstractModel {
|
export default class AbstractModel {
|
||||||
|
|
||||||
|
@ -7,6 +8,9 @@ export default class AbstractModel {
|
||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
constructor(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
|
// 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())
|
defaults(this, omitBy(data, isNil), this.defaults())
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ export default class TaskModel extends AbstractModel {
|
||||||
|
|
||||||
this.id = Number(this.id)
|
this.id = Number(this.id)
|
||||||
this.listId = Number(this.listId)
|
this.listId = Number(this.listId)
|
||||||
|
|
||||||
// Make date objects from timestamps
|
// Make date objects from timestamps
|
||||||
this.dueDate = this.dueDate ? new Date(this.dueDate) : null
|
this.dueDate = this.dueDate ? new Date(this.dueDate) : null
|
||||||
this.startDate = this.startDate ? new Date(this.startDate) : null
|
this.startDate = this.startDate ? new Date(this.startDate) : null
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import {reduce, replace} from 'lodash'
|
import {reduce, replace} from 'lodash'
|
||||||
import {camelCase} from 'camel-case'
|
import { objectToSnakeCase } from '../helpers/case'
|
||||||
import {snakeCase} from 'snake-case'
|
|
||||||
|
|
||||||
let config = require('../../public/config.json')
|
let config = require('../../public/config.json')
|
||||||
|
|
||||||
|
@ -48,19 +47,19 @@ export default class AbstractService {
|
||||||
if (this.useUpdateInterceptor()) {
|
if (this.useUpdateInterceptor()) {
|
||||||
config.data = self.beforeUpdate(config.data)
|
config.data = self.beforeUpdate(config.data)
|
||||||
}
|
}
|
||||||
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
|
config.data = JSON.stringify(objectToSnakeCase(config.data))
|
||||||
break
|
break
|
||||||
case 'put':
|
case 'put':
|
||||||
if (this.useCreateInterceptor()) {
|
if (this.useCreateInterceptor()) {
|
||||||
config.data = self.beforeCreate(config.data)
|
config.data = self.beforeCreate(config.data)
|
||||||
}
|
}
|
||||||
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
|
config.data = JSON.stringify(objectToSnakeCase(config.data))
|
||||||
break
|
break
|
||||||
case 'delete':
|
case 'delete':
|
||||||
if (this.useDeleteInterceptor()) {
|
if (this.useDeleteInterceptor()) {
|
||||||
config.data = self.beforeDelete(config.data)
|
config.data = self.beforeDelete(config.data)
|
||||||
}
|
}
|
||||||
config.data = JSON.stringify(this.modelToSnakeCase(config.data))
|
config.data = JSON.stringify(objectToSnakeCase(config.data))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
|
@ -239,32 +238,6 @@ export default class AbstractService {
|
||||||
// Preprocessors
|
// 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
|
* Default preprocessor for get requests
|
||||||
* @param model
|
* @param model
|
||||||
|
@ -338,7 +311,6 @@ export default class AbstractService {
|
||||||
return this.errorHandler(error)
|
return this.errorHandler(error)
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
response.data = this.modelToCamelCase(response.data)
|
|
||||||
return Promise.resolve(this.modelGetFactory(response.data))
|
return Promise.resolve(this.modelGetFactory(response.data))
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
@ -381,7 +353,6 @@ export default class AbstractService {
|
||||||
if (response.data === null) {
|
if (response.data === null) {
|
||||||
return Promise.resolve([])
|
return Promise.resolve([])
|
||||||
}
|
}
|
||||||
response.data = this.modelToCamelCase(response.data)
|
|
||||||
return Promise.resolve(this.modelGetAllFactory(response.data))
|
return Promise.resolve(this.modelGetAllFactory(response.data))
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
@ -407,7 +378,6 @@ export default class AbstractService {
|
||||||
return this.errorHandler(error)
|
return this.errorHandler(error)
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
response.data = this.modelToCamelCase(response.data)
|
|
||||||
return Promise.resolve(this.modelCreateFactory(response.data))
|
return Promise.resolve(this.modelCreateFactory(response.data))
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
@ -433,7 +403,6 @@ export default class AbstractService {
|
||||||
return this.errorHandler(error)
|
return this.errorHandler(error)
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
response.data = this.modelToCamelCase(response.data)
|
|
||||||
return Promise.resolve(this.modelUpdateFactory(response.data))
|
return Promise.resolve(this.modelUpdateFactory(response.data))
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
@ -459,7 +428,6 @@ export default class AbstractService {
|
||||||
return this.errorHandler(error)
|
return this.errorHandler(error)
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
response.data = this.modelToCamelCase(response.data)
|
|
||||||
return Promise.resolve(response.data)
|
return Promise.resolve(response.data)
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
|
Loading…
Reference in a new issue