From a9847edccbe182a99330da959051b92e641f4102 Mon Sep 17 00:00:00 2001 From: snarcraft Date: Fri, 2 Feb 2024 19:35:44 +0100 Subject: [PATCH] feat(server): add base option to h3 handler --- README.md | 4 +++- src/server.ts | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 50c20046..8ea156b7 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,9 @@ const ipx = createIPX({ httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }), }); -const app = createApp().use("/", createIPXH3Handler(ipx)); +const base = '/' + +const app = createApp().use(base, createIPXH3Handler(ipx, { base })); listen(toNodeListener(app)); ``` diff --git a/src/server.ts b/src/server.ts index a1e7feff..68a0526a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -22,11 +22,11 @@ import { IPX } from "./ipx"; const MODIFIER_SEP = /[&,]/g; const MODIFIER_VAL_SEP = /[:=_]/; -export function createIPXH3Handler(ipx: IPX) { +export function createIPXH3Handler(ipx: IPX, options = { base: "/" }) { const _handler = async (event: H3Event) => { // Parse URL const [modifiersString = "", ...idSegments] = event.path - .slice(1 /* leading slash */) + .replace(options.base, "") .split("/"); const id = safeString(decode(idSegments.join("/"))); @@ -47,7 +47,7 @@ export function createIPXH3Handler(ipx: IPX) { }); } - // Contruct modifiers + // Construct modifiers const modifiers: Record = Object.create(null); // Read modifiers from first segment