From 7a7918d2d397022006e01ccfe4284fdae9e57a96 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Thu, 23 Jan 2025 19:01:51 +0900 Subject: [PATCH] fix(factory): correct the type of `factory.createMiddleware()` (#3849) --- src/helper/factory/index.test.ts | 56 ++++++++++++++++++++++---------- src/helper/factory/index.ts | 8 +++-- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/helper/factory/index.test.ts b/src/helper/factory/index.test.ts index 59b170940..537006c96 100644 --- a/src/helper/factory/index.test.ts +++ b/src/helper/factory/index.test.ts @@ -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' @@ -327,25 +328,44 @@ describe('createHandler', () => { }) }) -describe('createApp', () => { - type Env = { Variables: { foo: string } } - const factory = createFactory({ - initApp: (app) => { - app.use((c, next) => { - c.set('foo', 'bar') - return next() +describe('createFactory', () => { + describe('createApp', () => { + type Env = { Variables: { foo: string } } + const factory = createFactory({ + 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() + 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() - 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 + type Expected = { + '*': {} + } + type verify = Expect> }) - const res = await app.request('/') - expect(res.status).toBe(200) - expect(await res.text()).toBe('bar') }) }) diff --git a/src/helper/factory/index.ts b/src/helper/factory/index.ts index 2790c3321..b3e9a9ad8 100644 --- a/src/helper/factory/index.ts +++ b/src/helper/factory/index.ts @@ -283,7 +283,7 @@ export interface CreateHandlersInterface { ] } -export class Factory { +export class Factory { private initApp?: InitApp constructor(init?: { initApp?: InitApp }) { @@ -298,7 +298,9 @@ export class Factory { return app } - createMiddleware = (middleware: MiddlewareHandler) => middleware + createMiddleware = ( + middleware: MiddlewareHandler + ): MiddlewareHandler => middleware createHandlers: CreateHandlersInterface = (...handlers: any) => { // @ts-expect-error this should not be typed @@ -306,7 +308,7 @@ export class Factory { } } -export const createFactory = (init?: { +export const createFactory = (init?: { initApp?: InitApp }): Factory => new Factory(init)