Skip to content

Commit

Permalink
fix(factory): revert PR #3498 (#3515)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Oct 15, 2024
1 parent fc9cc6d commit f9e6ea7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
46 changes: 21 additions & 25 deletions src/helper/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,30 @@ describe('createMiddleware', () => {
expect(url.pathname).toBe('/message')
})

describe('Relax types for Bindings and Variables', () => {
it('Should not throw a type error', () => {
type Bindings = {
MY_VAR_IN_BINDINGS: string
}

const app = new Hono<{ Bindings: Bindings }>()
it('Should pass generics types to chained handlers', () => {
type Bindings = {
MY_VAR_IN_BINDINGS: string
}

type Variables = {
MY_VAR: string
}
type Variables = {
MY_VAR: string
}

const middleware = (_variable: string) =>
createMiddleware<{ Variables: Variables }>(async (c, next) => {
await next()
})
const app = new Hono<{ Bindings: Bindings }>()

app.get(
'/',
createMiddleware<{ Bindings: Bindings }>(async (c, next) => {
const mw = middleware(c.env.MY_VAR_IN_BINDINGS)
await mw(c, next) // `c` does not throw an error
}),
(c) => {
return c.json({})
}
)
})
app.get(
'/',
createMiddleware<{ Variables: Variables }>(async (c, next) => {
await next()
}),
createMiddleware(async (c, next) => {
await next()
}),
async (c) => {
const v = c.get('MY_VAR')
expectTypeOf(v).toEqualTypeOf<string>()
}
)
})
})

Expand Down
17 changes: 3 additions & 14 deletions src/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Hono } from '../../hono'
import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../types'
import type { Simplify } from '../../utils/types'

type InitApp<E extends Env = Env> = (app: Hono<E>) => void

Expand Down Expand Up @@ -238,20 +237,10 @@ export const createFactory = <E extends Env = any, P extends string = any>(init?
initApp?: InitApp<E>
}): Factory<E, P> => new Factory<E, P>(init)

// Add `any` if it's undefined to relax types
// { Variables: Variables } => { Bindings: any, Variables: any }
// { Bindings: Bindings } => { Bindings: Bindings, Variables: any }

type AddAnyToEnv<T extends { Variables?: object; Bindings?: object }> = {
Bindings: undefined extends T['Bindings'] ? any : T['Bindings']
Variables: undefined extends T['Variables'] ? any : T['Variables']
}

export const createMiddleware = <
E extends Env = any,
P extends string = string,
I extends Input = {},
E2 extends Env = Simplify<AddAnyToEnv<E>>
I extends Input = {}
>(
middleware: MiddlewareHandler<E2, P, I>
): MiddlewareHandler<E2, P, I> => createFactory<E2, P>().createMiddleware<I>(middleware)
middleware: MiddlewareHandler<E, P, I>
): MiddlewareHandler<E, P, I> => createFactory<E, P>().createMiddleware<I>(middleware)

0 comments on commit f9e6ea7

Please sign in to comment.