Skip to content

Commit 99f67e2

Browse files
committed
use generic for hook
1 parent 79e1f59 commit 99f67e2

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

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

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

44
import { Hub, makeSession, Scope } from '../../src';
@@ -1742,11 +1742,31 @@ describe('BaseClient', () => {
17421742
traceId: '86f39e84263a4de99c326acab3bfe3bd',
17431743
} as Transaction;
17441744

1745-
client?.on('startTransaction', (transaction: Transaction) => {
1745+
client?.on<TransactionHook>('startTransaction', (transaction: Transaction) => {
17461746
expect(transaction).toEqual(mockTransaction);
17471747
});
17481748

1749-
client?.emit('startTransaction', mockTransaction);
1749+
client?.emit<TransactionHook>('startTransaction', mockTransaction);
1750+
});
1751+
1752+
it('should call a beforeEnvelope hook', () => {
1753+
expect.assertions(1);
1754+
1755+
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
1756+
const client = new TestClient(options);
1757+
1758+
const mockEnvelope = [
1759+
{
1760+
event_id: '12345',
1761+
},
1762+
{},
1763+
] as Envelope;
1764+
1765+
client?.on<EnvelopeHook>('beforeEnvelope', (envelope: Envelope) => {
1766+
expect(envelope).toEqual(mockEnvelope);
1767+
});
1768+
1769+
client?.emit<EnvelopeHook>('beforeEnvelope', mockEnvelope);
17501770
});
17511771
});
17521772
});

packages/types/src/client.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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 { EnvelopeHook, TransactionHook } from './hooks';
5+
import type { Hook } from './hooks';
66
import type { Integration, IntegrationClass } from './integration';
77
import type { ClientOptions } from './options';
88
import type { Scope } from './scope';
@@ -155,22 +155,11 @@ export interface Client<O extends ClientOptions = ClientOptions> {
155155
/**
156156
* Register a callback for transaction start and finish.
157157
*/
158-
on?(hook: TransactionHook['name'], callback: TransactionHook['callback']): void;
159-
160-
/**
161-
* Register a callback for envelope creation and sending.
162-
*/
163-
on?(hook: EnvelopeHook['name'], callback: EnvelopeHook['callback']): void;
164-
165-
/**
166-
* Fire a hook event for transaction start and finish. Expects to be given a transaction as the
167-
* second argument.
168-
*/
169-
emit?(hook: TransactionHook['name'], ...params: Parameters<TransactionHook['callback']>): void;
158+
on?<HookType extends Hook>(hook: HookType['name'], callback: HookType['callback']): void;
170159

171160
/*
172161
* Fire a hook event for envelope creation and sending. Expects to be given an envelope as the
173162
* second argument.
174163
*/
175-
emit?(hook: EnvelopeHook['name'], ...params: Parameters<EnvelopeHook['callback']>): void;
164+
emit?<HookType extends Hook>(hook: HookType['name'], ...args: Parameters<HookType['callback']>): void;
176165
}

0 commit comments

Comments
 (0)