@@ -35,7 +35,14 @@ import {
3535 Watchable ,
3636} from './storage' ;
3737import { Timeline } from './timeline' ;
38- import { DestinationFilters , EventType , SegmentAPISettings } from './types' ;
38+ import {
39+ DestinationFilters ,
40+ EventType ,
41+ SegmentAPISettings ,
42+ SegmentAPIConsentSettings ,
43+ EdgeFunctionSettings ,
44+ EnrichmentClosure ,
45+ } from './types' ;
3946import {
4047 Config ,
4148 Context ,
@@ -59,7 +66,6 @@ import {
5966 SegmentError ,
6067 translateHTTPError ,
6168} from './errors' ;
62- import type { SegmentAPIConsentSettings } from '.' ;
6369
6470type OnPluginAddedCallback = ( plugin : Plugin ) => void ;
6571
@@ -125,6 +131,11 @@ export class SegmentClient {
125131 */
126132 readonly consentSettings : Watchable < SegmentAPIConsentSettings | undefined > ;
127133
134+ /**
135+ * Access or subscribe to edge functions settings
136+ */
137+ readonly edgeFunctionSettings : Watchable < EdgeFunctionSettings | undefined > ;
138+
128139 /**
129140 * Access or subscribe to destination filter settings
130141 */
@@ -212,6 +223,11 @@ export class SegmentClient {
212223 onChange : this . store . consentSettings . onChange ,
213224 } ;
214225
226+ this . edgeFunctionSettings = {
227+ get : this . store . edgeFunctionSettings . get ,
228+ onChange : this . store . edgeFunctionSettings . onChange ,
229+ } ;
230+
215231 this . filters = {
216232 get : this . store . filters . get ,
217233 onChange : this . store . filters . onChange ,
@@ -307,20 +323,26 @@ export class SegmentClient {
307323 const settingsEndpoint = `${ settingsPrefix } /${ this . config . writeKey } /settings` ;
308324
309325 try {
310- const res = await fetch ( settingsEndpoint ) ;
326+ const res = await fetch ( settingsEndpoint , {
327+ headers : {
328+ 'Cache-Control' : 'no-cache' ,
329+ } ,
330+ } ) ;
311331 checkResponseForErrors ( res ) ;
312332
313333 const resJson : SegmentAPISettings =
314334 ( await res . json ( ) ) as SegmentAPISettings ;
315335 const integrations = resJson . integrations ;
316336 const consentSettings = resJson . consentSettings ;
337+ const edgeFunctionSettings = resJson . edgeFunction ;
317338 const filters = this . generateFiltersMap (
318339 resJson . middlewareSettings ?. routingRules ?? [ ]
319340 ) ;
320341 this . logger . info ( 'Received settings from Segment succesfully.' ) ;
321342 await Promise . all ( [
322343 this . store . settings . set ( integrations ) ,
323344 this . store . consentSettings . set ( consentSettings ) ,
345+ this . store . edgeFunctionSettings . set ( edgeFunctionSettings ) ,
324346 this . store . filters . set ( filters ) ,
325347 ] ) ;
326348 } catch ( e ) {
@@ -422,8 +444,9 @@ export class SegmentClient {
422444 this . timeline . remove ( plugin ) ;
423445 }
424446
425- async process ( incomingEvent : SegmentEvent ) {
447+ async process ( incomingEvent : SegmentEvent , enrichment ?: EnrichmentClosure ) {
426448 const event = this . applyRawEventData ( incomingEvent ) ;
449+ event . enrichment = enrichment ;
427450
428451 if ( this . isReady . value ) {
429452 return this . startTimelineProcessing ( event ) ;
@@ -536,47 +559,63 @@ export class SegmentClient {
536559 }
537560 }
538561
539- async screen ( name : string , options ?: JsonMap ) {
562+ async screen (
563+ name : string ,
564+ options ?: JsonMap ,
565+ enrichment ?: EnrichmentClosure
566+ ) {
540567 const event = createScreenEvent ( {
541568 name,
542569 properties : options ,
543570 } ) ;
544571
545- await this . process ( event ) ;
572+ await this . process ( event , enrichment ) ;
546573 this . logger . info ( 'SCREEN event saved' , event ) ;
547574 }
548575
549- async track ( eventName : string , options ?: JsonMap ) {
576+ async track (
577+ eventName : string ,
578+ options ?: JsonMap ,
579+ enrichment ?: EnrichmentClosure
580+ ) {
550581 const event = createTrackEvent ( {
551582 event : eventName ,
552583 properties : options ,
553584 } ) ;
554585
555- await this . process ( event ) ;
586+ await this . process ( event , enrichment ) ;
556587 this . logger . info ( 'TRACK event saved' , event ) ;
557588 }
558589
559- async identify ( userId ?: string , userTraits ?: UserTraits ) {
590+ async identify (
591+ userId ?: string ,
592+ userTraits ?: UserTraits ,
593+ enrichment ?: EnrichmentClosure
594+ ) {
560595 const event = createIdentifyEvent ( {
561596 userId : userId ,
562597 userTraits : userTraits ,
563598 } ) ;
564599
565- await this . process ( event ) ;
600+ await this . process ( event , enrichment ) ;
566601 this . logger . info ( 'IDENTIFY event saved' , event ) ;
567602 }
568603
569- async group ( groupId : string , groupTraits ?: GroupTraits ) {
604+ async group (
605+ groupId : string ,
606+ groupTraits ?: GroupTraits ,
607+ enrichment ?: EnrichmentClosure
608+ ) {
570609 const event = createGroupEvent ( {
571610 groupId,
572611 groupTraits,
573612 } ) ;
574613
575- await this . process ( event ) ;
614+ await this . process ( event , enrichment ) ;
576615 this . logger . info ( 'GROUP event saved' , event ) ;
577616 }
578617
579- async alias ( newUserId : string ) {
618+ async alias ( newUserId : string , enrichment ?: EnrichmentClosure ) {
580619 // We don't use a concurrency safe version of get here as we don't want to lock the values yet,
581620 // we will update the values correctly when InjectUserInfo processes the change
582621 const { anonymousId, userId : previousUserId } = this . store . userInfo . get ( ) ;
@@ -587,7 +626,7 @@ export class SegmentClient {
587626 newUserId,
588627 } ) ;
589628
590- await this . process ( event ) ;
629+ await this . process ( event , enrichment ) ;
591630 this . logger . info ( 'ALIAS event saved' , event ) ;
592631 }
593632
@@ -721,7 +760,11 @@ export class SegmentClient {
721760 * @param callback Function to call
722761 */
723762 onPluginLoaded ( callback : OnPluginAddedCallback ) {
724- this . onPluginAddedObservers . push ( callback ) ;
763+ const i = this . onPluginAddedObservers . push ( callback ) ;
764+
765+ return ( ) => {
766+ this . onPluginAddedObservers . splice ( i , 1 ) ;
767+ } ;
725768 }
726769
727770 private triggerOnPluginLoaded ( plugin : Plugin ) {
0 commit comments