UNPKG

6.02 kBTypeScriptView Raw
1declare namespace normalizeUrl {
2 interface Options {
3 /**
4 @default 'http:'
5 */
6 readonly defaultProtocol?: string;
7
8 /**
9 Prepends `defaultProtocol` to the URL if it's protocol-relative.
10
11 @default true
12
13 @example
14 ```
15 normalizeUrl('//sindresorhus.com:80/');
16 //=> 'http://sindresorhus.com'
17
18 normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
19 //=> '//sindresorhus.com'
20 ```
21 */
22 readonly normalizeProtocol?: boolean;
23
24 /**
25 Normalizes `https:` URLs to `http:`.
26
27 @default false
28
29 @example
30 ```
31 normalizeUrl('https://sindresorhus.com:80/');
32 //=> 'https://sindresorhus.com'
33
34 normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
35 //=> 'http://sindresorhus.com'
36 ```
37 */
38 readonly forceHttp?: boolean;
39
40 /**
41 Normalizes `http:` URLs to `https:`.
42
43 This option can't be used with the `forceHttp` option at the same time.
44
45 @default false
46
47 @example
48 ```
49 normalizeUrl('https://sindresorhus.com:80/');
50 //=> 'https://sindresorhus.com'
51
52 normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});
53 //=> 'https://sindresorhus.com'
54 ```
55 */
56 readonly forceHttps?: boolean;
57
58 /**
59 Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of a URL.
60
61 @default true
62
63 @example
64 ```
65 normalizeUrl('user:password@sindresorhus.com');
66 //=> 'https://sindresorhus.com'
67
68 normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
69 //=> 'https://user:password@sindresorhus.com'
70 ```
71 */
72 readonly stripAuthentication?: boolean;
73
74 /**
75 Removes hash from the URL.
76
77 @default false
78
79 @example
80 ```
81 normalizeUrl('sindresorhus.com/about.html#contact');
82 //=> 'http://sindresorhus.com/about.html#contact'
83
84 normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
85 //=> 'http://sindresorhus.com/about.html'
86 ```
87 */
88 readonly stripHash?: boolean;
89
90 /**
91 Removes HTTP(S) protocol from an URL `http://sindresorhus.com` → `sindresorhus.com`.
92
93 @default false
94
95 @example
96 ```
97 normalizeUrl('https://sindresorhus.com');
98 //=> 'https://sindresorhus.com'
99
100 normalizeUrl('sindresorhus.com', {stripProtocol: true});
101 //=> 'sindresorhus.com'
102 ```
103 */
104 readonly stripProtocol?: boolean;
105
106 /**
107 Strip the [text fragment](https://web.dev/text-fragments/) part of the URL
108
109 __Note:__ The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
110
111 @default true
112
113 @example
114 ```
115 normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
116 //=> 'http://sindresorhus.com/about.html#'
117
118 normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
119 //=> 'http://sindresorhus.com/about.html#section'
120
121 normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
122 //=> 'http://sindresorhus.com/about.html#:~:text=hello'
123
124 normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
125 //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
126 ```
127 */
128 readonly stripTextFragment?: boolean;
129
130 /**
131 Removes `www.` from the URL.
132
133 @default true
134
135 @example
136 ```
137 normalizeUrl('http://www.sindresorhus.com');
138 //=> 'http://sindresorhus.com'
139
140 normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
141 //=> 'http://www.sindresorhus.com'
142 ```
143 */
144 readonly stripWWW?: boolean;
145
146 /**
147 Removes query parameters that matches any of the provided strings or regexes.
148
149 @default [/^utm_\w+/i]
150
151 @example
152 ```
153 normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
154 removeQueryParameters: ['ref']
155 });
156 //=> 'http://sindresorhus.com/?foo=bar'
157 ```
158 */
159 readonly removeQueryParameters?: ReadonlyArray<RegExp | string>;
160
161 /**
162 Removes trailing slash.
163
164 __Note__: Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
165
166 @default true
167
168 @example
169 ```
170 normalizeUrl('http://sindresorhus.com/redirect/');
171 //=> 'http://sindresorhus.com/redirect'
172
173 normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
174 //=> 'http://sindresorhus.com/redirect/'
175
176 normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
177 //=> 'http://sindresorhus.com'
178 ```
179 */
180 readonly removeTrailingSlash?: boolean;
181
182 /**
183 Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`.
184
185 @default true
186
187 @example
188 ```
189 normalizeUrl('https://sindresorhus.com/');
190 //=> 'https://sindresorhus.com'
191
192 normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
193 //=> 'https://sindresorhus.com/'
194 ```
195 */
196 readonly removeSingleSlash?: boolean;
197
198 /**
199 Removes the default directory index file from path that matches any of the provided strings or regexes.
200 When `true`, the regex `/^index\.[a-z]+$/` is used.
201
202 @default false
203
204 @example
205 ```
206 normalizeUrl('www.sindresorhus.com/foo/default.php', {
207 removeDirectoryIndex: [/^default\.[a-z]+$/]
208 });
209 //=> 'http://sindresorhus.com/foo'
210 ```
211 */
212 readonly removeDirectoryIndex?: ReadonlyArray<RegExp | string>;
213
214 /**
215 Sorts the query parameters alphabetically by key.
216
217 @default true
218
219 @example
220 ```
221 normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
222 sortQueryParameters: false
223 });
224 //=> 'http://sindresorhus.com/?b=two&a=one&c=three'
225 ```
226 */
227 readonly sortQueryParameters?: boolean;
228 }
229}
230
231/**
232[Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL.
233
234@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
235
236@example
237```
238import normalizeUrl = require('normalize-url');
239
240normalizeUrl('sindresorhus.com');
241//=> 'http://sindresorhus.com'
242
243normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
244//=> 'http://sindresorhus.com/baz?a=foo&b=bar'
245```
246*/
247declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string;
248
249export = normalizeUrl;