https://github.com/tunnckocore/urlpattern-router
An isomorphic router based on URLPattern Web spec in 180 lines (including 60 lines of types, 40 lines of comments). Works for @denoland, Bun, @nodejs, @cloudflare, Browsers, or anything that has support for URLPattern.
https://github.com/tunnckocore/urlpattern-router
Last synced: about 1 year ago
JSON representation
An isomorphic router based on URLPattern Web spec in 180 lines (including 60 lines of types, 40 lines of comments). Works for @denoland, Bun, @nodejs, @cloudflare, Browsers, or anything that has support for URLPattern.
- Host: GitHub
- URL: https://github.com/tunnckocore/urlpattern-router
- Owner: tunnckoCore
- License: apache-2.0
- Created: 2022-10-06T13:21:01.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-09T18:17:17.000Z (about 3 years ago)
- Last Synced: 2025-04-08T07:42:56.360Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern
- Size: 8.11 MB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# urlpattern-router
> Router based on [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) Web Specification, _in 150 lines_ and used in Production!
> An **isomorphic router** for **@denoland**, **@nodejs** (probably), **@cloudflare**, Browsers, or anything that has support for it!
See the examples. Briefly, the basic stuff of a router, LOL.
**Note:** You can also somehow import [urlpattern-polyfill](https://unpkg.com/urlpattern-polyfill) for environments that don't have it yet - Deno, Deno Deploy, and Cloudflare does.
```js
// Cloudflare or others
import { Router, json, ok, notFound } from "urlpattern-router";
// Or Deno: `https://ghuc.cc/tunnckoCore/urlpattern-router/index.js`
// import { Router, json, ok, notFound } from "urlpattern-router";
const router = new Router({ baseURL: "http://some-domain.com" });
router
.get("/", async () => new Response("Homepage hehe!"))
.get("/item/:id", (req, { params }) => ok(`Matching id: ${params.id}`))
.get("/api/hello", () => json({ hello: "world" }))
.get("/api/hi/:name", (req, { params: { name } }) => json({ hi: name }));
// or router._routes
console.log(router);
```
### by request method
- router.all
- router.any
- router.get
- router.post
- router.put
- router.patch
- router.delete
- router.head
- router.options
### handling
- router.addRoute - used internally for above methods
- router.match
- router.matchRequest
- router.handle - calls the handler for matching route
- router.fetch - useful for Cloudflare Workers
- router.clear - clear routes