Skip to content
Myr

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

2 files+43 added−21 removed

modified:src/helper/factory/index.test.ts+38 added-18 removed
1 unmodified line
2import { expectTypeOf } from 'vitest'3import { hc } from '../../client'4import type { ClientRequest } from '../../client/types'5import { Hono } from '../../index'6import type { ToSchema, TypedResponse } from '../../types'7import type { ContentfulStatusCode, StatusCode } from '../../utils/http-status'6import type { ExtractSchema, ToSchema, TypedResponse } from '../../types'7import type { ContentfulStatusCode } from '../../utils/http-status'8import type { Equal, Expect } from '../../utils/types'9import { validator } from '../../validator'10import { createFactory, createMiddleware } from './index'1112describe('createMiddleware', () => {
314 unmodified lines
327 })328 })329})330331describe('createFactory', () => {330describe('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)353354 describe('createMiddleware', () => {355 it('Should set the correct type', () => {356 const factory = createFactory()357358 const middleware = factory.createMiddleware(async (_, next) => {359 await next()360 })361362 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})371372describe('Lint rules', () => {
7 unmodified lines
modified:src/helper/factory/index.ts+5 added-3 removed
281 unmodified lines
282 H<E11, P, I10, R>283 ]284}285286export class Factory<E extends Env = any, P extends string = any> {286export class Factory<E extends Env = Env, P extends string = string> {287 private initApp?: InitApp<E>288289 constructor(init?: { initApp?: InitApp<E> }) {290 this.initApp = init?.initApp
6 unmodified lines
297 }298 return app299 }300301 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> => middleware304305 createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {306 // @ts-expect-error this should not be typed307 return handlers.filter((handler) => handler !== undefined)308 }309}310309export const createFactory = <E extends Env = any, P extends string = any>(init?: {311export const createFactory = <E extends Env = Env, P extends string = string>(init?: {312 initApp?: InitApp<E>313}): Factory<E, P> => new Factory<E, P>(init)314315export const createMiddleware = <
6 unmodified lines