Closed brillout closed 3 months ago
I was going to say that it should not declare its route, to keep things simple.
A package using universal-middleware
could also export its "suggested" route like this:
import telefuncHandler from "telefunc/middleware/hono";
import telefuncRoute from "telefunc/middleware/hono-route";
...
app.post(telefuncRoute, telefuncHandler());
But this route will:
"*"
, but in h3 is "/**"
Let me explain this second point:
const handler = telefuncHandler({
route: "/api/telefunc"
});
// We can't use `telefuncRoute` anymore
app.post("/api/telefunc", handler);
Those two points non problems if packages leveraging universal-middleware
are used directly by the user, as one can override the routes manually.
But even for a package such as vike-telefunc
, either the default route will be applied by default, or non-default if the user overrides them.
So the only issue remaining is the first point. How to represent a universal route, and how to convert such route to adapters compatible ones.
We can create a @universal-middleware/tools
package that would export helpers like toHonoRoute(route: string): string
, that can then be used by vike-telefunc
or alike.
Actually, how about we make all +middleware
be catch-all middlewares that are self-responsible for routing?
Telefunc example:
// node_modules/vike-telefunc/dist/integration/+middleware.js
export { middleware }
import { telefunc, config, provideTelefuncContext } from 'telefunc';
async function middleware(request, ctx) {
// Needs to be called not only for Telefunc requests, but also for page
// requests (in order to support SSR).
provideTelefuncContext(ctx)
if (request.url === config.telefuncUrl || '/_telefunc') {
const response = await telefunc({
url: request.url,
method: request.method,
body: await request.text()
})
return new Response(response.body, {
headers: new Headers({ contentType: response.contentType }),
status: response.statusCode
})
}
}
It does work indeed :)
Another pro: we can implement it in Vike land including the magic number, so I guess we can close this.
Thanks for the conversation :)
How could Telefunc's universal middleware define its URL route? Maybe we'll need https://github.com/magne4000/universal-middleware/discussions/4 at some point :thinking: