Skip to content

Commit c5c8d82

Browse files
committed
reduce
1 parent 8771e99 commit c5c8d82

File tree

4 files changed

+27
-48
lines changed

4 files changed

+27
-48
lines changed

packages/core/src/integration.ts

+7-29
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,8 @@ function findIndex<T>(arr: T[], callback: (item: T) => boolean): number {
170170
* This will ensure to add the given name both to the function definition (as id),
171171
* as well as to the integration return value.
172172
*/
173-
export function makeIntegrationFn<
174-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
175-
Fn extends (...rest: any[]) => Partial<IntegrationFnResult>,
176-
>(name: string, fn: Fn): ((...rest: Parameters<Fn>) => ReturnType<Fn> & { name: string }) & { id: string } {
177-
const patchedFn = addNameToIntegrationFnResult(name, fn) as ((
178-
...rest: Parameters<Fn>
179-
) => ReturnType<Fn> & { name: string }) & { id: string };
180-
181-
patchedFn.id = name;
182-
return patchedFn;
173+
export function makeIntegrationFn<Fn extends IntegrationFn>(fn: Fn): Fn {
174+
return fn;
183175
}
184176

185177
/**
@@ -188,33 +180,19 @@ export function makeIntegrationFn<
188180
*
189181
* @deprecated This will be removed in v8!
190182
*/
191-
export function convertIntegrationFnToClass<
192-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
193-
Fn extends IntegrationFn<(...rest: any[]) => IntegrationFnResult>,
194-
>(fn: Fn): IntegrationClass<Integration> {
183+
export function convertIntegrationFnToClass<Fn extends IntegrationFn>(
184+
name: string,
185+
fn: Fn,
186+
): IntegrationClass<Integration> {
195187
return Object.assign(
196188
// eslint-disable-next-line @typescript-eslint/no-explicit-any
197189
function ConvertedIntegration(...rest: any[]) {
198190
return {
199191
// eslint-disable-next-line @typescript-eslint/no-empty-function
200192
setupOnce: () => {},
201193
...fn(...rest),
202-
name: fn.id,
203194
};
204195
},
205-
{ id: fn.id },
196+
{ id: name },
206197
) as unknown as IntegrationClass<Integration>;
207198
}
208-
209-
function addNameToIntegrationFnResult<
210-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
211-
Fn extends (...rest: any[]) => Partial<IntegrationFnResult>,
212-
>(name: string, fn: Fn): (...rest: Parameters<Fn>) => ReturnType<Fn> & { name: string } {
213-
const patchedFn = (...rest: Parameters<Fn>): ReturnType<Fn> & { name: string } => {
214-
const result = fn(...rest);
215-
result.name = name;
216-
return result as ReturnType<Fn> & { name: string };
217-
};
218-
219-
return patchedFn;
220-
}

packages/core/src/integrations/inboundfilters.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export interface InboundFiltersOptions {
2929
disableTransactionDefaults: boolean;
3030
}
3131

32-
const inboundFiltersIntegration = makeIntegrationFn('InboundFilters', (options: Partial<InboundFiltersOptions>) => {
32+
const INTEGRATION_NAME = 'InboundFilters';
33+
const inboundFiltersIntegration = makeIntegrationFn((options: Partial<InboundFiltersOptions>) => {
3334
return {
35+
name: INTEGRATION_NAME,
3436
processEvent(event, _hint, client) {
3537
const clientOptions = client.getOptions();
3638
const mergedOptions = _mergeOptions(options, clientOptions);
@@ -41,7 +43,7 @@ const inboundFiltersIntegration = makeIntegrationFn('InboundFilters', (options:
4143

4244
/** Inbound filters configurable by the user */
4345
// eslint-disable-next-line deprecation/deprecation
44-
export const InboundFilters = convertIntegrationFnToClass(inboundFiltersIntegration);
46+
export const InboundFilters = convertIntegrationFnToClass(INTEGRATION_NAME, inboundFiltersIntegration);
4547

4648
function _mergeOptions(
4749
internalOptions: Partial<InboundFiltersOptions> = {},

packages/core/test/lib/integration.test.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,12 @@ describe('addIntegration', () => {
657657

658658
describe('makeIntegrationFn', () => {
659659
it('works with a minimal integration', () => {
660-
const myIntegration = makeIntegrationFn('testName', () => {
661-
return {};
660+
const myIntegration = makeIntegrationFn(() => {
661+
return {
662+
name: 'testName'
663+
};
662664
});
663665

664-
expect(myIntegration.id).toBe('testName');
665-
666666
const integration = myIntegration();
667667
expect(integration).toEqual({
668668
name: 'testName',
@@ -673,12 +673,12 @@ describe('makeIntegrationFn', () => {
673673
});
674674

675675
it('works with integration options', () => {
676-
const myIntegration = makeIntegrationFn('testName', (_options: { xxx: string }) => {
677-
return {};
676+
const myIntegration = makeIntegrationFn((_options: { xxx: string }) => {
677+
return {
678+
name: 'testName'
679+
};
678680
});
679681

680-
expect(myIntegration.id).toBe('testName');
681-
682682
const integration = myIntegration({ xxx: 'aa' });
683683
expect(integration).toEqual({
684684
name: 'testName',
@@ -696,17 +696,16 @@ describe('makeIntegrationFn', () => {
696696
const processEvent = jest.fn();
697697
const preprocessEvent = jest.fn();
698698

699-
const myIntegration = makeIntegrationFn('testName', () => {
699+
const myIntegration = makeIntegrationFn(() => {
700700
return {
701+
name: 'testName',
701702
setup,
702703
setupOnce,
703704
processEvent,
704705
preprocessEvent,
705706
};
706707
});
707708

708-
expect(myIntegration.id).toBe('testName');
709-
710709
const integration = myIntegration();
711710
expect(integration).toEqual({
712711
name: 'testName',
@@ -724,9 +723,9 @@ describe('makeIntegrationFn', () => {
724723
describe('convertIntegrationFnToClass', () => {
725724
/* eslint-disable deprecation/deprecation */
726725
it('works with a minimal integration', () => {
727-
const integrationFn = makeIntegrationFn('testName', () => ({}));
726+
const integrationFn = makeIntegrationFn(() => ({ name: 'testName'}));
728727

729-
const IntegrationClass = convertIntegrationFnToClass(integrationFn);
728+
const IntegrationClass = convertIntegrationFnToClass('testName', integrationFn);
730729

731730
expect(IntegrationClass.id).toBe('testName');
732731

@@ -743,16 +742,17 @@ describe('convertIntegrationFnToClass', () => {
743742
const processEvent = jest.fn();
744743
const preprocessEvent = jest.fn();
745744

746-
const integrationFn = makeIntegrationFn('testName', () => {
745+
const integrationFn = makeIntegrationFn( () => {
747746
return {
747+
name: 'testName',
748748
setup,
749749
setupOnce,
750750
processEvent,
751751
preprocessEvent,
752752
};
753753
});
754754

755-
const IntegrationClass = convertIntegrationFnToClass(integrationFn);
755+
const IntegrationClass = convertIntegrationFnToClass('testName', integrationFn);
756756

757757
expect(IntegrationClass.id).toBe('testName');
758758

packages/types/src/integration.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ export interface IntegrationClass<T> {
1616
/**
1717
* An integration in function form.
1818
* This is expected to return an integration result,
19-
* and also have a `name` property that holds the integration name (so we can e.g. filter these before actually calling them).
2019
*/
21-
export type IntegrationFn<Fn extends (...rest: any[]) => IntegrationFnResult> = { id: string } & Fn;
20+
export type IntegrationFn = (...rest: any[]) => IntegrationFnResult;
2221

2322
export interface IntegrationFnResult {
2423
/**

0 commit comments

Comments
 (0)