Skip to content
Myr

fix(jwt): fix `JwtVariables` for `ContextVariableMap` (#4764)

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 e4602ad · Browse files · 2 files+31 added−2 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+31 added−2 removed

  • tests:+30 added−1 removed
  • other:+1 added−1 removed

2 files+31 added−2 removed

modified:src/middleware/jwt/index.test.ts+30 added-1 removed
1import { describe } from 'vitest'1import { describe, expectTypeOf } from 'vitest'2import { Hono } from '../../hono'3import { HTTPException } from '../../http-exception'4import { jwt } from '.'5import type { JwtVariables } from '.'67describe('JWT', () => {8 describe('Credentials in header', () => {9 let handlerExecuted: boolean
647 unmodified lines
657 jwt({ secret: 'a-secret' })658 }).toThrow('JWT auth middleware requires options for "alg"')659 })660 })661662 describe('Type tests', () => {663 it('Should infer correct payload type when JwtVariables<T> is specified', () => {664 type User = {665 id: string666 email: string667 isAdmin: boolean668 }669670 const app = new Hono<{ Variables: JwtVariables<User> }>()671672 app.get('/', (c) => {673 const payload = c.var.jwtPayload674 expectTypeOf(payload).toEqualTypeOf<User>()675 return c.json(payload)676 })677 })678679 it('Should infer unknown when JwtVariables is used without type parameter in Variables', () => {680 const app = new Hono<{ Variables: JwtVariables }>()681682 app.get('/', (c) => {683 const payload = c.var.jwtPayload684 expectTypeOf(payload).toEqualTypeOf<any>()685 return c.json(payload)686 })687 })688 })689})
modified:src/middleware/jwt/index.ts+1 added-1 removed