hono icon indicating copy to clipboard operation
hono copied to clipboard

Provide a way to skip `.route()` group prefixing for certain routes / methods

Open NamesMT opened this issue 11 months ago • 3 comments

What is the feature you are proposing?

Linking with NamesMT/hono-adapter-aws-lambda#1 The linked repo is my fork of hono/aws-lambda adapter, which adds support for LambdaTriggerEvents like S3, SQS triggers. The support works by registering routes on TRIGGER method, like: TRIGGER aws:s3, in which, the eventSource, which is aws:s3, should never be prefixed.

A way to skip .route() group prefixing for certain routes/methods would be amazing.

NamesMT avatar Mar 22 '24 20:03 NamesMT

Hi @NamesMT

I'm sorry. I can't fully understand what you are intending. Could you share the expected API with the actual code?

yusukebe avatar Mar 26 '24 19:03 yusukebe

Hi @yusukebe,

The following patch is a rough example of the feature (skipping subPath for certain methods):

diff --git a/src/hono-base.ts b/src/hono-base.ts
index d849fce..ae04e49 100644
--- a/src/hono-base.ts
+++ b/src/hono-base.ts
@@ -186,7 +186,10 @@ class Hono<
     SubBasePath extends string
   >(
     path: SubPath,
-    app?: Hono<SubEnv, SubSchema, SubBasePath>
+    app?: Hono<SubEnv, SubSchema, SubBasePath>,
+    options?: {
+      noSubPathMethods: string[]
+    }
   ): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {
     const subApp = this.basePath(path)
 
@@ -205,7 +208,10 @@ class Hono<
         ;(handler as any)[COMPOSED_HANDLER] = r.handler
       }
 
-      subApp.addRoute(r.method, r.path, handler)
+      if (options?.noSubPathMethods?.includes(r.method)) {
+        app.addRoute(r.method, r.path, handler)
+      } else
+        subApp.addRoute(r.method, r.path, handler)
     })
     return this
   }

Expected behavior:

childApp.get('/G', c => c.text('hello'))
childApp.post('/P', c => c.text('hello'))
app.route('/child', appC, { noSubPathMethods: ['POST'] } ) // => GET /child/G, POST /P

NamesMT avatar Mar 28 '24 08:03 NamesMT

Thanks! @NamesMT

I got it well. Give me a minute to think about it.

yusukebe avatar Mar 28 '24 08:03 yusukebe