-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathEventBuilder.ts
231 lines (189 loc) · 6.59 KB
/
EventBuilder.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { ExceptionlessClient } from "./ExceptionlessClient.js";
import { Event, EventType, KnownEventDataKeys } from "./models/Event.js";
import { ManualStackingInfo } from "./models/data/ManualStackingInfo.js";
import { UserInfo } from "./models/data/UserInfo.js";
import { EventContext } from "./models/EventContext.js";
import { isEmpty, stringify } from "./Utils.js";
import { EventPluginContext } from "./plugins/EventPluginContext.js";
export class EventBuilder {
public target: Event;
public client: ExceptionlessClient;
public context: EventContext;
private _validIdentifierErrorMessage = "must contain between 8 and 100 alphanumeric or '-' characters.";
constructor(event: Event, client: ExceptionlessClient, context?: EventContext) {
this.target = event;
this.client = client;
this.context = context || new EventContext();
}
public setType(type: EventType): EventBuilder {
if (type) {
this.target.type = type;
}
return this;
}
public setSource(source: string): EventBuilder {
if (source) {
this.target.source = source;
}
return this;
}
public setReferenceId(referenceId: string): EventBuilder {
if (!this.isValidIdentifier(referenceId)) {
throw new Error(`ReferenceId ${this._validIdentifierErrorMessage}`);
}
this.target.reference_id = referenceId;
return this;
}
/**
* Allows you to reference a parent event by its ReferenceId property. This allows you to have parent and child relationships.
* @param name Reference name
* @param id The reference id that points to a specific event
*/
public setEventReference(name: string, id: string): EventBuilder {
if (!name) {
throw new Error("Invalid name");
}
if (!id || !this.isValidIdentifier(id)) {
throw new Error(`Id ${this._validIdentifierErrorMessage}`);
}
this.setProperty("@ref:" + name, id);
return this;
}
public setMessage(message: string | null | undefined): EventBuilder {
if (message) {
this.target.message = message;
}
return this;
}
public setGeo(latitude: number, longitude: number): EventBuilder {
if (latitude < -90.0 || latitude > 90.0) {
throw new Error("Must be a valid latitude value between -90.0 and 90.0.");
}
if (longitude < -180.0 || longitude > 180.0) {
throw new Error("Must be a valid longitude value between -180.0 and 180.0.");
}
this.target.geo = `${latitude},${longitude}`;
return this;
}
public setUserIdentity(userInfo: UserInfo): EventBuilder;
public setUserIdentity(identity: string): EventBuilder;
public setUserIdentity(identity: string, name: string): EventBuilder;
public setUserIdentity(userInfoOrIdentity: UserInfo | string, name?: string): EventBuilder {
const userInfo = typeof userInfoOrIdentity !== "string" ? userInfoOrIdentity : { identity: userInfoOrIdentity, name };
if (!userInfo || (!userInfo.identity && !userInfo.name)) {
return this;
}
this.setProperty(KnownEventDataKeys.UserInfo, userInfo);
return this;
}
/**
* Sets the user"s description of the event.
*
* @param emailAddress The email address
* @param description The user"s description of the event.
*/
public setUserDescription(emailAddress: string, description: string): EventBuilder {
if (emailAddress && description) {
this.setProperty(KnownEventDataKeys.UserDescription, {
email_address: emailAddress,
description
});
}
return this;
}
/**
* Changes default stacking behavior by setting manual
* stacking information.
* @param signatureData A dictionary of strings to use for stacking.
* @param title An optional title for the stacking information.
*/
public setManualStackingInfo(signatureData: Record<string, string>, title?: string): EventBuilder {
if (signatureData) {
const stack: ManualStackingInfo = { signature_data: signatureData };
if (title) {
stack.title = title;
}
this.setProperty(KnownEventDataKeys.ManualStackingInfo, stack);
}
return this;
}
/**
* Changes default stacking behavior by setting the stacking key.
* @param manualStackingKey The manual stacking key.
* @param title An optional title for the stacking information.
*/
public setManualStackingKey(manualStackingKey: string, title?: string): EventBuilder {
if (manualStackingKey) {
const data = { ManualStackingKey: manualStackingKey };
this.setManualStackingInfo(data, title);
}
return this;
}
/**
* Sets the event value.
* @param value The value of the event.
*/
public setValue(value: number): EventBuilder {
if (value) {
this.target.value = value;
}
return this;
}
public addTags(...tags: string[]): EventBuilder {
this.target.tags = [...(this.target.tags || []), ...tags];
return this;
}
/**
* Adds the object to extended data. Uses @excludedPropertyNames
* to exclude data from being included in the event.
* @param name The name of the object to add.
* @param value The data object to add.
* @param maxDepth The max depth of the object to include.
* @param excludedPropertyNames Any property names that should be excluded.
*/
public setProperty(name: string, value: unknown, maxDepth?: number, excludedPropertyNames?: string[]): EventBuilder {
if (!name || value === undefined || value == null) {
return this;
}
if (!this.target.data) {
this.target.data = {};
}
const exclusions = this.client.config.dataExclusions.concat(excludedPropertyNames || []);
const json = stringify(value, exclusions, maxDepth);
if (!isEmpty(json)) {
this.target.data[name] = JSON.parse(json);
}
return this;
}
public setContextProperty(name: string, value: unknown): EventBuilder {
this.context[name] = value;
return this;
}
public markAsCritical(critical: boolean): EventBuilder {
if (critical) {
this.addTags("Critical");
}
return this;
}
public submit(): Promise<EventPluginContext> {
return this.client.submitEvent(this.target, this.context);
}
private isValidIdentifier(value: string): boolean {
if (!value) {
return true;
}
if (value.length < 8 || value.length > 100) {
return false;
}
for (let index = 0; index < value.length; index++) {
const code = value.charCodeAt(index);
const isDigit = code >= 48 && code <= 57;
const isLetter = (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
const isMinus = code === 45;
if (!(isDigit || isLetter) && !isMinus) {
return false;
}
}
return true;
}
}