url-pattern
Minified4.45 KBMinzipped1.69 KBType-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"); // undefinedReach 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-patternsh
pnpm add @monstermann/url-patternsh
yarn add @monstermann/url-patternsh
bun add @monstermann/url-pattern