Skip to content

Commit 8e971c3

Browse files
committed
Added Typescript version of SDK
1 parent 0ed23c0 commit 8e971c3

10 files changed

Lines changed: 1199 additions & 0 deletions
File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace QueueIT.KnownUserV3.SDK {
2+
export interface IHttpRequest {
3+
getUserAgent(): string;
4+
getHeader(name: string): string;
5+
getAbsoluteUri(): string;
6+
getUserHostAddress(): string;
7+
getCookieValue(cookieKey: string): string;
8+
}
9+
10+
export interface IHttpResponse {
11+
setCookie(cookieName: string, cookieValue: string, domain: string, expiration);
12+
}
13+
14+
export interface IHttpContextProvider {
15+
getHttpRequest(): IHttpRequest;
16+
getHttpResponse(): IHttpResponse;
17+
}
18+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
namespace QueueIT.KnownUserV3.SDK.IntegrationConfig {
2+
export interface IIntegrationEvaluator {
3+
getMatchedIntegrationConfig(
4+
customerIntegration: CustomerIntegration, currentPageUrl: string, request: IHttpRequest): IntegrationConfigModel;
5+
}
6+
7+
export class IntegrationEvaluator implements IIntegrationEvaluator {
8+
public getMatchedIntegrationConfig(customerIntegration: CustomerIntegration, currentPageUrl: string, request: IHttpRequest): IntegrationConfigModel {
9+
10+
if (!request)
11+
throw new KnownUserException("request is null");
12+
13+
if (!customerIntegration)
14+
throw new KnownUserException("customerIntegration is null");
15+
16+
for (var integration of customerIntegration.Integrations || []) {
17+
for (var trigger of integration.Triggers) {
18+
if (this.evaluateTrigger(trigger, currentPageUrl, request)) {
19+
return integration;
20+
}
21+
}
22+
}
23+
return null;
24+
}
25+
26+
private evaluateTrigger(trigger: TriggerModel, currentPageUrl: string, request: IHttpRequest) {
27+
let part;
28+
if (trigger.LogicalOperator === LogicalOperatorType.Or) {
29+
for (part of trigger.TriggerParts) {
30+
if (this.evaluateTriggerPart(part, currentPageUrl, request))
31+
return true;
32+
}
33+
return false;
34+
}
35+
else {
36+
for (part of trigger.TriggerParts) {
37+
if (!this.evaluateTriggerPart(part, currentPageUrl, request))
38+
return false;
39+
}
40+
return true;
41+
}
42+
}
43+
44+
private evaluateTriggerPart(triggerPart: TriggerPart, currentPageUrl: string, request: IHttpRequest): boolean {
45+
switch (triggerPart.ValidatorType) {
46+
case ValidatorType.UrlValidator:
47+
return UrlValidatorHelper.evaluate(triggerPart, currentPageUrl);
48+
case ValidatorType.CookieValidator:
49+
return CookieValidatorHelper.evaluate(triggerPart, request);
50+
case ValidatorType.UserAgentValidator:
51+
return UserAgentValidatorHelper.evaluate(triggerPart, request.getUserAgent());
52+
case ValidatorType.HttpHeaderValidator:
53+
return HttpHeaderValidatorHelper.evaluate(triggerPart, request.getHeader(triggerPart.HttpHeaderName));
54+
default:
55+
return false;
56+
}
57+
}
58+
}
59+
60+
class UrlValidatorHelper {
61+
public static evaluate(triggerPart: TriggerPart, url: string): boolean {
62+
return ComparisonOperatorHelper.evaluate(
63+
triggerPart.Operator,
64+
triggerPart.IsNegative,
65+
triggerPart.IsIgnoreCase,
66+
this.getUrlPart(triggerPart, url),
67+
triggerPart.ValueToCompare,
68+
triggerPart.ValuesToCompare);
69+
}
70+
71+
private static getUrlPart(triggerPart: TriggerPart, url: string): string {
72+
switch (triggerPart.UrlPart) {
73+
case UrlPartType.PagePath:
74+
return this.getPathFromUrl(url);
75+
case UrlPartType.PageUrl:
76+
return url;
77+
case UrlPartType.HostName:
78+
return this.getHostNameFromUrl(url);
79+
default:
80+
return "";
81+
}
82+
}
83+
84+
public static getHostNameFromUrl(url: string): string {
85+
86+
let urlMatcher = /^(([^:/\?#]+):)?(\/\/([^/\?#]*))?([^\?#]*)(\?([^#]*))?(#(.*))?/;
87+
let match = urlMatcher.exec(url);
88+
if (match && match[4])
89+
return match[4];
90+
return "";
91+
}
92+
93+
public static getPathFromUrl(url: string): string {
94+
let urlMatcher = /^(([^:/\?#]+):)?(\/\/([^/\?#]*))?([^\?#]*)(\?([^#]*))?(#(.*))?/;
95+
let match = urlMatcher.exec(url);
96+
if (match && match[5])
97+
return match[5];
98+
return "";
99+
}
100+
}
101+
102+
class CookieValidatorHelper {
103+
public static evaluate(triggerPart: TriggerPart, request: IHttpRequest): boolean {
104+
return ComparisonOperatorHelper.evaluate(triggerPart.Operator,
105+
triggerPart.IsNegative,
106+
triggerPart.IsIgnoreCase,
107+
this.getCookie(triggerPart.CookieName, request),
108+
triggerPart.ValueToCompare,
109+
triggerPart.ValuesToCompare);
110+
}
111+
112+
private static getCookie(cookieName: string, request: IHttpRequest): string {
113+
var cookie = request.getCookieValue(cookieName);
114+
115+
if (!cookie)
116+
return "";
117+
118+
return cookie;
119+
}
120+
}
121+
122+
class UserAgentValidatorHelper {
123+
public static evaluate(triggerPart: TriggerPart, userAgent: string): boolean {
124+
125+
return ComparisonOperatorHelper.evaluate(triggerPart.Operator,
126+
triggerPart.IsNegative,
127+
triggerPart.IsIgnoreCase,
128+
userAgent,
129+
triggerPart.ValueToCompare,
130+
triggerPart.ValuesToCompare);
131+
}
132+
}
133+
134+
export class HttpHeaderValidatorHelper {
135+
public static evaluate(triggerPart: TriggerPart, headerValue: string): boolean {
136+
return ComparisonOperatorHelper.evaluate(triggerPart.Operator,
137+
triggerPart.IsNegative,
138+
triggerPart.IsIgnoreCase,
139+
headerValue,
140+
triggerPart.ValueToCompare,
141+
triggerPart.ValuesToCompare);
142+
}
143+
}
144+
145+
class ComparisonOperatorHelper {
146+
public static evaluate(opt: string, isNegative: boolean, isIgnoreCase: boolean, value: string,
147+
valueToCompare: string, valuesToCompare: Array<string>): boolean {
148+
149+
value = value || "";
150+
valueToCompare = valueToCompare || "";
151+
valuesToCompare = valuesToCompare || [];
152+
153+
switch (opt) {
154+
case ComparisonOperatorType.EqualS:
155+
return ComparisonOperatorHelper.equalS(value, valueToCompare, isNegative, isIgnoreCase);
156+
case ComparisonOperatorType.Contains:
157+
return ComparisonOperatorHelper.contains(value, valueToCompare, isNegative, isIgnoreCase);
158+
case ComparisonOperatorType.EqualsAny:
159+
return ComparisonOperatorHelper.equalsAny(value, valuesToCompare, isNegative, isIgnoreCase);
160+
case ComparisonOperatorType.ContainsAny:
161+
return ComparisonOperatorHelper.containsAny(value, valuesToCompare, isNegative, isIgnoreCase);
162+
default:
163+
return false;
164+
}
165+
}
166+
167+
private static contains(value: string, valueToCompare: string, isNegative: boolean, ignoreCase: boolean): boolean {
168+
if (valueToCompare === "*")
169+
return true;
170+
171+
var evaluation = false;
172+
173+
if (ignoreCase)
174+
evaluation = value.toUpperCase().indexOf(valueToCompare.toUpperCase()) !== -1;
175+
else
176+
evaluation = value.indexOf(valueToCompare) !== -1;
177+
178+
if (isNegative)
179+
return !evaluation;
180+
else
181+
return evaluation;
182+
}
183+
184+
private static equalS(value: string, valueToCompare: string, isNegative: boolean, ignoreCase: boolean): boolean {
185+
var evaluation = false;
186+
187+
if (ignoreCase)
188+
evaluation = value.toUpperCase() === valueToCompare.toUpperCase();
189+
else
190+
evaluation = value === valueToCompare;
191+
192+
if (isNegative)
193+
return !evaluation;
194+
else
195+
return evaluation;
196+
}
197+
198+
private static equalsAny(value: string, valuesToCompare: Array<string>, isNegative: boolean, isIgnoreCase: boolean): boolean {
199+
for (let valueToCompare of valuesToCompare) {
200+
if (ComparisonOperatorHelper.equalS(value, valueToCompare, false, isIgnoreCase))
201+
return !isNegative;
202+
}
203+
204+
return isNegative;
205+
}
206+
207+
private static containsAny(value: string, valuesToCompare: Array<string>, isNegative: boolean, isIgnoreCase: boolean): boolean {
208+
for (let valueToCompare of valuesToCompare) {
209+
if (ComparisonOperatorHelper.contains(value, valueToCompare, false, isIgnoreCase))
210+
return !isNegative;
211+
}
212+
213+
return isNegative;
214+
}
215+
}
216+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace QueueIT.KnownUserV3.SDK.IntegrationConfig {
2+
export class IntegrationConfigModel {
3+
Name: string;
4+
EventId: string;
5+
CookieDomain: string;
6+
LayoutName: string;
7+
Culture: string;
8+
ExtendCookieValidity: boolean | null;
9+
CookieValidityMinute: number | null;
10+
QueueDomain: string;
11+
RedirectLogic: string;
12+
ForcedTargetUrl: string;
13+
ActionType: string;
14+
Triggers: Array<TriggerModel>;
15+
}
16+
17+
export class CustomerIntegration {
18+
constructor() {
19+
this.Integrations = new Array<IntegrationConfigModel>();
20+
this.Version = -1;
21+
}
22+
//sorted list of integrations
23+
Integrations: Array<IntegrationConfigModel>;
24+
Version: number;
25+
}
26+
27+
export class TriggerPart {
28+
ValidatorType: string;
29+
Operator: string;
30+
ValueToCompare: string;
31+
ValuesToCompare: Array<string>;
32+
IsNegative: boolean;
33+
IsIgnoreCase: boolean;
34+
//UrlValidator
35+
UrlPart: string;
36+
//CookieValidator
37+
CookieName: string;
38+
//HttpHeaderValidator
39+
HttpHeaderName: string;
40+
}
41+
42+
export class TriggerModel {
43+
constructor() {
44+
this.TriggerParts = new Array<TriggerPart>();
45+
}
46+
TriggerParts: Array<TriggerPart>;
47+
LogicalOperator: string;
48+
}
49+
50+
export class ValidatorType {
51+
public static readonly UrlValidator = "UrlValidator";
52+
public static readonly CookieValidator = "CookieValidator";
53+
public static readonly UserAgentValidator = "UserAgentValidator";
54+
public static readonly HttpHeaderValidator = "HttpHeaderValidator";
55+
}
56+
57+
export class UrlPartType {
58+
static readonly HostName = "HostName";
59+
static readonly PagePath = "PagePath";
60+
static readonly PageUrl = "PageUrl";
61+
}
62+
63+
export class ComparisonOperatorType {
64+
static readonly EqualS = "Equals";
65+
static readonly Contains = "Contains";
66+
static readonly EqualsAny = "EqualsAny";
67+
static readonly ContainsAny = "ContainsAny";
68+
static readonly StartsWith = "StartsWith";
69+
static readonly EndsWith = "EndsWith";
70+
static readonly MatchesWith = "MatchesWith";
71+
}
72+
73+
export class LogicalOperatorType {
74+
public static readonly Or = "Or";
75+
public static readonly And = "And";
76+
}
77+
78+
export class ActionType {
79+
public static readonly IgnoreAction = "Ignore";
80+
public static readonly CancelAction = "Cancel";
81+
public static readonly QueueAction = "Queue";
82+
}
83+
}

0 commit comments

Comments
 (0)