-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
126 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
import type { HattipHandler } from "@hattip/core"; | ||
import { createRouter } from "@hattip/router"; | ||
import { createHandler } from "@universal-middleware/hattip"; | ||
import { createTodoHandler } from "./server/create-todo-handler"; | ||
import { vikeHandler } from "./server/vike-handler"; | ||
import type { HattipHandler } from "@hattip/core"; | ||
import { createRouter, type RouteHandler } from "@hattip/router"; | ||
|
||
type Middleware<Context extends Record<string | number | symbol, unknown>> = (request: Request, context: Context) => Response | void | Promise<Response> | Promise<void> | ||
|
||
function handlerAdapter<Context extends Record<string | number | symbol, unknown>>( | ||
handler: Middleware<Context>, | ||
): RouteHandler<unknown, unknown> { | ||
return (context) => { | ||
const rawContext = context as unknown as Record<string, unknown>; | ||
rawContext.context ??= {}; | ||
return handler(context.request, rawContext.context as Context); | ||
}; | ||
} | ||
|
||
const router = createRouter(); | ||
|
||
router.post("/api/todo/create", handlerAdapter(createTodoHandler)); | ||
router.post("/api/todo/create", createHandler(() => createTodoHandler)()); | ||
|
||
/** | ||
* Vike route | ||
* | ||
* @link {@see https://vike.dev} | ||
**/ | ||
router.use(handlerAdapter(vikeHandler)); | ||
router.use(createHandler(() => vikeHandler)()); | ||
|
||
export default router.buildHandler() as HattipHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,17 @@ | ||
import { createHandler } from "@universal-middleware/hono"; | ||
import { Hono } from "hono"; | ||
import { createMiddleware } from "hono/factory"; | ||
import { createTodoHandler } from "./server/create-todo-handler.js"; | ||
import { vikeHandler } from "./server/vike-handler"; | ||
|
||
type Middleware<Context extends Record<string | number | symbol, unknown>> = (request: Request, context: Context) => Response | void | Promise<Response> | Promise<void> | ||
|
||
export function handlerAdapter<Context extends Record<string | number | symbol, unknown>>( | ||
handler: Middleware<Context>, | ||
) { | ||
return createMiddleware(async (context, next) => { | ||
let ctx = context.get("context"); | ||
if (!ctx) { | ||
ctx = {}; | ||
context.set("context", ctx); | ||
} | ||
|
||
const res = await handler(context.req.raw, ctx as Context); | ||
context.set("context", ctx); | ||
|
||
if (!res) { | ||
await next(); | ||
} | ||
|
||
return res; | ||
}); | ||
} | ||
|
||
const app = new Hono(); | ||
|
||
app.post("/api/todo/create", handlerAdapter(createTodoHandler)); | ||
app.post("/api/todo/create", createHandler(() => createTodoHandler)()); | ||
|
||
/** | ||
* Vike route | ||
* | ||
* @link {@see https://vike.dev} | ||
**/ | ||
app.all("*", handlerAdapter(vikeHandler)); | ||
app.all("*", createHandler(() => vikeHandler)()); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
declare global { | ||
namespace Vike { | ||
interface PageContext { | ||
ctx?: { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
waitUntil(promise: Promise<any>): void; | ||
passThroughOnException(): void; | ||
}; | ||
env?: Record<string | symbol | number, unknown>; | ||
} | ||
} | ||
} | ||
|
||
export type {}; |
Oops, something went wrong.