1
1
import type { Event } from "./types" ;
2
- import { EnvironmentInfo } from "./env" ;
2
+ import type { EnvironmentInfo } from "./env" ;
3
3
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 ;
9
9
10
10
constructor ( appKey : string , baseUrl : string , env : EnvironmentInfo ) {
11
11
this . apiUrl = `${ baseUrl } /api/v0/events` ;
@@ -16,14 +16,7 @@ export class EventDispatcher {
16
16
} ) ;
17
17
}
18
18
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 ;
27
20
28
21
public async flush ( ) : Promise < void > {
29
22
if ( this . _events . length === 0 ) {
@@ -45,7 +38,7 @@ export class EventDispatcher {
45
38
}
46
39
}
47
40
48
- private async _sendEvents ( events : Event [ ] ) : Promise < void > {
41
+ protected async _sendEvents ( events : Event [ ] ) : Promise < void > {
49
42
try {
50
43
const res = await fetch ( this . apiUrl , {
51
44
method : "POST" ,
@@ -54,7 +47,7 @@ export class EventDispatcher {
54
47
body : JSON . stringify ( events ) ,
55
48
} ) ;
56
49
57
- if ( res . status < 300 ) {
50
+ if ( res . ok ) {
58
51
return Promise . resolve ( ) ;
59
52
}
60
53
@@ -74,4 +67,67 @@ export class EventDispatcher {
74
67
throw e ;
75
68
}
76
69
}
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
+ }
118
+ }
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
+ }
77
133
}
0 commit comments