| 2 | import type { Env, Hono, HonoRequest, MiddlewareHandler } from "hono"; |
| 3 | |
| 4 | export function createMiddleware( |
| 5 | client: TriggerClient, |
| 6 | path: string = "/api/trigger" |
| 7 | ): MiddlewareHandler { |
| 8 | return async (c, next) => { |
| 9 | if (c.req.path !== path) { |
| 10 | return await next(); |
| 11 | } |
| 12 | |
| 13 | if (c.req.method === "HEAD") { |
| 14 | return new Response(null, { status: 200 }); |
| 15 | } |
| 16 | |
| 17 | const request = convertToStandardRequest(c.req); |
| 18 | |
| 19 | const response = await client.handleRequest(request); |
| 20 | |
| 21 | return new Response(JSON.stringify(response.body), { |
| 22 | status: response.status, |
| 23 | headers: response.headers, |
| 24 | }); |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | export function addMiddleware<TEnv extends Env>( |
| 29 | app: Hono<TEnv>, |