https://github.com/korkje/rp1
Deno web framework
https://github.com/korkje/rp1
deno router typescript
Last synced: about 2 months ago
JSON representation
Deno web framework
- Host: GitHub
- URL: https://github.com/korkje/rp1
- Owner: korkje
- License: mit
- Created: 2023-08-07T05:27:47.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-30T17:32:27.000Z (over 1 year ago)
- Last Synced: 2025-02-02T17:58:41.697Z (over 1 year ago)
- Topics: deno, router, typescript
- Language: TypeScript
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# rp1 🚀
[](https://jsr.io/@korkje/rp1)
Blazingly fast and simple web framework for Deno, suitably named after the [rocket fuel](https://en.wikipedia.org/wiki/RP-1).
```ts
import Router from "jsr:@korkje/rp1";
const router = new Router();
router.get("/", () => "Hello World!");
Deno.serve(router.handle);
```
## Features
Infers parameters from the path, which is matched using [URLPattern](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API).
```ts
// 'params' is of type { id: string }
router.get("/users/:id", ({ params }) => {
const id = params.id;
// ...
});
// 'params' is of type { name: string, ext: string }
router.post("/files/:name(.+).:ext(\\w+)", ({ params }) => {
const { name, ext } = params;
// ...
});
```
Sane error handling. Thrown errors are logged (`console.error`), returned errors are not. Both get serialized to JSON. Optionally include stack trace.
```ts
import { error } from "jsr:@korkje/rp1";
// '{ "status": 418, "message": "I'm a teapot" }'
router.get("/coffee", () => {
throw error(418, "I'm a teapot");
// ^ Logged
});
// '{ "status": 402, "message": "Missing $$$", "stack": "..." }'
router.post("/cash", () => {
return error(402, "Missing $$$").expose();
// ^ Not logged ^ Include stack trace
});
```
Generates JSON for [serializable](https://www.json.org/json-en.html) values returned from handlers.
```ts
// '{ "data": [1, 2, 3] }' with a 200 status code
router.get("/json", () => ({ data: [1, 2, 3] }));
```
You can return `Response` objects. Note: headers already set on the the context's `response` object (e.g. by cors middleware) will be appended to the returned `Response` object's headers.
```ts
// 'Hello World!'
router.get("/hello", () => new Response("Hello World!"));
// 'Hello World!' with header
router.get("/hello", ({ response }) => {
response.headers.set("X-Greeting", "Hello World!");
// Will be appended to the returned Response object's headers
return new Response("Hello World!");
});
```
Easy to use middleware.
```ts
// Simple request logger
router.use(async ({ request }, next) => {
const method = request.method;
const url = new URL(request.url);
const path = url.pathname;
console.log(`${method} ${path}`);
await next();
});
```
CORS middleware included.
```ts
import { cors } from "jsr:@korkje/rp1";
router.use(cors());
```
...which is easily configurable.
```ts
import cors, { echo } from "jsr:@korkje/rp1/middleware/cors";
router.use(cors({
// Skip CORS for public paths
skip: ({ request }) => {
const url = new URL(request.url);
return url.pathname.startsWith("/public/");
},
origin: echo,
methods: ["GET", "POST"],
headers: "Content-Type",
// ...
}));
```
Supports sub-routes.
```ts
router.sub("/stats")
.get("/users", () => users.length) // GET /stats/users
.get("/posts", () => posts.length); // GET /stats/posts
```
Uses Deno's native `Request` object, so WebSockets are supported out of the box.
```ts
router.get("/ws", ({ request }) => {
const { socket, response } = Deno.upgradeWebSocket(request);
socket.onopen = () => {
console.log("Socket opened!");
};
// ...
return response;
});
```