|
1 |
| -import type { Client, Event, EventHint, Integration, IntegrationFn, IntegrationFnResult, Options } from '@sentry/types'; |
| 1 | +import type { |
| 2 | + Client, |
| 3 | + Event, |
| 4 | + EventHint, |
| 5 | + Integration, |
| 6 | + IntegrationClass, |
| 7 | + IntegrationFn, |
| 8 | + IntegrationFnResult, |
| 9 | + Options, |
| 10 | +} from '@sentry/types'; |
2 | 11 | import { arrayify, logger } from '@sentry/utils';
|
3 | 12 |
|
4 | 13 | import { DEBUG_BUILD } from './debug-build';
|
@@ -160,22 +169,44 @@ function findIndex<T>(arr: T[], callback: (item: T) => boolean): number {
|
160 | 169 | * Generate a full integration function from a simple function.
|
161 | 170 | * This will ensure to add the given name both to the function definition, as well as to the integration return value.
|
162 | 171 | */
|
163 |
| -export function makeIntegrationFn< |
164 |
| - Fn extends (...rest: any[]) => Partial<IntegrationFnResult>, |
165 |
| ->(name: string, fn: Fn): ((...rest: Parameters<Fn>) => ReturnType<Fn> & { name: string }) & { id: string } { |
| 172 | +export function makeIntegrationFn<Fn extends (...rest: any[]) => Partial<IntegrationFnResult>>( |
| 173 | + name: string, |
| 174 | + fn: Fn, |
| 175 | +): ((...rest: Parameters<Fn>) => ReturnType<Fn> & { name: string }) & { id: string } { |
166 | 176 | const patchedFn = addIdToIntegrationFnResult(name, fn);
|
167 | 177 |
|
168 |
| - |
169 | 178 | return Object.assign(patchedFn, { id: name });
|
170 | 179 | }
|
171 | 180 |
|
| 181 | +/** |
| 182 | + * Convert a new integration function to the legacy class syntax. |
| 183 | + * In v8, we can remove this and instead export the integration functions directly. |
| 184 | + */ |
| 185 | +export function convertIntegrationFnToClass<Fn extends IntegrationFn<(...rest: any[]) => IntegrationFnResult>>( |
| 186 | + fn: Fn, |
| 187 | +): IntegrationClass<Integration> { |
| 188 | + return Object.assign( |
| 189 | + function ConvertedIntegration(...rest: any[]) { |
| 190 | + const res = { |
| 191 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 192 | + setupOnce: () => {}, |
| 193 | + ...fn(...rest), |
| 194 | + name: fn.id, |
| 195 | + }; |
| 196 | + |
| 197 | + return res; |
| 198 | + }, |
| 199 | + { id: fn.id }, |
| 200 | + ) as unknown as IntegrationClass<Integration>; |
| 201 | +} |
| 202 | + |
172 | 203 | function addIdToIntegrationFnResult<Fn extends (...rest: any[]) => Partial<IntegrationFnResult>>(
|
173 | 204 | name: string,
|
174 | 205 | fn: Fn,
|
175 | 206 | ): (...rest: Parameters<Fn>) => ReturnType<Fn> & { name: string } {
|
176 |
| - const patchedFn = ((...rest: Parameters<Fn>): ReturnType<Fn> & { name: string } => { |
| 207 | + const patchedFn = (...rest: Parameters<Fn>): ReturnType<Fn> & { name: string } => { |
177 | 208 | return { ...fn(...rest), name } as ReturnType<Fn> & { name: string };
|
178 |
| - }); |
| 209 | + }; |
179 | 210 |
|
180 | 211 | return patchedFn;
|
181 | 212 | }
|
0 commit comments