summaryrefslogtreecommitdiff
path: root/ext/url/lib.deno_url.d.ts
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2021-09-08 11:14:29 +0200
committerGitHub <noreply@github.com>2021-09-08 11:14:29 +0200
commite07f28d301b990ebf534cbb8d5fa9f507475c89f (patch)
tree6a00f6a0abe0a7833a7d0feadf4e5d8c3509c12e /ext/url/lib.deno_url.d.ts
parent2de5587547247e3acdffecae1c74caf52a021580 (diff)
feat: add URLPattern API (#11941)
This adds support for the URLPattern API. The API is added in --unstable only, as it has not yet shipped in any browser. It is targeted for shipping in Chrome 95. Spec: https://wicg.github.io/urlpattern/ Co-authored-by: crowlKats < crowlkats@toaxl.com >
Diffstat (limited to 'ext/url/lib.deno_url.d.ts')
-rw-r--r--ext/url/lib.deno_url.d.ts136
1 files changed, 136 insertions, 0 deletions
diff --git a/ext/url/lib.deno_url.d.ts b/ext/url/lib.deno_url.d.ts
index df7acbe60..b2ef4095f 100644
--- a/ext/url/lib.deno_url.d.ts
+++ b/ext/url/lib.deno_url.d.ts
@@ -172,3 +172,139 @@ declare class URL {
username: string;
toJSON(): string;
}
+
+declare interface URLPatternInit {
+ protocol?: string;
+ username?: string;
+ password?: string;
+ hostname?: string;
+ port?: string;
+ pathname?: string;
+ search?: string;
+ hash?: string;
+ baseURL?: string;
+}
+
+declare type URLPatternInput = string | URLPatternInit;
+
+declare interface URLPatternComponentResult {
+ input: string;
+ groups: Record<string, string>;
+}
+
+/** `URLPatternResult` is the object returned from `URLPattern.match`. */
+declare interface URLPatternResult {
+ /** The inputs provided when matching. */
+ inputs: [URLPatternInit] | [URLPatternInit, string];
+
+ /** The matched result for the `protocol` matcher. */
+ protocol: URLPatternComponentResult;
+ /** The matched result for the `username` matcher. */
+ username: URLPatternComponentResult;
+ /** The matched result for the `password` matcher. */
+ password: URLPatternComponentResult;
+ /** The matched result for the `hostname` matcher. */
+ hostname: URLPatternComponentResult;
+ /** The matched result for the `port` matcher. */
+ port: URLPatternComponentResult;
+ /** The matched result for the `pathname` matcher. */
+ pathname: URLPatternComponentResult;
+ /** The matched result for the `search` matcher. */
+ search: URLPatternComponentResult;
+ /** The matched result for the `hash` matcher. */
+ hash: URLPatternComponentResult;
+}
+
+/**
+ * The URLPattern API provides a web platform primitive for matching URLs based
+ * on a convenient pattern syntax.
+ *
+ * The syntax is based on path-to-regexp. Wildcards, named capture groups,
+ * regular groups, and group modifiers are all supported.
+ *
+ * ```ts
+ * // Specify the pattern as structured data.
+ * const pattern = new URLPattern({ pathname: "/users/:user" });
+ * const match = pattern.match("/users/joe");
+ * console.log(match.pathname.groups.user); // joe
+ * ```
+ *
+ * ```ts
+ * // Specify a fully qualified string pattern.
+ * const pattern = new URLPattern("https://example.com/books/:id");
+ * console.log(pattern.test("https://example.com/books/123")); // true
+ * console.log(pattern.test("https://deno.land/books/123")); // false
+ * ```
+ *
+ * ```ts
+ * // Specify a relative string pattern with a base URL.
+ * const pattern = new URLPattern("/:article", "https://blog.example.com");
+ * console.log(pattern.test("https://blog.example.com/article")); // true
+ * console.log(pattern.test("https://blog.example.com/article/123")); // false
+ * ```
+ */
+declare class URLPattern {
+ constructor(input: URLPatternInput, baseURL?: string);
+
+ /**
+ * Test if the given input matches the stored pattern.
+ *
+ * The input can either be provided as a url string (with an optional base),
+ * or as individual components in the form of an object.
+ *
+ * ```ts
+ * const pattern = new URLPattern("https://example.com/books/:id");
+ *
+ * // Test a url string.
+ * console.log(pattern.test("https://example.com/books/123")); // true
+ *
+ * // Test a relative url with a base.
+ * console.log(pattern.test("/books/123", "https://example.com")); // true
+ *
+ * // Test an object of url components.
+ * console.log(pattern.test({ pathname: "/books/123" })); // true
+ * ```
+ */
+ test(input: URLPatternInput, baseURL?: string): boolean;
+
+ /**
+ * Match the given input against the stored pattern.
+ *
+ * The input can either be provided as a url string (with an optional base),
+ * or as individual components in the form of an object.
+ *
+ * ```ts
+ * const pattern = new URLPattern("https://example.com/books/:id");
+ *
+ * // Match a url string.
+ * let match = pattern.match("https://example.com/books/123");
+ * console.log(match.pathname.groups.id); // 123
+ *
+ * // Match a relative url with a base.
+ * match = pattern.match("/books/123", "https://example.com");
+ * console.log(match.pathname.groups.id); // 123
+ *
+ * // Match an object of url components.
+ * match = pattern.match({ pathname: "/books/123" });
+ * console.log(match.pathname.groups.id); // 123
+ * ```
+ */
+ exec(input: URLPatternInput, baseURL?: string): URLPatternResult | null;
+
+ /** The pattern string for the `protocol`. */
+ readonly protocol: string;
+ /** The pattern string for the `username`. */
+ readonly username: string;
+ /** The pattern string for the `password`. */
+ readonly password: string;
+ /** The pattern string for the `hostname`. */
+ readonly hostname: string;
+ /** The pattern string for the `port`. */
+ readonly port: string;
+ /** The pattern string for the `pathname`. */
+ readonly pathname: string;
+ /** The pattern string for the `search`. */
+ readonly search: string;
+ /** The pattern string for the `hash`. */
+ readonly hash: string;
+}