Debian Codegraph @

url/types/url.d.ts

raw
1 /// <reference types="@dop251/types-goja_nodejs-global" />
2 declare module 'url' {
3 /**
4 * Returns the [Punycode](https://tools.ietf.org/html/rfc5891#section-4.4) ASCII serialization of the `domain`. If `domain` is an
5 * invalid domain, the empty string is returned.
6 *
7 * It performs the inverse operation to {@link domainToUnicode}.
8 *
9 * ```js
10 * import url from 'node:url';
11 *
12 * console.log(url.domainToASCII('español.com'));
13 * // Prints xn--espaol-zwa.com
14 * console.log(url.domainToASCII('中文.com'));
15 * // Prints xn--fiq228c.com
16 * console.log(url.domainToASCII('xn--iñvalid.com'));
17 * // Prints an empty string
18 * ```
19 * @since v7.4.0, v6.13.0
20 */
21 function domainToASCII(domain: string): string;
22 /**
23 * Returns the Unicode serialization of the `domain`. If `domain` is an invalid
24 * domain, the empty string is returned.
25 *
26 * It performs the inverse operation to {@link domainToASCII}.
27 *
28 * ```js
29 * import url from 'node:url';
30 *
31 * console.log(url.domainToUnicode('xn--espaol-zwa.com'));
32 * // Prints español.com
33 * console.log(url.domainToUnicode('xn--fiq228c.com'));
34 * // Prints 中文.com
35 * console.log(url.domainToUnicode('xn--iñvalid.com'));
36 * // Prints an empty string
37 * ```
38 * @since v7.4.0, v6.13.0
39 */
40 function domainToUnicode(domain: string): string;
41
42 /**
43 * Browser-compatible `URL` class, implemented by following the WHATWG URL
44 * Standard. [Examples of parsed URLs](https://url.spec.whatwg.org/#example-url-parsing) may be found in the Standard itself.
45 * The `URL` class is also available on the global object.
46 *
47 * In accordance with browser conventions, all properties of `URL` objects
48 * are implemented as getters and setters on the class prototype, rather than as
49 * data properties on the object itself. Thus, unlike `legacy urlObject`s,
50 * using the `delete` keyword on any properties of `URL` objects (e.g. `delete myURL.protocol`, `delete myURL.pathname`, etc) has no effect but will still
51 * return `true`.
52 * @since v7.0.0, v6.13.0
53 */
54 class URL {
55 constructor(input: string | { toString: () => string }, base?: string | URL);
56 /**
57 * Gets and sets the fragment portion of the URL.
58 *
59 * ```js
60 * const myURL = new URL('https://example.org/foo#bar');
61 * console.log(myURL.hash);
62 * // Prints #bar
63 *
64 * myURL.hash = 'baz';
65 * console.log(myURL.href);
66 * // Prints https://example.org/foo#baz
67 * ```
68 *
69 * Invalid URL characters included in the value assigned to the `hash` property
70 * are `percent-encoded`. The selection of which characters to
71 * percent-encode may vary somewhat from what the {@link parse} and {@link format} methods would produce.
72 */
73 hash: string;
74 /**
75 * Gets and sets the host portion of the URL.
76 *
77 * ```js
78 * const myURL = new URL('https://example.org:81/foo');
79 * console.log(myURL.host);
80 * // Prints example.org:81
81 *
82 * myURL.host = 'example.com:82';
83 * console.log(myURL.href);
84 * // Prints https://example.com:82/foo
85 * ```
86 *
87 * Invalid host values assigned to the `host` property are ignored.
88 */
89 host: string;
90 /**
91 * Gets and sets the host name portion of the URL. The key difference between`url.host` and `url.hostname` is that `url.hostname` does _not_ include the
92 * port.
93 *
94 * ```js
95 * const myURL = new URL('https://example.org:81/foo');
96 * console.log(myURL.hostname);
97 * // Prints example.org
98 *
99 * // Setting the hostname does not change the port
100 * myURL.hostname = 'example.com';
101 * console.log(myURL.href);
102 * // Prints https://example.com:81/foo
103 *
104 * // Use myURL.host to change the hostname and port
105 * myURL.host = 'example.org:82';
106 * console.log(myURL.href);
107 * // Prints https://example.org:82/foo
108 * ```
109 *
110 * Invalid host name values assigned to the `hostname` property are ignored.
111 */
112 hostname: string;
113 /**
114 * Gets and sets the serialized URL.
115 *
116 * ```js
117 * const myURL = new URL('https://example.org/foo');
118 * console.log(myURL.href);
119 * // Prints https://example.org/foo
120 *
121 * myURL.href = 'https://example.com/bar';
122 * console.log(myURL.href);
123 * // Prints https://example.com/bar
124 * ```
125 *
126 * Getting the value of the `href` property is equivalent to calling {@link toString}.
127 *
128 * Setting the value of this property to a new value is equivalent to creating a
129 * new `URL` object using `new URL(value)`. Each of the `URL` object's properties will be modified.
130 *
131 * If the value assigned to the `href` property is not a valid URL, a `TypeError` will be thrown.
132 */
133 href: string;
134 /**
135 * Gets the read-only serialization of the URL's origin.
136 *
137 * ```js
138 * const myURL = new URL('https://example.org/foo/bar?baz');
139 * console.log(myURL.origin);
140 * // Prints https://example.org
141 * ```
142 *
143 * ```js
144 * const idnURL = new URL('https://測試');
145 * console.log(idnURL.origin);
146 * // Prints https://xn--g6w251d
147 *
148 * console.log(idnURL.hostname);
149 * // Prints xn--g6w251d
150 * ```
151 */
152 readonly origin: string;
153 /**
154 * Gets and sets the password portion of the URL.
155 *
156 * ```js
157 * const myURL = new URL('https://abc:[email protected]');
158 * console.log(myURL.password);
159 * // Prints xyz
160 *
161 * myURL.password = '123';
162 * console.log(myURL.href);
163 * // Prints https://abc:[email protected]/
164 * ```
165 *
166 * Invalid URL characters included in the value assigned to the `password` property
167 * are `percent-encoded`. The selection of which characters to
168 * percent-encode may vary somewhat from what the {@link parse} and {@link format} methods would produce.
169 */
170 password: string;
171 /**
172 * Gets and sets the path portion of the URL.
173 *
174 * ```js
175 * const myURL = new URL('https://example.org/abc/xyz?123');
176 * console.log(myURL.pathname);
177 * // Prints /abc/xyz
178 *
179 * myURL.pathname = '/abcdef';
180 * console.log(myURL.href);
181 * // Prints https://example.org/abcdef?123
182 * ```
183 *
184 * Invalid URL characters included in the value assigned to the `pathname` property are `percent-encoded`. The selection of which characters
185 * to percent-encode may vary somewhat from what the {@link parse} and {@link format} methods would produce.
186 */
187 pathname: string;
188 /**
189 * Gets and sets the port portion of the URL.
190 *
191 * The port value may be a number or a string containing a number in the range `0` to `65535` (inclusive). Setting the value to the default port of the `URL` objects given `protocol` will
192 * result in the `port` value becoming
193 * the empty string (`''`).
194 *
195 * The port value can be an empty string in which case the port depends on
196 * the protocol/scheme:
197 *
198 * <omitted>
199 *
200 * Upon assigning a value to the port, the value will first be converted to a
201 * string using `.toString()`.
202 *
203 * If that string is invalid but it begins with a number, the leading number is
204 * assigned to `port`.
205 * If the number lies outside the range denoted above, it is ignored.
206 *
207 * ```js
208 * const myURL = new URL('https://example.org:8888');
209 * console.log(myURL.port);
210 * // Prints 8888
211 *
212 * // Default ports are automatically transformed to the empty string
213 * // (HTTPS protocol's default port is 443)
214 * myURL.port = '443';
215 * console.log(myURL.port);
216 * // Prints the empty string
217 * console.log(myURL.href);
218 * // Prints https://example.org/
219 *
220 * myURL.port = 1234;
221 * console.log(myURL.port);
222 * // Prints 1234
223 * console.log(myURL.href);
224 * // Prints https://example.org:1234/
225 *
226 * // Completely invalid port strings are ignored
227 * myURL.port = 'abcd';
228 * console.log(myURL.port);
229 * // Prints 1234
230 *
231 * // Leading numbers are treated as a port number
232 * myURL.port = '5678abcd';
233 * console.log(myURL.port);
234 * // Prints 5678
235 *
236 * // Non-integers are truncated
237 * myURL.port = 1234.5678;
238 * console.log(myURL.port);
239 * // Prints 1234
240 *
241 * // Out-of-range numbers which are not represented in scientific notation
242 * // will be ignored.
243 * myURL.port = 1e10; // 10000000000, will be range-checked as described below
244 * console.log(myURL.port);
245 * // Prints 1234
246 * ```
247 *
248 * Numbers which contain a decimal point,
249 * such as floating-point numbers or numbers in scientific notation,
250 * are not an exception to this rule.
251 * Leading numbers up to the decimal point will be set as the URL's port,
252 * assuming they are valid:
253 *
254 * ```js
255 * myURL.port = 4.567e21;
256 * console.log(myURL.port);
257 * // Prints 4 (because it is the leading number in the string '4.567e21')
258 * ```
259 */
260 port: string;
261 /**
262 * Gets and sets the protocol portion of the URL.
263 *
264 * ```js
265 * const myURL = new URL('https://example.org');
266 * console.log(myURL.protocol);
267 * // Prints https:
268 *
269 * myURL.protocol = 'ftp';
270 * console.log(myURL.href);
271 * // Prints ftp://example.org/
272 * ```
273 *
274 * Invalid URL protocol values assigned to the `protocol` property are ignored.
275 */
276 protocol: string;
277 /**
278 * Gets and sets the serialized query portion of the URL.
279 *
280 * ```js
281 * const myURL = new URL('https://example.org/abc?123');
282 * console.log(myURL.search);
283 * // Prints ?123
284 *
285 * myURL.search = 'abc=xyz';
286 * console.log(myURL.href);
287 * // Prints https://example.org/abc?abc=xyz
288 * ```
289 *
290 * Any invalid URL characters appearing in the value assigned the `search` property will be `percent-encoded`. The selection of which
291 * characters to percent-encode may vary somewhat from what the {@link parse} and {@link format} methods would produce.
292 */
293 search: string;
294 /**
295 * Gets the `URLSearchParams` object representing the query parameters of the
296 * URL. This property is read-only but the `URLSearchParams` object it provides
297 * can be used to mutate the URL instance; to replace the entirety of query
298 * parameters of the URL, use the {@link search} setter. See `URLSearchParams` documentation for details.
299 *
300 * Use care when using `.searchParams` to modify the `URL` because,
301 * per the WHATWG specification, the `URLSearchParams` object uses
302 * different rules to determine which characters to percent-encode. For
303 * instance, the `URL` object will not percent encode the ASCII tilde (`~`)
304 * character, while `URLSearchParams` will always encode it:
305 *
306 * ```js
307 * const myURL = new URL('https://example.org/abc?foo=~bar');
308 *
309 * console.log(myURL.search); // prints ?foo=~bar
310 *
311 * // Modify the URL via searchParams...
312 * myURL.searchParams.sort();
313 *
314 * console.log(myURL.search); // prints ?foo=%7Ebar
315 * ```
316 */
317 readonly searchParams: URLSearchParams;
318 /**
319 * Gets and sets the username portion of the URL.
320 *
321 * ```js
322 * const myURL = new URL('https://abc:[email protected]');
323 * console.log(myURL.username);
324 * // Prints abc
325 *
326 * myURL.username = '123';
327 * console.log(myURL.href);
328 * // Prints https://123:[email protected]/
329 * ```
330 *
331 * Any invalid URL characters appearing in the value assigned the `username` property will be `percent-encoded`. The selection of which
332 * characters to percent-encode may vary somewhat from what the {@link parse} and {@link format} methods would produce.
333 */
334 username: string;
335 /**
336 * The `toString()` method on the `URL` object returns the serialized URL. The
337 * value returned is equivalent to that of {@link href} and {@link toJSON}.
338 */
339 toString(): string;
340 /**
341 * The `toJSON()` method on the `URL` object returns the serialized URL. The
342 * value returned is equivalent to that of {@link href} and {@link toString}.
343 *
344 * This method is automatically called when an `URL` object is serialized
345 * with [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
346 *
347 * ```js
348 * const myURLs = [
349 * new URL('https://www.example.com'),
350 * new URL('https://test.example.org'),
351 * ];
352 * console.log(JSON.stringify(myURLs));
353 * // Prints ["https://www.example.com/","https://test.example.org/"]
354 * ```
355 */
356 toJSON(): string;
357 }
358 interface URLSearchParamsIterator<T> extends GojaNodeJS.Iterator<T, GojaNodeJS.BuiltinIteratorReturn, unknown> {
359 [Symbol.iterator](): URLSearchParamsIterator<T>;
360 }
361
362 /**
363 * The `URLSearchParams` API provides read and write access to the query of a `URL`. The `URLSearchParams` class can also be used standalone with one of the
364 * four following constructors.
365 * The `URLSearchParams` class is also available on the global object.
366 *
367 * The WHATWG `URLSearchParams` interface and the `querystring` module have
368 * similar purpose, but the purpose of the `querystring` module is more
369 * general, as it allows the customization of delimiter characters (`&#x26;` and `=`).
370 * On the other hand, this API is designed purely for URL query strings.
371 *
372 * ```js
373 * const myURL = new URL('https://example.org/?abc=123');
374 * console.log(myURL.searchParams.get('abc'));
375 * // Prints 123
376 *
377 * myURL.searchParams.append('abc', 'xyz');
378 * console.log(myURL.href);
379 * // Prints https://example.org/?abc=123&#x26;abc=xyz
380 *
381 * myURL.searchParams.delete('abc');
382 * myURL.searchParams.set('a', 'b');
383 * console.log(myURL.href);
384 * // Prints https://example.org/?a=b
385 *
386 * const newSearchParams = new URLSearchParams(myURL.searchParams);
387 * // The above is equivalent to
388 * // const newSearchParams = new URLSearchParams(myURL.search);
389 *
390 * newSearchParams.append('a', 'c');
391 * console.log(myURL.href);
392 * // Prints https://example.org/?a=b
393 * console.log(newSearchParams.toString());
394 * // Prints a=b&#x26;a=c
395 *
396 * // newSearchParams.toString() is implicitly called
397 * myURL.search = newSearchParams;
398 * console.log(myURL.href);
399 * // Prints https://example.org/?a=b&#x26;a=c
400 * newSearchParams.delete('a');
401 * console.log(myURL.href);
402 * // Prints https://example.org/?a=b&#x26;a=c
403 * ```
404 * @since v7.5.0, v6.13.0
405 */
406 class URLSearchParams implements Iterable<[string, string]> {
407 constructor(
408 init?:
409 | URLSearchParams
410 | string
411 | Record<string, string | readonly string[]>
412 | Iterable<[string, string]>
413 | ReadonlyArray<[string, string]>,
414 );
415 /**
416 * Append a new name-value pair to the query string.
417 */
418 append(name: string, value: string): void;
419 /**
420 * If `value` is provided, removes all name-value pairs
421 * where name is `name` and value is `value`.
422 *
423 * If `value` is not provided, removes all name-value pairs whose name is `name`.
424 */
425 delete(name: string, value?: string): void;
426 /**
427 * Returns an ES6 `Iterator` over each of the name-value pairs in the query.
428 * Each item of the iterator is a JavaScript `Array`. The first item of the `Array` is the `name`, the second item of the `Array` is the `value`.
429 *
430 * Alias for `urlSearchParams[@@iterator]()`.
431 */
432 entries(): URLSearchParamsIterator<[string, string]>;
433 /**
434 * Iterates over each name-value pair in the query and invokes the given function.
435 *
436 * ```js
437 * const myURL = new URL('https://example.org/?a=b&#x26;c=d');
438 * myURL.searchParams.forEach((value, name, searchParams) => {
439 * console.log(name, value, myURL.searchParams === searchParams);
440 * });
441 * // Prints:
442 * // a b true
443 * // c d true
444 * ```
445 * @param fn Invoked for each name-value pair in the query
446 * @param thisArg To be used as `this` value for when `fn` is called
447 */
448 forEach<TThis = this>(
449 fn: (this: TThis, value: string, name: string, searchParams: URLSearchParams) => void,
450 thisArg?: TThis,
451 ): void;
452 /**
453 * Returns the value of the first name-value pair whose name is `name`. If there
454 * are no such pairs, `null` is returned.
455 * @return or `null` if there is no name-value pair with the given `name`.
456 */
457 get(name: string): string | null;
458 /**
459 * Returns the values of all name-value pairs whose name is `name`. If there are
460 * no such pairs, an empty array is returned.
461 */
462 getAll(name: string): string[];
463 /**
464 * Checks if the `URLSearchParams` object contains key-value pair(s) based on `name` and an optional `value` argument.
465 *
466 * If `value` is provided, returns `true` when name-value pair with
467 * same `name` and `value` exists.
468 *
469 * If `value` is not provided, returns `true` if there is at least one name-value
470 * pair whose name is `name`.
471 */
472 has(name: string, value?: string): boolean;
473 /**
474 * Returns an ES6 `Iterator` over the names of each name-value pair.
475 *
476 * ```js
477 * const params = new URLSearchParams('foo=bar&#x26;foo=baz');
478 * for (const name of params.keys()) {
479 * console.log(name);
480 * }
481 * // Prints:
482 * // foo
483 * // foo
484 * ```
485 */
486 keys(): URLSearchParamsIterator<string>;
487 /**
488 * Sets the value in the `URLSearchParams` object associated with `name` to `value`. If there are any pre-existing name-value pairs whose names are `name`,
489 * set the first such pair's value to `value` and remove all others. If not,
490 * append the name-value pair to the query string.
491 *
492 * ```js
493 * const params = new URLSearchParams();
494 * params.append('foo', 'bar');
495 * params.append('foo', 'baz');
496 * params.append('abc', 'def');
497 * console.log(params.toString());
498 * // Prints foo=bar&#x26;foo=baz&#x26;abc=def
499 *
500 * params.set('foo', 'def');
501 * params.set('xyz', 'opq');
502 * console.log(params.toString());
503 * // Prints foo=def&#x26;abc=def&#x26;xyz=opq
504 * ```
505 */
506 set(name: string, value: string): void;
507 /**
508 * The total number of parameter entries.
509 * @since v19.8.0
510 */
511 readonly size: number;
512 /**
513 * Sort all existing name-value pairs in-place by their names. Sorting is done
514 * with a [stable sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability), so relative order between name-value pairs
515 * with the same name is preserved.
516 *
517 * This method can be used, in particular, to increase cache hits.
518 *
519 * ```js
520 * const params = new URLSearchParams('query[]=abc&#x26;type=search&#x26;query[]=123');
521 * params.sort();
522 * console.log(params.toString());
523 * // Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search
524 * ```
525 * @since v7.7.0, v6.13.0
526 */
527 sort(): void;
528 /**
529 * Returns the search parameters serialized as a string, with characters
530 * percent-encoded where necessary.
531 */
532 toString(): string;
533 /**
534 * Returns an ES6 `Iterator` over the values of each name-value pair.
535 */
536 values(): URLSearchParamsIterator<string>;
537 [Symbol.iterator](): URLSearchParamsIterator<[string, string]>;
538 }
539 }
540
541 declare module "node:url" {
542 export * from "url";
543 }