11import type { Event } from "./types" ;
22import { EnvironmentInfo } from "./env" ;
33
4- export class EventDispatcher {
5- private _events : Event [ ] = [ ] ;
6- private MAX_BATCH_SIZE = 25 ;
7- private headers : Headers ;
8- private apiUrl : string ;
4+ export abstract class EventDispatcher {
5+ protected _events : Event [ ] = [ ] ;
6+ protected MAX_BATCH_SIZE = 25 ;
7+ protected headers : Headers ;
8+ protected apiUrl : string ;
99
1010 constructor ( appKey : string , baseUrl : string , env : EnvironmentInfo ) {
1111 this . apiUrl = `${ baseUrl } /api/v0/events` ;
@@ -16,14 +16,7 @@ export class EventDispatcher {
1616 } ) ;
1717 }
1818
19- public enqueue ( evt : Event | Event [ ] ) {
20- if ( Array . isArray ( evt ) ) {
21- this . _events . push ( ...evt ) ;
22- return ;
23- }
24-
25- this . _events . push ( evt ) ;
26- }
19+ public abstract enqueue ( evt : Event | Event [ ] ) : void ;
2720
2821 public async flush ( ) : Promise < void > {
2922 if ( this . _events . length === 0 ) {
@@ -45,7 +38,7 @@ export class EventDispatcher {
4538 }
4639 }
4740
48- private async _sendEvents ( events : Event [ ] ) : Promise < void > {
41+ protected async _sendEvents ( events : Event [ ] ) : Promise < void > {
4942 try {
5043 const res = await fetch ( this . apiUrl , {
5144 method : "POST" ,
@@ -54,7 +47,7 @@ export class EventDispatcher {
5447 body : JSON . stringify ( events ) ,
5548 } ) ;
5649
57- if ( res . status < 300 ) {
50+ if ( res . ok ) {
5851 return Promise . resolve ( ) ;
5952 }
6053
@@ -74,4 +67,67 @@ export class EventDispatcher {
7467 throw e ;
7568 }
7669 }
70+
71+ protected async _sendEvent ( event : Event ) : Promise < void > {
72+ try {
73+ const res = await fetch ( this . apiUrl , {
74+ method : "POST" ,
75+ headers : this . headers ,
76+ credentials : "omit" ,
77+ body : JSON . stringify ( event ) ,
78+ } ) ;
79+
80+ if ( res . ok ) {
81+ return Promise . resolve ( ) ;
82+ }
83+
84+ const reason = `${ res . status } ${ await res . text ( ) } ` ;
85+ if ( res . status < 500 ) {
86+ console . warn (
87+ `Aptabase: Failed to send event because of ${ reason } . Will not retry.`
88+ ) ;
89+ return Promise . resolve ( ) ;
90+ }
91+
92+ throw new Error ( reason ) ;
93+ } catch ( e ) {
94+ console . error ( `Aptabase: Failed to send event. Reason: ${ e } ` ) ;
95+ throw e ;
96+ }
97+ }
98+ }
99+
100+ export class WebEventDispatcher extends EventDispatcher {
101+ constructor ( appKey : string , baseUrl : string , env : EnvironmentInfo ) {
102+ super ( appKey , baseUrl , env ) ;
103+ this . apiUrl = `${ baseUrl } /api/v0/event` ;
104+ this . headers = new Headers ( {
105+ "Content-Type" : "application/json" ,
106+ "App-Key" : appKey ,
107+ // No User-Agent header for web
108+ } ) ;
109+ }
110+
111+ public enqueue ( evt : Event | Event [ ] ) : void {
112+ if ( Array . isArray ( evt ) ) {
113+ evt . forEach ( ( event ) => this . _sendEvent ( event ) ) ;
114+ } else {
115+ this . _sendEvent ( evt ) ;
116+ }
117+ }
77118}
119+
120+ export class NativeEventDispatcher extends EventDispatcher {
121+ constructor ( appKey : string , baseUrl : string , env : EnvironmentInfo ) {
122+ super ( appKey , baseUrl , env ) ;
123+ this . apiUrl = `${ baseUrl } /api/v0/events` ;
124+ }
125+
126+ public enqueue ( evt : Event | Event [ ] ) : void {
127+ if ( Array . isArray ( evt ) ) {
128+ this . _events . push ( ...evt ) ;
129+ } else {
130+ this . _events . push ( evt ) ;
131+ }
132+ }
133+ }
0 commit comments