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
Provenance
- Commit
f9e6ea7- Tree
ce0f917- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
fc9cc6d
2 files changed+24 added−39 removed
modified:src/helper/factory/index.test.ts+21 added-25 removed
35 unmodified lines36 const url = client.message.$url()37 expect(url.pathname).toBe('/message')38 })39 40- 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+ }44 46- const app = new Hono<{ Bindings: Bindings }>()45+ type Variables = {46+ MY_VAR: string47+ }48 48- type Variables = {49- MY_VAR: string50- }49+ const app = new Hono<{ Bindings: Bindings }>()50 52- const middleware = (_variable: string) =>53- createMiddleware<{ Variables: Variables }>(async (c, next) => {54- await next()55- })56-57- 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 })66 67 describe('createHandler', () => {194 unmodified linesmodified:src/helper/factory/index.ts+3 added-14 removed
4 unmodified lines5 6 /* eslint-disable @typescript-eslint/no-explicit-any */7 import { Hono } from '../../hono'8 import type { Env, H, HandlerResponse, Input, MiddlewareHandler } from '../../types'9-import type { Simplify } from '../../utils/types'9 10 type InitApp<E extends Env = Env> = (app: Hono<E>) => void11 12 export interface CreateHandlersInterface<E extends Env, P extends string> {223 unmodified lines236 export const createFactory = <E extends Env = any, P extends string = any>(init?: {237 initApp?: InitApp<E>238 }): Factory<E, P> => new Factory<E, P>(init)239 241-// Add `any` if it's undefined to relax types242-// { Variables: Variables } => { Bindings: any, Variables: any }243-// { Bindings: Bindings } => { Bindings: Bindings, Variables: any }244-245-type 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-}249-240 export 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)