/
index.main.js
212 lines (166 loc) · 5.76 KB
/
index.main.js
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class LisURLQueryString {
static encodeParamValue(value) {
let encoded = LisURLEncoder.RFC3986 ? encodeURIComponent(value).replace(/[!'()*]/g, function (c) {
return "%" + c.charCodeAt(0).toString(16);
}) : encodeURIComponent(value);
return LisURLEncoder.X_WWW_FORM_URLENCODED ? encoded.replace(new RegExp("%20", "g"), "+") : encoded;
}
static encode(queryArray) {
return queryArray.map(function (_ref) {
let {
key,
value
} = _ref;
return key + "=" + LisURLQueryString.encodeParamValue(value);
}).join("&");
}
static decode(queryString) {
return Array.from(new URLSearchParams(queryString)).filter(function (entry) {
return entry.filter(function (e) {
return e && e !== "";
}).length > 0;
}).map(function (entry) {
return {
key: entry[0],
value: decodeURIComponent(entry[1].replace(/\+/g, " "))
};
});
}
}
class LisURLEncode {
static queryArray(queryArray) {
if (queryArray) {
try {
return LisURLQueryString.encode(queryArray);
} catch (e) {
if (LisURLEncoder.DEBUGGING) {
console.warn(e);
}
}
}
return null;
}
static baseURL(baseURL) {
let base = "";
if (baseURL) {
try {
base = encodeURI(baseURL);
} catch (e) {
console.warn(e);
}
}
return base;
}
static url(baseURL, queryArray) {
const base = LisURLEncode.baseURL(baseURL);
const queryString = LisURLEncode.queryArray(queryArray);
return base + (queryString !== "" && base !== "" ? (LisURLEncoder.AUTO_QUESTION_MARK ? "?" : "") + queryString : "");
}
}
class LisURLDecode {
static queryString(url) {
if (url) {
try {
return LisURLQueryString.decode(new URL(url).searchParams.toString());
} catch (e) {
if (LisURLEncoder.DEBUGGING) {
console.warn(e);
}
}
}
return "";
}
static baseURL(url) {
let base = "";
if (url) {
try {
base = decodeURI(url).split("?")[0];
} catch (e) {
if (LisURLEncoder.DEBUGGING) {
console.warn(e);
}
}
}
return base;
}
static url(url) {
let baseURL = "";
let queryString = "";
if (url && url !== "") {
baseURL = LisURLDecode.baseURL(url);
queryString = LisURLDecode.queryString(url);
}
return {
baseURL,
queryString
};
}
}
class LisURLEncoder {
static decode(url) {
if (!new RegExp(LisURLEncoder.URL_REGEX, "g").test(url)) {
if (LisURLEncoder.DEBUGGING) {
console.warn("The URL specified for decoding is not a URL");
}
return null;
}
return LisURLDecode.url(url);
}
static encode(baseURL, queryArray) {
const testURL = new RegExp(LisURLEncoder.URL_REGEX, "g").test(baseURL);
if (!testURL) {
if (LisURLEncoder.DEBUGGING) {
console.warn("The base URL specified for encoding is not a URL");
}
return "";
}
const testReservedChars = LisURLEncoder.RESERVED_CHARS.some(function (c) {
return baseURL.includes(c);
});
if (testReservedChars) {
if (LisURLEncoder.DEBUGGING) {
console.warn("The base URL specified contains reserved chars");
}
return "";
}
const testParamKeys = queryArray.some(function (_ref2) {
let {
key
} = _ref2;
return new RegExp(LisURLEncoder.PARAM_KEY_REGEX, "g").test(key);
});
if (!testParamKeys) {
if (LisURLEncoder.DEBUGGING) {
console.warn("Check your param keys because some of them do not respect the validation");
}
return "";
}
return LisURLEncode.url(baseURL, queryArray);
}
static reset() {
LisURLEncoder.X_WWW_FORM_URLENCODED = false;
LisURLEncoder.AUTO_QUESTION_MARK = true;
LisURLEncoder.RFC3986 = true;
LisURLEncoder.URL_REGEX = "^(?:(?:(?:https?|ftp):)?\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)" + "(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z0-9\\u00a1-\\uffff][a-z0-9\\u00a1-\\uffff_-]{0,62}" + ")?[a-z0-9\\u00a1-\\uffff]\\.)+(?:[a-z\\u00a1-\\uffff]{2,}\\.?))(?::\\d{2,5})?(?:[/?#]\\S*)?$";
LisURLEncoder.RESERVED_CHARS = ["&", "=", "?", "+"];
LisURLEncoder.PARAM_KEY_REGEX = "^[a-zA-Z0-9]*$";
}
}
_defineProperty(LisURLEncoder, "DEBUGGING", false);
_defineProperty(LisURLEncoder, "X_WWW_FORM_URLENCODED", false);
_defineProperty(LisURLEncoder, "URL_REGEX", "^(?:(?:(?:https?|ftp):)?\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)" + "(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z0-9\\u00a1-\\uffff][a-z0-9\\u00a1-\\uffff_-]{0,62}" + ")?[a-z0-9\\u00a1-\\uffff]\\.)+(?:[a-z\\u00a1-\\uffff]{2,}\\.?))(?::\\d{2,5})?(?:[/?#]\\S*)?$");
_defineProperty(LisURLEncoder, "RESERVED_CHARS", ["&", "=", "?", "+"]);
_defineProperty(LisURLEncoder, "PARAM_KEY_REGEX", "^[a-zA-Z0-9]*$");
_defineProperty(LisURLEncoder, "AUTO_QUESTION_MARK", true);
_defineProperty(LisURLEncoder, "RFC3986", false);
if (typeof module !== 'undefined') {
module.exports = LisURLEncoder;
}
var _default = LisURLEncoder;
exports.default = _default;