Skip to content

Commit

Permalink
Move router out of framework and into a first-party module.
Browse files Browse the repository at this point in the history
  • Loading branch information
briward committed Oct 29, 2024
1 parent 810ddbc commit c1fb407
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 358 deletions.
64 changes: 8 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="./assets//logo.svg" width="300" />
<img src="./assets//logo.png" width="300" />
</p>

<p align="center">
Expand All @@ -11,7 +11,7 @@

# About Raptor

Raptor is a container-based middleware framework written for use with Deno.
Raptor is a modular, container-based middleware framework written for use with Deno.

This framework is heavily inspired by many who came before it, such as Oak,
Express and Slim Framework in PHP.
Expand Down Expand Up @@ -69,66 +69,18 @@ The context object is provided as the first parameter of a middleware handler fu
}
```

## Using the built-in router middleware

The built-in router works in the same way as regular middleware, allowing you to
pass in routes using Web API standard URL patterns. See
[mozilla.org/URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern)
for more information.

### Adding routes to the router

```ts
import {
Kernel,
type Context,
Router,
Route
} from "jsr:@raptor/framework";

const app = new Kernel();

const router = new Router();

const route = new Route({
name: "person.read",
method: "GET",
pathname: new URLPattern("/person/:name");
handler: (context: Context) => {
const { name } = context.params;

context.response.body = `Hello ${name}`;
}
});

router.add(route);

app.add(router);
```

### Route parameters

Route parameters are processed and available via Context (`context.params`) if
they are present in the URLPattern pathname.

### Returning JSON responses

If a JSON object is assigned to the response body then the response content-type will be automatically set to `application/json`. You can override the header by manually assigning it within a middleware callback as follows:

```ts
app.add({
handler: (context: Context) => {
context.response.headers.set('content-type', 'text/plain');
}
});
```

## Error handling

Errors are caught and returned in the response object. If the JSON content-type
is set within a middleware callback, all errors thrown in subsequent callbacks
will respond with JSON, by design.

# Available modules

Raptor is designed to be an unopinionated modular framework, allowing you to decide what batteries you wish to include or not. Here is a list of the first-party modules available:

* Router middleware: [https://jsr.io/@raptor/router](https://jsr.io/@raptor/router)

# License

_Copyright 2024, @briward. All rights reserved. The framework is licensed under
Expand Down
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raptor/framework",
"version": "0.1.0",
"version": "0.2.0",
"exports": "./mod.ts",
"publish": {
"exclude": [
Expand Down
6 changes: 1 addition & 5 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import "npm:reflect-metadata@0.2.2";

import Kernel from "./src/kernel.ts";
import Route from "./src/routing/route.ts";
import Request from "./src/http/request.ts";
import Router from "./src/routing/router.ts";
import Response from "./src/http/response.ts";
import NotFound from "./src/error/not-found.ts";
import BadRequest from "./src/error/bad-request.ts";
Expand All @@ -14,15 +12,13 @@ import ServerError from "./src/error/server-error.ts";
// Export all available interfaces/types.
export type { Error } from "./src/error/interfaces/error.ts";
export type { Context } from "./src/http/interfaces/context.ts";
export type { RouteOptions } from "./src/routing/interfaces/route-options.ts";
export type { Middleware } from "./src/http/interfaces/middleware.ts";

export {
BadRequest,
Kernel,
NotFound,
Request,
Response,
Route,
Router,
ServerError,
};
2 changes: 1 addition & 1 deletion src/http/interfaces/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type Context from "../context.ts";
/**
* The middleware definition.
*/
export default interface Middleware {
export interface Middleware {
/**
* The handler method for middleware.
*
Expand Down
2 changes: 1 addition & 1 deletion src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import HttpResponse from "./http/response.ts";
import TypeError from "./error/type-error.ts";

import type { Error } from "./error/interfaces/error.ts";
import type Middleware from "./http/interfaces/middleware.ts";
import type { Middleware } from "./http/interfaces/middleware.ts";
import { container, type DependencyContainer } from "npm:tsyringe@^4.8.0";

/**
Expand Down
24 changes: 0 additions & 24 deletions src/routing/interfaces/route-options.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/routing/route.ts

This file was deleted.

109 changes: 0 additions & 109 deletions src/routing/router.ts

This file was deleted.

Loading

0 comments on commit c1fb407

Please sign in to comment.