Repository navigation
feat(browser): add LoaderSentry type #25357
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
Merged
Lms24
merged 4 commits into
getsentry:develop
from
faizansaiyed123:feat/loader-sentry-type
Oct 16, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71de7e2
feat(browser): add LoaderSentry type
faizansaiyed123 40c2d23
test(browser): enable loader type tests
faizansaiyed123 d30573d
ref(browser): Use named type imports for LoaderSentry
Lms24 14abcc2
docs(browser): Show full SDK cast inside onLoad in LoaderSentry example
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,46 @@ | ||
| import type { addBreadcrumb, captureEvent, captureException, captureMessage, Scope } from '@sentry/core'; | ||
| import type { showReportDialog } from './report-dialog'; | ||
| import type { forceLoad, init, onLoad } from './sdk'; | ||
|
|
||
| /** Queued calls are replayed once the SDK loads, so they can't return the SDK method's result. */ | ||
| type Queued<Fn> = Fn extends (...args: infer Args) => unknown ? (...args: Args) => void : never; | ||
|
|
||
| /** | ||
| * The subset of the Sentry browser API that is safe to call on the CDN loader's | ||
| * `window.Sentry` stub before the full SDK has loaded. | ||
| * | ||
| * Any other SDK API needs to be wrapped in `Sentry.onLoad()`. Inside the callback, the full SDK is | ||
| * loaded, so `window.Sentry` can be cast to the full SDK type. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import type * as SentryBrowser from '@sentry/browser'; | ||
| * import type { LoaderSentry } from '@sentry/browser'; | ||
| * | ||
| * declare global { | ||
| * interface Window { | ||
| * Sentry: LoaderSentry; | ||
| * } | ||
| * } | ||
| * | ||
| * window.Sentry.captureException(new Error('Something went wrong')); | ||
| * | ||
| * window.Sentry.onLoad(() => { | ||
| * const Sentry = window.Sentry as typeof SentryBrowser; | ||
| * Sentry.init({ release: 'my-app@1.0.0' }); | ||
| * Sentry.addIntegration(Sentry.replayIntegration()); | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export interface LoaderSentry { | ||
| init: Queued<typeof init>; | ||
| addBreadcrumb: Queued<typeof addBreadcrumb>; | ||
| captureMessage: Queued<typeof captureMessage>; | ||
| captureException: Queued<typeof captureException>; | ||
| captureEvent: Queued<typeof captureEvent>; | ||
| showReportDialog: Queued<typeof showReportDialog>; | ||
| // Only the callback overload: there's no way to get hold of a `Scope` before the SDK has loaded. | ||
| withScope: (callback: (scope: Scope) => void) => void; | ||
| onLoad: typeof onLoad; | ||
| forceLoad: typeof forceLoad; | ||
| } |
This file contains hidden or 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,48 @@ | ||
| import { expectTypeOf, it, describe } from 'vitest'; | ||
| import type { Scope } from '@sentry/core'; | ||
| import type { LoaderSentry } from '../src/loader-types'; | ||
|
|
||
| describe('LoaderSentry', () => { | ||
| it('exposes the APIs supported by the loader', () => { | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('init'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('addBreadcrumb'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('captureMessage'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('captureException'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('captureEvent'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('withScope'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('showReportDialog'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('onLoad'); | ||
| expectTypeOf<LoaderSentry>().toHaveProperty('forceLoad'); | ||
| }); | ||
|
|
||
| it('does not expose unsupported APIs', () => { | ||
| expectTypeOf<LoaderSentry>().not.toHaveProperty('addIntegration'); | ||
| expectTypeOf<LoaderSentry>().not.toHaveProperty('captureFeedback'); | ||
| expectTypeOf<LoaderSentry>().not.toHaveProperty('flush'); | ||
| expectTypeOf<LoaderSentry>().not.toHaveProperty('close'); | ||
| }); | ||
|
|
||
| it('uses void return types for queued methods', () => { | ||
| expectTypeOf<LoaderSentry['init']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['addBreadcrumb']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['captureMessage']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['captureException']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['captureEvent']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['withScope']>().returns.toBeVoid(); | ||
| expectTypeOf<LoaderSentry['showReportDialog']>().returns.toBeVoid(); | ||
| }); | ||
|
|
||
| it('keeps the parameters of the SDK methods', () => { | ||
| expectTypeOf<LoaderSentry['init']>().toBeCallableWith({ dsn: 'https://public@dsn.ingest.sentry.io/1337' }); | ||
| expectTypeOf<LoaderSentry['captureException']>().toBeCallableWith(new Error('test'), { tags: { foo: 'bar' } }); | ||
| expectTypeOf<LoaderSentry['captureMessage']>().toBeCallableWith('test', 'warning'); | ||
| expectTypeOf<LoaderSentry['onLoad']>().toBeCallableWith(() => {}); | ||
| }); | ||
|
|
||
| it('only exposes the callback overload of withScope', () => { | ||
| expectTypeOf<LoaderSentry['withScope']>().toBeCallableWith(scope => { | ||
| scope.setTag('foo', 'bar'); | ||
| }); | ||
| expectTypeOf<LoaderSentry['withScope']>().parameters.toEqualTypeOf<[callback: (scope: Scope) => void]>(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.