Skip to content

Commit 2a36898

Browse files
committed
feat: allow any state, make Partial<> internally
Signed-off-by: Muthu Kumar <[email protected]>
1 parent 4ef83c5 commit 2a36898

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

serve/core.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Context<State = {}> = {
1010
respond: (body?: Response | BodyInit | null, init?: ResponseInit) => Promise<void>;
1111
html: (body: string, init?: ResponseInit) => Promise<void>;
1212
render: (body: HyperNode, init?: ResponseInit) => Promise<void>;
13-
state: State;
13+
state: Partial<State>;
1414
};
1515

1616
export function o<State = {}>(
@@ -20,7 +20,8 @@ export function o<State = {}>(
2020
return (ctx: Context<State>, next: Next) => g(ctx, () => f(ctx, next));
2121
}
2222

23-
export function router<State = {}>(...fs: Middleware<State>[]) {
23+
export function router<State = {}>(...fs: Middleware<State>[]): Middleware<State> {
24+
if (fs.length === 0) throw new TypeError("router requires at least one Middleware");
2425
return fs.reduceRight(o);
2526
}
2627

@@ -61,7 +62,7 @@ function Context(e: Deno.RequestEvent): Context {
6162

6263
export const noop = async (): Promise<void> => void 0;
6364

64-
export function serve(opts: Deno.ListenOptions, handler: Middleware) {
65+
export function serve<State>(opts: Deno.ListenOptions, handler: Middleware<State>) {
6566
async function handleHttp(conn: Deno.Conn) {
6667
for await (const e of Deno.serveHttp(conn)) {
6768
const ctx = Context(e);

serve/methods.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ export const post = method("POST");
2525
export const put = method("PUT");
2626
export const patch = method("PATCH");
2727
export const del = method("DELETE");
28+
29+
export const use = router;

version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const version = "2.0.0-alpha.10";
1+
export const version = "2.0.0-alpha.11";

0 commit comments

Comments
 (0)