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

refactor(types): move HandlerInterface's (path, handler)s overloads down #3072

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,46 @@ describe('Env types with chained routes - test only types', () => {
})
})

/**
* Ref: https://github.com/honojs/hono/issues/3027
*/
describe('Env types with validator as first middleware - test only types', () => {
const app = new Hono<{ Variables: { testVar: string } }>()
it('Should not throw a type error', () => {
const testApp = app.get(
validator('json', () => {
return {
cd: 'bar',
}
}),
async (c) => {
const foo = c.req.valid('json') // Error here
return c.json(1)
}
)

const dummyMiddleware1 = createMiddleware(async (c, next) => {
await next()
})
// Multiple levels of middleware
const testApp2 = app.post(
validator('json', () => {
return {
cd: 'bar',
}
}),
dummyMiddleware1,
createMiddleware(async (c, next) => {
await next()
}),
async (c) => {
const foo = c.req.valid('json') // Error here also
return c.json(1)
}
)
})
})

describe('Env types with `use` middleware - test only types', () => {
const app = new Hono()

Expand Down
140 changes: 70 additions & 70 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ export interface HandlerInterface<
handler: H<E2, P, I, R>
): Hono<IntersectNonAnyTypes<[E, E2]>, S & ToSchema<M, P, I, MergeTypedResponse<R>>, BasePath>

// app.get(path, handler)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
R extends HandlerResponse<any> = any,
I extends Input = BlankInput,
E2 extends Env = E
>(
path: P,
handler: H<E2, MergedPath, I, R>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>, BasePath>

// app.get(handler x2)
<
P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
Expand All @@ -149,19 +137,17 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x2)
// app.get(path, handler)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
R extends HandlerResponse<any> = any,
I extends Input = BlankInput,
I2 extends Input = I,
E2 extends Env = E,
E3 extends Env = IntersectNonAnyTypes<[E, E2]>
E2 extends Env = E
>(
path: P,
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2, R>]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I2, MergeTypedResponse<R>>, BasePath>
handler: H<E2, MergedPath, I, R>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 3)
<
Expand All @@ -181,21 +167,19 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x3)
// app.get(path, handler x2)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
R extends HandlerResponse<any> = any,
I extends Input = BlankInput,
I2 extends Input = I,
I3 extends Input = I & I2,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>
E3 extends Env = IntersectNonAnyTypes<[E, E2]>
>(
path: P,
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>, BasePath>
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2, R>]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I2, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 4)
<
Expand All @@ -217,28 +201,21 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x4)
// app.get(path, handler x3)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
R extends HandlerResponse<any> = any,
I extends Input = BlankInput,
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3,
E2 extends Env = E,
E3 extends Env = E,
E4 extends Env = E,
E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>
E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>
>(
path: P,
...handlers: [
H<E2, MergedPath, I>,
H<E3, MergedPath, I2>,
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4, MergeTypedResponse<R>>, BasePath>
...handlers: [H<E2, MergedPath, I>, H<E3, MergedPath, I2>, H<E4, MergedPath, I3, R>]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 5)
<
Expand All @@ -262,7 +239,7 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x5)
// app.get(path, handler x4)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
Expand All @@ -271,22 +248,19 @@ export interface HandlerInterface<
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3,
I5 extends Input = I & I2 & I3 & I4,
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]>
E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>
>(
path: P,
...handlers: [
H<E2, MergedPath, I>,
H<E3, MergedPath, I2>,
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5, R>
H<E5, MergedPath, I4, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5, MergeTypedResponse<R>>, BasePath>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 6)
<
Expand Down Expand Up @@ -319,7 +293,7 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x6)
// app.get(path, handler x5)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
Expand All @@ -329,24 +303,21 @@ export interface HandlerInterface<
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3,
I5 extends Input = I & I2 & I3 & I4,
I6 extends Input = I & I2 & I3 & I4 & I5,
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]>
E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]>
>(
path: P,
...handlers: [
H<E2, MergedPath, I>,
H<E3, MergedPath, I2>,
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6, R>
H<E6, MergedPath, I5, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I6, MergeTypedResponse<R>>, BasePath>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 7)
<
Expand Down Expand Up @@ -382,7 +353,7 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x7)
// app.get(path, handler x6)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
Expand All @@ -393,14 +364,12 @@ export interface HandlerInterface<
I4 extends Input = I & I2 & I3,
I5 extends Input = I & I2 & I3 & I4,
I6 extends Input = I & I2 & I3 & I4 & I5,
I7 extends Input = I & I2 & I3 & I4 & I5 & I6,
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]>
E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>
>(
path: P,
...handlers: [
Expand All @@ -409,10 +378,9 @@ export interface HandlerInterface<
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6>,
H<E8, MergedPath, I7, R>
H<E7, MergedPath, I6, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I7, MergeTypedResponse<R>>, BasePath>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I6, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 8)
<
Expand Down Expand Up @@ -451,7 +419,7 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x8)
// app.get(path, handler x7)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
Expand All @@ -463,15 +431,13 @@ export interface HandlerInterface<
I5 extends Input = I & I2 & I3 & I4,
I6 extends Input = I & I2 & I3 & I4 & I5,
I7 extends Input = I & I2 & I3 & I4 & I5 & I6,
I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7,
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]>
E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>
>(
path: P,
...handlers: [
Expand All @@ -481,10 +447,9 @@ export interface HandlerInterface<
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6>,
H<E8, MergedPath, I7>,
H<E9, MergedPath, I8, R>
H<E8, MergedPath, I7, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I8, MergeTypedResponse<R>>, BasePath>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I7, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 9)
<
Expand Down Expand Up @@ -526,7 +491,7 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x9)
// app.get(path, handler x8)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
Expand All @@ -539,16 +504,14 @@ export interface HandlerInterface<
I6 extends Input = I & I2 & I3 & I4 & I5,
I7 extends Input = I & I2 & I3 & I4 & I5 & I6,
I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7,
I9 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7 & I8,
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]>
E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>
>(
path: P,
...handlers: [
Expand All @@ -559,10 +522,9 @@ export interface HandlerInterface<
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6>,
H<E8, MergedPath, I7>,
H<E9, MergedPath, I8>,
H<E10, MergedPath, I9, R>
H<E9, MergedPath, I8, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<R>>, BasePath>
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I8, MergeTypedResponse<R>>, BasePath>

// app.get(handler x 10)
<
Expand Down Expand Up @@ -607,6 +569,44 @@ export interface HandlerInterface<
BasePath
>

// app.get(path, handler x9)
<
P extends string,
MergedPath extends MergePath<BasePath, P> = MergePath<BasePath, P>,
R extends HandlerResponse<any> = any,
I extends Input = BlankInput,
I2 extends Input = I,
I3 extends Input = I & I2,
I4 extends Input = I & I2 & I3,
I5 extends Input = I & I2 & I3 & I4,
I6 extends Input = I & I2 & I3 & I4 & I5,
I7 extends Input = I & I2 & I3 & I4 & I5 & I6,
I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7,
I9 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7 & I8,
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: [
H<E2, MergedPath, I>,
H<E3, MergedPath, I2>,
H<E4, MergedPath, I3>,
H<E5, MergedPath, I4>,
H<E6, MergedPath, I5>,
H<E7, MergedPath, I6>,
H<E8, MergedPath, I7>,
H<E9, MergedPath, I8>,
H<E10, MergedPath, I9, R>
]
): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I9, MergeTypedResponse<R>>, BasePath>

// app.get(path, handler x10)
<
P extends string,
Expand Down