Skip to content

Commit d643aac

Browse files
allow passing generics onto Hono (#262)
1 parent 7e2f971 commit d643aac

File tree

7 files changed

+40
-19
lines changed

7 files changed

+40
-19
lines changed

lib/deps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export { StringReader } from "https://deno.land/std@0.176.0/io/string_reader.ts"
2121

2222
export { wait } from "https://deno.land/x/wait@0.1.12/mod.ts";
2323

24-
export { type Context, Hono } from "https://deno.land/x/hono@v3.2.7/mod.ts";
24+
export { Hono } from "https://deno.land/x/hono@v3.2.7/mod.ts";
2525
export { getFilePath } from "https://deno.land/x/hono@v3.2.7/utils/filepath.ts";
2626
export { getMimeType } from "https://deno.land/x/hono@v3.2.7/utils/mime.ts";
2727
export { logger } from "https://deno.land/x/hono@v3.2.7/middleware/logger/index.ts";

lib/server.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { assert, dotenv, Hono, resolve, toFileUrl } from "./deps.ts";
33
import { ensureMinDenoVersion } from "./dev/ensureMinDenoVersion.ts";
44
import { log } from "./logger.ts";
55
import { serveStatic } from "./middleware/serveStatic.ts";
6-
import { CreateServerOptions, Mode } from "./types.ts";
6+
import { CreateServerOptions, Env, Mode } from "./types.ts";
77
import { UltraServer } from "./ultra.ts";
88
import { resolveImportMapPath } from "./utils/import-map.ts";
9-
import type { Context, Next } from "./types.ts";
109

1110
/**
1211
* Dotenv
@@ -27,9 +26,15 @@ const defaultOptions = {
2726
"https://ga.jspm.io/npm:es-module-shims@1.6.2/dist/es-module-shims.js",
2827
};
2928

30-
export async function createServer(
29+
export async function createServer<
30+
// deno-lint-ignore no-explicit-any
31+
E extends Env = any,
32+
// deno-lint-ignore ban-types
33+
S = {},
34+
BasePath extends string = "/",
35+
>(
3136
options: CreateServerOptions = {},
32-
): Promise<UltraServer> {
37+
) {
3338
const resolvedOptions = {
3439
...defaultOptions,
3540
...options,
@@ -47,7 +52,7 @@ export async function createServer(
4752
const root = Deno.cwd();
4853
const assetManifestPath = toFileUrl(resolve(root, "asset-manifest.json"));
4954

50-
const server = new UltraServer(root, {
55+
const server = new UltraServer<E, S, BasePath>(root, {
5156
mode,
5257
entrypoint: browserEntrypoint,
5358
importMapPath: options.importMapPath
@@ -69,16 +74,13 @@ export async function createServer(
6974

7075
// Serve anything else static at "/"
7176
// deno-fmt-ignore
72-
server.get("/ultra/*", (
73-
context: Context,
74-
next: Next,
75-
): Promise<Response | undefined> => {
77+
server.get("/ultra/*", (context, next) => {
7678
const path = new URL(context.req.url).pathname.slice(`/ultra`.length)
7779
return serveStatic({
7880
root: server.ultraDir,
7981
path: path,
8082
cache: mode !== "development",
81-
})(context,next);
83+
})(context, next);
8284
});
8385

8486
// Serve anything else static at "/"
@@ -102,8 +104,14 @@ export async function createServer(
102104
return server;
103105
}
104106

105-
export function createRouter() {
106-
return new Hono();
107+
export function createRouter<
108+
// deno-lint-ignore no-explicit-any
109+
E extends Env = any,
110+
// deno-lint-ignore ban-types
111+
S = {},
112+
BasePath extends string = "/",
113+
>() {
114+
return new Hono<E, S, BasePath>();
107115
}
108116

109117
export function assertServerOptions(

lib/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Context } from "https://deno.land/x/hono@v3.2.7/mod.ts";
1+
export type { Context, Env } from "https://deno.land/x/hono@v3.2.7/mod.ts";
22
export type { Next } from "https://deno.land/x/hono@v3.2.7/types.ts";
33
export type { StatusCode } from "https://deno.land/x/hono@v3.2.7/utils/http-status.ts";
44

lib/ultra.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "./deps.ts";
1111
import { log } from "./logger.ts";
1212
import { renderToStream } from "./render.ts";
13-
import { Context, ImportMap, Mode } from "./types.ts";
13+
import { Context, Env, ImportMap, Mode } from "./types.ts";
1414
import { toUltraUrl } from "./utils/url.ts";
1515

1616
type UltraServerRenderOptions = {
@@ -29,7 +29,12 @@ type UltraServerOptions = {
2929
entrypoint?: string;
3030
};
3131

32-
export class UltraServer extends Hono {
32+
export class UltraServer<
33+
E extends Env = Env,
34+
// deno-lint-ignore ban-types
35+
S = {},
36+
BasePath extends string = "/",
37+
> extends Hono<E, S, BasePath> {
3338
public importMap: ImportMap | undefined;
3439
public assetManifest: Map<string, string> | undefined;
3540

server.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export { type Context } from "./lib/deps.ts";
21
export { createRouter, createServer } from "./lib/server.ts";
3-
export type { StatusCode } from "./lib/types.ts";
2+
export type { Context, StatusCode } from "./lib/types.ts";

test/fixture.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ serve((request) => {
1616
args: [
1717
"test",
1818
"-A",
19+
"--reload",
1920
],
2021
cwd: join(Deno.cwd(), "test", "fixture"),
2122
}).spawn();

test/fixture/server.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import { TRPCServerProvider } from "./src/trpc/server.tsx";
88
import { serverSheet, TWProvider } from "./src/context/twind.tsx";
99
import { theme } from "./theme.ts";
1010

11-
const server = await createServer({
11+
type Environment = {
12+
Variables: {
13+
foo?: string;
14+
};
15+
};
16+
17+
const server = await createServer<Environment, { foo: boolean }>({
1218
importMapPath: import.meta.resolve("./importMap.json"),
1319
browserEntrypoint: import.meta.resolve("./client.tsx"),
1420
});
1521

1622
server.use("*", async (context, next) => {
1723
context.header("x-foo", "bar");
24+
context.set("foo", "bar");
25+
1826
await next();
1927
});
2028

0 commit comments

Comments
 (0)