Jack Franklin | a5fd0a4 | 2022-12-09 11:30:24 +0000 | [diff] [blame] | 1 | declare 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 | If a boolean is provided, `true` will remove all the query parameters. |
| 160 | |
| 161 | ``` |
| 162 | normalizeUrl('www.sindresorhus.com?foo=bar', { |
| 163 | removeQueryParameters: true |
| 164 | }); |
| 165 | //=> 'http://sindresorhus.com' |
| 166 | ``` |
| 167 | |
| 168 | `false` will not remove any query parameter. |
| 169 | |
| 170 | ``` |
| 171 | normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', { |
| 172 | removeQueryParameters: false |
| 173 | }); |
| 174 | //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test' |
| 175 | ``` |
| 176 | */ |
| 177 | readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean; |
| 178 | |
| 179 | /** |
| 180 | Removes trailing slash. |
| 181 | |
| 182 | __Note__: Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`. |
| 183 | |
| 184 | @default true |
| 185 | |
| 186 | @example |
| 187 | ``` |
| 188 | normalizeUrl('http://sindresorhus.com/redirect/'); |
| 189 | //=> 'http://sindresorhus.com/redirect' |
| 190 | |
| 191 | normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false}); |
| 192 | //=> 'http://sindresorhus.com/redirect/' |
| 193 | |
| 194 | normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false}); |
| 195 | //=> 'http://sindresorhus.com' |
| 196 | ``` |
| 197 | */ |
| 198 | readonly removeTrailingSlash?: boolean; |
| 199 | |
| 200 | /** |
| 201 | Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`. |
| 202 | |
| 203 | @default true |
| 204 | |
| 205 | @example |
| 206 | ``` |
| 207 | normalizeUrl('https://sindresorhus.com/'); |
| 208 | //=> 'https://sindresorhus.com' |
| 209 | |
| 210 | normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false}); |
| 211 | //=> 'https://sindresorhus.com/' |
| 212 | ``` |
| 213 | */ |
| 214 | readonly removeSingleSlash?: boolean; |
| 215 | |
| 216 | /** |
| 217 | Removes the default directory index file from path that matches any of the provided strings or regexes. |
| 218 | When `true`, the regex `/^index\.[a-z]+$/` is used. |
| 219 | |
| 220 | @default false |
| 221 | |
| 222 | @example |
| 223 | ``` |
| 224 | normalizeUrl('www.sindresorhus.com/foo/default.php', { |
| 225 | removeDirectoryIndex: [/^default\.[a-z]+$/] |
| 226 | }); |
| 227 | //=> 'http://sindresorhus.com/foo' |
| 228 | ``` |
| 229 | */ |
| 230 | readonly removeDirectoryIndex?: ReadonlyArray<RegExp | string>; |
| 231 | |
| 232 | /** |
| 233 | Sorts the query parameters alphabetically by key. |
| 234 | |
| 235 | @default true |
| 236 | |
| 237 | @example |
| 238 | ``` |
| 239 | normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', { |
| 240 | sortQueryParameters: false |
| 241 | }); |
| 242 | //=> 'http://sindresorhus.com/?b=two&a=one&c=three' |
| 243 | ``` |
| 244 | */ |
| 245 | readonly sortQueryParameters?: boolean; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL. |
| 251 | |
| 252 | @param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). |
| 253 | |
| 254 | @example |
| 255 | ``` |
| 256 | import normalizeUrl = require('normalize-url'); |
| 257 | |
| 258 | normalizeUrl('sindresorhus.com'); |
| 259 | //=> 'http://sindresorhus.com' |
| 260 | |
| 261 | normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo'); |
| 262 | //=> 'http://sindresorhus.com/baz?a=foo&b=bar' |
| 263 | ``` |
| 264 | */ |
| 265 | declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string; |
| 266 | |
| 267 | export = normalizeUrl; |