Skip to content

fix: Ensure all integration classes have correct types #10183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
HandlerDataFetch,
HandlerDataHistory,
HandlerDataXhr,
Integration,
IntegrationClass,
IntegrationFn,
} from '@sentry/types';
import type {
Expand Down Expand Up @@ -55,7 +57,7 @@ const MAX_ALLOWED_STRING_LENGTH = 1024;

const INTEGRATION_NAME = 'Breadcrumbs';

const breadcrumbsIntegration: IntegrationFn = (options: Partial<BreadcrumbsOptions> = {}) => {
const breadcrumbsIntegration = ((options: Partial<BreadcrumbsOptions> = {}) => {
const _options = {
console: true,
dom: true,
Expand Down Expand Up @@ -91,13 +93,31 @@ const breadcrumbsIntegration: IntegrationFn = (options: Partial<BreadcrumbsOptio
}
},
};
};
}) satisfies IntegrationFn;

/**
* Default Breadcrumbs instrumentations
*/
// eslint-disable-next-line deprecation/deprecation
export const Breadcrumbs = convertIntegrationFnToClass(INTEGRATION_NAME, breadcrumbsIntegration);
export const Breadcrumbs = convertIntegrationFnToClass(INTEGRATION_NAME, breadcrumbsIntegration) as IntegrationClass<
Integration & { setup: (client: Client) => void }
> & {
new (
options?: Partial<{
console: boolean;
dom:
| boolean
| {
serializeAttribute?: string | string[];
maxStringLength?: number;
};
fetch: boolean;
history: boolean;
sentry: boolean;
xhr: boolean;
}>,
): Integration;
};

/**
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
Expand Down
10 changes: 6 additions & 4 deletions packages/browser/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { convertIntegrationFnToClass } from '@sentry/core';
import type { Event, Exception, IntegrationFn, StackFrame } from '@sentry/types';
import type { Event, Exception, Integration, IntegrationClass, IntegrationFn, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';

const INTEGRATION_NAME = 'Dedupe';

const dedupeIntegration: IntegrationFn = () => {
const dedupeIntegration = (() => {
let previousEvent: Event | undefined;

return {
Expand All @@ -31,11 +31,13 @@ const dedupeIntegration: IntegrationFn = () => {
return (previousEvent = currentEvent);
},
};
};
}) satisfies IntegrationFn;

/** Deduplication filter */
// eslint-disable-next-line deprecation/deprecation
export const Dedupe = convertIntegrationFnToClass(INTEGRATION_NAME, dedupeIntegration);
export const Dedupe = convertIntegrationFnToClass(INTEGRATION_NAME, dedupeIntegration) as IntegrationClass<
Integration & { processEvent: (event: Event) => Event }
>;

