Skip to content

Commit

Permalink
feat: proper Deno.serve support (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen authored Aug 25, 2023
1 parent 641ed3a commit 1aeb488
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
deno-version: v1.x

- name: Run deno lint
run: deno lint --unstable
run: deno lint

fmt:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
deno-version: v1.x

- name: Run deno test
run: deno test --allow-all --unstable --import-map import_map.json
run: deno test

doc:
runs-on: ubuntu-latest
Expand All @@ -57,4 +57,4 @@ jobs:
deno-version: v1.x

- name: Run deno test --doc
run: deno test --allow-all --unstable --doc --import-map import_map.json
run: deno test --doc
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ the web-standard
provide fast and easy route matching.

```ts
import { serve } from "https://deno.land/std/http/server.ts";
import { router } from "https://deno.land/x/rutt/mod.ts";

await serve(
await Deno.serve(
router({
"/": (_req) => new Response("Hello world!", { status: 200 }),
}),
);
).finished;
```

## Projects using `rutt`
Expand Down
5 changes: 2 additions & 3 deletions examples/hello_world.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { router } from "https://deno.land/x/rutt/mod.ts";

await serve(
await Deno.serve(
router({
"/": (_req) => new Response("Hello world!", { status: 200 }),
}),
);
).finished;
5 changes: 2 additions & 3 deletions examples/nested_routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { router } from "https://deno.land/x/rutt/mod.ts";

await serve(
await Deno.serve(
router({
"/hello": {
"/world": (_req) => new Response("Hello world!", { status: 200 }),
},
}),
);
).finished;
5 changes: 2 additions & 3 deletions examples/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { router } from "https://deno.land/x/rutt/mod.ts";

await serve(
await Deno.serve(
router({
"/hello/:name": (_req, _, { name }) =>
new Response(`Hello ${name}`, { status: 200 }),
}),
);
).finished;
20 changes: 9 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@
*
* @example
* ```ts
* import { serve } from "https://deno.land/std/http/server.ts";
* import { router } from "https://deno.land/x/rutt/mod.ts";
*
* await serve(
* await Deno.serve(
* router({
* "/": (_req) => new Response("Hello world!", { status: 200 }),
* }),
* );
* ).finished;
* ```
*
* @module
*/

import type { ConnInfo } from "https://deno.land/std@0.177.0/http/server.ts";

/**
* Provides arbitrary context to {@link Handler} functions along with
* {@link ConnInfo connection information}.
*/
export type HandlerContext<T = unknown> = T & ConnInfo;
export type HandlerContext<T = unknown> = T & Deno.ServeHandlerInfo;

/**
* A handler for HTTP requests. Consumes a request and {@link HandlerContext}
Expand Down Expand Up @@ -124,7 +121,7 @@ export interface RouterOptions<T> {
/**
* A known HTTP method.
*/
export type KnownMethod = typeof knownMethods[number];
export type KnownMethod = (typeof knownMethods)[number];

/**
* All known HTTP methods.
Expand Down Expand Up @@ -244,14 +241,13 @@ export function buildInternalRoutes<T = unknown>(
*
* @example
* ```ts
* import { serve } from "https://deno.land/std/http/server.ts";
* import { router } from "https://deno.land/x/rutt/mod.ts";
*
* await serve(
* await Deno.serve(
* router({
* "/": (_req) => new Response("Hello world!", { status: 200 }),
* }),
* );
* ).finished;
* ```
*
* @param routes A record of all routes and their corresponding handler functions
Expand Down Expand Up @@ -279,7 +275,9 @@ export function router<T = unknown>(
for (const { pattern, methods } of internalRoutes) {
const res = pattern.exec(req.url);
const groups = (pattern instanceof URLPattern
? (res as URLPatternResult | null)?.pathname.groups
? ((res as URLPatternResult | null)?.pathname.groups as
| Record<string, string>
| undefined)
: (res as RegExpExecArray | null)?.groups) ?? {};

for (const key in groups) {
Expand Down
10 changes: 2 additions & 8 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { ConnInfo } from "https://deno.land/std@0.177.0/http/server.ts";
import {
assert,
assertEquals,
assertIsError,
} from "https://deno.land/std@0.177.0/testing/asserts.ts";
} from "https://deno.land/std@0.200.0/assert/mod.ts";
import { router } from "./mod.ts";

const TEST_CONN_INFO: ConnInfo = {
localAddr: {
transport: "tcp",
hostname: "test",
port: 80,
},
const TEST_CONN_INFO: Deno.ServeHandlerInfo = {
remoteAddr: {
transport: "tcp",
hostname: "test",
Expand Down

0 comments on commit 1aeb488

Please sign in to comment.