UNPKG

6.59 kBTypeScriptView Raw
1declare module 'url' {
2 import { ClientRequestArgs } from 'node:http';
3 import { ParsedUrlQuery, ParsedUrlQueryInput } from 'querystring';
4
5 // Input to `url.format`
6 interface UrlObject {
7 auth?: string | null | undefined;
8 hash?: string | null | undefined;
9 host?: string | null | undefined;
10 hostname?: string | null | undefined;
11 href?: string | null | undefined;
12 pathname?: string | null | undefined;
13 protocol?: string | null | undefined;
14 search?: string | null | undefined;
15 slashes?: boolean | null | undefined;
16 port?: string | number | null | undefined;
17 query?: string | null | ParsedUrlQueryInput | undefined;
18 }
19
20 // Output of `url.parse`
21 interface Url {
22 auth: string | null;
23 hash: string | null;
24 host: string | null;
25 hostname: string | null;
26 href: string;
27 path: string | null;
28 pathname: string | null;
29 protocol: string | null;
30 search: string | null;
31 slashes: boolean | null;
32 port: string | null;
33 query: string | null | ParsedUrlQuery;
34 }
35
36 interface UrlWithParsedQuery extends Url {
37 query: ParsedUrlQuery;
38 }
39
40 interface UrlWithStringQuery extends Url {
41 query: string | null;
42 }
43
44 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
45 function parse(urlStr: string): UrlWithStringQuery;
46 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
47 function parse(urlStr: string, parseQueryString: false | undefined, slashesDenoteHost?: boolean): UrlWithStringQuery;
48 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
49 function parse(urlStr: string, parseQueryString: true, slashesDenoteHost?: boolean): UrlWithParsedQuery;
50 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
51 function parse(urlStr: string, parseQueryString: boolean, slashesDenoteHost?: boolean): Url;
52
53 function format(URL: URL, options?: URLFormatOptions): string;
54 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
55 function format(urlObject: UrlObject | string): string;
56 /** @deprecated since v11.0.0 - Use the WHATWG URL API. */
57 function resolve(from: string, to: string): string;
58
59 function domainToASCII(domain: string): string;
60 function domainToUnicode(domain: string): string;
61
62 /**
63 * This function ensures the correct decodings of percent-encoded characters as
64 * well as ensuring a cross-platform valid absolute path string.
65 * @param url The file URL string or URL object to convert to a path.
66 */
67 function fileURLToPath(url: string | URL): string;
68
69 /**
70 * This function ensures that path is resolved absolutely, and that the URL
71 * control characters are correctly encoded when converting into a File URL.
72 * @param url The path to convert to a File URL.
73 */
74 function pathToFileURL(url: string): URL;
75 /**
76 * This utility function converts a URL object into an ordinary options object as
77 * expected by the `http.request()` and `https.request()` APIs.
78 *
79 * ```js
80 * import { urlToHttpOptions } from 'url';
81 * const myURL = new URL('https://a:b@測試?abc#foo');
82 *
83 * console.log(urlToHttpOptions(myURL));
84 *
85 * {
86 * protocol: 'https:',
87 * hostname: 'xn--g6w251d',
88 * hash: '#foo',
89 * search: '?abc',
90 * pathname: '/',
91 * path: '/?abc',
92 * href: 'https://a:b@xn--g6w251d/?abc#foo',
93 * auth: 'a:b'
94 * }
95 *
96 * ```
97 * @since v14.18.0
98 * @param url The `WHATWG URL` object to convert to an options object.
99 * @return Options object
100 */
101 function urlToHttpOptions(url: URL): ClientRequestArgs;
102
103 interface URLFormatOptions {
104 auth?: boolean | undefined;
105 fragment?: boolean | undefined;
106 search?: boolean | undefined;
107 unicode?: boolean | undefined;
108 }
109
110 class URL {
111 constructor(input: string, base?: string | URL);
112 hash: string;
113 host: string;
114 hostname: string;
115 href: string;
116 readonly origin: string;
117 password: string;
118 pathname: string;
119 port: string;
120 protocol: string;
121 search: string;
122 readonly searchParams: URLSearchParams;
123 username: string;
124 toString(): string;
125 toJSON(): string;
126 }
127
128 class URLSearchParams implements Iterable<[string, string]> {
129 constructor(init?: URLSearchParams | string | Record<string, string | ReadonlyArray<string>> | Iterable<[string, string]> | ReadonlyArray<[string, string]>);
130 readonly size: number;
131 append(name: string, value: string): void;
132 delete(name: string): void;
133 entries(): IterableIterator<[string, string]>;
134 forEach(callback: (value: string, name: string, searchParams: URLSearchParams) => void, thisArg?: any): void;
135 get(name: string): string | null;
136 getAll(name: string): string[];
137 has(name: string): boolean;
138 keys(): IterableIterator<string>;
139 set(name: string, value: string): void;
140 sort(): void;
141 toString(): string;
142 values(): IterableIterator<string>;
143 [Symbol.iterator](): IterableIterator<[string, string]>;
144 }
145
146 import { URL as _URL, URLSearchParams as _URLSearchParams } from 'url';
147 global {
148 interface URLSearchParams extends _URLSearchParams {}
149 interface URL extends _URL {}
150 interface Global {
151 URL: typeof _URL;
152 URLSearchParams: typeof _URLSearchParams;
153 }
154 /**
155 * `URL` class is a global reference for `require('url').URL`
156 * https://nodejs.org/api/url.html#the-whatwg-url-api
157 * @since v10.0.0
158 */
159 var URL:
160 // For compatibility with "dom" and "webworker" URL declarations
161 typeof globalThis extends { onmessage: any, URL: infer URL }
162 ? URL
163 : typeof _URL;
164 /**
165 * `URLSearchParams` class is a global reference for `require('url').URLSearchParams`.
166 * https://nodejs.org/api/url.html#class-urlsearchparams
167 * @since v10.0.0
168 */
169 var URLSearchParams:
170 // For compatibility with "dom" and "webworker" URLSearchParams declarations
171 typeof globalThis extends { onmessage: any, URLSearchParams: infer URLSearchParams }
172 ? URLSearchParams
173 : typeof _URLSearchParams;
174 }
175}
176declare module 'node:url' {
177 export * from 'url';
178}