Skip to content

Commit c5547ee

Browse files
committed
rename all the things
1 parent 58c067e commit c5547ee

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EventProcessor } from '@sentry/types';
55
import { nextRouterInstrumentation } from './performance/client';
66
import { buildMetadata } from './utils/metadata';
77
import { NextjsOptions } from './utils/nextjsOptions';
8-
import { addIntegration, UserIntegrations } from './utils/userIntegrations';
8+
import { addOrUpdateIntegration, UserIntegrations } from './utils/userIntegrations';
99

1010
export * from '@sentry/react';
1111
export { nextRouterInstrumentation } from './performance/client';
@@ -57,14 +57,14 @@ export function init(options: NextjsOptions): void {
5757
});
5858
}
5959

60-
function createClientIntegrations(integrations?: UserIntegrations): UserIntegrations {
60+
function createClientIntegrations(userIntegrations?: UserIntegrations): UserIntegrations {
6161
const defaultBrowserTracingIntegration = new BrowserTracing({
6262
tracingOrigins: [...defaultRequestInstrumentationOptions.tracingOrigins, /^(api\/)/],
6363
routingInstrumentation: nextRouterInstrumentation,
6464
});
6565

66-
if (integrations) {
67-
return addIntegration(defaultBrowserTracingIntegration, integrations, {
66+
if (userIntegrations) {
67+
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
6868
BrowserTracing: { keyPath: 'options.routingInstrumentation', value: nextRouterInstrumentation },
6969
});
7070
} else {

packages/nextjs/src/index.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as path from 'path';
1010
import { isBuild } from './utils/isBuild';
1111
import { buildMetadata } from './utils/metadata';
1212
import { NextjsOptions } from './utils/nextjsOptions';
13-
import { addIntegration } from './utils/userIntegrations';
13+
import { addOrUpdateIntegration } from './utils/userIntegrations';
1414

1515
export * from '@sentry/node';
1616
export { captureUnderscoreErrorException } from './utils/_error';
@@ -109,14 +109,14 @@ function addServerIntegrations(options: NextjsOptions): void {
109109
});
110110

111111
if (options.integrations) {
112-
options.integrations = addIntegration(defaultRewriteFramesIntegration, options.integrations);
112+
options.integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, options.integrations);
113113
} else {
114114
options.integrations = [defaultRewriteFramesIntegration];
115115
}
116116

117117
if (hasTracingEnabled(options)) {
118118
const defaultHttpTracingIntegration = new Integrations.Http({ tracing: true });
119-
options.integrations = addIntegration(defaultHttpTracingIntegration, options.integrations, {
119+
options.integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, options.integrations, {
120120
Http: { keyPath: '_tracing', value: true },
121121
});
122122
}
Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Integration } from '@sentry/types';
22

3-
export type UserFunctionIntegrations = (integrations: Integration[]) => Integration[];
4-
export type UserIntegrations = Integration[] | UserFunctionIntegrations;
3+
export type UserIntegrationsFunction = (integrations: Integration[]) => Integration[];
4+
export type UserIntegrations = Integration[] | UserIntegrationsFunction;
55

6-
type Options = {
6+
type ForcedIntegrationOptions = {
77
[integrationName: string]:
88
| {
99
keyPath: string;
@@ -25,69 +25,72 @@ type Options = {
2525
function setNestedKey(obj: Record<string, any>, keyPath: string, value: unknown): void {
2626
// Ex. foo.bar.zoop will extract foo and bar.zoop
2727
const match = keyPath.match(/([a-z]+)\.(.*)/i);
28+
// The match will be null when there's no more recursing to do, i.e., when we've reached the right level of the object
2829
if (match === null) {
2930
obj[keyPath] = value;
3031
} else {
32+
// `match[1]` is the initial segment of the path, and `match[2]` is the remainder of the path
3133
setNestedKey(obj[match[1]], match[2], value);
3234
}
3335
}
3436

3537
/**
36-
* Retrieves the patched integrations with the provided integration.
38+
* Enforces inclusion of a given integration with specified options in an integration array originally determined by the
39+
* user, by either including the given default instance or by patching an existing user instance with the given options.
3740
*
38-
* The integration must be present in the final user integrations, and they are compared
39-
* by integration name. If the user has defined one, there's nothing to patch; if not,
40-
* the provided integration is added.
41+
* Ideally this would happen when integrations are set up, but there isn't currently a mechanism there for merging
42+
* options from a default integration instance with those from a user-provided instance of the same integration, only
43+
* for allowing the user to override a default instance entirely. (TODO: Fix that.)
4144
*
42-
* @param integration The integration to patch, if necessary.
45+
* @param defaultIntegrationInstance An instance of the integration with the correct options already set
4346
* @param userIntegrations Integrations defined by the user.
44-
* @param options options to update for a particular integration
45-
* @returns Final integrations, patched if necessary.
47+
* @param forcedOptions Options with which to patch an existing user-derived instance on the integration.
48+
* @returns A final integrations array.
4649
*/
47-
export function addIntegration(
48-
integration: Integration,
50+
export function addOrUpdateIntegration(
51+
defaultIntegrationInstance: Integration,
4952
userIntegrations: UserIntegrations,
50-
options: Options = {},
53+
forcedOptions: ForcedIntegrationOptions = {},
5154
): UserIntegrations {
5255
if (Array.isArray(userIntegrations)) {
53-
return addIntegrationToArray(integration, userIntegrations, options);
56+
return addOrUpdateIntegrationInArray(defaultIntegrationInstance, userIntegrations, forcedOptions);
5457
} else {
55-
return addIntegrationToFunction(integration, userIntegrations, options);
58+
return addOrUpdateIntegrationInFunction(defaultIntegrationInstance, userIntegrations, forcedOptions);
5659
}
5760
}
5861

59-
function addIntegrationToArray(
60-
integration: Integration,
62+
function addOrUpdateIntegrationInArray(
63+
defaultIntegrationInstance: Integration,
6164
userIntegrations: Integration[],
62-
options: Options,
65+
forcedOptions: ForcedIntegrationOptions,
6366
): Integration[] {
6467
let includesName = false;
6568
// eslint-disable-next-line @typescript-eslint/prefer-for-of
6669
for (let x = 0; x < userIntegrations.length; x++) {
67-
if (userIntegrations[x].name === integration.name) {
70+
if (userIntegrations[x].name === defaultIntegrationInstance.name) {
6871
includesName = true;
6972
}
7073

71-
const op = options[userIntegrations[x].name];
72-
if (op) {
73-
setNestedKey(userIntegrations[x], op.keyPath, op.value);
74+
const optionToSet = forcedOptions[userIntegrations[x].name];
75+
if (optionToSet) {
76+
setNestedKey(userIntegrations[x], optionToSet.keyPath, optionToSet.value);
7477
}
7578
}
7679

7780
if (includesName) {
7881
return userIntegrations;
7982
}
80-
return [...userIntegrations, integration];
83+
return [...userIntegrations, defaultIntegrationInstance];
8184
}
8285

83-
function addIntegrationToFunction(
84-
integration: Integration,
85-
userIntegrationsFunc: UserFunctionIntegrations,
86-
options: Options,
87-
): UserFunctionIntegrations {
88-
const wrapper: UserFunctionIntegrations = defaultIntegrations => {
86+
function addOrUpdateIntegrationInFunction(
87+
defaultIntegrationInstance: Integration,
88+
userIntegrationsFunc: UserIntegrationsFunction,
89+
forcedOptions: ForcedIntegrationOptions,
90+
): UserIntegrationsFunction {
91+
const wrapper: UserIntegrationsFunction = defaultIntegrations => {
8992
const userFinalIntegrations = userIntegrationsFunc(defaultIntegrations);
90-
return addIntegrationToArray(integration, userFinalIntegrations, options);
93+
return addOrUpdateIntegrationInArray(defaultIntegrationInstance, userFinalIntegrations, forcedOptions);
9194
};
9295
return wrapper;
9396
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { RewriteFrames } from '@sentry/integrations';
22
import { Integration } from '@sentry/types';
33

4-
import { addIntegration, UserFunctionIntegrations } from '../../src/utils/userIntegrations';
4+
import { addOrUpdateIntegration, UserIntegrationsFunction } from '../../src/utils/userIntegrations';
55

66
const testIntegration = new RewriteFrames();
77

88
describe('user integrations without any integrations', () => {
99
test('as an array', () => {
1010
const userIntegrations: Integration[] = [];
1111
// Should get a single integration
12-
let finalIntegrations = addIntegration(testIntegration, userIntegrations);
12+
let finalIntegrations = addOrUpdateIntegration(testIntegration, userIntegrations);
1313
expect(finalIntegrations).toBeInstanceOf(Array);
1414
finalIntegrations = finalIntegrations as Integration[];
1515
expect(finalIntegrations).toHaveLength(1);
1616
expect(finalIntegrations[0]).toMatchObject(testIntegration);
1717
});
1818

1919
test('as a function', () => {
20-
const userIntegrationFnc: UserFunctionIntegrations = (): Integration[] => [];
20+
const userIntegrationFnc: UserIntegrationsFunction = (): Integration[] => [];
2121
// Should get a single integration
22-
const integrationWrapper = addIntegration(testIntegration, userIntegrationFnc);
22+
const integrationWrapper = addOrUpdateIntegration(testIntegration, userIntegrationFnc);
2323
expect(integrationWrapper).toBeInstanceOf(Function);
24-
const finalIntegrations = (integrationWrapper as UserFunctionIntegrations)([]);
24+
const finalIntegrations = (integrationWrapper as UserIntegrationsFunction)([]);
2525
expect(finalIntegrations).toHaveLength(1);
2626
expect(finalIntegrations[0]).toMatchObject(testIntegration);
2727
});
@@ -31,20 +31,20 @@ describe('user integrations with integrations', () => {
3131
test('as an array', () => {
3232
const userIntegrations = [new RewriteFrames()];
3333
// Should get the same array (with no patches)
34-
const finalIntegrations = addIntegration(testIntegration, userIntegrations);
34+
const finalIntegrations = addOrUpdateIntegration(testIntegration, userIntegrations);
3535
expect(finalIntegrations).toMatchObject(userIntegrations);
3636
});
3737

3838
test('as a function', () => {
3939
const userIntegrations = [new RewriteFrames()];
40-
const integrationsFnc: UserFunctionIntegrations = (_integrations: Integration[]): Integration[] => {
40+
const integrationsFnc: UserIntegrationsFunction = (_integrations: Integration[]): Integration[] => {
4141
return userIntegrations;
4242
};
4343
// Should get a function that returns the test integration
44-
let finalIntegrations = addIntegration(testIntegration, integrationsFnc);
44+
let finalIntegrations = addOrUpdateIntegration(testIntegration, integrationsFnc);
4545
expect(typeof finalIntegrations === 'function').toBe(true);
4646
expect(finalIntegrations).toBeInstanceOf(Function);
47-
finalIntegrations = finalIntegrations as UserFunctionIntegrations;
47+
finalIntegrations = finalIntegrations as UserIntegrationsFunction;
4848
expect(finalIntegrations([])).toMatchObject(userIntegrations);
4949
});
5050
});

0 commit comments

Comments
 (0)