UNPKG

4.45 kBPlain TextView Raw
1export const fixedURLEnc = (str: string): string => {
2 return encodeURIComponent(str).replace(/[!'()*]/g, (c) => {
3 return `%${c.charCodeAt(0).toString(16).toUpperCase()}`;
4 });
5};
6
7/**
8 * SocketConfig Class
9 */
10export class SocketConfig {
11 /**
12 * API system entity. "54cd" for LIVE system; "1234" for OT&E system
13 */
14 private entity: string;
15 /**
16 * account name
17 */
18 private login: string;
19 /**
20 * one time password (2FA)
21 */
22 private otp: string;
23 /**
24 * account password
25 */
26 private pw: string;
27 /**
28 * remote ip address (ip filter)
29 */
30 private remoteaddr: string;
31 /**
32 * API session id
33 */
34 private session: string;
35 /**
36 * subuser account name (subuser specific data view)
37 */
38 private user: string;
39
40 public constructor() {
41 this.entity = "";
42 this.login = "";
43 this.otp = "";
44 this.pw = "";
45 this.remoteaddr = "";
46 this.session = "";
47 this.user = "";
48 }
49
50 /**
51 * Create POST data string out of connection data
52 * @returns POST data string
53 */
54 public getPOSTData(): string {
55 let data = "";
56 if (this.entity !== "") {
57 data += `${fixedURLEnc("s_entity")}=${fixedURLEnc(this.entity)}&`;
58 }
59 if (this.login !== "") {
60 data += `${fixedURLEnc("s_login")}=${fixedURLEnc(this.login)}&`;
61 }
62 if (this.otp !== "") {
63 data += `${fixedURLEnc("s_otp")}=${fixedURLEnc(this.otp)}&`;
64 }
65 if (this.pw !== "") {
66 data += `${fixedURLEnc("s_pw")}=${fixedURLEnc(this.pw)}&`;
67 }
68 if (this.remoteaddr !== "") {
69 data += `${fixedURLEnc("s_remoteaddr")}=${fixedURLEnc(this.remoteaddr)}&`;
70 }
71 if (this.session !== "") {
72 data += `${fixedURLEnc("s_session")}=${fixedURLEnc(this.session)}&`;
73 }
74 if (this.user !== "") {
75 data += `${fixedURLEnc("s_user")}=${fixedURLEnc(this.user)}&`;
76 }
77 return data;
78 }
79
80 /**
81 * Get API Session ID in use
82 * @returns API Session ID
83 */
84 public getSession(): string {
85 return this.session;
86 }
87
88 /**
89 * Get API System Entity in use
90 * @returns API System Entity
91 */
92 public getSystemEntity(): string {
93 return this.entity;
94 }
95
96 /**
97 * Set account name to use
98 * @param value account name
99 * @returns Current SocketConfig instance for method chaining
100 */
101 public setLogin(value: string): SocketConfig {
102 this.session = "";
103 this.login = value;
104 return this;
105 }
106
107 /**
108 * Set one time password to use
109 * @param value one time password
110 * @returns Current SocketConfig instance for method chaining
111 */
112 public setOTP(value: string): SocketConfig {
113 this.session = "";
114 this.otp = value;
115 return this;
116 }
117
118 /**
119 * Set account password to use
120 * @param value account password
121 * @returns Current SocketConfig instance for method chaining
122 */
123 public setPassword(value: string): SocketConfig {
124 this.session = "";
125 this.pw = value;
126 return this;
127 }
128
129 /**
130 * Set Remote IP Address to use
131 * @param value remote ip address
132 * @returns Current SocketConfig instance for method chaining
133 */
134 public setRemoteAddress(value: string): SocketConfig {
135 this.remoteaddr = value;
136 return this;
137 }
138
139 /**
140 * Set API Session ID to use
141 * @param value API Session ID
142 * @returns Current SocketConfig instance for method chaining
143 */
144 public setSession(value: string): SocketConfig {
145 this.session = value;
146 this.login = "";
147 this.pw = "";
148 this.otp = "";
149 return this;
150 }
151
152 /**
153 * Set API System Entity to use
154 * This is set to 54cd / LIVE System by default
155 * @param value API System Entity
156 * @returns Current SocketConfig instance for method chaining
157 */
158 public setSystemEntity(value: string): SocketConfig {
159 this.entity = value;
160 return this;
161 }
162
163 /**
164 * Set subuser account name (for subuser data view)
165 * @param value subuser account name
166 * @returns Current SocketConfig instance for method chaining
167 */
168 public setUser(value: string): SocketConfig {
169 this.user = value;
170 return this;
171 }
172}