UNPKG

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