Skip to content

Commit

Permalink
Merge pull request #62 from tlconnor/master
Browse files Browse the repository at this point in the history
Expose resource level metadata
  • Loading branch information
richmolj authored Sep 15, 2020
2 parents 6f15dbd + 84f69e3 commit eda1f6a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export class SpraypaintBase {
@nonenumerable private _links!: ModelRecord<this>
@nonenumerable private _originalLinks!: ModelRecord<this>
@nonenumerable private __meta__: any
@nonenumerable private _metaDirty: boolean = false
@nonenumerable private _errors: ValidationErrors<this> = {}

constructor(attrs?: Record<string, any>) {
Expand Down Expand Up @@ -651,8 +652,19 @@ export class SpraypaintBase {
}
}

setMeta(metaObj: object | undefined) {
setMeta(metaObj: object | undefined, markDirty = true) {
this.__meta__ = metaObj
if (markDirty) {
this._metaDirty = true
}
}

get meta(): object {
return this.__meta__ || {}
}

get isMetaDirty(): boolean {
return this._metaDirty
}

relationshipResourceIdentifiers(relationNames: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Deserializer {
instance.assignLinks(datum.links)

// assign meta
instance.setMeta(datum.meta)
instance.setMeta(datum.meta, false)

// so we don't double-process the same thing
// must push before relationships
Expand Down
5 changes: 5 additions & 0 deletions src/util/write-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export class WritePayload<T extends SpraypaintBase> {
json.included = this.included
}

const _meta: object = this.model.meta
if (this.model.isMetaDirty && Object.keys(_meta).length > 0) {
data.meta = _meta
}

return json
}

Expand Down
7 changes: 5 additions & 2 deletions test/unit/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ describe("Model", () => {

it("assigns metadata correctly", () => {
const instance = ApplicationRecord.fromJsonapi(doc.data, doc)
expect(instance.__meta__).to.eql({
expect(instance.meta).to.eql({
big: true
})
})
Expand Down Expand Up @@ -1577,6 +1577,9 @@ describe("Model", () => {
links: {
self: { href: "/api/person/1", meta: { count: 10 } },
web_view: "/person/1"
},
meta: {
editable: true
}
}
}
Expand All @@ -1587,7 +1590,7 @@ describe("Model", () => {
meta: { count: 10 }
})
expect(person.links.webView).to.eq("/person/1")
expect(person.links.comments).to
expect(person.meta).to.deep.equal({ editable: true })
}

it("from instance", () => {
Expand Down
43 changes: 42 additions & 1 deletion test/unit/write-payload.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { sinon, expect } from "../test-helper"
import { WritePayload } from "../../src/util/write-payload"
import { Person, PersonWithDasherizedKeys, Author, Genre, Book } from "../fixtures"
import {
Person,
PersonWithDasherizedKeys,
Author,
Genre,
Book
} from "../fixtures"

describe("WritePayload", () => {
it("Does not serialize number attributes as empty string", () => {
Expand Down Expand Up @@ -46,6 +52,41 @@ describe("WritePayload", () => {
})
})

describe("metadata", () => {
it("sends metadata if modified by the user", () => {
let person = new Person({ first_name: "Joe", age: 23 })
person.setMeta({ mock: "metadata" })
let payload = new WritePayload(person)
expect(payload.asJSON()).to.deep.equal({
data: {
type: "people",
attributes: {
age: 23,
first_name: "Joe"
},
meta: {
mock: "metadata"
}
}
})
})

it("does not send unmodified metadata", () => {
let person = new Person({ first_name: "Joe", age: 23 })
person.setMeta({ mock: "metadata" }, false)
let payload = new WritePayload(person)
expect(payload.asJSON()).to.deep.equal({
data: {
type: "people",
attributes: {
age: 23,
first_name: "Joe"
}
}
})
})
})

describe("sends persisted singular relationships defined via", () => {
const genre = new Genre({ name: "Horror", id: "1" })
genre.isPersisted = true
Expand Down

0 comments on commit eda1f6a

Please sign in to comment.