Skip to content

Commit c5420a5

Browse files
committed
use server and client instead of from and to
1 parent 0070e40 commit c5420a5

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

src/model.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ import { cloneDeep } from "./util/clonedeep"
3939
import { nonenumerable } from "./util/decorators"
4040
import { IncludeScopeHash } from "./util/include-directive"
4141

42+
export type KeyCaseValue = "dash" | "camel" | "snake"
43+
4244
export interface KeyCase {
43-
from: "dash" | "camel" | "snake"
44-
to: "dash" | "camel" | "snake"
45+
server: KeyCaseValue
46+
client: KeyCaseValue
4547
}
4648

4749
export interface ModelConfiguration {
@@ -133,7 +135,7 @@ export class JSORMBase {
133135
static endpoint: string
134136
static isBaseClass: boolean
135137
static jwt?: string
136-
static keyCase: KeyCase = { from: "snake", to: "camel" }
138+
static keyCase: KeyCase = { server: "snake", client: "camel" }
137139
static strictAttributes: boolean = false
138140
static logger: ILogger = defaultLogger
139141
static sync: boolean = false
@@ -349,7 +351,7 @@ export class JSORMBase {
349351
id?: string
350352
temp_id?: string
351353
stale: boolean = false
352-
storeKey: string = ''
354+
storeKey: string = ""
353355

354356
@nonenumerable relationships: Record<string, JSORMBase | JSORMBase[]> = {}
355357
@nonenumerable klass: typeof JSORMBase
@@ -430,45 +432,45 @@ export class JSORMBase {
430432
}
431433

432434
_onStoreChange: Function
433-
private onStoreChange() : Function {
435+
private onStoreChange(): Function {
434436
if (this._onStoreChange) return this._onStoreChange
435-
this._onStoreChange = (_event: any, attrs: any) => {
436-
Object.keys(attrs).forEach((k) => {
437+
this._onStoreChange = (_event: any, attrs: any) => {
438+
Object.keys(attrs).forEach(k => {
437439
let self = this as any
438440
if (self[k] !== attrs[k]) self[k] = attrs[k]
439441
})
440442
}
441443
return this._onStoreChange
442444
}
443445

444-
unlisten() : void {
446+
unlisten(): void {
445447
if (!this.klass.sync) return
446448
if (this.storeKey) {
447449
EventBus.removeEventListener(this.storeKey, this.onStoreChange())
448450
}
449451

450-
Object.keys(this.relationships).forEach((k) => {
452+
Object.keys(this.relationships).forEach(k => {
451453
let related = this.relationships[k]
452454

453455
if (related) {
454456
if (Array.isArray(related)) {
455-
related.forEach((r) => r.unlisten() )
457+
related.forEach(r => r.unlisten())
456458
} else {
457459
related.unlisten()
458460
}
459461
}
460462
})
461463
}
462464

463-
listen() : void {
465+
listen(): void {
464466
if (!this.klass.sync) return
465-
if (!this._onStoreChange) { // not already registered
467+
if (!this._onStoreChange) {
468+
// not already registered
466469
EventBus.addEventListener(this.storeKey, this.onStoreChange())
467470
}
468471
}
469472

470-
471-
reset() : void {
473+
reset(): void {
472474
if (this.klass.sync) {
473475
this.klass.store.updateOrCreate(this)
474476
this.listen()
@@ -759,7 +761,7 @@ export class JSORMBase {
759761
}
760762

761763
static serializeKey(key: string): string {
762-
switch (this.keyCase.from) {
764+
switch (this.keyCase.server) {
763765
case "dash": {
764766
return dasherize(underscore(key))
765767
}
@@ -773,7 +775,7 @@ export class JSORMBase {
773775
}
774776

775777
static deserializeKey(key: string): string {
776-
switch (this.keyCase.to) {
778+
switch (this.keyCase.client) {
777779
case "dash": {
778780
return dasherize(underscore(key))
779781
}
@@ -885,4 +887,4 @@ export const isModelInstance = (arg: any): arg is JSORMBase => {
885887
return false
886888
}
887889
return isModelClass(arg.constructor.currentClass)
888-
}
890+
}

test/fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export class PersonWithExtraAttr extends Person {
3030
extraThing: string
3131
}
3232

33-
@Model({ keyCase: { from: "snake", to: "snake" } })
33+
@Model({ keyCase: { server: "snake", client: "snake" } })
3434
export class PersonWithoutCamelizedKeys extends Person {
3535
@Attr first_name: string
3636
}
3737

38-
@Model({ keyCase: { from: "dash", to: "camel" } })
38+
@Model({ keyCase: { server: "dash", client: "camel" } })
3939
export class PersonWithDasherizedKeys extends Person {}
4040

4141
@Model({
@@ -70,7 +70,7 @@ export const NonFictionAuthor = Author.extend({
7070
static: {
7171
endpoint: "/v1/non_fiction_authors",
7272
jsonapiType: "non_fiction_authors",
73-
keyCase: { from: "snake", to: "snake" }
73+
keyCase: { server: "snake", client: "snake" }
7474
},
7575

7676
attrs: {

test/integration/validations.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ describe("validations", () => {
124124

125125
describe("when keyCase.to is snake", () => {
126126
beforeEach(() => {
127-
instance.klass.keyCase.to = "snake"
127+
instance.klass.keyCase.client = "snake"
128128
})
129129

130130
afterEach(() => {
131-
instance.klass.keyCase.to = "camel"
131+
instance.klass.keyCase.client = "camel"
132132
})
133133

134134
it("does not camelize the error keys", async () => {

test/unit/decorators.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ describe("Decorators", () => {
3535

3636
it("preserves defaults for unspecified items", () => {
3737
expect(TestModel.baseUrl).to.eq("http://please-set-a-base-url.com")
38-
expect(TestModel.keyCase.from).to.eq("snake")
39-
expect(TestModel.keyCase.to).to.eq("camel")
38+
expect(TestModel.keyCase.server).to.eq("snake")
39+
expect(TestModel.keyCase.client).to.eq("camel")
4040
})
4141

4242
it("correctly assigns options", () => {

test/unit/model.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ describe("Model", () => {
447447

448448
it("preserves defaults for unspecified items", () => {
449449
expect(MyModel.baseUrl).to.eq("http://please-set-a-base-url.com")
450-
expect(MyModel.keyCase.from).to.eq("snake")
451-
expect(MyModel.keyCase.to).to.eq("camel")
450+
expect(MyModel.keyCase.server).to.eq("snake")
451+
expect(MyModel.keyCase.client).to.eq("camel")
452452
})
453453

454454
it("correctly assigns options", () => {
@@ -1177,8 +1177,8 @@ describe("Model", () => {
11771177

11781178
beforeEach(() => {
11791179
ApplicationRecord.sync = true
1180-
sinon.spy(EventBus, 'removeEventListener')
1181-
book = new Book({ id : 1 })
1180+
sinon.spy(EventBus, "removeEventListener")
1181+
book = new Book({ id: 1 })
11821182
book.isPersisted = true
11831183
author = new Author({ id: 1, books: [book] })
11841184
author.isPersisted = true
@@ -1211,7 +1211,7 @@ describe("Model", () => {
12111211

12121212
beforeEach(() => {
12131213
ApplicationRecord.sync = true
1214-
sinon.spy(EventBus, 'addEventListener')
1214+
sinon.spy(EventBus, "addEventListener")
12151215
author = new Author()
12161216
})
12171217

0 commit comments

Comments
 (0)