@api-client/core 0.3.5 → 0.3.8
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 +5 -0
- package/build/browser.js +14 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +13 -1
- package/build/index.js +25 -1
- 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/fs/Fs.d.ts +52 -0
- package/build/src/lib/fs/Fs.js +245 -0
- package/build/src/lib/fs/Fs.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/lib/timers/Timers.d.ts +5 -0
- package/build/src/lib/timers/Timers.js +10 -0
- package/build/src/lib/timers/Timers.js.map +1 -0
- package/build/src/mocking/ProjectMock.d.ts +13 -0
- package/build/src/mocking/ProjectMock.js +16 -0
- package/build/src/mocking/ProjectMock.js.map +1 -0
- package/build/src/mocking/lib/Request.d.ts +32 -0
- package/build/src/mocking/lib/Request.js +63 -0
- package/build/src/mocking/lib/Request.js.map +1 -0
- package/build/src/mocking/lib/Response.d.ts +33 -0
- package/build/src/mocking/lib/Response.js +79 -0
- package/build/src/mocking/lib/Response.js.map +1 -0
- package/build/src/runtime/node/BaseRunner.d.ts +21 -0
- package/build/src/runtime/node/BaseRunner.js +27 -0
- package/build/src/runtime/node/BaseRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
- package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
- package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
- package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRunner.d.ts +164 -62
- package/build/src/runtime/node/ProjectRunner.js +191 -146
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
- package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
- package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
- package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
- package/build/src/runtime/reporters/Reporter.d.ts +62 -0
- package/build/src/runtime/reporters/Reporter.js +98 -0
- package/build/src/runtime/reporters/Reporter.js.map +1 -0
- package/build/src/testing/TestCliHelper.d.ts +29 -0
- package/build/src/testing/TestCliHelper.js +80 -0
- package/build/src/testing/TestCliHelper.js.map +1 -0
- package/build/src/testing/getPort.d.ts +52 -0
- package/build/src/testing/getPort.js +169 -0
- package/build/src/testing/getPort.js.map +1 -0
- package/package.json +3 -2
- package/src/lib/calculators/DataCalculator.ts +91 -0
- package/src/lib/fs/Fs.ts +258 -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/lib/timers/Timers.ts +9 -0
- package/src/mocking/LegacyInterfaces.ts +1 -1
- package/src/mocking/ProjectMock.ts +20 -0
- package/src/mocking/lib/Request.ts +85 -0
- package/src/mocking/lib/Response.ts +101 -0
- package/src/runtime/node/BaseRunner.ts +29 -0
- package/src/runtime/node/ProjectParallelRunner.ts +234 -0
- package/src/runtime/node/ProjectRequestRunner.ts +281 -0
- package/src/runtime/node/ProjectRunner.ts +279 -186
- package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
- package/src/runtime/node/ProjectSerialRunner.ts +36 -0
- package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
- package/src/runtime/reporters/Reporter.ts +142 -0
- package/src/testing/TestCliHelper.ts +87 -0
- package/src/testing/getPort.ts +212 -0
package/src/lib/fs/Fs.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { writeFile, mkdir, rm, readdir, stat, access, readFile, copyFile as fsCopyFile } from 'fs/promises';
|
|
2
|
+
import { constants } from 'fs';
|
|
3
|
+
import { join, dirname } from 'path';
|
|
4
|
+
|
|
5
|
+
export interface JsonReadOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether it should throw an error when a reading error occurs.
|
|
8
|
+
*/
|
|
9
|
+
throws?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// function statPromise(filePath: string): Promise<fsSync.Stats> {
|
|
13
|
+
// return new Promise((resolve, reject) => {
|
|
14
|
+
// fsSync.stat(filePath, (err, stats) => {
|
|
15
|
+
// if (err) {
|
|
16
|
+
// reject(err);
|
|
17
|
+
// } else {
|
|
18
|
+
// resolve(stats);
|
|
19
|
+
// }
|
|
20
|
+
// });
|
|
21
|
+
// });
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks whether a file exists in the location.
|
|
26
|
+
*/
|
|
27
|
+
export async function pathExists(filePath: string): Promise<boolean> {
|
|
28
|
+
// return new Promise((resolve) => {
|
|
29
|
+
// fsSync.stat(filePath, (err, stats) => {
|
|
30
|
+
// if (err) {
|
|
31
|
+
// resolve(false);
|
|
32
|
+
// } else {
|
|
33
|
+
// resolve(true);
|
|
34
|
+
// }
|
|
35
|
+
// });
|
|
36
|
+
// });
|
|
37
|
+
try {
|
|
38
|
+
await stat(filePath);
|
|
39
|
+
return true;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Tests a user's permissions for the file or directory specified by filePath.
|
|
47
|
+
* @param filePath The path to test
|
|
48
|
+
* @returns True when the path can be read by the current user.
|
|
49
|
+
*/
|
|
50
|
+
export async function canRead(filePath: string): Promise<boolean> {
|
|
51
|
+
const exists = await pathExists(filePath);
|
|
52
|
+
if (!exists) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// return new Promise((resolve) => {
|
|
57
|
+
// fsSync.access(filePath, constants.R_OK, (err) => {
|
|
58
|
+
// if (err) {
|
|
59
|
+
// resolve(false);
|
|
60
|
+
// } else {
|
|
61
|
+
// resolve(true);
|
|
62
|
+
// }
|
|
63
|
+
// });
|
|
64
|
+
// });
|
|
65
|
+
try {
|
|
66
|
+
await access(filePath, constants.R_OK);
|
|
67
|
+
return true;
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Tests a user's permissions for the file or directory specified by filePath.
|
|
75
|
+
* @param filePath The path to test
|
|
76
|
+
* @returns True when the path can be written to by the current user.
|
|
77
|
+
*/
|
|
78
|
+
export async function canWrite(filePath: string): Promise<boolean> {
|
|
79
|
+
const exists = await pathExists(filePath);
|
|
80
|
+
if (!exists) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// return new Promise((resolve) => {
|
|
84
|
+
// fsSync.access(filePath, constants.W_OK, (err) => {
|
|
85
|
+
// if (err) {
|
|
86
|
+
// resolve(false);
|
|
87
|
+
// } else {
|
|
88
|
+
// resolve(true);
|
|
89
|
+
// }
|
|
90
|
+
// });
|
|
91
|
+
// });
|
|
92
|
+
try {
|
|
93
|
+
await access(filePath, constants.W_OK);
|
|
94
|
+
return true;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Reads the contents of a JSON file.
|
|
102
|
+
*
|
|
103
|
+
* @param filePath The path to the JSON file to read.
|
|
104
|
+
* @returns The contents of the file. When `throws` options is not set and error occurs then it returns an empty file.
|
|
105
|
+
*/
|
|
106
|
+
export async function readJson(filePath: string, opts: JsonReadOptions={}): Promise<unknown> {
|
|
107
|
+
const readable = await canRead(filePath);
|
|
108
|
+
if (!readable) {
|
|
109
|
+
if (opts.throws) {
|
|
110
|
+
throw new Error(`Unable to read file: ${filePath}. Access is denied.`);
|
|
111
|
+
}
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
// return new Promise((resolve, reject) => {
|
|
115
|
+
// fsSync.readFile(filePath, 'utf8', (err, contents) => {
|
|
116
|
+
// if (err) {
|
|
117
|
+
// reject(err);
|
|
118
|
+
// } else {
|
|
119
|
+
// let data = {};
|
|
120
|
+
// try {
|
|
121
|
+
// data = JSON.parse(contents);
|
|
122
|
+
// } catch (e) {
|
|
123
|
+
// if (opts.throws) {
|
|
124
|
+
// const err = new Error(`Invalid JSON contents for file: ${filePath}.`);
|
|
125
|
+
// reject(err);
|
|
126
|
+
// return;
|
|
127
|
+
// }
|
|
128
|
+
// }
|
|
129
|
+
// resolve(data);
|
|
130
|
+
// }
|
|
131
|
+
// });
|
|
132
|
+
// });
|
|
133
|
+
const contents = await readFile(filePath, 'utf8');
|
|
134
|
+
let data = {};
|
|
135
|
+
try {
|
|
136
|
+
data = JSON.parse(contents);
|
|
137
|
+
} catch (e) {
|
|
138
|
+
if (opts.throws) {
|
|
139
|
+
throw new Error(`Invalid JSON contents for file: ${filePath}.`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Writes the contents to the file.
|
|
147
|
+
*
|
|
148
|
+
* @param filePath The file to write to. It replaces the contents.
|
|
149
|
+
* @param contents The contents to write.
|
|
150
|
+
*/
|
|
151
|
+
export async function writeJson(filePath: string, contents: string|any): Promise<void> {
|
|
152
|
+
const destParent = dirname(filePath);
|
|
153
|
+
await ensureDir(destParent);
|
|
154
|
+
const parentWritable = await canWrite(destParent);
|
|
155
|
+
if (!parentWritable) {
|
|
156
|
+
throw new Error(`Unable to write to location: ${parentWritable}. Access is denied.`);
|
|
157
|
+
}
|
|
158
|
+
const data = typeof contents === 'string' ? contents : JSON.stringify(contents);
|
|
159
|
+
await writeFile(filePath, data);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Ensures the directory exists.
|
|
164
|
+
*/
|
|
165
|
+
export async function ensureDir(dirPath: string): Promise<void> {
|
|
166
|
+
const readable = await canRead(dirPath);
|
|
167
|
+
if (readable) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
await mkdir(dirPath, { recursive: true });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Removes contents of the directory, leaving the directory in the filesystem.
|
|
175
|
+
*/
|
|
176
|
+
export async function emptyDir(dirPath: string): Promise<void> {
|
|
177
|
+
const exists = await pathExists(dirPath);
|
|
178
|
+
if (!exists) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const writeable = await canWrite(dirPath);
|
|
182
|
+
if (!writeable) {
|
|
183
|
+
throw new Error(`Unable to clear directory: ${dirPath}. Access is denied.`);
|
|
184
|
+
}
|
|
185
|
+
const items = await readdir(dirPath, 'utf8');
|
|
186
|
+
for (const item of items) {
|
|
187
|
+
const file = join(dirPath, item);
|
|
188
|
+
await rm(file, { recursive: true });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Copies a file
|
|
194
|
+
*/
|
|
195
|
+
async function copyFile(source: string, dest: string): Promise<void> {
|
|
196
|
+
const destParent = dirname(dest);
|
|
197
|
+
await ensureDir(destParent);
|
|
198
|
+
await fsCopyFile(source, dest);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// /**
|
|
202
|
+
// * Copies a file
|
|
203
|
+
// * @param {string} source
|
|
204
|
+
// * @param {string} dest
|
|
205
|
+
// * @returns {Promise<void>}
|
|
206
|
+
// */
|
|
207
|
+
// async function copyFile(source, dest) {
|
|
208
|
+
// const destParent = dirname(dest);
|
|
209
|
+
// await ensureDir(destParent);
|
|
210
|
+
// return new Promise((resolve, reject) => {
|
|
211
|
+
// fsSync.copyFile(source, dest, (err) => {
|
|
212
|
+
// if (err) {
|
|
213
|
+
// reject(err);
|
|
214
|
+
// } else {
|
|
215
|
+
// resolve();
|
|
216
|
+
// }
|
|
217
|
+
// });
|
|
218
|
+
// });
|
|
219
|
+
// }
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Copies a directory and its contents.
|
|
223
|
+
*/
|
|
224
|
+
async function copyDirectory(source: string, dest: string): Promise<void> {
|
|
225
|
+
await ensureDir(dest);
|
|
226
|
+
// const entries = fsSync.readdirSync(source, { withFileTypes: true, encoding: 'utf8' });
|
|
227
|
+
const entries = await readdir(source, { withFileTypes: true, encoding: 'utf8' });
|
|
228
|
+
for (const entry of entries) {
|
|
229
|
+
const srcFile = join(source, entry.name);
|
|
230
|
+
const destFile = join(dest, entry.name);
|
|
231
|
+
const srcStat = await stat(srcFile);
|
|
232
|
+
if (srcStat.isDirectory()) {
|
|
233
|
+
await copyDirectory(srcFile, destFile);
|
|
234
|
+
} else {
|
|
235
|
+
await copyFile(srcFile, destFile);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Copies a file or a directory to the destination location.
|
|
242
|
+
* It creates the destination folder when missing.
|
|
243
|
+
*
|
|
244
|
+
* @param source The source file or folder.
|
|
245
|
+
* @param dest The destination file or folder.
|
|
246
|
+
*/
|
|
247
|
+
export async function copy(source: string, dest: string): Promise<void> {
|
|
248
|
+
const existing = await pathExists(source);
|
|
249
|
+
if (!existing) {
|
|
250
|
+
throw new Error(`Specified path does not exist: ${source}`);
|
|
251
|
+
}
|
|
252
|
+
const srcStat = await stat(source);
|
|
253
|
+
if (srcStat.isDirectory()) {
|
|
254
|
+
await copyDirectory(source, dest);
|
|
255
|
+
} else {
|
|
256
|
+
await copyFile(source, dest);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class UrlEncoder {
|
|
2
|
+
/**
|
|
3
|
+
* Returns a string where all characters that are not valid for a URL
|
|
4
|
+
* component have been escaped. The escaping of a character is done by
|
|
5
|
+
* converting it into its UTF-8 encoding and then encoding each of the
|
|
6
|
+
* resulting bytes as a %xx hexadecimal escape sequence.
|
|
7
|
+
* <p>
|
|
8
|
+
* Note: this method will convert any the space character into its escape
|
|
9
|
+
* short form, '+' rather than %20. It should therefore only be used for
|
|
10
|
+
* query-string parts.
|
|
11
|
+
*
|
|
12
|
+
* <p>
|
|
13
|
+
* The following character sets are <em>not</em> escaped by this method:
|
|
14
|
+
* <ul>
|
|
15
|
+
* <li>ASCII digits or letters</li>
|
|
16
|
+
* <li>ASCII punctuation characters:
|
|
17
|
+
*
|
|
18
|
+
* <pre>- _ . ! ~ * ' ( )</pre>
|
|
19
|
+
* </li>
|
|
20
|
+
* </ul>
|
|
21
|
+
* </p>
|
|
22
|
+
*
|
|
23
|
+
* <p>
|
|
24
|
+
* Notice that this method <em>does</em> encode the URL component delimiter
|
|
25
|
+
* characters:<blockquote>
|
|
26
|
+
*
|
|
27
|
+
* <pre>
|
|
28
|
+
* ; / ? : & = + $ , #
|
|
29
|
+
* </pre>
|
|
30
|
+
*
|
|
31
|
+
* </blockquote>
|
|
32
|
+
* </p>
|
|
33
|
+
*
|
|
34
|
+
* @param str A string containing invalid URL characters
|
|
35
|
+
* @param replacePlus When set it replaces `%20` with `+`.
|
|
36
|
+
* @returns a string with all invalid URL characters escaped
|
|
37
|
+
*/
|
|
38
|
+
static encodeQueryString(str: string, replacePlus?: boolean): string {
|
|
39
|
+
if (!str) {
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
// normalize
|
|
43
|
+
let result = str.toString().replace(/\r?\n/g, "\r\n");
|
|
44
|
+
// encode
|
|
45
|
+
result = encodeURIComponent(result);
|
|
46
|
+
if (replacePlus) {
|
|
47
|
+
// replace "%20" with "+" when needed
|
|
48
|
+
result = result.replace(/%20/g, "+");
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns a string where all URL component escape sequences have been
|
|
55
|
+
* converted back to their original character representations.
|
|
56
|
+
*
|
|
57
|
+
* Note: this method will convert the space character escape short form, '+',
|
|
58
|
+
* into a space. It should therefore only be used for query-string parts.
|
|
59
|
+
*
|
|
60
|
+
* @param str A string containing encoded URL component sequences
|
|
61
|
+
* @param replacePlus When set it replaces `+` with `%20`.
|
|
62
|
+
* @returns string with no encoded URL component encoded sequences
|
|
63
|
+
*/
|
|
64
|
+
static decodeQueryString(str: string, replacePlus?: boolean): string {
|
|
65
|
+
if (!str) {
|
|
66
|
+
return str;
|
|
67
|
+
}
|
|
68
|
+
let result = str;
|
|
69
|
+
if (replacePlus) {
|
|
70
|
+
result = str.replace(/\+/g, "%20");
|
|
71
|
+
}
|
|
72
|
+
return decodeURIComponent(result);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -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
|
+
}
|