fix(factory): correct the type of `factory.createMiddleware()` (#3849)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 3fba7a5 · Browse files · 2 files·+43 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+43 added−21 removed
- tests:+38 added−18 removed
- other:+5 added−3 removed
Provenance
- Commit
7a7918d- Tree
c5d3201- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
3fba7a5
2 files changed+43 added−21 removed
modified:src/helper/factory/index.test.ts+38 added-18 removed
1 unmodified line2 import { expectTypeOf } from 'vitest'3 import { hc } from '../../client'4 import type { ClientRequest } from '../../client/types'5 import { Hono } from '../../index'6-import type { ToSchema, TypedResponse } from '../../types'7-import type { ContentfulStatusCode, StatusCode } from '../../utils/http-status'6+import type { ExtractSchema, ToSchema, TypedResponse } from '../../types'7+import type { ContentfulStatusCode } from '../../utils/http-status'8+import type { Equal, Expect } from '../../utils/types'9 import { validator } from '../../validator'10 import { createFactory, createMiddleware } from './index'11 12 describe('createMiddleware', () => {314 unmodified lines327 })328 })329 })330 331+describe('createFactory', () => {330-describe('createApp', () => {331- type Env = { Variables: { foo: string } }332- const factory = createFactory<Env>({333- initApp: (app) => {334- app.use((c, next) => {335- c.set('foo', 'bar')336- return next()332+ describe('createApp', () => {333+ type Env = { Variables: { foo: string } }334+ const factory = createFactory<Env>({335+ initApp: (app) => {336+ app.use((c, next) => {337+ c.set('foo', 'bar')338+ return next()339+ })340+ },341+ })342+ const app = factory.createApp()343+ it('Should set the correct type and initialize the app', async () => {344+ app.get('/', (c) => {345+ expectTypeOf(c.var.foo).toEqualTypeOf<string>()346+ return c.text(c.var.foo)347 })338- },348+ const res = await app.request('/')349+ expect(res.status).toBe(200)350+ expect(await res.text()).toBe('bar')351+ })352 })340- const app = factory.createApp()341- it('Should set the correct type and initialize the app', async () => {342- app.get('/', (c) => {343- expectTypeOf(c.var.foo).toEqualTypeOf<string>()344- return c.text(c.var.foo)353+354+ describe('createMiddleware', () => {355+ it('Should set the correct type', () => {356+ const factory = createFactory()357+358+ const middleware = factory.createMiddleware(async (_, next) => {359+ await next()360+ })361+362+ const routes = new Hono().use('*', middleware)363+ type Actual = ExtractSchema<typeof routes>364+ type Expected = {365+ '*': {}366+ }367+ type verify = Expect<Equal<Expected, Actual>>368 })346- const res = await app.request('/')347- expect(res.status).toBe(200)348- expect(await res.text()).toBe('bar')369 })370 })371 372 describe('Lint rules', () => {7 unmodified linesmodified:src/helper/factory/index.ts+5 added-3 removed
281 unmodified lines282 H<E11, P, I10, R>283 ]284 }285 286-export class Factory<E extends Env = any, P extends string = any> {286+export class Factory<E extends Env = Env, P extends string = string> {287 private initApp?: InitApp<E>288 289 constructor(init?: { initApp?: InitApp<E> }) {290 this.initApp = init?.initApp6 unmodified lines297 }298 return app299 }300 301- createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware301+ createMiddleware = <I extends Input = {}>(302+ middleware: MiddlewareHandler<E, P, I>303+ ): MiddlewareHandler<E, P, I> => middleware304 305 createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {306 // @ts-expect-error this should not be typed307 return handlers.filter((handler) => handler !== undefined)308 }309 }310 309-export const createFactory = <E extends Env = any, P extends string = any>(init?: {311+export const createFactory = <E extends Env = Env, P extends string = string>(init?: {312 initApp?: InitApp<E>313 }): Factory<E, P> => new Factory<E, P>(init)314 315 export const createMiddleware = <6 unmodified lines