-
-
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.
Merge pull request #10 from vikejs/runtime
feat: forward runtime context
- Loading branch information
Showing
23 changed files
with
265 additions
and
447 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
Oops, something went wrong.