|
| 1 | +/// <reference path="references.ts" /> |
| 2 | + |
| 3 | +module Exceptionless { |
| 4 | + export class EventBuilder { |
| 5 | + public target:IEvent; |
| 6 | + public client:ExceptionlessClient; |
| 7 | + public pluginContextData:ContextData; |
| 8 | + |
| 9 | + constructor(event:IEvent, client:ExceptionlessClient, pluginContextData?:ContextData) { |
| 10 | + this.target = event; |
| 11 | + this.client = client; |
| 12 | + this.pluginContextData = pluginContextData; |
| 13 | + } |
| 14 | + |
| 15 | + public setType(type:string): EventBuilder { |
| 16 | + if (!!type && type.length > 0) { |
| 17 | + this.target.type = type; |
| 18 | + } |
| 19 | + |
| 20 | + return this; |
| 21 | + } |
| 22 | + |
| 23 | + public setSource(source:string): EventBuilder { |
| 24 | + if (!!source && source.length > 0) { |
| 25 | + this.target.source = source; |
| 26 | + } |
| 27 | + |
| 28 | + return this; |
| 29 | + } |
| 30 | + |
| 31 | + public setSessionId(sessionId:string): EventBuilder { |
| 32 | + if (!this.isValidIdentifier(sessionId)) { |
| 33 | + throw new Error("SessionId must contain between 8 and 100 alphanumeric or '-' characters."); |
| 34 | + } |
| 35 | + |
| 36 | + this.target.session_id = sessionId; |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + public setReferenceId(referenceId:string): EventBuilder { |
| 41 | + if (!this.isValidIdentifier(referenceId)) { |
| 42 | + throw new Error("SessionId must contain between 8 and 100 alphanumeric or '-' characters."); |
| 43 | + } |
| 44 | + |
| 45 | + this.target.reference_id = referenceId; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public setMessage(message:string): EventBuilder { |
| 50 | + if (!!message && message.length > 0) { |
| 51 | + this.target.message = message; |
| 52 | + } |
| 53 | + |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public setGeo(latitude: number, longitude: number): EventBuilder { |
| 58 | + if (latitude < -90.0 || latitude > 90.0) |
| 59 | + throw new Error('Must be a valid latitude value between -90.0 and 90.0.'); |
| 60 | + if (longitude < -180.0 || longitude > 180.0) |
| 61 | + throw new Error('Must be a valid longitude value between -180.0 and 180.0.'); |
| 62 | + |
| 63 | + this.target.geo = latitude + ',' + longitude; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public setValue(value:number): EventBuilder { |
| 68 | + if (!!value) { |
| 69 | + this.target.value = value; |
| 70 | + } |
| 71 | + |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + public addTags(...tags:string[]): EventBuilder { |
| 76 | + if (!tags || tags.length === 0) { |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + if (!this.target.tags) { |
| 81 | + this.target.tags = []; |
| 82 | + } |
| 83 | + |
| 84 | + for (var index = 0; index < tags.length; index++) { |
| 85 | + if (tags[index] && this.target.tags.indexOf(tags[index]) < 0) { |
| 86 | + this.target.tags.push(tags[index]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public setProperty(name:string, value:any): EventBuilder { |
| 94 | + if (!name || (value === undefined || value == null)) { |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + if (!this.target.data) { |
| 99 | + this.target.data = {}; |
| 100 | + } |
| 101 | + |
| 102 | + this.target.data[name] = value; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public markAsCritical(critical:boolean): EventBuilder { |
| 107 | + if (critical) { |
| 108 | + this.addTags('Critical'); |
| 109 | + } |
| 110 | + |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public submit(): Promise<any> { |
| 115 | + return this.client.submitEvent(this.target, this.pluginContextData); |
| 116 | + } |
| 117 | + |
| 118 | + private isValidIdentifier(value:string): boolean { |
| 119 | + if (!value || !value.length) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + if (value.length < 8 || value.length > 100) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + for (var index = 0; index < value.length; index++) { |
| 128 | + var code = value.charCodeAt(index); |
| 129 | + var isDigit = (code >= 48) && (code <= 57); |
| 130 | + var isLetter = ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122)); |
| 131 | + var isMinus = code === 45; |
| 132 | + |
| 133 | + if (!(isDigit || isLetter) && !isMinus) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments