Skip to content
Myr

fix(factory): revert PR #3498 (#3515)

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 fc9cc6d · Browse files · 2 files+24 added−39 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+24 added−39 removed

  • tests:+21 added−25 removed
  • other:+3 added−14 removed

2 files+24 added−39 removed

modified:src/helper/factory/index.test.ts+21 added-25 removed
35 unmodified lines
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 }40 it('Should pass generics types to chained handlers', () => {41 type Bindings = {42 MY_VAR_IN_BINDINGS: string43 }4446 const app = new Hono<{ Bindings: Bindings }>()45 type Variables = {46 MY_VAR: string47 }4848 type Variables = {49 MY_VAR: string50 }49 const app = new Hono<{ Bindings: Bindings }>()5052 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 })51 app.get(52 '/',53 createMiddleware<{ Variables: Variables }>(async (c, next) => {54 await next()55 }),56 createMiddleware(async (c, next) => {57 await next()58 }),59 async (c) => {60 const v = c.get('MY_VAR')61 expectTypeOf(v).toEqualTypeOf<string>()62 }63 )64 })65})6667describe('createHandler', () => {
194 unmodified lines
modified:src/helper/factory/index.ts+3 added-14 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'910type InitApp<E extends Env = Env> = (app: Hono<E>) => void1112export interface CreateHandlersInterface<E extends Env, P extends string> {
223 unmodified lines
236export const createFactory = <E extends Env = any, P extends string = any>(init?: {237 initApp?: InitApp<E>238}): Factory<E, P> => new Factory<E, P>(init)239241// 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}249240export const createMiddleware = <241 E extends Env = any,242 P extends string = string,253 I extends Input = {},254 E2 extends Env = Simplify<AddAnyToEnv<E>>243 I extends Input = {}244>(256 middleware: MiddlewareHandler<E2, P, I>257): MiddlewareHandler<E2, P, I> => createFactory<E2, P>().createMiddleware<I>(middleware)245 middleware: MiddlewareHandler<E, P, I>246): MiddlewareHandler<E, P, I> => createFactory<E, P>().createMiddleware<I>(middleware)