Skip to content

Commit 5da4167

Browse files
committed
add deserializeKey and serializeKey to model
1 parent 38a7a39 commit 5da4167

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/model.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
IncludeScope
2424
} from "./scope"
2525
import { JsonapiTypeRegistry } from "./jsonapi-type-registry"
26-
import { camelize, underscore } from "inflected"
26+
import { camelize, underscore, dasherize } from "inflected"
2727
import { ILogger, logger as defaultLogger } from "./logger"
2828
import { MiddlewareStack, BeforeFilter, AfterFilter } from "./middleware-stack"
2929

@@ -468,7 +468,7 @@ export class JSORMBase {
468468
let attributeName = key
469469

470470
if (this.klass.camelizeKeys) {
471-
attributeName = camelize(underscore(key), false)
471+
attributeName = this.deserializeKey(key)
472472
}
473473

474474
if (key === "id" || this.klass.attributeList[attributeName]) {
@@ -741,6 +741,17 @@ export class JSORMBase {
741741
})
742742
}
743743

744+
serializeKey(key: string): string {
745+
if (this.klass.letterCase == "dasherized") {
746+
return dasherize(underscore(key))
747+
}
748+
return underscore(key)
749+
}
750+
751+
deserializeKey(key: string): string {
752+
return camelize(underscore(key), false)
753+
}
754+
744755
private async _handleResponse(
745756
response: JsonapiResponse,
746757
callback: () => void

src/util/validation-errors.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { JSORMBase } from "../model"
2-
import { camelize, underscore } from "inflected"
32
import { JsonapiResponseDoc, JsonapiErrorMeta } from "../jsonapi-spec"
43

54
export class ValidationErrors {
@@ -44,7 +43,7 @@ export class ValidationErrors {
4443
let attribute = meta.attribute
4544

4645
if (this.model.klass.camelizeKeys) {
47-
attribute = camelize(underscore(attribute), false)
46+
attribute = this.model.deserializeKey(attribute)
4847
}
4948

5049
errorsAccumulator[attribute] = meta.message

src/util/write-payload.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { JSORMBase, ModelRecord } from "../model"
22
import { IncludeDirective, IncludeScopeHash } from "./include-directive"
33
import { IncludeScope } from "../scope"
44
import { tempId } from "./temp-id"
5-
import { underscore, dasherize } from "inflected"
65
import {
76
JsonapiRequestDoc,
87
JsonapiResourceIdentifier,
@@ -34,7 +33,7 @@ export class WritePayload<T extends JSORMBase> {
3433

3534
this._eachAttribute((key, value) => {
3635
if (!this.model.isPersisted || this.model.changes()[key]) {
37-
attrs[this._letterCaseKey(key)] = value
36+
attrs[this.model.serializeKey(key)] = value
3837
}
3938
})
4039

@@ -121,7 +120,7 @@ export class WritePayload<T extends JSORMBase> {
121120
}
122121

123122
if (data) {
124-
_relationships[this._letterCaseKey(key)] = { data }
123+
_relationships[this.model.serializeKey(key)] = { data }
125124
}
126125
}
127126
})
@@ -254,11 +253,4 @@ export class WritePayload<T extends JSORMBase> {
254253
}
255254
})
256255
}
257-
258-
private _letterCaseKey(key): string {
259-
if (this.model.klass.letterCase == "dasherized") {
260-
return dasherize(underscore(key))
261-
}
262-
return underscore(key)
263-
}
264256
}

0 commit comments

Comments
 (0)