Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!(types): Fix context type when chaining routes with middlewares #2046

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 10 additions & 46 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,7 @@ export interface HandlerInterface<
>(
path: P,
handler: H<E2, MergedPath, I, R>
): Hono<
IntersectNonAnyTypes<[E, E2]>,
S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x2)
<
Expand All @@ -412,11 +408,7 @@ export interface HandlerInterface<
>(
path: P,
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2, R>]
): Hono<
IntersectNonAnyTypes<[E, E2, E3]>,
S & ToSchema<M, MergePath<BasePath, P>, I2['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I2['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x3)
<
Expand All @@ -432,11 +424,7 @@ export interface HandlerInterface<
>(
path: P,
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4]>,
S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x4)
<
Expand All @@ -459,11 +447,7 @@ export interface HandlerInterface<
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5]>,
S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x5)
<
Expand All @@ -489,11 +473,7 @@ export interface HandlerInterface<
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>,
S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x6)
<
Expand Down Expand Up @@ -522,11 +502,7 @@ export interface HandlerInterface<
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>,
S & ToSchema<M, MergePath<BasePath, P>, I6['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I6['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x7)
<
Expand Down Expand Up @@ -558,11 +534,7 @@ export interface HandlerInterface<
H<E7, MergedPath, I6>,
H<E8, MergedPath, I7, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>,
S & ToSchema<M, MergePath<BasePath, P>, I7['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I7['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x8)
<
Expand Down Expand Up @@ -597,11 +569,7 @@ export interface HandlerInterface<
H<E8, MergedPath, I7>,
H<E9, MergedPath, I8, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>,
S & ToSchema<M, MergePath<BasePath, P>, I8['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I8['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x9)
<
Expand Down Expand Up @@ -639,11 +607,7 @@ export interface HandlerInterface<
H<E9, MergedPath, I8>,
H<E10, MergedPath, I9, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>,
S & ToSchema<M, MergePath<BasePath, P>, I9['in'], MergeTypedResponseData<R>>,
BasePath
>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I9['in'], MergeTypedResponseData<R>>, BasePath>

// app.get(path, handler x10)
<
Expand Down Expand Up @@ -685,7 +649,7 @@ export interface HandlerInterface<
H<E11, MergedPath, I10, R>
]
): Hono<
IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>,
E,
S & ToSchema<M, MergePath<BasePath, P>, I10['in'], MergeTypedResponseData<R>>,
BasePath
>
Expand Down
176 changes: 24 additions & 152 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,158 +836,6 @@ describe('c.var with chaining - test only types', () => {
return c.json(0)
})

// app.get('/', handler...)

new Hono().get('/', mw1).get('/', (c) => {
yusukebe marked this conversation as resolved.
Show resolved Hide resolved
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
expectTypeOf(c.var.foo6).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
expectTypeOf(c.var.foo6).toEqualTypeOf<string>()
expectTypeOf(c.var.foo7).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
expectTypeOf(c.var.foo6).toEqualTypeOf<string>()
expectTypeOf(c.var.foo7).toEqualTypeOf<string>()
expectTypeOf(c.var.foo8).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
expectTypeOf(c.var.foo6).toEqualTypeOf<string>()
expectTypeOf(c.var.foo7).toEqualTypeOf<string>()
expectTypeOf(c.var.foo8).toEqualTypeOf<string>()
expectTypeOf(c.var.foo9).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, mw10).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo10')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
expectTypeOf(c.var.foo4).toEqualTypeOf<string>()
expectTypeOf(c.var.foo5).toEqualTypeOf<string>()
expectTypeOf(c.var.foo6).toEqualTypeOf<string>()
expectTypeOf(c.var.foo7).toEqualTypeOf<string>()
expectTypeOf(c.var.foo8).toEqualTypeOf<string>()
expectTypeOf(c.var.foo9).toEqualTypeOf<string>()
expectTypeOf(c.var.foo10).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {
expectTypeOf(c.req.valid('query')).toMatchTypeOf<{
bar1: number
Expand All @@ -1003,6 +851,30 @@ describe('c.var with chaining - test only types', () => {

return c.json(0)
})

type Env = {
Variables: {
init: number
}
}

new Hono<Env>()
.get('/', mw1, (c) => {
expectTypeOf(c.get('init')).toEqualTypeOf<number>()
expectTypeOf(c.var.init).toEqualTypeOf<number>()
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
return c.json(0)
})
.get('/', (c) => {
expectTypeOf(c.get('init')).toEqualTypeOf<number>()
expectTypeOf(c.var.init).toEqualTypeOf<number>()
// @ts-expect-error foo1 is not typed
c.get('foo1')
// @ts-expect-error foo1 is not typed
c.var.foo1
return c.json(0)
})
yusukebe marked this conversation as resolved.
Show resolved Hide resolved
})
})

Expand Down