diff --git a/src/types.test.ts b/src/types.test.ts index 784e2418..5ee4d972 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -1838,3 +1838,390 @@ describe('Env types with `use` middleware - test only types', () => { }) }) }) + +describe('Env types and a path type with `app.use(path, handler...)` - test only types', () => { + it('Should not throw a type error', () => { + type Env = { + Variables: { + foo: string + } + } + + // app.use(path, handler) + new Hono() + .use('/:id', async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x2) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x3) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x4) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x5) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x6) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x7) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x8) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x9) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + + // app.use(path, handler x10) + new Hono() + .use( + '/:id', + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + }, + async (c, next) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + await next() + } + ) + .get((c) => { + expectTypeOf(c.var.foo).toEqualTypeOf() + expectTypeOf(c.req.param('id')).toEqualTypeOf() + return c.json(0) + }) + }) +}) diff --git a/src/types.ts b/src/types.ts index bcb378e8..a81858de 100644 --- a/src/types.ts +++ b/src/types.ts @@ -701,6 +701,16 @@ export interface MiddlewareHandlerInterface< ...handlers: [MiddlewareHandler, MiddlewareHandler] ): Hono, S, BasePath> + // app.get(path, handler) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E + >( + path: P, + handler: MiddlewareHandler + ): Hono, ChangePathOfSchema, BasePath> + // app.use(handler x3) < E2 extends Env = E, @@ -711,6 +721,17 @@ export interface MiddlewareHandlerInterface< ...handlers: [MiddlewareHandler, MiddlewareHandler, MiddlewareHandler] ): Hono, S, BasePath> + // app.get(path, handler x2) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = IntersectNonAnyTypes<[E, E2]> + >( + path: P, + ...handlers: [MiddlewareHandler, MiddlewareHandler] + ): Hono, ChangePathOfSchema, BasePath> + // app.use(handler x4) < E2 extends Env = E, @@ -727,6 +748,18 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x3) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]> + >( + path: P, + ...handlers: [MiddlewareHandler, MiddlewareHandler, MiddlewareHandler] + ): Hono, ChangePathOfSchema, BasePath> + // app.use(handler x5) < E2 extends Env = E, @@ -745,6 +778,24 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x4) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono, ChangePathOfSchema, BasePath> + // app.use(handler x6) < E2 extends Env = E, @@ -765,6 +816,30 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x5) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>, + ChangePathOfSchema, + BasePath + > + // app.use(handler x7) < E2 extends Env = E, @@ -787,6 +862,32 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x6) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = E, + E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>, + ChangePathOfSchema, + BasePath + > + // app.use(handler x8) < E2 extends Env = E, @@ -811,6 +912,34 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x7) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = E, + E7 extends Env = E, + E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>, + ChangePathOfSchema, + BasePath + > + // app.use(handler x9) < E2 extends Env = E, @@ -837,6 +966,36 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x8) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = E, + E7 extends Env = E, + E8 extends Env = E, + E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>, + ChangePathOfSchema, + BasePath + > + // app.use(handler x10) < E2 extends Env = E, @@ -865,6 +1024,38 @@ export interface MiddlewareHandlerInterface< ] ): Hono, S, BasePath> + // app.get(path, handler x9) + < + P extends string, + MergedPath extends MergePath = MergePath, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = E, + E7 extends Env = E, + E8 extends Env = E, + E9 extends Env = E, + E10 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]> + >( + path: P, + ...handlers: [ + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler, + MiddlewareHandler + ] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>, + ChangePathOfSchema, + BasePath + > + //// app.use(path, ...handlers[])

( path: P, @@ -1612,6 +1803,10 @@ export type Schema = { } } +type ChangePathOfSchema = keyof S extends never + ? { [K in Path]: never } + : { [K in keyof S as Path]: S[K] } + export type Endpoint = { input: Partial output: any