Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type {
} from '@sentry/core';

export type { BrowserOptions } from './client';
export type { LoaderSentry } from './loader';

export {
addEventProcessor,
Expand Down
27 changes: 27 additions & 0 deletions packages/browser/src/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

type LoaderApi = typeof import('@sentry/core') &
typeof import('./sdk') &
typeof import('./report-dialog');

type VoidReturningFunction<T> = T extends (...args: infer Args) => unknown
? (...args: Args) => void
: never;

type LoaderQueuedMethod =
| 'init'
| 'addBreadcrumb'
| 'captureMessage'
| 'captureException'
| 'captureEvent'
| 'withScope'
| 'showReportDialog';

/**
* The subset of the Sentry browser API supported by the CDN loader.
*
* Queued calls do not synchronously return the result of the underlying SDK
* method, so their return types are void.
*/
export type LoaderSentry = {
[Method in LoaderQueuedMethod]: VoidReturningFunction<LoaderApi[Method]>;
} & Pick<LoaderApi, 'onLoad' | 'forceLoad'>;
33 changes: 33 additions & 0 deletions packages/browser/test/loader.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expectTypeOf, it, describe } from 'vitest';
import type { LoaderSentry } from '../src/loader';

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();
});
});
Comment thread
cursor[bot] marked this conversation as resolved.
4 changes: 4 additions & 0 deletions packages/browser/tsconfig.loader.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.test.json",
"include": ["test/loader.test-d.ts"]
}
6 changes: 6 additions & 0 deletions packages/browser/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { defineConfig } from 'vitest/config';
import baseConfig from '../../vite/vite.config';

Expand All @@ -9,5 +10,10 @@ export default defineConfig({
fakeTimers: {
toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date'],
},
typecheck: {
enabled: true,
include: ['test/loader.test-d.ts'],
tsconfig: './tsconfig.loader.test.json',
},
},
});