Skip to content
Myr

fix(factory): relax Bindings and Variables for `createMiddleware` (#3498)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(factory): relax Bindings and Variables types * add a break

authored by Yusuke Wada · committed by GitHub · parent 31b4cd4 · Browse files · 2 files+44 added−3 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+44 added−3 removed

  • tests:+30 added−0 removed
  • other:+14 added−3 removed

2 files+44 added−3 removed

modified:src/helper/factory/index.test.ts+30 added
34 unmodified lines
35 const client = hc<typeof route>('http://localhost')36 const url = client.message.$url()37 expect(url.pathname).toBe('/message')38 })3940 describe('Relax types for Bindings and Variables', () => {41 it('Should not throw a type error', () => {42 type Bindings = {43 MY_VAR_IN_BINDINGS: string44 }4546 const app = new Hono<{ Bindings: Bindings }>()4748 type Variables = {49 MY_VAR: string50 }5152 const middleware = (_variable: string) =>53 createMiddleware<{ Variables: Variables }>(async (c, next) => {54 await next()55 })5657 app.get(58 '/',59 createMiddleware<{ Bindings: Bindings }>(async (c, next) => {60 const mw = middleware(c.env.MY_VAR_IN_BINDINGS)61 await mw(c, next) // `c` does not throw an error62 }),63 (c) => {64 return c.json({})65 }66 )67 })68 })69})7071describe('createHandler', () => {72 const mw = (message: string) =>
193 unmodified lines
modified:src/helper/factory/index.ts+14 added-3 removed
4 unmodified lines
56/* eslint-disable @typescript-eslint/no-explicit-any */7import { Hono } from '../../hono'8import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../types'9import type { Simplify } from '../../utils/types'1011type InitApp<E extends Env = Env> = (app: Hono<E>) => void1213export interface CreateHandlersInterface<E extends Env, P extends string> {
223 unmodified lines
237export const createFactory = <E extends Env = any, P extends string = any>(init?: {238 initApp?: InitApp<E>239}): Factory<E, P> => new Factory<E, P>(init)240241// Add `any` if it's undefined to relax types242// { Variables: Variables } => { Bindings: any, Variables: any }243// { Bindings: Bindings } => { Bindings: Bindings, Variables: any }244245type AddAnyToEnv<T extends { Variables?: object; Bindings?: object }> = {246 Bindings: undefined extends T['Bindings'] ? any : T['Bindings']247 Variables: undefined extends T['Variables'] ? any : T['Variables']248}249250export const createMiddleware = <251 E extends Env = any,252 P extends string = string,243 I extends Input = {}253 I extends Input = {},254 E2 extends Env = Simplify<AddAnyToEnv<E>>255>(245 middleware: MiddlewareHandler<E, P, I>246): MiddlewareHandler<E, P, I> => createFactory<E, P>().createMiddleware<I>(middleware)256 middleware: MiddlewareHandler<E2, P, I>257): MiddlewareHandler<E2, P, I> => createFactory<E2, P>().createMiddleware<I>(middleware)