index.d.ts 6.3 KB

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