@atsail/browser 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +31 -6
- package/dist/transport.d.ts +2 -1
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,13 +5,15 @@ var DEFAULT_MAX_BATCH_SIZE = 20;
|
|
|
5
5
|
|
|
6
6
|
class Transport {
|
|
7
7
|
publishableKey;
|
|
8
|
+
environment;
|
|
8
9
|
endpoint;
|
|
9
10
|
flushIntervalMs;
|
|
10
11
|
maxBatchSize;
|
|
11
12
|
queue = [];
|
|
12
13
|
timer = null;
|
|
13
|
-
constructor(publishableKey, endpoint = DEFAULT_ENDPOINT, flushIntervalMs = DEFAULT_FLUSH_INTERVAL_MS, maxBatchSize = DEFAULT_MAX_BATCH_SIZE) {
|
|
14
|
+
constructor(publishableKey, environment, endpoint = DEFAULT_ENDPOINT, flushIntervalMs = DEFAULT_FLUSH_INTERVAL_MS, maxBatchSize = DEFAULT_MAX_BATCH_SIZE) {
|
|
14
15
|
this.publishableKey = publishableKey;
|
|
16
|
+
this.environment = environment;
|
|
15
17
|
this.endpoint = endpoint;
|
|
16
18
|
this.flushIntervalMs = flushIntervalMs;
|
|
17
19
|
this.maxBatchSize = maxBatchSize;
|
|
@@ -32,7 +34,11 @@ class Transport {
|
|
|
32
34
|
return;
|
|
33
35
|
const events = this.queue;
|
|
34
36
|
this.queue = [];
|
|
35
|
-
const body = JSON.stringify({
|
|
37
|
+
const body = JSON.stringify({
|
|
38
|
+
publishableKey: this.publishableKey,
|
|
39
|
+
environment: this.environment,
|
|
40
|
+
events
|
|
41
|
+
});
|
|
36
42
|
if (useBeacon && navigator.sendBeacon) {
|
|
37
43
|
const blob = new Blob([body], { type: "application/json" });
|
|
38
44
|
navigator.sendBeacon(this.endpoint, blob);
|
|
@@ -130,20 +136,33 @@ function installXhrInterceptor(emit) {
|
|
|
130
136
|
|
|
131
137
|
// src/index.ts
|
|
132
138
|
var transport = null;
|
|
139
|
+
var LOCAL_LIKE_ENVIRONMENTS = /^(development|dev|local|test)$/i;
|
|
133
140
|
function init(options) {
|
|
134
141
|
if (transport)
|
|
135
142
|
return;
|
|
136
143
|
if (typeof window === "undefined")
|
|
137
144
|
return;
|
|
138
|
-
|
|
145
|
+
if (!options.environment) {
|
|
146
|
+
console.warn("[atsail] init() requires an `environment` option — SDK not started");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const isLocalLike = LOCAL_LIKE_ENVIRONMENTS.test(options.environment.trim());
|
|
150
|
+
if (isLocalLike && !options.enabled) {
|
|
151
|
+
console.warn(`[atsail] staying quiet in environment "${options.environment}" — pass enabled: true to capture anyway`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
transport = new Transport(options.publishableKey, options.environment, options.endpoint, options.flushIntervalMs, options.maxBatchSize);
|
|
155
|
+
transport.enqueue({ type: "init", timestamp: Date.now() });
|
|
139
156
|
const emit = transport.enqueue.bind(transport);
|
|
140
157
|
installGlobalHandlers(emit);
|
|
141
158
|
installFetchInterceptor(emit);
|
|
142
159
|
installXhrInterceptor(emit);
|
|
143
160
|
}
|
|
144
161
|
function captureException(error, context) {
|
|
145
|
-
if (!transport)
|
|
162
|
+
if (!transport) {
|
|
163
|
+
console.warn("[atsail] captureException called before init() — event dropped");
|
|
146
164
|
return;
|
|
165
|
+
}
|
|
147
166
|
transport.enqueue({
|
|
148
167
|
type: "exception",
|
|
149
168
|
message: error instanceof Error ? error.message : String(error),
|
|
@@ -153,8 +172,10 @@ function captureException(error, context) {
|
|
|
153
172
|
});
|
|
154
173
|
}
|
|
155
174
|
function captureMessage(message, level = "info", context) {
|
|
156
|
-
if (!transport)
|
|
175
|
+
if (!transport) {
|
|
176
|
+
console.warn("[atsail] captureMessage called before init() — event dropped");
|
|
157
177
|
return;
|
|
178
|
+
}
|
|
158
179
|
transport.enqueue({
|
|
159
180
|
type: "message",
|
|
160
181
|
message,
|
|
@@ -164,7 +185,11 @@ function captureMessage(message, level = "info", context) {
|
|
|
164
185
|
});
|
|
165
186
|
}
|
|
166
187
|
function flush() {
|
|
167
|
-
transport
|
|
188
|
+
if (!transport) {
|
|
189
|
+
console.warn("[atsail] flush called before init() — nothing to send");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
transport.flush();
|
|
168
193
|
}
|
|
169
194
|
export {
|
|
170
195
|
init,
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { AtsailEvent } from "./types";
|
|
2
2
|
export declare class Transport {
|
|
3
3
|
private readonly publishableKey;
|
|
4
|
+
private readonly environment;
|
|
4
5
|
private readonly endpoint;
|
|
5
6
|
private readonly flushIntervalMs;
|
|
6
7
|
private readonly maxBatchSize;
|
|
7
8
|
private queue;
|
|
8
9
|
private timer;
|
|
9
|
-
constructor(publishableKey: string, endpoint?: string, flushIntervalMs?: number, maxBatchSize?: number);
|
|
10
|
+
constructor(publishableKey: string, environment: string, endpoint?: string, flushIntervalMs?: number, maxBatchSize?: number);
|
|
10
11
|
enqueue(event: AtsailEvent): void;
|
|
11
12
|
flush(useBeacon?: boolean): void;
|
|
12
13
|
destroy(): void;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export type EventLevel = "info" | "warning" | "error";
|
|
2
2
|
export type AtsailEvent = {
|
|
3
|
+
type: "init";
|
|
4
|
+
timestamp: number;
|
|
5
|
+
} | {
|
|
3
6
|
type: "exception";
|
|
4
7
|
message: string;
|
|
5
8
|
stack?: string;
|
|
@@ -30,6 +33,8 @@ export type AtsailEvent = {
|
|
|
30
33
|
};
|
|
31
34
|
export type InitOptions = {
|
|
32
35
|
publishableKey: string;
|
|
36
|
+
environment: string;
|
|
37
|
+
enabled?: boolean;
|
|
33
38
|
endpoint?: string;
|
|
34
39
|
flushIntervalMs?: number;
|
|
35
40
|
maxBatchSize?: number;
|