Skip to content

Commit 55d708f

Browse files
authored
feat(core): Add addIntegration method to client (#6651)
1 parent 6cfd730 commit 55d708f

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

packages/core/src/baseclient.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737

3838
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
3939
import { createEventEnvelope, createSessionEnvelope } from './envelope';
40-
import { IntegrationIndex, setupIntegrations } from './integration';
40+
import { IntegrationIndex, setupIntegration, setupIntegrations } from './integration';
4141
import { Scope } from './scope';
4242
import { updateSession } from './session';
4343
import { prepareEvent } from './utils/prepareEvent';
@@ -291,6 +291,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
291291
}
292292
}
293293

294+
/**
295+
* @inheritDoc
296+
*/
297+
public addIntegration(integration: Integration): void {
298+
setupIntegration(integration, this._integrations);
299+
}
300+
294301
/**
295302
* @inheritDoc
296303
*/

packages/core/src/integration.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ export function setupIntegrations(integrations: Integration[]): IntegrationIndex
8888
const integrationIndex: IntegrationIndex = {};
8989

9090
integrations.forEach(integration => {
91-
integrationIndex[integration.name] = integration;
92-
93-
if (installedIntegrations.indexOf(integration.name) === -1) {
94-
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
95-
installedIntegrations.push(integration.name);
96-
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
97-
}
91+
setupIntegration(integration, integrationIndex);
9892
});
9993

10094
return integrationIndex;
10195
}
96+
97+
/** Setup a single integration. */
98+
export function setupIntegration(integration: Integration, integrationIndex: IntegrationIndex): void {
99+
integrationIndex[integration.name] = integration;
100+
101+
if (installedIntegrations.indexOf(integration.name) === -1) {
102+
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
103+
installedIntegrations.push(integration.name);
104+
__DEBUG_BUILD__ && logger.log(`Integration installed: ${integration.name}`);
105+
}
106+
}

packages/types/src/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ export interface Client<O extends ClientOptions = ClientOptions> {
108108
/** Returns the client's instance of the given integration class, it any. */
109109
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null;
110110

111+
/**
112+
* Add an integration to the client.
113+
* This can be used to e.g. lazy load integrations.
114+
* In most cases, this should not be necessary, and you're better off just passing the integrations via `integrations: []` at initialization time.
115+
* However, if you find the need to conditionally load & add an integration, you can use `addIntegration` to do so.
116+
*
117+
* TODO (v8): Make this a required method.
118+
* */
119+
addIntegration?(integration: Integration): void;
120+
111121
/** This is an internal function to setup all integrations that should run on the client */
112122
setupIntegrations(): void;
113123

packages/types/src/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
222222
*/
223223
tracesSampler?: (samplingContext: SamplingContext) => number | boolean;
224224

225-
// TODO v8: Narrow the response type to `ErrorEvent` - this is technically a breaking change.
225+
// TODO (v8): Narrow the response type to `ErrorEvent` - this is technically a breaking change.
226226
/**
227227
* An event-processing callback for error and message events, guaranteed to be invoked after all other event
228228
* processors, which allows an event to be modified or dropped.
@@ -236,7 +236,7 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
236236
*/
237237
beforeSend?: (event: ErrorEvent, hint: EventHint) => PromiseLike<Event | null> | Event | null;
238238

239-
// TODO v8: Narrow the response type to `TransactionEvent` - this is technically a breaking change.
239+
// TODO (v8): Narrow the response type to `TransactionEvent` - this is technically a breaking change.
240240
/**
241241
* An event-processing callback for transaction events, guaranteed to be invoked after all other event
242242
* processors. This allows an event to be modified or dropped before it's sent.

0 commit comments

Comments
 (0)