index.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. type QueryValue = string | number | undefined | null | boolean | Array<QueryValue> | Record<string, any>;
  2. type QueryObject = Record<string, QueryValue | QueryValue[]>;
  3. type ParsedQuery = Record<string, string | string[]>;
  4. /**
  5. * Parses and decodes a query string into an object.
  6. *
  7. * input can be a query string with or without the leading `?`
  8. *
  9. * @note
  10. * The `__proto__` and `constructor` keys are ignored to prevent prototype pollution.
  11. *
  12. * @group Query_utils
  13. */
  14. declare function parseQuery<T extends ParsedQuery = ParsedQuery>(parametersString?: string): T;
  15. /**
  16. * Encodes a pair of key and value into a url query string value.
  17. *
  18. * If the value is an array, it will be encoded as multiple key-value pairs with the same key.
  19. *
  20. * @group Query_utils
  21. */
  22. declare function encodeQueryItem(key: string, value: QueryValue | QueryValue[]): string;
  23. /**
  24. * Stringfies and encodes a query object into a query string.
  25. *
  26. * @group Query_utils
  27. */
  28. declare function stringifyQuery(query: QueryObject): string;
  29. /**
  30. * Encode characters that need to be encoded on the path, search and hash
  31. * sections of the URL.
  32. *
  33. * @group encoding_utils
  34. *
  35. * @param text - string to encode
  36. * @returns encoded string
  37. */
  38. declare function encode(text: string | number): string;
  39. /**
  40. * Encode characters that need to be encoded on the hash section of the URL.
  41. *
  42. * @group encoding_utils
  43. *
  44. * @param text - string to encode
  45. * @returns encoded string
  46. */
  47. declare function encodeHash(text: string): string;
  48. /**
  49. * Encode characters that need to be encoded query values on the query
  50. * section of the URL.
  51. *
  52. * @group encoding_utils
  53. *
  54. * @param input - string to encode
  55. * @returns encoded string
  56. */
  57. declare function encodeQueryValue(input: QueryValue): string;
  58. /**
  59. * Encode characters that need to be encoded query values on the query
  60. * section of the URL and also encodes the `=` character.
  61. *
  62. * @group encoding_utils
  63. *
  64. * @param text - string to encode
  65. */
  66. declare function encodeQueryKey(text: string | number): string;
  67. /**
  68. * Encode characters that need to be encoded on the path section of the URL.
  69. *
  70. * @group encoding_utils
  71. *
  72. * @param text - string to encode
  73. * @returns encoded string
  74. */
  75. declare function encodePath(text: string | number): string;
  76. /**
  77. * Encode characters that need to be encoded on the path section of the URL as a
  78. * param. This function encodes everything `encodePath` does plus the
  79. * slash (`/`) character.
  80. *
  81. * @group encoding_utils
  82. *
  83. * @param text - string to encode
  84. * @returns encoded string
  85. */
  86. declare function encodeParam(text: string | number): string;
  87. /**
  88. * Decode text using `decodeURIComponent`. Returns the original text if it
  89. * fails.
  90. *
  91. * @group encoding_utils
  92. *
  93. * @param text - string to decode
  94. * @returns decoded string
  95. */
  96. declare function decode(text?: string | number): string;
  97. /**
  98. * Decode path section of URL (consistent with encodePath for slash encoding).
  99. *
  100. * @group encoding_utils
  101. *
  102. * @param text - string to decode
  103. * @returns decoded string
  104. */
  105. declare function decodePath(text: string): string;
  106. /**
  107. * Decodes query key (consistent with `encodeQueryKey` for plus encoding).
  108. *
  109. * @group encoding_utils
  110. *
  111. * @param text - string to decode
  112. * @returns decoded string
  113. */
  114. declare function decodeQueryKey(text: string): string;
  115. /**
  116. * Decode query value (consistent with encodeQueryValue for plus encoding).
  117. *
  118. * @group encoding_utils
  119. *
  120. * @param text - string to decode
  121. * @returns decoded string
  122. */
  123. declare function decodeQueryValue(text: string): string;
  124. /**
  125. * Encodes hostname with punycode encoding.
  126. *
  127. * @group encoding_utils
  128. */
  129. declare function encodeHost(name?: string): string;
  130. declare const protocolRelative: unique symbol;
  131. interface ParsedURL {
  132. protocol?: string;
  133. host?: string;
  134. auth?: string;
  135. href?: string;
  136. pathname: string;
  137. hash: string;
  138. search: string;
  139. [protocolRelative]?: boolean;
  140. }
  141. interface ParsedAuth {
  142. username: string;
  143. password: string;
  144. }
  145. interface ParsedHost {
  146. hostname: string;
  147. port: string;
  148. }
  149. /**
  150. * Takes a URL string and returns an object with the URL's `protocol`, `auth`, `host`, `pathname`, `search`, and `hash`.
  151. *
  152. * @example
  153. *
  154. * ```js
  155. * parseURL("http://foo.com/foo?test=123#token");
  156. * // { protocol: 'http:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
  157. *
  158. * parseURL("foo.com/foo?test=123#token");
  159. * // { pathname: 'foo.com/foo', search: '?test=123', hash: '#token' }
  160. *
  161. * parseURL("foo.com/foo?test=123#token", "https://");
  162. * // { protocol: 'https:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
  163. * ```
  164. *
  165. * @group parsing_utils
  166. *
  167. * @param [input] - The URL to parse.
  168. * @param [defaultProto] - The default protocol to use if the input doesn't have one.
  169. * @returns A parsed URL object.
  170. */
  171. declare function parseURL(input?: string, defaultProto?: string): ParsedURL;
  172. /**
  173. * Splits the input string into three parts, and returns an object with those three parts.
  174. *
  175. * @group parsing_utils
  176. *
  177. * @param [input] - The URL to parse.
  178. * @returns An object with three properties: `pathname`, `search`, and `hash`.
  179. */
  180. declare function parsePath(input?: string): ParsedURL;
  181. /**
  182. * Takes a string of the form `username:password` and returns an object with the username and
  183. * password decoded.
  184. *
  185. * @group parsing_utils
  186. *
  187. * @param [input] - The URL to parse.
  188. * @returns An object with two properties: username and password.
  189. */
  190. declare function parseAuth(input?: string): ParsedAuth;
  191. /**
  192. * Takes a string, and returns an object with two properties: `hostname` and `port`.
  193. *
  194. * @group parsing_utils
  195. *
  196. * @param [input] - The URL to parse.
  197. * @returns A function that takes a string and returns an object with two properties: `hostname` and
  198. * `port`.
  199. */
  200. declare function parseHost(input?: string): ParsedHost;
  201. /**
  202. * Takes a `ParsedURL` object and returns the stringified URL.
  203. *
  204. * @group parsing_utils
  205. *
  206. * @example
  207. *
  208. * ```js
  209. * const obj = parseURL("http://foo.com/foo?test=123#token");
  210. * obj.host = "bar.com";
  211. *
  212. * stringifyParsedURL(obj); // "http://bar.com/foo?test=123#token"
  213. * ```
  214. *
  215. * @param [parsed] - The parsed URL
  216. * @returns A stringified URL.
  217. */
  218. declare function stringifyParsedURL(parsed: Partial<ParsedURL>): string;
  219. /**
  220. * Parses a url and returns last segment in path as filename.
  221. *
  222. * If `{ strict: true }` is passed as the second argument, it will only return the last segment only if ending with an extension.
  223. *
  224. * @group parsing_utils
  225. *
  226. * @example
  227. *
  228. * ```js
  229. * // Result: filename.ext
  230. * parseFilename("http://example.com/path/to/filename.ext");
  231. *
  232. * // Result: undefined
  233. * parseFilename("/path/to/.hidden-file", { strict: true });
  234. * ```
  235. */
  236. declare function parseFilename(input: string, { strict }: {
  237. strict: any;
  238. }): string | undefined;
  239. /**
  240. * @deprecated use native URL with `new URL(input)` or `ufo.parseURL(input)`
  241. */
  242. declare class $URL implements URL {
  243. protocol: string;
  244. host: string;
  245. auth: string;
  246. pathname: string;
  247. query: QueryObject;
  248. hash: string;
  249. constructor(input?: string);
  250. get hostname(): string;
  251. get port(): string;
  252. get username(): string;
  253. get password(): string;
  254. get hasProtocol(): number;
  255. get isAbsolute(): number | boolean;
  256. get search(): string;
  257. get searchParams(): URLSearchParams;
  258. get origin(): string;
  259. get fullpath(): string;
  260. get encodedAuth(): string;
  261. get href(): string;
  262. append(url: $URL): void;
  263. toJSON(): string;
  264. toString(): string;
  265. }
  266. /**
  267. * @deprecated use native URL with `new URL(input)` or `ufo.parseURL(input)`
  268. */
  269. declare function createURL(input: string): $URL;
  270. /**
  271. * Check if a path starts with `./` or `../`.
  272. *
  273. * @example
  274. * ```js
  275. * isRelative("./foo"); // true
  276. * ```
  277. *
  278. * @group utils
  279. */
  280. declare function isRelative(inputString: string): boolean;
  281. interface HasProtocolOptions {
  282. acceptRelative?: boolean;
  283. strict?: boolean;
  284. }
  285. /**
  286. * Checks if the input has a protocol.
  287. *
  288. * You can use `{ acceptRelative: true }` to accept relative URLs as valid protocol.
  289. *
  290. * @group utils
  291. */
  292. declare function hasProtocol(inputString: string, opts?: HasProtocolOptions): boolean;
  293. /** @deprecated Same as { hasProtocol(inputString, { acceptRelative: true }) */
  294. declare function hasProtocol(inputString: string, acceptRelative: boolean): boolean;
  295. /**
  296. * Checks if the input protocol is any of the dangerous `blob:`, `data:`, `javascript`: or `vbscript:` protocols.
  297. *
  298. * @group utils
  299. */
  300. declare function isScriptProtocol(protocol?: string): boolean;
  301. /**
  302. * Checks if the input has a trailing slash.
  303. *
  304. * @group utils
  305. */
  306. declare function hasTrailingSlash(input?: string, respectQueryAndFragment?: boolean): boolean;
  307. /**
  308. * Removes trailing slash from the URL or pathname.
  309. *
  310. * If second argument is true, it will only remove the trailing slash if it's not part of the query or fragment with cost of more expensive operations.
  311. *
  312. * @example
  313. *
  314. * ```js
  315. * withoutTrailingSlash("/foo/"); // "/foo"
  316. *
  317. * withoutTrailingSlash("/path/?query=true", true); // "/path?query=true"
  318. * ```
  319. *
  320. * @group utils
  321. */
  322. declare function withoutTrailingSlash(input?: string, respectQueryAndFragment?: boolean): string;
  323. /**
  324. * Ensures url ends with a trailing slash.
  325. *
  326. * If seccond argument is `true`, it will only add the trailing slash if it's not part of the query or fragment with cost of more expensive operation.
  327. *
  328. * @example
  329. *
  330. * ```js
  331. * withTrailingSlash("/foo"); // "/foo/"
  332. *
  333. * withTrailingSlash("/path?query=true", true); // "/path/?query=true"
  334. * ```
  335. *
  336. * @group utils
  337. */
  338. declare function withTrailingSlash(input?: string, respectQueryAndFragment?: boolean): string;
  339. /**
  340. * Checks if the input has a leading slash. (e.g. `/foo`)
  341. *
  342. * @group utils
  343. */
  344. declare function hasLeadingSlash(input?: string): boolean;
  345. /**
  346. * Removes leading slash from the URL or pathname.
  347. *
  348. * @group utils
  349. */
  350. declare function withoutLeadingSlash(input?: string): string;
  351. /**
  352. * Ensures the URL or pathname has a leading slash.
  353. *
  354. * @group utils
  355. */
  356. declare function withLeadingSlash(input?: string): string;
  357. /**
  358. * Removes double slashes from the URL.
  359. *
  360. * @example
  361. *
  362. * ```js
  363. * cleanDoubleSlashes("//foo//bar//"); // "/foo/bar/"
  364. *
  365. * cleanDoubleSlashes("http://example.com/analyze//http://localhost:3000//");
  366. * // Returns "http://example.com/analyze/http://localhost:3000/"
  367. * ```
  368. *
  369. * @group utils
  370. */
  371. declare function cleanDoubleSlashes(input?: string): string;
  372. /**
  373. * Ensures the URL or pathname has a trailing slash.
  374. *
  375. * If input aleady start with base, it will not be added again.
  376. *
  377. * @group utils
  378. */
  379. declare function withBase(input: string, base: string): string;
  380. /**
  381. * Removes the base from the URL or pathname.
  382. *
  383. * If input does not start with base, it will not be removed.
  384. *
  385. * @group utils
  386. */
  387. declare function withoutBase(input: string, base: string): string;
  388. /**
  389. * Add/Replace the query section of the URL.
  390. *
  391. * @example
  392. *
  393. * ```js
  394. * withQuery("/foo?page=a", { token: "secret" }); // "/foo?page=a&token=secret"
  395. * ```
  396. *
  397. * @group utils
  398. */
  399. declare function withQuery(input: string, query: QueryObject): string;
  400. /**
  401. * Parses and decods the query object of an input URL into an object.
  402. *
  403. * @example
  404. *
  405. * ```js
  406. * getQuery("http://foo.com/foo?test=123&unicode=%E5%A5%BD");
  407. * // { test: "123", unicode: "好" }
  408. * ```
  409. * @group utils
  410. */
  411. declare function getQuery<T extends ParsedQuery = ParsedQuery>(input: string): T;
  412. /**
  413. * Checks if the input url is empty or `/`.
  414. *
  415. * @group utils
  416. */
  417. declare function isEmptyURL(url: string): boolean;
  418. /**
  419. * Checks if the input url is not empty nor `/`.
  420. *
  421. * @group utils
  422. */
  423. declare function isNonEmptyURL(url: string): boolean;
  424. /**
  425. * Joins multiple URL segments into a single URL.
  426. *
  427. * @example
  428. *
  429. * ```js
  430. * joinURL("a", "/b", "/c"); // "a/b/c"
  431. * ```
  432. *
  433. * @group utils
  434. */
  435. declare function joinURL(base: string, ...input: string[]): string;
  436. /**
  437. * Joins multiple URL segments into a single URL and also handles relative paths with `./` and `../`.
  438. *
  439. * @example
  440. *
  441. * ```js
  442. * joinRelativeURL("/a", "../b", "./c"); // "/b/c"
  443. * ```
  444. *
  445. * @group utils
  446. */
  447. declare function joinRelativeURL(..._input: string[]): string;
  448. /**
  449. * Adds or replaces url protocol to `http://`.
  450. *
  451. * @example
  452. *
  453. * ```js
  454. * withHttp("https://example.com"); // http://example.com
  455. * ```
  456. *
  457. * @group utils
  458. */
  459. declare function withHttp(input: string): string;
  460. /**
  461. * Adds or replaces url protocol to `https://`.
  462. *
  463. * @example
  464. *
  465. * ```js
  466. * withHttps("http://example.com"); // https://example.com
  467. * ```
  468. *
  469. * @group utils
  470. */
  471. declare function withHttps(input: string): string;
  472. /**
  473. * Removes the protocol from the input.
  474. *
  475. * @example
  476. * ```js
  477. * withoutProtocol("http://example.com"); // "example.com"
  478. * ```
  479. */
  480. declare function withoutProtocol(input: string): string;
  481. /**
  482. * Adds or Replaces protocol of the input URL.
  483. *
  484. * @example
  485. * ```js
  486. * withProtocol("http://example.com", "ftp://"); // "ftp://example.com"
  487. * ```
  488. *
  489. * @group utils
  490. */
  491. declare function withProtocol(input: string, protocol: string): string;
  492. /**
  493. * Normlizes inputed url:
  494. *
  495. * - Ensures url is properly encoded
  496. * - Ensures pathname starts with slash
  497. * - Preserves protocol/host if provided
  498. *
  499. * @example
  500. *
  501. * ```js
  502. * normalizeURL("test?query=123 123#hash, test");
  503. * // Returns "test?query=123%20123#hash,%20test"
  504. *
  505. * normalizeURL("http://localhost:3000");
  506. * // Returns "http://localhost:3000"
  507. * ```
  508. *
  509. * @group utils
  510. */
  511. declare function normalizeURL(input: string): string;
  512. /**
  513. * Resolves multiple URL segments into a single URL.
  514. *
  515. * @example
  516. *
  517. * ```js
  518. * resolveURL("http://foo.com/foo?test=123#token", "bar", "baz");
  519. * // Returns "http://foo.com/foo/bar/baz?test=123#token"
  520. * ```
  521. *
  522. * @group utils
  523. */
  524. declare function resolveURL(base?: string, ...inputs: string[]): string;
  525. /**
  526. * Check two paths are equal or not. Trailing slash and encoding are normalized before comparison.
  527. *
  528. * @example
  529. * ```js
  530. * isSamePath("/foo", "/foo/"); // true
  531. * ```
  532. *
  533. * @group utils
  534. */
  535. declare function isSamePath(p1: string, p2: string): boolean;
  536. interface CompareURLOptions {
  537. trailingSlash?: boolean;
  538. leadingSlash?: boolean;
  539. encoding?: boolean;
  540. }
  541. /**
  542. * Checks if two paths are equal regardless of encoding, trailing slash, and leading slash differences.
  543. *
  544. * You can make slash check strict by setting `{ trailingSlash: true, leadingSlash: true }` as options.
  545. *
  546. * You can make encoding check strict by setting `{ encoding: true }` as options.
  547. *
  548. * @example
  549. *
  550. * ```js
  551. * isEqual("/foo", "foo"); // true
  552. * isEqual("foo/", "foo"); // true
  553. * isEqual("/foo bar", "/foo%20bar"); // true
  554. *
  555. * // Strict compare
  556. * isEqual("/foo", "foo", { leadingSlash: true }); // false
  557. * isEqual("foo/", "foo", { trailingSlash: true }); // false
  558. * isEqual("/foo bar", "/foo%20bar", { encoding: true }); // false
  559. * ```
  560. *
  561. * @group utils
  562. */
  563. declare function isEqual(a: string, b: string, options?: CompareURLOptions): boolean;
  564. /**
  565. * Add/Replace the fragment section of the URL.
  566. *
  567. * @example
  568. *
  569. * ```js
  570. * withFragment("/foo", "bar"); // "/foo#bar"
  571. * withFragment("/foo#bar", "baz"); // "/foo#baz"
  572. * withFragment("/foo#bar", ""); // "/foo"
  573. * ```
  574. *
  575. * @group utils
  576. */
  577. declare function withFragment(input: string, hash: string): string;
  578. /**
  579. * Removes the fragment section from the URL.
  580. *
  581. * @example
  582. *
  583. * ```js
  584. * withoutFragment("http://example.com/foo?q=123#bar")
  585. * // Returns "http://example.com/foo?q=123"
  586. * ```
  587. *
  588. * @group utils
  589. */
  590. declare function withoutFragment(input: string): string;
  591. /**
  592. * Removes the host from the URL preserving everything else.
  593. *
  594. * @example
  595. * ```js
  596. * withoutHost("http://example.com/foo?q=123#bar")
  597. * // Returns "/foo?q=123#bar"
  598. * ```
  599. *
  600. * @group utils
  601. */
  602. declare function withoutHost(input: string): string;
  603. export { $URL, type HasProtocolOptions, type ParsedAuth, type ParsedHost, type ParsedQuery, type ParsedURL, type QueryObject, type QueryValue, cleanDoubleSlashes, createURL, decode, decodePath, decodeQueryKey, decodeQueryValue, encode, encodeHash, encodeHost, encodeParam, encodePath, encodeQueryItem, encodeQueryKey, encodeQueryValue, getQuery, hasLeadingSlash, hasProtocol, hasTrailingSlash, isEmptyURL, isEqual, isNonEmptyURL, isRelative, isSamePath, isScriptProtocol, joinRelativeURL, joinURL, normalizeURL, parseAuth, parseFilename, parseHost, parsePath, parseQuery, parseURL, resolveURL, stringifyParsedURL, stringifyQuery, withBase, withFragment, withHttp, withHttps, withLeadingSlash, withProtocol, withQuery, withTrailingSlash, withoutBase, withoutFragment, withoutHost, withoutLeadingSlash, withoutProtocol, withoutTrailingSlash };