Skip to content

Commit

Permalink
fix(factory): correct the type of factory.createMiddleware() (#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jan 23, 2025
1 parent 3fba7a5 commit 7a7918d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
56 changes: 38 additions & 18 deletions src/helper/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { expectTypeOf } from 'vitest'
import { hc } from '../../client'
import type { ClientRequest } from '../../client/types'
import { Hono } from '../../index'
import type { ToSchema, TypedResponse } from '../../types'
import type { ContentfulStatusCode, StatusCode } from '../../utils/http-status'
import type { ExtractSchema, ToSchema, TypedResponse } from '../../types'
import type { ContentfulStatusCode } from '../../utils/http-status'
import type { Equal, Expect } from '../../utils/types'
import { validator } from '../../validator'
import { createFactory, createMiddleware } from './index'

Expand Down Expand Up @@ -327,25 +328,44 @@ describe('createHandler', () => {
})
})

describe('createApp', () => {
type Env = { Variables: { foo: string } }
const factory = createFactory<Env>({
initApp: (app) => {
app.use((c, next) => {
c.set('foo', 'bar')
return next()
describe('createFactory', () => {
describe('createApp', () => {
type Env = { Variables: { foo: string } }
const factory = createFactory<Env>({
initApp: (app) => {
app.use((c, next) => {
c.set('foo', 'bar')
return next()
})
},
})
const app = factory.createApp()
it('Should set the correct type and initialize the app', async () => {
app.get('/', (c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
return c.text(c.var.foo)
})
},
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toBe('bar')
})
})
const app = factory.createApp()
it('Should set the correct type and initialize the app', async () => {
app.get('/', (c) => {
expectTypeOf(c.var.foo).toEqualTypeOf<string>()
return c.text(c.var.foo)

describe('createMiddleware', () => {
it('Should set the correct type', () => {
const factory = createFactory()

const middleware = factory.createMiddleware(async (_, next) => {
await next()
})

const routes = new Hono().use('*', middleware)
type Actual = ExtractSchema<typeof routes>
type Expected = {
'*': {}
}
type verify = Expect<Equal<Expected, Actual>>
})
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toBe('bar')
})
})

Expand Down
8 changes: 5 additions & 3 deletions src/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export interface CreateHandlersInterface<E extends Env, P extends string> {
]
}

export class Factory<E extends Env = any, P extends string = any> {
export class Factory<E extends Env = Env, P extends string = string> {
private initApp?: InitApp<E>

constructor(init?: { initApp?: InitApp<E> }) {
Expand All @@ -298,15 +298,17 @@ export class Factory<E extends Env = any, P extends string = any> {
return app
}

createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware
createMiddleware = <I extends Input = {}>(
middleware: MiddlewareHandler<E, P, I>
): MiddlewareHandler<E, P, I> => middleware

createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {
// @ts-expect-error this should not be typed
return handlers.filter((handler) => handler !== undefined)
}
}

export const createFactory = <E extends Env = any, P extends string = any>(init?: {
export const createFactory = <E extends Env = Env, P extends string = string>(init?: {
initApp?: InitApp<E>
}): Factory<E, P> => new Factory<E, P>(init)

Expand Down

0 comments on commit 7a7918d

Please sign in to comment.