https://github.com/briward/raptor-router
The first-party router extension for Raptor.
https://github.com/briward/raptor-router
deno raptor typescript
Last synced: over 1 year ago
JSON representation
The first-party router extension for Raptor.
- Host: GitHub
- URL: https://github.com/briward/raptor-router
- Owner: briward
- License: mit
- Created: 2024-10-29T15:34:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-11T18:50:23.000Z (over 1 year ago)
- Last Synced: 2025-01-08T20:22:54.905Z (over 1 year ago)
- Topics: deno, raptor, typescript
- Language: TypeScript
- Homepage:
- Size: 86.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About Raptor
See more information about the Raptor framework here: https://jsr.io/@raptor/framework.
# Usage
> [!NOTE]
> This is currently under heavy development and is not yet suitable for production use. Please proceed with caution.
## Installation
To start using the router, simply install into an existing Raptor application via the CLI or import it directly from JSR.
### Using the Deno CLI
```
deno add jsr:@raptor/router
```
### Importing with JSR
Raptor is also available to import directly via JSR:
[https://jsr.io/@raptor/router](https://jsr.io/@raptor/router)
## Usage
The built-in router operates similarly to standard Raptor middleware, enabling you to define routes using Web API standard URL patterns. For further details, visit [mozilla.org/URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern).
### Adding routes to the router
```ts
import { Kernel, Context } from "jsr:@raptor/framework";
import { Router, Route, HttpMethod } from "jsr:@raptor/router";
const app = new Kernel();
const router = new Router();
const route = new Route({
name: "index",
method: HttpMethod.GET,
pathname: "/",
handler: () => 'Hello, Dr Malcolm!'
});
router.add(route);
app.add((context: Context) => router.handler(context));
app.serve({ port: 8000 });
```
### Route parameters
Route parameter values are processed and available via the router's context object (`context.params`) if they are found in the route's pathname. Make sure to import the router's `Context` object, rather than the base Raptor `Context` object.
```ts
import { Route, Context, HttpMethod } from "jsr:@raptor/router";
/* ... */
const route = new Route({
name: "person.read",
method: HttpMethod.GET,
pathname: "/person/:name";
handler: (context: Context) => {
const { name } = context.params;
return `Hello ${name}`;
}
});
```
# License
_Copyright 2024, @briward. All rights reserved. The framework is licensed under
the MIT license._