Skip to content
Myr

feat(types): allow passing `interface`s as Bindings / Variables (#3136)

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

* feat(types): allow passing `interface`s as Bindings / Variables * test(context): add test for c.var * fix lint warning * test(types): add test for Bindings types

authored by ottomated · committed by GitHub · parent 9987b59 · Browse files · 6 files+119 added−9 removed
This commit

State

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

Diff stats

6 files+119 added−9 removed

  • tests:+106 added−0 removed
  • other:+13 added−9 removed

6 files+119 added−9 removed

modified:src/preset/quick.test.ts+33 added
modified:src/preset/tiny.test.ts+33 added
modified:src/context.test.ts+7 added
86 unmodified lines
87 expect(c.get('foo')).toBe('bar')88 expect(c.get('foo2')).toBe(undefined)89 })9091 it('c.var', async () => {92 expect(c.var.foo).toBe(undefined)93 c.set('foo', 'bar')94 expect(c.var.foo).toBe('bar')95 expect(c.var.foo2).toBe(undefined)96 })9798 it('c.notFound()', async () => {99 const res = c.notFound()100 expect(res).instanceOf(Response)101 })
364 unmodified lines
modified:src/context.ts+11 added-7 removed
263 unmodified lines
264 * })265 * ```266 */267 env: E['Bindings'] = {}268 #var: E['Variables'] | undefined268 #var: Map<unknown, unknown> | undefined269 finalized: boolean = false270 /**271 * `.error` can get the error object from the middleware if the Handler throws an error.272 *
247 unmodified lines
520 * })521 * ```522```523 */524 set: Set<E> = (key: string, value: unknown) => {525 this.#var ??= {}526 this.#var[key as string] = value524 set: Set<E> = (key: unknown, value: unknown) => {525 this.#var ??= new Map()526 this.#var.set(key, value)527 }528529 /**530 * `.get()` can use the value specified by the key.
7 unmodified lines
538 * return c.text(`The message is "${message}"`)539 * })540 * ```541 */542 get: Get<E> = (key: string) => {543 return this.#var ? this.#var[key] : undefined542 get: Get<E> = (key: unknown) => {543 return this.#var ? this.#var.get(key) : undefined544 }545546 /**547 * `.var` can access the value of a variable.
9 unmodified lines
557 get var(): Readonly<558 // eslint-disable-next-line @typescript-eslint/no-explicit-any559 ContextVariableMap & (IsAny<E['Variables']> extends true ? Record<string, any> : E['Variables'])560 > {561 return { ...this.#var } as never561 if (!this.#var) {562 // eslint-disable-next-line @typescript-eslint/no-explicit-any563 return {} as any564 }565 return Object.fromEntries(this.#var)566 }567568 newResponse: NewResponse = (569 data: Data | null,
226 unmodified lines
modified:src/hono.test.ts+33 added
3583 unmodified lines
3584 const res = await a.request('/sub/foo')3585 expect(res.status).toBe(200)3586 })3587})35883589describe('Generics for Bindings and Variables', () => {3590 interface CloudflareBindings {3591 MY_VARIABLE: string3592 }35933594 it('Should not throw type errors', () => {3595 // @ts-expect-error Bindings should extend object3596 new Hono<{3597 Bindings: number3598 }>()35993600 const appWithInterface = new Hono<{3601 Bindings: CloudflareBindings3602 }>()36033604 appWithInterface.get('/', (c) => {3605 expectTypeOf(c.env.MY_VARIABLE).toMatchTypeOf<string>()3606 return c.text('/')3607 })36083609 const appWithType = new Hono<{3610 Bindings: {3611 foo: string3612 }3613 }>()36143615 appWithType.get('/', (c) => {3616 expectTypeOf(c.env.foo).toMatchTypeOf<string>()3617 return c.text('Hello Hono!')3618 })3619 })3620})
modified:src/types.ts+2 added-2 removed
22 unmodified lines
23////// Values //////24////// //////25////////////////////////////////////////2627export type Bindings = Record<string, unknown>28export type Variables = Record<string, unknown>27export type Bindings = object28export type Variables = object2930export type BlankEnv = {}31export type Env = {32 Bindings?: Bindings
1977 unmodified lines