@monstermann/url-pattern
v0.1.0
Published
Type-safe url pattern matching.
Readme
Type-safe url pattern matching.
Example
const matchPattern = pathPattern("/api/:version{v1|v2}/users/:id");
const result = matchPattern("/api/v1/users/123");type Result =
| undefined
| {
version: "v1" | "v2";
id: string;
};const result = { id: "123", version: "v1" };Two functions
pathPattern matches the path only, and ignores the protocol and host entirely:
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:
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
npm install @monstermann/url-patternpnpm add @monstermann/url-patternyarn add @monstermann/url-patternbun add @monstermann/url-pattern