index.d.cts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. * The 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. * Encodes characters that need to be encoded in 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. * Encodes characters that need to be encoded in 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. * Encodes characters that need to be encoded for query values in 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. * Encodes characters that need to be encoded for query values in 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. * Encodes characters that need to be encoded in 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. * Encodes characters that need to be encoded in 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. * Decodes 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. * Decodes 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. * Decodes 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. * @param [input] - The URL to parse.
  237. * @param [opts] - Options to use while parsing
  238. */
  239. declare function parseFilename(input?: string, opts?: {
  240. strict?: boolean;
  241. }): string | undefined;
  242. /**
  243. * @deprecated use native URL with `new URL(input)` or `ufo.parseURL(input)`
  244. */
  245. declare class $URL implements URL {
  246. protocol: string;
  247. host: string;
  248. auth: string;
  249. pathname: string;
  250. query: QueryObject;
  251. hash: string;
  252. constructor(input?: string);
  253. get hostname(): string;
  254. get port(): string;
  255. get username(): string;
  256. get password(): string;
  257. get hasProtocol(): number;
  258. get isAbsolute(): number | boolean;
  259. get search(): string;
  260. get searchParams(): URLSearchParams;
  261. get origin(): string;
  262. get fullpath(): string;
  263. get encodedAuth(): string;
  264. get href(): string;
  265. append(url: $URL): void;
  266. toJSON(): string;
  267. toString(): string;
  268. }
  269. /**
  270. * @deprecated use native URL with `new URL(input)` or `ufo.parseURL(input)`
  271. */
  272. declare function createURL(input: string): $URL;
  273. /**
  274. * Check if a path starts with `./` or `../`.
  275. *
  276. * @example
  277. * ```js
  278. * isRelative("./foo"); // true
  279. * ```
  280. *
  281. * @group utils
  282. */
  283. declare function isRelative(inputString: string): boolean;
  284. interface HasProtocolOptions {
  285. acceptRelative?: boolean;
  286. strict?: boolean;
  287. }
  288. /**
  289. * Checks if the input has a protocol.
  290. *
  291. * You can use `{ acceptRelative: true }` to accept relative URLs as valid protocol.
  292. *
  293. * @group utils
  294. */
  295. declare function hasProtocol(inputString: string, opts?: HasProtocolOptions): boolean;
  296. /** @deprecated Same as { hasProtocol(inputString, { acceptRelative: true }) */
  297. declare function hasProtocol(inputString: string, acceptRelative: boolean): boolean;
  298. /**
  299. * Checks if the input protocol is any of the dangerous `blob:`, `data:`, `javascript`: or `vbscript:` protocols.
  300. *
  301. * @group utils
  302. */
  303. declare function isScriptProtocol(protocol?: string): boolean;
  304. /**
  305. * Checks if the input has a trailing slash.
  306. *
  307. * @group utils
  308. */
  309. declare function hasTrailingSlash(input?: string, respectQueryAndFragment?: boolean): boolean;
  310. /**
  311. * Removes the trailing slash from the URL or pathname.
  312. *
  313. * 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.
  314. *
  315. * @example
  316. *
  317. * ```js
  318. * withoutTrailingSlash("/foo/"); // "/foo"
  319. *
  320. * withoutTrailingSlash("/path/?query=true", true); // "/path?query=true"
  321. * ```
  322. *
  323. * @group utils
  324. */
  325. declare function withoutTrailingSlash(input?: string, respectQueryAndFragment?: boolean): string;
  326. /**
  327. * Ensures the URL ends with a trailing slash.
  328. *
  329. * If second 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.
  330. *
  331. * @example
  332. *
  333. * ```js
  334. * withTrailingSlash("/foo"); // "/foo/"
  335. *
  336. * withTrailingSlash("/path?query=true", true); // "/path/?query=true"
  337. * ```
  338. *
  339. * @group utils
  340. */
  341. declare function withTrailingSlash(input?: string, respectQueryAndFragment?: boolean): string;
  342. /**
  343. * Checks if the input has a leading slash (e.g. `/foo`).
  344. *
  345. * @group utils
  346. */
  347. declare function hasLeadingSlash(input?: string): boolean;
  348. /**
  349. * Removes leading slash from the URL or pathname.
  350. *
  351. * @group utils
  352. */
  353. declare function withoutLeadingSlash(input?: string): string;
  354. /**
  355. * Ensures the URL or pathname has a leading slash.
  356. *
  357. * @group utils
  358. */
  359. declare function withLeadingSlash(input?: string): string;
  360. /**
  361. * Removes double slashes from the URL.
  362. *
  363. * @example
  364. *
  365. * ```js
  366. * cleanDoubleSlashes("//foo//bar//"); // "/foo/bar/"
  367. *
  368. * cleanDoubleSlashes("http://example.com/analyze//http://localhost:3000//");
  369. * // Returns "http://example.com/analyze/http://localhost:3000/"
  370. * ```
  371. *
  372. * @group utils
  373. */
  374. declare function cleanDoubleSlashes(input?: string): string;
  375. /**
  376. * Ensures the URL or pathname starts with base.
  377. *
  378. * If input already starts with base, it will not be added again.
  379. *
  380. * @group utils
  381. */
  382. declare function withBase(input: string, base: string): string;
  383. /**
  384. * Removes the base from the URL or pathname.
  385. *
  386. * If input does not start with base, it will not be removed.
  387. *
  388. * @group utils
  389. */
  390. declare function withoutBase(input: string, base: string): string;
  391. /**
  392. * Add/Replace the query section of the URL.
  393. *
  394. * @example
  395. *
  396. * ```js
  397. * withQuery("/foo?page=a", { token: "secret" }); // "/foo?page=a&token=secret"
  398. * ```
  399. *
  400. * @group utils
  401. */
  402. declare function withQuery(input: string, query: QueryObject): string;
  403. /**
  404. * Removes the query section of the URL.
  405. *
  406. * @example
  407. *
  408. * ```js
  409. * filterQuery("/foo?bar=1&baz=2", (key) => key !== "bar"); // "/foo?baz=2"
  410. * ```
  411. *
  412. * @group utils
  413. */
  414. declare function filterQuery(input: string, predicate: (key: string, value: string | string[]) => boolean): string;
  415. /**
  416. * Parses and decodes the query object of an input URL into an object.
  417. *
  418. * @example
  419. *
  420. * ```js
  421. * getQuery("http://foo.com/foo?test=123&unicode=%E5%A5%BD");
  422. * // { test: "123", unicode: "好" }
  423. * ```
  424. * @group utils
  425. */
  426. declare function getQuery<T extends ParsedQuery = ParsedQuery>(input: string): T;
  427. /**
  428. * Checks if the input URL is empty or `/`.
  429. *
  430. * @group utils
  431. */
  432. declare function isEmptyURL(url: string): boolean;
  433. /**
  434. * Checks if the input URL is neither empty nor `/`.
  435. *
  436. * @group utils
  437. */
  438. declare function isNonEmptyURL(url: string): boolean;
  439. /**
  440. * Joins multiple URL segments into a single URL.
  441. *
  442. * @example
  443. *
  444. * ```js
  445. * joinURL("a", "/b", "/c"); // "a/b/c"
  446. * ```
  447. *
  448. * @group utils
  449. */
  450. declare function joinURL(base: string, ...input: string[]): string;
  451. /**
  452. * Joins multiple URL segments into a single URL and also handles relative paths with `./` and `../`.
  453. *
  454. * @example
  455. *
  456. * ```js
  457. * joinRelativeURL("/a", "../b", "./c"); // "/b/c"
  458. * ```
  459. *
  460. * @group utils
  461. */
  462. declare function joinRelativeURL(..._input: string[]): string;
  463. /**
  464. * Adds or replaces the URL protocol to `http://`.
  465. *
  466. * @example
  467. *
  468. * ```js
  469. * withHttp("https://example.com"); // http://example.com
  470. * ```
  471. *
  472. * @group utils
  473. */
  474. declare function withHttp(input: string): string;
  475. /**
  476. * Adds or replaces the URL protocol to `https://`.
  477. *
  478. * @example
  479. *
  480. * ```js
  481. * withHttps("http://example.com"); // https://example.com
  482. * ```
  483. *
  484. * @group utils
  485. */
  486. declare function withHttps(input: string): string;
  487. /**
  488. * Removes the protocol from the input.
  489. *
  490. * @example
  491. * ```js
  492. * withoutProtocol("http://example.com"); // "example.com"
  493. * ```
  494. */
  495. declare function withoutProtocol(input: string): string;
  496. /**
  497. * Adds or replaces protocol of the input URL.
  498. *
  499. * @example
  500. * ```js
  501. * withProtocol("http://example.com", "ftp://"); // "ftp://example.com"
  502. * ```
  503. *
  504. * @group utils
  505. */
  506. declare function withProtocol(input: string, protocol: string): string;
  507. /**
  508. * Normalizes the input URL:
  509. *
  510. * - Ensures the URL is properly encoded
  511. * - Ensures pathname starts with a slash
  512. * - Preserves protocol/host if provided
  513. *
  514. * @example
  515. *
  516. * ```js
  517. * normalizeURL("test?query=123 123#hash, test");
  518. * // Returns "test?query=123%20123#hash,%20test"
  519. *
  520. * normalizeURL("http://localhost:3000");
  521. * // Returns "http://localhost:3000"
  522. * ```
  523. *
  524. * @group utils
  525. */
  526. declare function normalizeURL(input: string): string;
  527. /**
  528. * Resolves multiple URL segments into a single URL.
  529. *
  530. * @example
  531. *
  532. * ```js
  533. * resolveURL("http://foo.com/foo?test=123#token", "bar", "baz");
  534. * // Returns "http://foo.com/foo/bar/baz?test=123#token"
  535. * ```
  536. *
  537. * @group utils
  538. */
  539. declare function resolveURL(base?: string, ...inputs: string[]): string;
  540. /**
  541. * Check if two paths are equal or not. Trailing slash and encoding are normalized before comparison.
  542. *
  543. * @example
  544. * ```js
  545. * isSamePath("/foo", "/foo/"); // true
  546. * ```
  547. *
  548. * @group utils
  549. */
  550. declare function isSamePath(p1: string, p2: string): boolean;
  551. interface CompareURLOptions {
  552. trailingSlash?: boolean;
  553. leadingSlash?: boolean;
  554. encoding?: boolean;
  555. }
  556. /**
  557. * Checks if two paths are equal regardless of encoding, trailing slash, and leading slash differences.
  558. *
  559. * You can make slash check strict by setting `{ trailingSlash: true, leadingSlash: true }` as options.
  560. *
  561. * You can make encoding check strict by setting `{ encoding: true }` as options.
  562. *
  563. * @example
  564. *
  565. * ```js
  566. * isEqual("/foo", "foo"); // true
  567. * isEqual("foo/", "foo"); // true
  568. * isEqual("/foo bar", "/foo%20bar"); // true
  569. *
  570. * // Strict compare
  571. * isEqual("/foo", "foo", { leadingSlash: true }); // false
  572. * isEqual("foo/", "foo", { trailingSlash: true }); // false
  573. * isEqual("/foo bar", "/foo%20bar", { encoding: true }); // false
  574. * ```
  575. *
  576. * @group utils
  577. */
  578. declare function isEqual(a: string, b: string, options?: CompareURLOptions): boolean;
  579. /**
  580. * Adds or replaces the fragment section of the URL.
  581. *
  582. * @example
  583. *
  584. * ```js
  585. * withFragment("/foo", "bar"); // "/foo#bar"
  586. * withFragment("/foo#bar", "baz"); // "/foo#baz"
  587. * withFragment("/foo#bar", ""); // "/foo"
  588. * ```
  589. *
  590. * @group utils
  591. */
  592. declare function withFragment(input: string, hash: string): string;
  593. /**
  594. * Removes the fragment section from the URL.
  595. *
  596. * @example
  597. *
  598. * ```js
  599. * withoutFragment("http://example.com/foo?q=123#bar")
  600. * // Returns "http://example.com/foo?q=123"
  601. * ```
  602. *
  603. * @group utils
  604. */
  605. declare function withoutFragment(input: string): string;
  606. /**
  607. * Removes the host from the URL while preserving everything else.
  608. *
  609. * @example
  610. * ```js
  611. * withoutHost("http://example.com/foo?q=123#bar")
  612. * // Returns "/foo?q=123#bar"
  613. * ```
  614. *
  615. * @group utils
  616. */
  617. declare function withoutHost(input: string): string;
  618. export { $URL, cleanDoubleSlashes, createURL, decode, decodePath, decodeQueryKey, decodeQueryValue, encode, encodeHash, encodeHost, encodeParam, encodePath, encodeQueryItem, encodeQueryKey, encodeQueryValue, filterQuery, 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 };
  619. export type { HasProtocolOptions, ParsedAuth, ParsedHost, ParsedQuery, ParsedURL, QueryObject, QueryValue };