@@ -3,6 +3,35 @@ import { Integration } from '@sentry/types';
33export type UserFunctionIntegrations = ( integrations : Integration [ ] ) => Integration [ ] ;
44export type UserIntegrations = Integration [ ] | UserFunctionIntegrations ;
55
6+ type Options = {
7+ [ integrationName : string ] :
8+ | {
9+ keyPath : string ;
10+ value : unknown ;
11+ }
12+ | undefined ;
13+ } ;
14+
15+ /**
16+ * Recursively traverses an object to update an existing nested key.
17+ * Note: The provided key path must include existing properties,
18+ * the function will not create objects while traversing.
19+ *
20+ * @param obj An object to update
21+ * @param value The value to update the nested key with
22+ * @param keyPath The path to the key to update ex. fizz.buzz.foo
23+ */
24+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25+ function setNestedKey ( obj : Record < string , any > , keyPath : string , value : unknown ) : void {
26+ // Ex. foo.bar.zoop will extract foo and bar.zoop
27+ const match = keyPath . match ( / ( [ a - z ] + ) \. ( .* ) / i) ;
28+ if ( match === null ) {
29+ obj [ keyPath ] = value ;
30+ } else {
31+ setNestedKey ( obj [ match [ 1 ] ] , match [ 2 ] , value ) ;
32+ }
33+ }
34+
635/**
736 * Retrieves the patched integrations with the provided integration.
837 *
@@ -12,18 +41,40 @@ export type UserIntegrations = Integration[] | UserFunctionIntegrations;
1241 *
1342 * @param integration The integration to patch, if necessary.
1443 * @param userIntegrations Integrations defined by the user.
44+ * @param options options to update for a particular integration
1545 * @returns Final integrations, patched if necessary.
1646 */
17- export function addIntegration ( integration : Integration , userIntegrations : UserIntegrations ) : UserIntegrations {
47+ export function addIntegration (
48+ integration : Integration ,
49+ userIntegrations : UserIntegrations ,
50+ options : Options = { } ,
51+ ) : UserIntegrations {
1852 if ( Array . isArray ( userIntegrations ) ) {
19- return addIntegrationToArray ( integration , userIntegrations ) ;
53+ return addIntegrationToArray ( integration , userIntegrations , options ) ;
2054 } else {
21- return addIntegrationToFunction ( integration , userIntegrations ) ;
55+ return addIntegrationToFunction ( integration , userIntegrations , options ) ;
2256 }
2357}
2458
25- function addIntegrationToArray ( integration : Integration , userIntegrations : Integration [ ] ) : Integration [ ] {
26- if ( userIntegrations . map ( int => int . name ) . includes ( integration . name ) ) {
59+ function addIntegrationToArray (
60+ integration : Integration ,
61+ userIntegrations : Integration [ ] ,
62+ options : Options ,
63+ ) : Integration [ ] {
64+ let includesName = false ;
65+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
66+ for ( let x = 0 ; x < userIntegrations . length ; x ++ ) {
67+ if ( userIntegrations [ x ] . name === integration . name ) {
68+ includesName = true ;
69+ }
70+
71+ const op = options [ userIntegrations [ x ] . name ] ;
72+ if ( op ) {
73+ setNestedKey ( userIntegrations [ x ] , op . keyPath , op . value ) ;
74+ }
75+ }
76+
77+ if ( includesName ) {
2778 return userIntegrations ;
2879 }
2980 return [ ...userIntegrations , integration ] ;
@@ -32,10 +83,11 @@ function addIntegrationToArray(integration: Integration, userIntegrations: Integ
3283function addIntegrationToFunction (
3384 integration : Integration ,
3485 userIntegrationsFunc : UserFunctionIntegrations ,
86+ options : Options ,
3587) : UserFunctionIntegrations {
3688 const wrapper : UserFunctionIntegrations = defaultIntegrations => {
3789 const userFinalIntegrations = userIntegrationsFunc ( defaultIntegrations ) ;
38- return addIntegrationToArray ( integration , userFinalIntegrations ) ;
90+ return addIntegrationToArray ( integration , userFinalIntegrations , options ) ;
3991 } ;
4092 return wrapper ;
4193}
0 commit comments