Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V7: allow top level metadata #787

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ It remains possible to supply initial metadata in configuration:
})
```

All metadata is now required to have a section. If you were previously supplying metadata without a section name (this would display on your dashboard under the "Custom" tab), we have enabled passing `null` as the section name for continuity:

```js
- bugsnagClient.metaData.assetUrl = config.assetUrl
+ Bugsnag.addMetadata(null, 'assetUrl', config.assetUrl)
```

You should only use this method if you have a custom filter based on some top-level metadata, otherwise you should supply a section name for your metadata.

#### Context

On the client, context is now managed via `get/setContext()`:
Expand Down
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
4 changes: 2 additions & 2 deletions packages/core/types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ declare class Client {
): void;

// metadata
public addMetadata(section: string, values: { [key: string]: any }): void;
public addMetadata(section: string, key: string, value: any): void;
public addMetadata(section: string | null, values: { [key: string]: any }): void;
public addMetadata(section: string | null, key: string, value: any): void;
bengourley marked this conversation as resolved.
Show resolved Hide resolved
public getMetadata(section: string, key?: string): any;
public clearMetadata(section: string, key?: string): void;

Expand Down