Skip to content

url-pattern ​

Minified4.45 KBMinzipped1.69 KB

Type-safe url pattern matching.

Example ​

ts
const matchPattern = pathPattern("/api/:version{v1|v2}/users/:id");

const result = matchPattern("/api/v1/users/123");
ts
type Result =
    | undefined
    | {
          version: "v1" | "v2";
          id: string;
      };
ts
const result = { id: "123", version: "v1" };

Two functions ​

pathPattern matches the path only, and ignores the protocol and host entirely:

ts
const matchPattern = pathPattern("/:workspaceId/task/:taskId");

matchPattern("/ws1/task/t1"); // {...}
matchPattern("https://anything.com/ws1/task/t1"); // {...}

urlPattern matches the whole url, and requires a protocol and a host:

ts
const matchPattern = urlPattern("miko://:workspaceId/task/:taskId");

matchPattern("miko://ws1/task/t1"); // {...}
matchPattern("https://evil.com/ws1/task/t1"); // undefined

Reach for urlPattern whenever the origin is part of what you are asserting — deep links, webhooks, anything where a url from somewhere else must not be mistaken for one of yours.

Installation ​

sh
npm install @monstermann/url-pattern
sh
pnpm add @monstermann/url-pattern
sh
yarn add @monstermann/url-pattern
sh
bun add @monstermann/url-pattern