Skip to content

Commit

Permalink
feat(core): Support adding top-level metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bengourley committed Mar 30, 2020
1 parent 5297a50 commit 526c897
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/core/lib/metadata-delegate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assign = require('./es-utils/assign')

const add = (state, section, keyOrObj, maybeVal) => {
if (!section) return
if (!section && section !== null) return
let updates

// addMetadata("section", null) -> clears section
Expand All @@ -14,11 +14,14 @@ const add = (state, section, keyOrObj, maybeVal) => {
// exit if we don't have an updates object at this point
if (!updates) return

// ensure a section with this name exists
if (!state[section]) state[section] = {}

// merge the updates with the existing section
state[section] = assign({}, state[section], updates)
if (section !== null) {
// ensure a section with this name exists
if (!state[section] || typeof state[section] !== 'object') state[section] = {}
// merge the updates with the existing section
state[section] = assign({}, state[section], updates)
} else {
assign(state, updates)
}
}

const get = (state, section, key) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ describe('@bugsnag/core/client', () => {
expect(client.getMetadata('a', 'b')).toBe(undefined)
client.clearMetadata('a')
expect(client.getMetadata('a')).toBe(undefined)

// check that top-level metadata can be added with section=null
client.addMetadata(null, 'top', 'val')
expect(client._metadata).toEqual({ top: 'val' })
client.addMetadata('top', 'key', 'val')
expect(client._metadata).toEqual({ top: { key: 'val' } })

client._metadata = {}

client.addMetadata('replace', 'key', 'origval')
client.addMetadata(null, 'replace', { diffkey: 'replaceval' })
expect(client._metadata).toEqual({ replace: { diffkey: 'replaceval' } })
})

it('can be set in config', () => {
Expand Down

0 comments on commit 526c897

Please sign in to comment.