{"id":19435727,"url":"https://github.com/dbushell/velocirouter","last_synced_at":"2025-04-24T21:30:41.589Z","repository":{"id":203887802,"uuid":"710608324","full_name":"dbushell/velocirouter","owner":"dbushell","description":"A minimal async Request → Response router powered by URL Pattern API magic ✨","archived":false,"fork":false,"pushed_at":"2024-10-09T07:26:37.000Z","size":51,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-09T09:22:37.657Z","etag":null,"topics":["async","deno","javascript","router","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dbushell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-27T03:55:52.000Z","updated_at":"2024-10-09T07:26:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"1add2068-accc-4a9b-b1be-d35a51a81dfb","html_url":"https://github.com/dbushell/velocirouter","commit_stats":null,"previous_names":["dbushell/velocirouter"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbushell%2Fvelocirouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbushell%2Fvelocirouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbushell%2Fvelocirouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbushell%2Fvelocirouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dbushell","download_url":"https://codeload.github.com/dbushell/velocirouter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223968382,"owners_count":17233445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","deno","javascript","router","typescript"],"created_at":"2024-11-10T15:07:48.563Z","updated_at":"2024-11-10T15:07:49.185Z","avatar_url":"https://github.com/dbushell.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🦕 VelociRouter\n\n[![JSR](https://jsr.io/badges/@ssr/velocirouter?labelColor=98e6c8\u0026color=333)](https://jsr.io/@ssr/velocirouter) [![NPM](https://img.shields.io/npm/v/@dbushell/velocirouter?labelColor=98e6c8\u0026color=333)](https://www.npmjs.com/package/@dbushell/velocirouter)\n\nA minimal async `Request` → `Response` router powered by [URL Pattern API](https://urlpattern.spec.whatwg.org/) magic ✨\n\n## Usage\n\nRoute handles are attached using an [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) name:\n\n```javascript\nconst router = new Router();\n\nrouter.get('*', () =\u003e {\n  return new Response('Hello, World!');\n});\n```\n\n`Router` method names like `get` and `post` are lower case.\n\nRequests are passed through **all matching handles** in the order they were attached.\n\nThe first parameter is a [URL Pattern API](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API) input. String inputs are matched against the URL pathname. Object inputs are used to match parts of the URL. The second parameter is a handle function.\n\n```javascript\nrouter.get({pathname: '/hello/:name'}, ({match}) =\u003e {\n  const {name} = match.pathname.groups;\n  return new Response(`Hello ${name}!`);\n});\n```\n\nFor fastest performance provide a full `URLPattern` instance:\n\n```javascript\nrouter.get(new URLPattern({pathname: '/:slug([\\w-]+)'}), () =\u003e {\n  // Pattern matches [a-zA-Z0-9_-]\n});\n```\n\n## Options\n\nThe `Router` class accepts the following configuration:\n\n```javascript\nconst router = new Router({\n  onError: (error, request) =\u003e {\n    console.error(error);\n    return new Response(null {status: 500});\n  },\n  onNoMatch: (request) =\u003e {\n    return new Response(null, {status: 404});\n  },\n  autoHead: false\n});\n```\n\n`onError` - a fallback handle when an error is thrown. Default is a 500 response.\n\n`onNoMatch` - a fallback handle when **no response** is returned from any matching routes. Default is a 404 response.\n\n`autoHead` - automatically generate corresponding [`HEAD`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) handles for any `GET` handles attached. Default is `true`.\n\n## Handle Functions\n\nThe handle function receives a `props` object as the only argument.\n\nThe `props` object includes:\n\n* `request` - the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object matching the route pattern\n* `response` - the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object returned by a previous handle (or `undefined`)\n* `match` - the `URLPatternResult`\n* `platform` - any platform specific data\n* `stopPropagation` - a function to stop any further handles being called\n\n### Handle Return Values\n\nIf the handle returns `void` or `undefined` it has no effect on the route. Any previous handle's `Response` is used.\n\nIf the handle returns `null` any previous handles are ignored. The route will be handled by `onNoMatch` unless any following handles exist.\n\nIf the handles returns a `Response` that becomes the route's response unless any following handles have an effect.\n\nHandles can return an object: `{request, response}`. The `request` property changes the routes `Request` passed to any following handles. The optional `response` property follows the same rules above.\n\nIf an uncaught error is thrown inside a handle the `onError` option is used.\n\n## Middleware\n\nMiddleware is added with the `use` method:\n\n```javascript\nrouter.use(({request}) =\u003e {\n  console.log(`[${request.method}] ${request.url}`);\n});\n```\n\nHandles attached with `use` match **all requests**. They are executed in order **before** all other route handles.\n\nA special `all` handle will match all HTTP methods with a pattern:\n\n```javascript\nrouter.all({pathname: '*'}, ({response}) =\u003e {\n  if (response) {\n    response.headers.set('x-powered-by', 'velocirouter');\n  }\n});\n```\n\nHandles attached with `all` are executed in order alongside route handles **after** any middleware.\n\n## Notes\n\nOnly Deno and Chromium based browsers have [URL Pattern API support](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern) right now. Other runtimes like Bun and Node require a [polyfill](https://github.com/kenchris/urlpattern-polyfill).\n\nInspired by [Polka](https://github.com/lukeed/polka) and [Hono](https://github.com/honojs).\n\n* * *\n\n[MIT License](/LICENSE) | Copyright © 2024 [David Bushell](https://dbushell.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbushell%2Fvelocirouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdbushell%2Fvelocirouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbushell%2Fvelocirouter/lists"}