function _shouldDropEvent(currentEvent: Event, previousEvent?: Event): boolean {
if (!previousEvent) {
Expand Down
21 changes: 17 additions & 4 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { captureEvent, convertIntegrationFnToClass, getClient } from '@sentry/core';
import type { Client, Event, IntegrationFn, Primitive, StackParser } from '@sentry/types';
import type {
Client,
Event,
Integration,
IntegrationClass,
IntegrationFn,
Primitive,
StackParser,
} from '@sentry/types';
import {
addGlobalErrorInstrumentationHandler,
addGlobalUnhandledRejectionInstrumentationHandler,
Expand All @@ -22,7 +30,7 @@ type GlobalHandlersIntegrations = Record<GlobalHandlersIntegrationsOptionKeys, b

const INTEGRATION_NAME = 'GlobalHandlers';

const globalHandlersIntegrations: IntegrationFn = (options: Partial<GlobalHandlersIntegrations> = {}) => {
const globalHandlersIntegration = ((options: Partial<GlobalHandlersIntegrations> = {}) => {
const _options = {
onerror: true,
onunhandledrejection: true,
Expand All @@ -45,11 +53,16 @@ const globalHandlersIntegrations: IntegrationFn = (options: Partial<GlobalHandle
}
},
};
};
}) satisfies IntegrationFn;

/** Global handlers */
// eslint-disable-next-line deprecation/deprecation
export const GlobalHandlers = convertIntegrationFnToClass(INTEGRATION_NAME, globalHandlersIntegrations);
export const GlobalHandlers = convertIntegrationFnToClass(
INTEGRATION_NAME,
globalHandlersIntegration,
) as IntegrationClass<Integration & { setup: (client: Client) => void }> & {
new (options?: Partial<GlobalHandlersIntegrations>): Integration;
};

function _installGlobalOnErrorHandler(client: Client): void {
addGlobalErrorInstrumentationHandler(data => {
Expand Down
10 changes: 6 additions & 4 deletions packages/browser/src/integrations/httpcontext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { convertIntegrationFnToClass } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import type { Event, Integration, IntegrationClass, IntegrationFn } from '@sentry/types';

import { WINDOW } from '../helpers';

const INTEGRATION_NAME = 'HttpContext';

const httpContextIntegration: IntegrationFn = () => {
const httpContextIntegration = (() => {
return {
name: INTEGRATION_NAME,
// TODO v8: Remove this
Expand All @@ -31,8 +31,10 @@ const httpContextIntegration: IntegrationFn = () => {
event.request = request;
},
};
};
}) satisfies IntegrationFn;

/** HttpContext integration collects information about HTTP request headers */
// eslint-disable-next-line deprecation/deprecation
export const HttpContext = convertIntegrationFnToClass(INTEGRATION_NAME, httpContextIntegration);
export const HttpContext = convertIntegrationFnToClass(INTEGRATION_NAME, httpContextIntegration) as IntegrationClass<
Integration & { preprocessEvent: (event: Event) => void }
>;
10 changes: 6 additions & 4 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { convertIntegrationFnToClass } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import type { Client, Event, EventHint, Integration, IntegrationClass, IntegrationFn } from '@sentry/types';
import { applyAggregateErrorsToEvent } from '@sentry/utils';
import { exceptionFromError } from '../eventbuilder';

Expand All @@ -13,7 +13,7 @@ const DEFAULT_LIMIT = 5;

const INTEGRATION_NAME = 'LinkedErrors';

const linkedErrorsIntegration: IntegrationFn = (options: LinkedErrorsOptions = {}) => {
const linkedErrorsIntegration = ((options: LinkedErrorsOptions = {}) => {
const limit = options.limit || DEFAULT_LIMIT;
const key = options.key || DEFAULT_KEY;

Expand All @@ -36,8 +36,10 @@ const linkedErrorsIntegration: IntegrationFn = (options: LinkedErrorsOptions = {
);
},
};
};
}) satisfies IntegrationFn;

/** Aggregrate linked errors in an event. */
// eslint-disable-next-line deprecation/deprecation
export const LinkedErrors = convertIntegrationFnToClass(INTEGRATION_NAME, linkedErrorsIntegration);
export const LinkedErrors = convertIntegrationFnToClass(INTEGRATION_NAME, linkedErrorsIntegration) as IntegrationClass<
Integration & { preprocessEvent: (event: Event, hint: EventHint, client: Client) => void }
> & { new (options?: { key?: string; limit?: number }): Integration };
19 changes: 15 additions & 4 deletions packages/browser/src/integrations/trycatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { convertIntegrationFnToClass } from '@sentry/core';
import type { IntegrationFn, WrappedFunction } from '@sentry/types';
import type { Integration, IntegrationClass, IntegrationFn, WrappedFunction } from '@sentry/types';
import { fill, getFunctionName, getOriginalFunction } from '@sentry/utils';

import { WINDOW, wrap } from '../helpers';
Expand Down Expand Up @@ -50,7 +50,7 @@ interface TryCatchOptions {
eventTarget: boolean | string[];
}

const tryCatchIntegration: IntegrationFn = (options: Partial<TryCatchOptions> = {}) => {
const browserApiErrorsIntegration = ((options: Partial<TryCatchOptions> = {}) => {
const _options = {
XMLHttpRequest: true,
eventTarget: true,
Expand Down Expand Up @@ -88,11 +88,22 @@ const tryCatchIntegration: IntegrationFn = (options: Partial<TryCatchOptions> =
}
},
};
};
}) satisfies IntegrationFn;

/** Wrap timer functions and event targets to catch errors and provide better meta data */
// eslint-disable-next-line deprecation/deprecation
export const TryCatch = convertIntegrationFnToClass(INTEGRATION_NAME, tryCatchIntegration);
export const TryCatch = convertIntegrationFnToClass(
INTEGRATION_NAME,
browserApiErrorsIntegration,
) as IntegrationClass<Integration> & {
new (options?: {
setTimeout: boolean;
setInterval: boolean;
requestAnimationFrame: boolean;
XMLHttpRequest: boolean;
eventTarget: boolean | string[];
}): Integration;
};

function _wrapTimeFunction(original: () => void): () => number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
11 changes: 7 additions & 4 deletions packages/browser/src/profiling/integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { convertIntegrationFnToClass, getCurrentScope } from '@sentry/core';
import type { EventEnvelope, IntegrationFn, Transaction } from '@sentry/types';
import type { Client, EventEnvelope, Integration, IntegrationClass, IntegrationFn, Transaction } from '@sentry/types';
import type { Profile } from '@sentry/types/src/profiling';
import { logger } from '@sentry/utils';

Expand All @@ -18,7 +18,7 @@ import {

const INTEGRATION_NAME = 'BrowserProfiling';

const browserProfilingIntegration: IntegrationFn = () => {
const browserProfilingIntegration = (() => {
return {
name: INTEGRATION_NAME,
// TODO v8: Remove this
Expand Down Expand Up @@ -100,7 +100,7 @@ const browserProfilingIntegration: IntegrationFn = () => {
});
},
};
};
}) satisfies IntegrationFn;

/**
* Browser profiling integration. Stores any event that has contexts["profile"]["profile_id"]
Expand All @@ -112,4 +112,7 @@ const browserProfilingIntegration: IntegrationFn = () => {
* @experimental
*/
// eslint-disable-next-line deprecation/deprecation
export const BrowserProfilingIntegration = convertIntegrationFnToClass(INTEGRATION_NAME, browserProfilingIntegration);
export const BrowserProfilingIntegration = convertIntegrationFnToClass(
INTEGRATION_NAME,
browserProfilingIntegration,
) as IntegrationClass<Integration & { setup: (client: Client) => void }>;
1 change: 0 additions & 1 deletion packages/browser/test/unit/profiling/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('BrowserProfilingIntegration', () => {
Sentry.init({
tracesSampleRate: 1,
profilesSampleRate: 1,
debug: true,
environment: 'test-environment',
dsn: 'https://[email protected]/6625302',
transport: _opts => {
Expand Down
4 changes: 2 additions & 2 deletions packages/bun/src/integrations/bunserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { getSanitizedUrlString, parseUrl } from '@sentry/utils';

const INTEGRATION_NAME = 'BunServer';

const bunServerIntegration: IntegrationFn = () => {
const bunServerIntegration = (() => {
return {
name: INTEGRATION_NAME,
setupOnce() {
instrumentBunServe();
},
};
};
}) satisfies IntegrationFn;

/**
* Instruments `Bun.serve` to automatically create transactions and capture errors.
Expand Down
17 changes: 5 additions & 12 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client, Event, EventHint, Integration, IntegrationFn, Options } from '@sentry/types';
import type { Client, Event, EventHint, Integration, IntegrationClass, IntegrationFn, Options } from '@sentry/types';
import { arrayify, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './debug-build';
Expand Down Expand Up @@ -169,18 +169,11 @@ function findIndex<T>(arr: T[], callback: (item: T) => boolean): number {
export function convertIntegrationFnToClass<Fn extends IntegrationFn>(
name: string,
fn: Fn,
): Integration & {
id: string;
new (...args: Parameters<Fn>): Integration & ReturnType<Fn>;
} {
): IntegrationClass<Integration> {
return Object.assign(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function ConvertedIntegration(...rest: Parameters<Fn>) {
return fn(...rest);
function ConvertedIntegration(...args: Parameters<Fn>): Integration {
return fn(...args);
},
{ id: name },
) as unknown as Integration & {
id: string;
new (...args: Parameters<Fn>): Integration & ReturnType<Fn>;
};
) as unknown as IntegrationClass<Integration>;
}
11 changes: 7 additions & 4 deletions packages/core/src/integrations/functiontostring.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { IntegrationFn, WrappedFunction } from '@sentry/types';
import type { Integration, IntegrationClass, IntegrationFn, WrappedFunction } from '@sentry/types';
import { getOriginalFunction } from '@sentry/utils';
import { convertIntegrationFnToClass } from '../integration';

let originalFunctionToString: () => void;

const INTEGRATION_NAME = 'FunctionToString';

const functionToStringIntegration: IntegrationFn = () => {
const functionToStringIntegration = (() => {
return {
name: INTEGRATION_NAME,
setupOnce() {
Expand All @@ -26,8 +26,11 @@ const functionToStringIntegration: IntegrationFn = () => {
}
},
};
};
}) satisfies IntegrationFn;

/** Patch toString calls to return proper name for wrapped functions */
// eslint-disable-next-line deprecation/deprecation
export const FunctionToString = convertIntegrationFnToClass(INTEGRATION_NAME, functionToStringIntegration);
export const FunctionToString = convertIntegrationFnToClass(
INTEGRATION_NAME,
functionToStringIntegration,
) as IntegrationClass<Integration & { setupOnce: () => void }>;
23 changes: 19 additions & 4 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Event, IntegrationFn, StackFrame } from '@sentry/types';
import type { Client, Event, EventHint, Integration, IntegrationClass, IntegrationFn, StackFrame } from '@sentry/types';
import { getEventDescription, logger, stringMatchesSomePattern } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
Expand Down Expand Up @@ -30,7 +30,7 @@ export interface InboundFiltersOptions {
}

const INTEGRATION_NAME = 'InboundFilters';
const inboundFiltersIntegration: IntegrationFn = (options: Partial<InboundFiltersOptions>) => {
const inboundFiltersIntegration = ((options: Partial<InboundFiltersOptions> = {}) => {
return {
name: INTEGRATION_NAME,
// TODO v8: Remove this
Expand All @@ -41,11 +41,26 @@ const inboundFiltersIntegration: IntegrationFn = (options: Partial<InboundFilter
return _shouldDropEvent(event, mergedOptions) ? null : event;
},
};
};
}) satisfies IntegrationFn;

/** Inbound filters configurable by the user */
// eslint-disable-next-line deprecation/deprecation
export const InboundFilters = convertIntegrationFnToClass(INTEGRATION_NAME, inboundFiltersIntegration);
export const InboundFilters = convertIntegrationFnToClass(
INTEGRATION_NAME,
inboundFiltersIntegration,
) as IntegrationClass<Integration & { preprocessEvent: (event: Event, hint: EventHint, client: Client) => void }> & {
new (
options?: Partial<{
allowUrls: Array<string | RegExp>;
denyUrls: Array<string | RegExp>;
ignoreErrors: Array<string | RegExp>;
ignoreTransactions: Array<string | RegExp>;
ignoreInternal: boolean;
disableErrorDefaults: boolean;
disableTransactionDefaults: boolean;
}>,
): Integration;
};

function _mergeOptions(
internalOptions: Partial<InboundFiltersOptions> = {},
Expand Down
Loading