-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ convert plugin-browser-device to typescript es module
- Loading branch information
1 parent
92fb17c
commit a3a61cd
Showing
8 changed files
with
85 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import createRollupConfig from '../../.rollup/index.mjs' | ||
|
||
export default createRollupConfig({ | ||
input: 'src/device.ts', | ||
external: ['@bugsnag/cuid'], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import cuid from '@bugsnag/cuid' | ||
|
||
const BUGSNAG_ANONYMOUS_ID_KEY = 'bugsnag-anonymous-id' | ||
|
||
const getDeviceId = (win: Window) => { | ||
try { | ||
const storage = win.localStorage | ||
|
||
let id = storage.getItem(BUGSNAG_ANONYMOUS_ID_KEY) | ||
|
||
// If we get an ID, make sure it looks like a valid cuid. The length can | ||
// fluctuate slightly, so some leeway is built in | ||
if (id && /^c[a-z0-9]{20,32}$/.test(id)) { | ||
return id | ||
} | ||
|
||
id = cuid() | ||
|
||
storage.setItem(BUGSNAG_ANONYMOUS_ID_KEY, id) | ||
|
||
return id | ||
} catch (err) { | ||
// If localStorage is not available (e.g. because it's disabled) then give up | ||
} | ||
} | ||
|
||
export default getDeviceId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Event, Session } from '@bugsnag/core' | ||
|
||
const setDefaultUserId = (eventOrSession: Event | Session) => { | ||
// device id is also used to populate the user id field, if it's not already set | ||
const user = eventOrSession.getUser() | ||
if ((!user || !user.id) && eventOrSession.device) { | ||
eventOrSession.setUser(eventOrSession.device.id) | ||
} | ||
} | ||
|
||
export default setDefaultUserId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import plugin from '../device' | ||
import plugin from '../src/device' | ||
|
||
import Client, { | ||
SessionDeliveryPayload, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts"], | ||
"compilerOptions": { | ||
"target": "ES2020" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters