Skip to content

Commit 0bd5df0

Browse files
committed
simplify logic
1 parent c5547ee commit 0bd5df0

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,13 @@ export function init(options: NextjsOptions): void {
5757
});
5858
}
5959

60-
function createClientIntegrations(userIntegrations?: 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 (userIntegrations) {
67-
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
68-
BrowserTracing: { keyPath: 'options.routingInstrumentation', value: nextRouterInstrumentation },
69-
});
70-
} else {
71-
return [defaultBrowserTracingIntegration];
72-
}
66+
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
67+
'options.routingInstrumentation': nextRouterInstrumentation,
68+
});
7369
}

packages/nextjs/src/index.server.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,12 @@ function addServerIntegrations(options: NextjsOptions): void {
107107
return frame;
108108
},
109109
});
110-
111-
if (options.integrations) {
112-
options.integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, options.integrations);
113-
} else {
114-
options.integrations = [defaultRewriteFramesIntegration];
115-
}
110+
options.integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, options.integrations || []);
116111

117112
if (hasTracingEnabled(options)) {
118113
const defaultHttpTracingIntegration = new Integrations.Http({ tracing: true });
119114
options.integrations = addOrUpdateIntegration(defaultHttpTracingIntegration, options.integrations, {
120-
Http: { keyPath: '_tracing', value: true },
115+
_tracing: true,
121116
});
122117
}
123118
}

packages/nextjs/src/utils/userIntegrations.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ export type UserIntegrationsFunction = (integrations: Integration[]) => Integrat
44
export type UserIntegrations = Integration[] | UserIntegrationsFunction;
55

66
type ForcedIntegrationOptions = {
7-
[integrationName: string]:
8-
| {
9-
keyPath: string;
10-
value: unknown;
11-
}
12-
| undefined;
7+
[keyPath: string]: unknown;
138
};
149

1510
/**
@@ -30,7 +25,8 @@ function setNestedKey(obj: Record<string, any>, keyPath: string, value: unknown)
3025
obj[keyPath] = value;
3126
} else {
3227
// `match[1]` is the initial segment of the path, and `match[2]` is the remainder of the path
33-
setNestedKey(obj[match[1]], match[2], value);
28+
const innerObj = obj[match[1]];
29+
setNestedKey(innerObj, match[2], value);
3430
}
3531
}
3632

@@ -52,34 +48,26 @@ export function addOrUpdateIntegration(
5248
userIntegrations: UserIntegrations,
5349
forcedOptions: ForcedIntegrationOptions = {},
5450
): UserIntegrations {
55-
if (Array.isArray(userIntegrations)) {
56-
return addOrUpdateIntegrationInArray(defaultIntegrationInstance, userIntegrations, forcedOptions);
57-
} else {
58-
return addOrUpdateIntegrationInFunction(defaultIntegrationInstance, userIntegrations, forcedOptions);
59-
}
51+
return Array.isArray(userIntegrations)
52+
? addOrUpdateIntegrationInArray(defaultIntegrationInstance, userIntegrations, forcedOptions)
53+
: addOrUpdateIntegrationInFunction(defaultIntegrationInstance, userIntegrations, forcedOptions);
6054
}
6155

6256
function addOrUpdateIntegrationInArray(
6357
defaultIntegrationInstance: Integration,
6458
userIntegrations: Integration[],
6559
forcedOptions: ForcedIntegrationOptions,
6660
): Integration[] {
67-
let includesName = false;
68-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
69-
for (let x = 0; x < userIntegrations.length; x++) {
70-
if (userIntegrations[x].name === defaultIntegrationInstance.name) {
71-
includesName = true;
72-
}
61+
const userInstance = userIntegrations.find(integration => integration.name === defaultIntegrationInstance.name);
7362

74-
const optionToSet = forcedOptions[userIntegrations[x].name];
75-
if (optionToSet) {
76-
setNestedKey(userIntegrations[x], optionToSet.keyPath, optionToSet.value);
63+
if (userInstance) {
64+
for (const [keyPath, value] of Object.entries(forcedOptions)) {
65+
setNestedKey(userInstance, keyPath, value);
7766
}
78-
}
7967

80-
if (includesName) {
8168
return userIntegrations;
8269
}
70+
8371
return [...userIntegrations, defaultIntegrationInstance];
8472
}
8573

0 commit comments

Comments
 (0)