Skip to content

Repository files navigation

url-pattern

MinifiedMinzipped

Type-safe url pattern matching.

Documentation

Example

constmatchPattern=pathPattern("/api/:version{v1|v2}/users/:id");constresult=matchPattern("/api/v1/users/123");
typeResult=|undefined|{version: "v1"|"v2";id: string;};
constresult={id: "123",version: "v1"};

Two functions

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

constmatchPattern=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:

constmatchPattern=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

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

About

Type-safe url pattern matching.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages