@api-client/core 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/browser.d.ts +3 -0
- package/build/browser.js +6 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +3 -0
- package/build/index.js +6 -0
- package/build/index.js.map +1 -1
- package/build/src/lib/calculators/DataCalculator.d.ts +27 -0
- package/build/src/lib/calculators/DataCalculator.js +88 -0
- package/build/src/lib/calculators/DataCalculator.js.map +1 -0
- package/build/src/lib/parsers/UrlEncoder.d.ts +51 -0
- package/build/src/lib/parsers/UrlEncoder.js +74 -0
- package/build/src/lib/parsers/UrlEncoder.js.map +1 -0
- package/build/src/lib/parsers/UrlParser.d.ts +104 -0
- package/build/src/lib/parsers/UrlParser.js +189 -0
- package/build/src/lib/parsers/UrlParser.js.map +1 -0
- package/build/src/lib/parsers/UrlValueParser.d.ts +92 -0
- package/build/src/lib/parsers/UrlValueParser.js +172 -0
- package/build/src/lib/parsers/UrlValueParser.js.map +1 -0
- package/build/src/testing/TestCliHelper.d.ts +7 -1
- package/build/src/testing/TestCliHelper.js +10 -1
- package/build/src/testing/TestCliHelper.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/calculators/DataCalculator.ts +91 -0
- package/src/lib/parsers/UrlEncoder.ts +74 -0
- package/src/lib/parsers/UrlParser.ts +201 -0
- package/src/lib/parsers/UrlValueParser.ts +211 -0
- package/src/testing/TestCliHelper.ts +12 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { UrlValueParser, IUrlValueParserOptions } from './UrlValueParser.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A class to parse URL string.
|
|
5
|
+
*/
|
|
6
|
+
export class UrlParser extends UrlValueParser {
|
|
7
|
+
/**
|
|
8
|
+
* @param value The URL value
|
|
9
|
+
*/
|
|
10
|
+
constructor(value: string, opts?: IUrlValueParserOptions) {
|
|
11
|
+
super(opts);
|
|
12
|
+
this.value = value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns protocol value in format `protocol` + ':'
|
|
17
|
+
*
|
|
18
|
+
* @return The value of the protocol or `undefined` if the value is not set
|
|
19
|
+
*/
|
|
20
|
+
get protocol(): string | undefined {
|
|
21
|
+
return this.__data.protocol;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Sets value of the `protocol`
|
|
26
|
+
*
|
|
27
|
+
* @param value Protocol value.
|
|
28
|
+
*/
|
|
29
|
+
set protocol(value: string | undefined) {
|
|
30
|
+
this.__data.protocol = value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* It reads the authority part of the URL value. It doesn't parses it
|
|
35
|
+
* to host, port and credentials parts.
|
|
36
|
+
*
|
|
37
|
+
* @return The value of the host or `undefined` if the value is not set
|
|
38
|
+
*/
|
|
39
|
+
get host(): string | undefined {
|
|
40
|
+
return this.__data.host;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sets value of the `host`
|
|
45
|
+
*
|
|
46
|
+
* @param value Host value.
|
|
47
|
+
*/
|
|
48
|
+
set host(value: string | undefined) {
|
|
49
|
+
this.__data.host = value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns path part of the URL.
|
|
54
|
+
*
|
|
55
|
+
* @returns The value of the path or `undefined` if the value not set
|
|
56
|
+
*/
|
|
57
|
+
get path(): string | undefined {
|
|
58
|
+
return this.__data.path || '/';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Sets value of the `path`
|
|
63
|
+
*
|
|
64
|
+
* @param value Path value.
|
|
65
|
+
*/
|
|
66
|
+
set path(value: string | undefined) {
|
|
67
|
+
this.__data.path = value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns anchor part of the URL.
|
|
72
|
+
*
|
|
73
|
+
* @returns The value of the anchor or `undefined` if the value not set
|
|
74
|
+
*/
|
|
75
|
+
get anchor(): string | undefined {
|
|
76
|
+
return this.__data.anchor;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Sets value of the `anchor`
|
|
81
|
+
*
|
|
82
|
+
* @param value The anchor value.
|
|
83
|
+
*/
|
|
84
|
+
set anchor(value: string | undefined) {
|
|
85
|
+
this.__data.anchor = value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns search part of the URL.
|
|
90
|
+
*
|
|
91
|
+
* @returns the value of the search or `undefined` if the value not set
|
|
92
|
+
*/
|
|
93
|
+
get search(): string | undefined {
|
|
94
|
+
return this.__data.search;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sets value of the `search`
|
|
99
|
+
*
|
|
100
|
+
* @param value Search value.
|
|
101
|
+
*/
|
|
102
|
+
set search(value: string | undefined) {
|
|
103
|
+
this.__data.search = value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The URL value. It is the same as calling `toString()`.
|
|
108
|
+
*
|
|
109
|
+
* @return The URL value for current configuration.
|
|
110
|
+
*/
|
|
111
|
+
get value(): string {
|
|
112
|
+
return this.toString();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Sets value of the URL.
|
|
117
|
+
* It parses the url and sets properties.
|
|
118
|
+
*
|
|
119
|
+
* @param value The URL value.
|
|
120
|
+
*/
|
|
121
|
+
set value(value: string) {
|
|
122
|
+
this.protocol = this._parseProtocol(value);
|
|
123
|
+
this.host = this._parseHost(value);
|
|
124
|
+
this.path = this._parsePath(value);
|
|
125
|
+
this.anchor = this._parseAnchor(value);
|
|
126
|
+
this.search = this._parseSearch(value);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Returns an array of search params.
|
|
131
|
+
*
|
|
132
|
+
* @returns The list of search params. Each item contains an
|
|
133
|
+
* array where the first item is the name of the parameter and the second item is the
|
|
134
|
+
* value.
|
|
135
|
+
*/
|
|
136
|
+
get searchParams(): string[][] {
|
|
137
|
+
return this._parseSearchParams(this.search);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Sets the value of `search` and `searchParams`.
|
|
142
|
+
*
|
|
143
|
+
* @param value Search params list.
|
|
144
|
+
*/
|
|
145
|
+
set searchParams(value: string[][]) {
|
|
146
|
+
if (!value || !value.length) {
|
|
147
|
+
this.search = undefined;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.search = value.map((item) => {
|
|
151
|
+
let itemValue = item[1];
|
|
152
|
+
if (!item[0] && !itemValue) {
|
|
153
|
+
return '';
|
|
154
|
+
}
|
|
155
|
+
if (itemValue === undefined) {
|
|
156
|
+
itemValue = '';
|
|
157
|
+
} else if (typeof itemValue !== 'string') {
|
|
158
|
+
itemValue = String(itemValue);
|
|
159
|
+
}
|
|
160
|
+
return `${item[0]}=${itemValue}`;
|
|
161
|
+
})
|
|
162
|
+
.join(this.opts.queryDelimiter);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns the URL for current settings.
|
|
167
|
+
*
|
|
168
|
+
* @return The URL value.
|
|
169
|
+
*/
|
|
170
|
+
toString(): string {
|
|
171
|
+
let result = '';
|
|
172
|
+
if (this.protocol) {
|
|
173
|
+
result += this.protocol;
|
|
174
|
+
result += '//';
|
|
175
|
+
}
|
|
176
|
+
if (this.host) {
|
|
177
|
+
result += this.host;
|
|
178
|
+
}
|
|
179
|
+
if (this.path) {
|
|
180
|
+
if (this.path === '/' && !this.host && !this.search && !this.anchor) {
|
|
181
|
+
// ???
|
|
182
|
+
} else {
|
|
183
|
+
if (this.path[0] !== '/') {
|
|
184
|
+
result += '/';
|
|
185
|
+
}
|
|
186
|
+
result += this.path;
|
|
187
|
+
}
|
|
188
|
+
} else if (this.search || this.anchor) {
|
|
189
|
+
result += '/';
|
|
190
|
+
}
|
|
191
|
+
if (this.search) {
|
|
192
|
+
const p = this.searchParams;
|
|
193
|
+
this.searchParams = p;
|
|
194
|
+
result += `?${this.search}`;
|
|
195
|
+
}
|
|
196
|
+
if (this.anchor) {
|
|
197
|
+
result += `#${this.anchor}`;
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
export interface IUrlValueParserOptions {
|
|
2
|
+
/**
|
|
3
|
+
* A query string delimiter to use when processing query parameters.
|
|
4
|
+
*/
|
|
5
|
+
queryDelimiter?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface DataValues {
|
|
9
|
+
/**
|
|
10
|
+
* A protocol value in format `protocol` + ':'
|
|
11
|
+
*/
|
|
12
|
+
protocol?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The authority part of the URL value
|
|
15
|
+
*/
|
|
16
|
+
host?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Path part of the URL.
|
|
19
|
+
*/
|
|
20
|
+
path?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Anchor part of the URL.
|
|
23
|
+
*/
|
|
24
|
+
anchor?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Search part of the URL.
|
|
27
|
+
*/
|
|
28
|
+
search?: string;
|
|
29
|
+
opts?: IUrlValueParserOptions | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Implements logic for parsing URL string.
|
|
34
|
+
*/
|
|
35
|
+
export class UrlValueParser {
|
|
36
|
+
protected __data: DataValues;
|
|
37
|
+
|
|
38
|
+
constructor(opts?: IUrlValueParserOptions) {
|
|
39
|
+
this.__data = {};
|
|
40
|
+
this.opts = opts;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @returns Class options.
|
|
45
|
+
*/
|
|
46
|
+
get opts(): IUrlValueParserOptions {
|
|
47
|
+
return this.__data.opts || {
|
|
48
|
+
queryDelimiter: '&',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Sets parser options.
|
|
54
|
+
* Unknown options are ignored.
|
|
55
|
+
*/
|
|
56
|
+
set opts(opts: IUrlValueParserOptions | undefined) {
|
|
57
|
+
const options = (opts || {}) as IUrlValueParserOptions;
|
|
58
|
+
this.__data.opts = {
|
|
59
|
+
queryDelimiter: options.queryDelimiter || '&'
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns protocol value in format `protocol` + ':'
|
|
65
|
+
*
|
|
66
|
+
* @param value URL to parse.
|
|
67
|
+
* @return Value of the protocol or undefined if value not set
|
|
68
|
+
*/
|
|
69
|
+
protected _parseProtocol(value: string): string | undefined {
|
|
70
|
+
if (!value) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const delimiterIndex = value.indexOf('://');
|
|
74
|
+
if (delimiterIndex !== -1) {
|
|
75
|
+
return value.substring(0, delimiterIndex + 1);
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets a host value from the url.
|
|
82
|
+
* It reads the whole authority value of given `value`. It doesn't parses it
|
|
83
|
+
* to host, port and
|
|
84
|
+
* credentials parts. For URL panel it's enough.
|
|
85
|
+
*
|
|
86
|
+
* @param value The URL to parse
|
|
87
|
+
* @return Value of the host or undefined.
|
|
88
|
+
*/
|
|
89
|
+
protected _parseHost(value: string): string | undefined {
|
|
90
|
+
if (!value) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
let result = value;
|
|
94
|
+
const delimiterIndex = result.indexOf('://');
|
|
95
|
+
if (delimiterIndex !== -1) {
|
|
96
|
+
result = result.substring(delimiterIndex + 3);
|
|
97
|
+
}
|
|
98
|
+
if (!result) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
// We don't need specifics here (username, password, port)
|
|
102
|
+
const host = result.split('/')[0];
|
|
103
|
+
return host;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Parses the path part of the URL.
|
|
108
|
+
*
|
|
109
|
+
* @param value URL value
|
|
110
|
+
* @returns Path part of the URL
|
|
111
|
+
*/
|
|
112
|
+
protected _parsePath(value: string): string | undefined {
|
|
113
|
+
if (!value) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
let result = value;
|
|
117
|
+
const isBasePath = result[0] === '/';
|
|
118
|
+
if (!isBasePath) {
|
|
119
|
+
const index = result.indexOf('://');
|
|
120
|
+
if (index !== -1) {
|
|
121
|
+
result = result.substring(index + 3);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
let index = result.indexOf('?');
|
|
125
|
+
if (index !== -1) {
|
|
126
|
+
result = result.substring(0, index);
|
|
127
|
+
}
|
|
128
|
+
index = result.indexOf('#');
|
|
129
|
+
if (index !== -1) {
|
|
130
|
+
result = result.substring(0, index);
|
|
131
|
+
}
|
|
132
|
+
const lastIsSlash = result[result.length - 1] === '/';
|
|
133
|
+
const parts = result.split('/').filter((part) => !!part);
|
|
134
|
+
if (!isBasePath) {
|
|
135
|
+
parts.shift();
|
|
136
|
+
}
|
|
137
|
+
let path = `/${ parts.join('/')}`;
|
|
138
|
+
if (lastIsSlash && parts.length > 1) {
|
|
139
|
+
path += '/';
|
|
140
|
+
}
|
|
141
|
+
return path;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns query parameters string (without the '?' sign) as a whole.
|
|
146
|
+
*
|
|
147
|
+
* @param value The URL to parse
|
|
148
|
+
* @returns Value of the search string or undefined.
|
|
149
|
+
*/
|
|
150
|
+
protected _parseSearch(value: string): string | undefined {
|
|
151
|
+
if (!value) {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
let index = value.indexOf('?');
|
|
155
|
+
if (index === -1) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
const result = value.substring(index + 1);
|
|
159
|
+
index = result.indexOf('#');
|
|
160
|
+
if (index === -1) {
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
return result.substring(0, index);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Reads a value of the anchor (or hash) parameter without the `#` sign.
|
|
168
|
+
*
|
|
169
|
+
* @param value The URL to parse
|
|
170
|
+
* @returns Value of the anchor (hash) or undefined.
|
|
171
|
+
*/
|
|
172
|
+
protected _parseAnchor(value: string): string | undefined {
|
|
173
|
+
if (!value) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
const index = value.indexOf('#');
|
|
177
|
+
if (index === -1) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
return value.substring(index + 1);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Returns an array of items where each item is an array where first
|
|
185
|
+
* item is param name and second is it's value. Both always strings.
|
|
186
|
+
*
|
|
187
|
+
* @param search Parsed search parameter
|
|
188
|
+
* @returns Always returns an array.
|
|
189
|
+
*/
|
|
190
|
+
protected _parseSearchParams(search?: string): string[][] {
|
|
191
|
+
const result: string[][] = [];
|
|
192
|
+
if (!search) {
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
const parts = search.split(this.opts.queryDelimiter as string);
|
|
196
|
+
parts.forEach((item) => {
|
|
197
|
+
const _part = ['', ''];
|
|
198
|
+
const _params = item.split('=');
|
|
199
|
+
let _name = _params.shift();
|
|
200
|
+
if (!_name && _name !== '') {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
_name = _name.trim();
|
|
204
|
+
const _value = _params.join('=').trim();
|
|
205
|
+
_part[0] = _name;
|
|
206
|
+
_part[1] = _value;
|
|
207
|
+
result.push(_part);
|
|
208
|
+
});
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -4,6 +4,12 @@ export interface ITestRunCommandOptions {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export class TestCliHelper {
|
|
7
|
+
/**
|
|
8
|
+
* The globally set test timeout.
|
|
9
|
+
* @default 2000 The mocha default test timeout.
|
|
10
|
+
*/
|
|
11
|
+
static testTimeout = 2000;
|
|
12
|
+
|
|
7
13
|
static cleanTerminalOutput(s: string): string {
|
|
8
14
|
let result = s.trim();
|
|
9
15
|
result = result.replace(/[^\x20-\x7E\n]/gm, '');
|
|
@@ -37,9 +43,10 @@ export class TestCliHelper {
|
|
|
37
43
|
* ```
|
|
38
44
|
*
|
|
39
45
|
* @param fn The function to execute.
|
|
46
|
+
* @param timeout The test timeout. After this time the output is reset.
|
|
40
47
|
* @returns The terminal output.
|
|
41
48
|
*/
|
|
42
|
-
static async grabOutput(fn: () => Promise<void
|
|
49
|
+
static async grabOutput(fn: () => Promise<void>, timeout=TestCliHelper.testTimeout): Promise<string> {
|
|
43
50
|
const messages: string[] = [];
|
|
44
51
|
function noop(): void {
|
|
45
52
|
//
|
|
@@ -64,11 +71,15 @@ export class TestCliHelper {
|
|
|
64
71
|
process.stderr.write = messageHandler;
|
|
65
72
|
console.clear = noop;
|
|
66
73
|
|
|
74
|
+
const handle = setTimeout(() => stop(), timeout);
|
|
75
|
+
|
|
67
76
|
try {
|
|
68
77
|
await fn();
|
|
69
78
|
stop();
|
|
79
|
+
clearTimeout(handle);
|
|
70
80
|
} catch (e) {
|
|
71
81
|
stop();
|
|
82
|
+
clearTimeout(handle);
|
|
72
83
|
throw e;
|
|
73
84
|
}
|
|
74
85
|
return messages.join('');
|