forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmissionClientBase.ts
More file actions
73 lines (59 loc) · 2.44 KB
/
SubmissionClientBase.ts
File metadata and controls
73 lines (59 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Configuration } from "../configuration/Configuration.js";
import {
ClientSettings,
SettingsManager,
} from "../configuration/SettingsManager.js";
import { Event } from "../models/Event.js";
import { UserDescription } from "../models/data/UserDescription.js";
import { ISubmissionClient } from "./ISubmissionClient";
import { Response } from "./Response";
export interface FetchOptions {
method: "GET" | "POST";
body?: string;
}
export abstract class SubmissionClientBase implements ISubmissionClient {
protected readonly ConfigurationVersionHeader: string = "x-exceptionless-configversion";
public constructor(protected config: Configuration) { }
public getSettings(version: number): Promise<Response<ClientSettings>> {
const url = `${this.config.serverUrl}/api/v2/projects/config?v=${version}`;
return this.fetch<ClientSettings>(url, {
method: "GET",
});
}
public async submitEvents(events: Event[]): Promise<Response<void>> {
const url = `${this.config.serverUrl}/api/v2/events`;
const response = await this.fetch<void>(url, {
method: "POST",
body: JSON.stringify(events)
});
await this.updateSettingsVersion(response.settingsVersion);
return response;
}
public async submitUserDescription(referenceId: string, description: UserDescription): Promise<Response<void>> {
const url = `${this.config.serverUrl}/api/v2/events/by-ref/${
encodeURIComponent(referenceId)
}/user-description`;
const response = await this.fetch<void>(url, {
method: "POST",
body: JSON.stringify(description),
});
await this.updateSettingsVersion(response.settingsVersion);
return response;
}
public async submitHeartbeat(sessionIdOrUserId: string, closeSession: boolean): Promise<Response<void>> {
const url = `${this.config.heartbeatServerUrl}/api/v2/events/session/heartbeat?id=${sessionIdOrUserId}&close=${closeSession}`;
const response = await this.fetch<void>(url, {
method: "GET"
});
await this.updateSettingsVersion(response.settingsVersion);
return response;
}
protected async updateSettingsVersion(settingsVersion: number): Promise<void> {
if (!isNaN(settingsVersion)) {
await SettingsManager.checkVersion(settingsVersion, this.config);
} else {
this.config.services.log.error("No config version header was returned.");
}
}
protected abstract fetch<T>(url: string, options: FetchOptions): Promise<Response<T>>;
}