Skip to content

Commit 52cd479

Browse files
committed
ref: Make types explicit
1 parent 729dedb commit 52cd479

File tree

5 files changed

+71
-65
lines changed

5 files changed

+71
-65
lines changed

packages/core/src/baseclient.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import type {
99
Event,
1010
EventDropReason,
1111
EventHint,
12-
Hook,
13-
HookStore,
1412
Integration,
1513
IntegrationClass,
1614
Outcome,
@@ -19,6 +17,7 @@ import type {
1917
SessionAggregates,
2018
Severity,
2119
SeverityLevel,
20+
Transaction,
2221
TransactionEvent,
2322
Transport,
2423
} from '@sentry/types';
@@ -99,7 +98,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
9998
/** Holds flushable */
10099
private _outcomes: { [key: string]: number } = {};
101100

102-
private _hooks: HookStore = {};
101+
// eslint-disable-next-line @typescript-eslint/ban-types
102+
private _hooks: Record<string, Function[]> = {};
103103

104104
/**
105105
* Initializes this client instance.
@@ -355,24 +355,35 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
355355
}
356356
}
357357

358-
/**
359-
* @inheritDoc
360-
*/
361-
public on<HookType extends Hook>(hook: HookType['name'], callback: HookType['callback']): void {
358+
// Keep on() & emit() signatures in sync with types' client.ts interface
359+
360+
/** @inheritdoc */
361+
public on(hook: 'startTransaction' | 'finishTransaction', callback: (transaction: Transaction) => void): void;
362+
363+
/** @inheritdoc */
364+
public on(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
365+
366+
/** @inheritdoc */
367+
public on(hook: string, callback: unknown): void {
362368
if (!this._hooks[hook]) {
363369
this._hooks[hook] = [];
364370
}
365371

366-
(this._hooks[hook] as HookType['callback'][]).push(callback);
372+
// @ts-ignore We assue the types are correct
373+
this._hooks[hook].push(callback);
367374
}
368375

369-
/**
370-
* @inheritDoc
371-
*/
372-
public emit<HookType extends Hook>(hook: HookType['name'], ...args: Parameters<HookType['callback']>): void {
376+
/** @inheritdoc */
377+
public emit(hook: 'startTransaction' | 'finishTransaction', transaction: Transaction): void;
378+
379+
/** @inheritdoc */
380+
public emit(hook: 'beforeEnvelope', envelope: Envelope): void;
381+
382+
/** @inheritdoc */
383+
public emit(hook: string, ...rest: unknown[]): void {
373384
if (this._hooks[hook]) {
374-
// @ts-ignore it does not like ...args, but we know this is correct
375-
(this._hooks[hook] as HookType['callback'][]).forEach(callback => callback(...args));
385+
// @ts-ignore we cannot enforce the callback to match the hook
386+
this._hooks[hook].forEach(callback => callback(...rest));
376387
}
377388
}
378389

packages/core/test/lib/base.test.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, Span, Transaction, TransactionHook, EnvelopeHook, Envelope } from '@sentry/types';
1+
import type { Envelope, Event, Span, Transaction, Client } from '@sentry/types';
22
import { dsnToString, logger, SentryError, SyncPromise } from '@sentry/utils';
33

44
import { Hub, makeSession, Scope } from '../../src';
@@ -1732,41 +1732,45 @@ describe('BaseClient', () => {
17321732
});
17331733

17341734
describe('hooks', () => {
1735-
it('should call a startTransaction hook', () => {
1736-
expect.assertions(1);
1735+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
17371736

1738-
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
1739-
const client = new TestClient(options);
1737+
// Make sure types work for both Client & BaseClient
1738+
const scenarios = [
1739+
['BaseClient', new TestClient(options)],
1740+
['Client', new TestClient(options) as Client],
1741+
] as const;
17401742

1741-
const mockTransaction = {
1742-
traceId: '86f39e84263a4de99c326acab3bfe3bd',
1743-
} as Transaction;
1743+
describe.each(scenarios)('with client %s', (_, client) => {
1744+
it('should call a startTransaction hook', () => {
1745+
expect.assertions(1);
17441746

1745-
client?.on<TransactionHook>('startTransaction', (transaction: Transaction) => {
1746-
expect(transaction).toEqual(mockTransaction);
1747-
});
1747+
const mockTransaction = {
1748+
traceId: '86f39e84263a4de99c326acab3bfe3bd',
1749+
} as Transaction;
17481750

1749-
client?.emit<TransactionHook>('startTransaction', mockTransaction);
1750-
});
1751+
client.on?.('startTransaction', transaction => {
1752+
expect(transaction).toEqual(mockTransaction);
1753+
});
17511754

1752-
it('should call a beforeEnvelope hook', () => {
1753-
expect.assertions(1);
1755+
client.emit?.('startTransaction', mockTransaction);
1756+
});
17541757

1755-
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
1756-
const client = new TestClient(options);
1758+
it('should call a beforeEnvelope hook', () => {
1759+
expect.assertions(1);
17571760

1758-
const mockEnvelope = [
1759-
{
1760-
event_id: '12345',
1761-
},
1762-
{},
1763-
] as Envelope;
1761+
const mockEnvelope = [
1762+
{
1763+
event_id: '12345',
1764+
},
1765+
{},
1766+
] as Envelope;
17641767

1765-
client?.on<EnvelopeHook>('beforeEnvelope', (envelope: Envelope) => {
1766-
expect(envelope).toEqual(mockEnvelope);
1767-
});
1768+
client.on?.('beforeEnvelope', envelope => {
1769+
expect(envelope).toEqual(mockEnvelope);
1770+
});
17681771

1769-
client?.emit<EnvelopeHook>('beforeEnvelope', mockEnvelope);
1772+
client.emit?.('beforeEnvelope', mockEnvelope);
1773+
});
17701774
});
17711775
});
17721776
});

packages/types/src/client.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import type { EventDropReason } from './clientreport';
22
import type { DataCategory } from './datacategory';
33
import type { DsnComponents } from './dsn';
44
import type { Event, EventHint } from './event';
5-
import type { Hook } from './hooks';
65
import type { Integration, IntegrationClass } from './integration';
76
import type { ClientOptions } from './options';
87
import type { Scope } from './scope';
98
import type { SdkMetadata } from './sdkmetadata';
109
import type { Session, SessionAggregates } from './session';
1110
import type { Severity, SeverityLevel } from './severity';
1211
import type { Transport } from './transport';
12+
import { Transaction } from './transaction';
13+
import { Envelope } from './envelope';
1314

1415
/**
1516
* User-Facing Sentry SDK Client.
@@ -155,11 +156,22 @@ export interface Client<O extends ClientOptions = ClientOptions> {
155156
/**
156157
* Register a callback for transaction start and finish.
157158
*/
158-
on?<HookType extends Hook>(hook: HookType['name'], callback: HookType['callback']): void;
159+
on?(hook: 'startTransaction' | 'finishTransaction', callback: (transaction: Transaction) => void): void;
160+
161+
/**
162+
* Register a callback for transaction start and finish.
163+
*/
164+
on?(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
165+
166+
/**
167+
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
168+
* second argument.
169+
*/
170+
emit?(hook: 'startTransaction' | 'finishTransaction', transaction: Transaction): void;
159171

160172
/*
161173
* Fire a hook event for envelope creation and sending. Expects to be given an envelope as the
162174
* second argument.
163175
*/
164-
emit?<HookType extends Hook>(hook: HookType['name'], ...args: Parameters<HookType['callback']>): void;
176+
emit?(hook: 'beforeEnvelope', envelope: Envelope): void;
165177
}

packages/types/src/hooks.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/types/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,3 @@ export type { WrappedFunction } from './wrappedfunction';
9696
export type { Instrumenter } from './instrumenter';
9797

9898
export type { BrowserClientReplayOptions } from './browseroptions';
99-
export type { HookStore, Hook, TransactionHook, EnvelopeHook } from './hooks';

0 commit comments

Comments
 (0)