import { createLureHandler as coreCreateLureHandler } from "@lure-hooks/core"; import type { LureHandlerOptions } from "@lure-hooks/core"; import type { StandardSchemaV1 } from "@standard-schema/spec"; export async function createLureHandler( options: LureHandlerOptions, ): Promise<(req: Request) => Promise> { const basePath = "/" + options.basePath.replace(/^\/+/, "").replace(/\/+$/, ""); const handler = await coreCreateLureHandler(options); return async (req: Request): Promise => { const url = new URL(req.url); if (basePath !== "/" && url.pathname !== basePath && !url.pathname.startsWith(basePath + "/")) { return null; } const rawBody = new Uint8Array(await req.arrayBuffer()); const lureReq = { path: url.pathname, headers: req.headers, query: url.searchParams, rawBody, }; const handled = handler.handle(lureReq); return handled ? new Response(null, { status: 204 }) : null; }; }