Skip to content

Commit a950be0

Browse files
committed
[WIP] Added the fluent event builder.
1 parent da2489b commit a950be0

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

src/exceptionless.ts

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,38 @@ module Exceptionless {
99
this.config = new Configuration(apiKey, serverUrl);
1010
}
1111

12-
submit(events:IEvent) {
12+
public register(handler: () => void) {}
13+
14+
//log(source:string, message:string, level?:string) {
15+
// if (!source) {
16+
// source = (<any>(arguments.callee.caller)).name;
17+
// }
18+
//
19+
// var event:IEvent = { type: 'log', source: source, message: message };
20+
// if (level) {
21+
// event.data['@level'] = level;
22+
// }
23+
//
24+
// this.submit(event);
25+
//}
26+
//
27+
//feature(feature:string) {
28+
// if (feature) {
29+
// this.submit({type: 'usage', source: feature});
30+
// }
31+
//}
32+
//
33+
//error(exception:Error) {
34+
// // TODO:
35+
//}
36+
37+
submit(event:IEvent, pluginContextData?:IContextData) {
1338
if (!this.config.enabled) {
1439
this.config.log.info('Event submission is currently disabled');
1540
return;
1641
}
42+
43+
this.config.queue.enqueue(event);
1744
}
1845
}
1946

@@ -464,6 +491,113 @@ module Exceptionless {
464491
session_id?: string;
465492
}
466493

494+
export class EventBuilder {
495+
target: IEvent;
496+
client: ExceptionlessClient;
497+
pluginContextData: IContextData;
498+
499+
constructor(event:IEvent, client:ExceptionlessClient, pluginContextData?:IContextData) {
500+
this.target = event;
501+
this.client = client;
502+
this.pluginContextData = pluginContextData;
503+
}
504+
505+
public setType(type:string): EventBuilder {
506+
this.target.type = type;
507+
return this;
508+
}
509+
510+
public setSource(source:string): EventBuilder {
511+
this.target.source = source;
512+
return this;
513+
}
514+
515+
public setSessionId(sessionId:string): EventBuilder {
516+
if (!this.isValidIdentifier(sessionId)) {
517+
throw new Error("SessionId must contain between 8 and 100 alphanumeric or '-' characters.");
518+
}
519+
520+
this.target.session_id = sessionId;
521+
return this;
522+
}
523+
524+
public setReferenceId(referenceId:string): EventBuilder {
525+
if (!this.isValidIdentifier(referenceId)) {
526+
throw new Error("SessionId must contain between 8 and 100 alphanumeric or '-' characters.");
527+
}
528+
529+
this.target.reference_id = referenceId;
530+
return this;
531+
}
532+
533+
534+
private isValidIdentifier(value:string): boolean {
535+
if (value == null) {
536+
return true;
537+
}
538+
539+
if (value.length < 8 || value.length > 100) {
540+
return false;
541+
}
542+
543+
//for (int index = 0; index < value.Length; index++) {
544+
// if (!Char.IsLetterOrDigit(value[index]) && value[index] != '-')
545+
// return false;
546+
//}
547+
548+
return true;
549+
}
550+
551+
public setMessage(message:string): EventBuilder {
552+
this.target.message = message;
553+
return this;
554+
}
555+
556+
public setGeo(latitude: number, longitude: number): EventBuilder {
557+
if (latitude < -90.0 || latitude > 90.0)
558+
throw new Error('Must be a valid latitude value between -90.0 and 90.0.');
559+
if (longitude < -180.0 || longitude > 180.0)
560+
throw new Error('Must be a valid longitude value between -180.0 and 180.0.');
561+
562+
this.target.geo = latitude + ',' + longitude;
563+
return this;
564+
}
565+
566+
public setValue(value:number): EventBuilder {
567+
this.target.value = value;
568+
return this;
569+
}
570+
571+
public addTags(tags:string[]): EventBuilder {
572+
if (tags == null || tags.length === 0) {
573+
return this;
574+
}
575+
576+
//this.target.tags.AddRange(tags.Where(t => !String.IsNullOrWhiteSpace(t)).Select(t => t.Trim()));
577+
return this;
578+
}
579+
580+
public setProperty(name:string, value:any): EventBuilder {
581+
this.target.data[name] = value;
582+
return this;
583+
}
584+
585+
public setCritical(critical:boolean): EventBuilder {
586+
// check to see if it already contains the critical tag.
587+
if (critical) {
588+
this.target.tags.push('Critical');
589+
}
590+
591+
return this;
592+
}
593+
594+
public submit(): void {
595+
this.client.submit(this.target, this.pluginContextData);
596+
}
597+
}
598+
599+
export interface IContextData {}
600+
467601
export interface IUserDescription {
468602
email_address?: string;
469603
description?: string;

0 commit comments

Comments
 (0)