Skip to content

The first-party router extension for Raptor.

License

Notifications You must be signed in to change notification settings

briward/raptor-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

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

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.

Adding routes to the router

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.

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.

About

The first-party router extension for Raptor.

Topics

Resources

License

Stars

Watchers

Forks