{"id":13930226,"url":"https://github.com/berstend/tiny-request-router","last_synced_at":"2025-04-05T02:09:20.170Z","repository":{"id":35796538,"uuid":"219340802","full_name":"berstend/tiny-request-router","owner":"berstend","description":":rocket: Fast, generic and type safe router (match request method and path).","archived":false,"fork":false,"pushed_at":"2024-05-31T01:25:20.000Z","size":755,"stargazers_count":202,"open_issues_count":25,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T01:11:14.865Z","etag":null,"topics":["cloudflare-workers","request","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/berstend.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":"2019-11-03T17:45:36.000Z","updated_at":"2025-02-11T21:34:47.000Z","dependencies_parsed_at":"2024-06-18T16:56:10.541Z","dependency_job_id":null,"html_url":"https://github.com/berstend/tiny-request-router","commit_stats":{"total_commits":25,"total_committers":3,"mean_commits":8.333333333333334,"dds":"0.16000000000000003","last_synced_commit":"781f569f2a8ffe4c47a2de43c09774cfc14de07d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/berstend%2Ftiny-request-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/berstend%2Ftiny-request-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/berstend%2Ftiny-request-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/berstend%2Ftiny-request-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/berstend","download_url":"https://codeload.github.com/berstend/tiny-request-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276164,"owners_count":20912288,"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":["cloudflare-workers","request","router","typescript"],"created_at":"2024-08-07T18:05:17.268Z","updated_at":"2025-04-05T02:09:20.139Z","avatar_url":"https://github.com/berstend.png","language":"TypeScript","readme":"# tiny-request-router [![ ](https://travis-ci.org/berstend/tiny-request-router.svg?branch=master)](https://travis-ci.org/berstend/tiny-request-router) [![ ](https://img.shields.io/npm/v/tiny-request-router.svg)](https://www.npmjs.com/package/tiny-request-router)\n\n\u003e Fast, generic and type safe router (match request method and path).\n\n## Features\n\n- Minimal and opinionless router, can be used in any script and environment.\n- Matches a request method (e.g. `GET`) and a path (e.g. `/foobar`) against a list of routes\n- Uses [path-to-regexp](https://github.com/pillarjs/path-to-regexp), which is used by express and therefore familiar\n- Allows wildcards (e.g. `/user/(.*)/age`) and named parameters (e.g. `/info/:username/:age`)\n- Will not call your handlers automatically, as it only cares about matching\n- Battle hardened in production ([Cloudflare Worker](https://www.cloudflare.com/products/cloudflare-workers/) with 10M requests per day)\n- No magic, no assumptions, no fluff, type safe, tested\n\n### Route testing\n\n- You can use the [Express Route Tester](https://forbeslindesay.github.io/express-route-tester/) (select `2.0.0`) to debug your path patterns quickly\n\n## Installation\n\n```bash\nyarn add tiny-request-router\n# or\nnpm install --save tiny-request-router\n```\n\n## Usage (JavaScript/TypeScript)\n\n```typescript\nimport { Router } from 'tiny-request-router'\n// NodeJS: const { Router } = require('tiny-request-router')\n\nconst router = new Router()\n\nrouter\n  .get('/(v1|v2)/:name/:age', 'foo1')\n  .get('/info/(.*)/export', 'foo2')\n  .post('/upload/user', 'foo3')\n\nconst match1 = router.match('GET', '/v1/')\n// =\u003e null\n\nconst match2 = router.match('GET', '/v1/bob/22')\n// =\u003e { handler: 'foo1', params: { name: 'bob', age: '22' }, ... }\n```\n\n### Make your handlers type safe (TypeScript)\n\n```typescript\nimport { Router, Method, Params } from 'tiny-request-router'\n\n// Let the router know that handlers are async functions returning a Response\ntype Handler = (params: Params) =\u003e Promise\u003cResponse\u003e\n\nconst router = new Router\u003cHandler\u003e()\nrouter.all('*', async () =\u003e new Response('Hello'))\n\nconst match = router.match('GET' as Method, '/foobar')\nif (match) {\n  // Call the async function of that match\n  const response = await match.handler()\n  console.log(response) // =\u003e Response('Hello')\n}\n```\n\n## Example: Cloudflare Workers (JavaScript)\n\n_Use something like [wrangler](https://github.com/cloudflare/wrangler) to bundle the router with your worker code._\n\n```js\nimport { Router } from 'tiny-request-router'\n\nconst router = new Router()\nrouter.get('/worker', async () =\u003e new Response('Hi from worker!'))\nrouter.get('/hello/:name', async params =\u003e new Response(`Hello ${params.name}!`))\nrouter.post('/test', async () =\u003e new Response('Post received!'))\n\n// Main entry point in workers\naddEventListener('fetch', event =\u003e {\n  const request = event.request\n  const { pathname } = new URL(request.url)\n\n  const match = router.match(request.method, pathname)\n  if (match) {\n    event.respondWith(match.handler(match.params))\n  }\n})\n```\n\n---\n\n## API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n#### Table of Contents\n\n- [Method()](#method)\n- [RouteOptions()](#routeoptions)\n- [RouteMatch()](#routematch)\n- [class: Router](#class-router)\n  - [.routes](#routes)\n  - [.all(path, handler, options)](#allpath-handler-options)\n  - [.get(path, handler, options)](#getpath-handler-options)\n  - [.post(path, handler, options)](#postpath-handler-options)\n  - [.put(path, handler, options)](#putpath-handler-options)\n  - [.patch(path, handler, options)](#patchpath-handler-options)\n  - [.delete(path, handler, options)](#deletepath-handler-options)\n  - [.head(path, handler, options)](#headpath-handler-options)\n  - [.options(path, handler, options)](#optionspath-handler-options)\n  - [.match(method, path)](#matchmethod-path)\n\n### [Method()](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L6-L6)\n\nType: **(`\"GET\"` \\| `\"POST\"` \\| `\"PUT\"` \\| `\"PATCH\"` \\| `\"DELETE\"` \\| `\"HEAD\"` \\| `\"OPTIONS\"`)**\n\nValid HTTP methods for matching.\n\n---\n\n### [RouteOptions()](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L39-L39)\n\n**Extends: TokensToRegexpOptions**\n\nOptional route options.\n\nExample:\n\n```javascript\n// When `true` the regexp will be case sensitive. (default: `false`)\nsensitive?: boolean;\n\n// When `true` the regexp allows an optional trailing delimiter to match. (default: `false`)\nstrict?: boolean;\n\n// When `true` the regexp will match to the end of the string. (default: `true`)\nend?: boolean;\n\n// When `true` the regexp will match from the beginning of the string. (default: `true`)\nstart?: boolean;\n\n// Sets the final character for non-ending optimistic matches. (default: `/`)\ndelimiter?: string;\n\n// List of characters that can also be \"end\" characters.\nendsWith?: string;\n\n// Encode path tokens for use in the `RegExp`.\nencode?: (value: string) =\u003e string;\n```\n\n---\n\n### [RouteMatch()](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L67-L70)\n\n**Extends: Route\u0026lt;HandlerType\u003e**\n\nThe object returned when a route matches.\n\nThe handler can then be used to execute the relevant function.\n\nExample:\n\n```javascript\n{\n  params: Params\n  matches?: RegExpExecArray\n  method: Method | MethodWildcard\n  path: string\n  regexp: RegExp\n  options: RouteOptions\n  keys: Keys\n  handler: HandlerType\n}\n```\n\n---\n\n### class: [Router](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L86-L168)\n\nTiny request router. Allows overloading of handler type to be fully type safe.\n\nExample:\n\n```javascript\nimport { Router, Method, Params } from 'tiny-request-router'\n\n// Let the router know that handlers are async functions returning a Response\ntype Handler = (params: Params) =\u003e Promise\u003cResponse\u003e\n\nconst router = new Router\u003cHandler\u003e()\n```\n\n---\n\n#### .[routes](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L88-L88)\n\nList of all registered routes.\n\n---\n\n#### .[all(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L91-L93)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches any method.\n\n---\n\n#### .[get(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L95-L97)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the GET method.\n\n---\n\n#### .[post(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L99-L101)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the POST method.\n\n---\n\n#### .[put(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L103-L105)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the PUT method.\n\n---\n\n#### .[patch(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L107-L109)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the PATCH method.\n\n---\n\n#### .[delete(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L111-L113)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the DELETE method.\n\n---\n\n#### .[head(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L115-L117)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the HEAD method.\n\n---\n\n#### .[options(path, handler, options)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L119-L121)\n\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n- `handler` **HandlerType**\n- `options` **[RouteOptions](#routeoptions)** (optional, default `{}`)\n\nAdd a route that matches the OPTIONS method.\n\n---\n\n#### .[match(method, path)](https://github.com/berstend/tiny-request-router/blob/5e7d69be1e37a6d2d14c611efe77ba2ef6ea9f83/src/router.ts#L135-L152)\n\n- `method` **[Method](#method)**\n- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**\n\nReturns: **([RouteMatch](#routematch)\u0026lt;HandlerType\u003e | null)**\n\nMatch the provided method and path against the list of registered routes.\n\nExample:\n\n```javascript\nrouter.get('/foobar', async () =\u003e new Response('Hello'))\n\nconst match = router.match('GET', '/foobar')\nif (match) {\n  // Call the async function of that match\n  const response = await match.handler()\n  console.log(response) // =\u003e Response('Hello')\n}\n```\n\n---\n\n## More info\n\nPlease check out the [tiny source code](src/router.ts) or [tests](test/functionality.ts) for more info.\n\n## License\n\nMIT\n","funding_links":[],"categories":["typescript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberstend%2Ftiny-request-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fberstend%2Ftiny-request-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberstend%2Ftiny-request-router/lists"}