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
Provenance
- Commit
1cee728- Tree
93e86ce- Author
ottomated <31470743+ottomated@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9987b59
6 files changed+119 added−9 removed
modified:src/preset/quick.test.ts+33 added
5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041425 unmodified lines const app = new Hono() expect(getRouterName(app)).toBe('SmartRouter + LinearRouter') })})
describe('Generics for Bindings and Variables', () => { interface CloudflareBindings { MY_VARIABLE: string }
it('Should not throw type errors', () => { // @ts-expect-error Bindings should extend object new Hono<{ Bindings: number }>()
const appWithInterface = new Hono<{ Bindings: CloudflareBindings }>()
appWithInterface.get('/', (c) => { expectTypeOf(c.env.MY_VARIABLE).toMatchTypeOf<string>() return c.text('/') })
const appWithType = new Hono<{ Bindings: { foo: string } }>()
appWithType.get('/', (c) => { expectTypeOf(c.env.foo).toMatchTypeOf<string>() return c.text('Hello Hono!') }) })})modified:src/preset/tiny.test.ts+33 added
5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041425 unmodified lines const app = new Hono() expect(getRouterName(app)).toBe('PatternRouter') })})
describe('Generics for Bindings and Variables', () => { interface CloudflareBindings { MY_VARIABLE: string }
it('Should not throw type errors', () => { // @ts-expect-error Bindings should extend object new Hono<{ Bindings: number }>()
const appWithInterface = new Hono<{ Bindings: CloudflareBindings }>()
appWithInterface.get('/', (c) => { expectTypeOf(c.env.MY_VARIABLE).toMatchTypeOf<string>() return c.text('/') })
const appWithType = new Hono<{ Bindings: { foo: string } }>()
appWithType.get('/', (c) => { expectTypeOf(c.env.foo).toMatchTypeOf<string>() return c.text('Hello Hono!') }) })})modified:src/context.test.ts+7 added
86 unmodified lines87 expect(c.get('foo')).toBe('bar')88 expect(c.get('foo2')).toBe(undefined)89 })90 91+ 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+ })97+98 it('c.notFound()', async () => {99 const res = c.notFound()100 expect(res).instanceOf(Response)101 })364 unmodified linesmodified:src/context.ts+11 added-7 removed
263 unmodified lines264 * })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 lines520 * })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 }528 529 /**530 * `.get()` can use the value specified by the key.7 unmodified lines538 * 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 }545 546 /**547 * `.var` can access the value of a variable.9 unmodified lines557 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 }567 568 newResponse: NewResponse = (569 data: Data | null,226 unmodified linesmodified:src/hono.test.ts+33 added
3583 unmodified lines3584 const res = await a.request('/sub/foo')3585 expect(res.status).toBe(200)3586 })3587 })3588+3589+describe('Generics for Bindings and Variables', () => {3590+ interface CloudflareBindings {3591+ MY_VARIABLE: string3592+ }3593+3594+ it('Should not throw type errors', () => {3595+ // @ts-expect-error Bindings should extend object3596+ new Hono<{3597+ Bindings: number3598+ }>()3599+3600+ const appWithInterface = new Hono<{3601+ Bindings: CloudflareBindings3602+ }>()3603+3604+ appWithInterface.get('/', (c) => {3605+ expectTypeOf(c.env.MY_VARIABLE).toMatchTypeOf<string>()3606+ return c.text('/')3607+ })3608+3609+ const appWithType = new Hono<{3610+ Bindings: {3611+ foo: string3612+ }3613+ }>()3614+3615+ 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 lines23 ////// Values //////24 ////// //////25 ////////////////////////////////////////26 27-export type Bindings = Record<string, unknown>28-export type Variables = Record<string, unknown>27+export type Bindings = object28+export type Variables = object29 30 export type BlankEnv = {}31 export type Env = {32 Bindings?: Bindings1977 unmodified lines