types.test.ts
This file
Path
src/types.test.ts
4,139 lines 路 125.4 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
1/* eslint-disable @typescript-eslint/no-unused-vars */2import { expectTypeOf } from 'vitest'3import { hc } from './client'4import { Context } from './context'5import { createMiddleware } from './helper/factory'6import { testClient } from './helper/testing'7import { Hono } from './hono'8import { poweredBy } from './middleware/powered-by'9import type {10 AddParam,11 Env,12 ExtractSchema,13 Handler,14 InputToDataByTarget,15 MergePath,16 MergeSchemaPath,17 MiddlewareHandler,18 ParamKeyToRecord,19 ParamKeys,20 RemoveQuestion,21 ResponseFormat,22 ToSchema,23 TypedResponse,24} from './types'25import type { ContentfulStatusCode, StatusCode } from './utils/http-status'26import type { Equal, Expect } from './utils/types'27import { validator } from './validator'2829describe('Env', () => {30 test('Env', () => {31 type E = {32 Variables: {33 foo: string34 }35 Bindings: {36 FLAG: boolean37 }38 }39 const app = new Hono<E>()40 app.use('*', poweredBy())41 app.get('/', (c) => {42 const foo = c.get('foo')43 expectTypeOf(foo).toEqualTypeOf<string>()44 const FLAG = c.env.FLAG45 expectTypeOf(FLAG).toEqualTypeOf<boolean>()46 return c.text('foo')47 })48 })49})5051describe('HandlerInterface', () => {52 type Env = {}5354 type Payload = { foo: string; bar: boolean }5556 describe('no path pattern', () => {57 const app = new Hono<Env>()58 const middleware: MiddlewareHandler<59 Env,60 '/',61 {62 in: { json: Payload }63 out: { json: Payload }64 }65 > = async (_c, next) => {66 await next()67 }68 test('Context', () => {69 const route = app.get(middleware, (c) => {70 type Expected = Context<71 Env,72 '/',73 {74 in: { json: Payload }75 out: { json: Payload }76 }77 >78 expectTypeOf(c).toEqualTypeOf<Expected>()79 return c.json({80 message: 'Hello!',81 })82 })83 app.get(middleware, (c) => {84 const data = c.req.valid('json')85 expectTypeOf(data).toEqualTypeOf<Payload>()86 return c.json({87 message: 'Hello!',88 })89 })90 type Actual = ExtractSchema<typeof route>91 type Expected = {92 '/': {93 $get: {94 input: {95 json: Payload96 }97 output: {98 message: string99 }100 outputFormat: 'json'101 status: ContentfulStatusCode102 }103 }104 }105 type verify = Expect<Equal<Expected, Actual>>106 })107 })108109 describe('path pattern', () => {110 const app = new Hono<Env>()111 const middleware: MiddlewareHandler<112 Env,113 '/foo',114 { in: { json: Payload }; out: { json: Payload } },115 never116 > = async (_c, next) => {117 await next()118 }119120 test('Context and AppType', () => {121 const route = app.get('/foo', middleware, (c) => {122 type Expected = Context<Env, '/foo', { in: { json: Payload }; out: { json: Payload } }>123 expectTypeOf(c).toEqualTypeOf<Expected>()124 return c.json({125 message: 'Hello!',126 })127 })128 type Actual = ExtractSchema<typeof route>129 type Expected = {130 '/foo': {131 $get: {132 input: {133 json: {134 foo: string135 bar: boolean136 }137 }138 output: {139 message: string140 }141 outputFormat: 'json'142 status: ContentfulStatusCode143 }144 }145 }146 type verify = Expect<Equal<Expected, Actual>>147 })148 })149150 describe('With path parameters', () => {151 const app = new Hono<Env>()152 const middleware: MiddlewareHandler<Env, '/post/:id', {}, never> = async (_c, next) => {153 await next()154 }155 it('Should have the `param` type', () => {156 const route = app.get('/post/:id', middleware, (c) => {157 return c.text('foo')158 })159 type Actual = ExtractSchema<typeof route>160 type Expected = {161 '/post/:id': {162 $get: {163 input: {164 param: {165 id: string166 }167 }168 output: 'foo'169 outputFormat: 'text'170 status: ContentfulStatusCode171 }172 }173 }174 type verify = Expect<Equal<Expected, Actual>>175 })176 })177178 describe('Without path', () => {179 const app = new Hono<Env>().basePath('/foo/:foo')180181 it('With basePath and path params', () => {182 const route = app.get(async (c) => {183 const foo = c.req.param('foo')184 expect(typeof foo).toBe('string')185 return c.text(foo)186 })187 type Actual = ExtractSchema<typeof route>188189 type Expected = {190 '/foo/:foo': {191 $get: {192 input: {193 param: {194 foo: string195 }196 }197 output: string198 outputFormat: 'text'199 status: ContentfulStatusCode200 }201 }202 }203 type verify = Expect<Equal<Expected, Actual>>204 })205206 it('Chained', () => {207 const route = app.post('/books/:id').get((c) => {208 const id = c.req.param('id')209 return c.text(id)210 })211 type Actual = ExtractSchema<typeof route>212 type Expected = {213 '/foo/:foo/books/:id': {214 $get: {215 input: {216 param: {217 id: string218 } & {219 foo: string220 }221 }222 output: string223 outputFormat: 'text'224 status: ContentfulStatusCode225 }226 }227 } & {228 '/foo/:foo/books/:id': {229 $post: {230 input: {231 param: {232 id: string233 } & {234 foo: string235 }236 }237 output: {}238 outputFormat: ResponseFormat239 status: StatusCode240 }241 }242 }243 type verify = Expect<Equal<Expected, Actual>>244 })245246 it('should use the last registered path for a pathless handler', () => {247 const app = new Hono()248 .get('/foo', (c) => c.text('foo'))249 .get('/bar', (c) => c.text('bar'))250 .post((c) => c.text('baz'))251252 type Actual = ExtractSchema<typeof app>253 type Expected = {254 '/foo': {255 $get: {256 input: {}257 output: 'foo'258 outputFormat: 'text'259 status: ContentfulStatusCode260 }261 }262 } & {263 '/bar': {264 $get: {265 input: {}266 output: 'bar'267 outputFormat: 'text'268 status: ContentfulStatusCode269 }270 }271 } & {272 '/bar': {273 $post: {274 input: {}275 output: 'baz'276 outputFormat: 'text'277 status: ContentfulStatusCode278 }279 }280 }281 type verify = Expect<Equal<Expected, Actual>>282 })283 })284})285286describe('OnHandlerInterface', () => {287 const app = new Hono()288 test('Context', () => {289 const middleware: MiddlewareHandler<290 Env,291 '/purge',292 { in: { form: { id: string } }; out: { form: { id: number } } }293 > = async (_c, next) => {294 await next()295 }296 const route = app.on('PURGE', '/purge', middleware, (c) => {297 const data = c.req.valid('form')298 expectTypeOf(data).toEqualTypeOf<{ id: number }>()299 return c.json({300 success: true,301 })302 })303 type Actual = ExtractSchema<typeof route>304 type Expected = {305 '/purge': {306 $purge: {307 input: {308 form: {309 id: string310 }311 }312 output: {313 success: true314 }315 outputFormat: 'json'316 status: ContentfulStatusCode317 }318 }319 }320 type verify = Expect<Equal<Expected, Actual>>321 })322323 test('app.on(method[], path, middleware, handler)', () => {324 const middleware: MiddlewareHandler<{ Variables: { foo: string } }> = async () => {}325 const route = app.on(['GET', 'POST'], '/multi-method', middleware, (c) => {326 return c.json({ success: true })327 })328 type Actual = ExtractSchema<typeof route>329 type Expected = {330 '/multi-method': {331 $get: {332 input: {}333 output: {334 success: true335 }336 outputFormat: 'json'337 status: ContentfulStatusCode338 }339 $post: {340 input: {}341 output: {342 success: true343 }344 outputFormat: 'json'345 status: ContentfulStatusCode346 }347 }348 }349 type verify = Expect<Equal<Expected, Actual>>350 })351352 test('app.on(method, path[], middleware, handler) should not throw a type error', () => {353 const middleware: MiddlewareHandler<{ Variables: { foo: string } }> = async () => {}354 app.on('GET', ['/a', '/b'], middleware, (c) => {355 expectTypeOf(c.var.foo).toEqualTypeOf<string>()356 return c.json({})357 })358 })359360 test('app.on(method, path[], handler).get(pathless handler) - pathless handler should use last path', () => {361 const route = app362 .on('GET', ['/a', '/b'], (c) => {363 return c.json({ first: true })364 })365 .get((c) => {366 return c.json({ second: true })367 })368 type Actual = ExtractSchema<typeof route>369 type Expected = {370 '/a': {371 $get: {372 input: {}373 output: {374 first: true375 }376 outputFormat: 'json'377 status: ContentfulStatusCode378 }379 }380 '/b': {381 $get: {382 input: {}383 output: {384 first: true385 }386 outputFormat: 'json'387 status: ContentfulStatusCode388 }389 }390 } & {391 '/b': {392 $get: {393 input: {}394 output: {395 second: true396 }397 outputFormat: 'json'398 status: ContentfulStatusCode399 }400 }401 }402 type verify = Expect<Equal<Expected, Actual>>403 })404405 test('app.on(method, path, ...10 handlers) - last handler response type should be inferred', () => {406 const noop: MiddlewareHandler = async (_c, next) => {407 await next()408 }409 const route = app.on('GET', '/x10', noop, noop, noop, noop, noop, noop, noop, noop, noop, (c) =>410 c.json({ success: true })411 )412 type Actual = ExtractSchema<typeof route>['/x10']['$get']['output']413 type Expected = { success: true }414 type verify = Expect<Equal<Expected, Actual>>415 })416417 test('app.on(method[], path, ...9 handlers) - last handler response type should be inferred', () => {418 const noop: MiddlewareHandler = async (_c, next) => {419 await next()420 }421 const route = app.on(['GET'], '/x9-arr', noop, noop, noop, noop, noop, noop, noop, noop, (c) =>422 c.json({ success: true })423 )424 type Actual = ExtractSchema<typeof route>['/x9-arr']['$get']['output']425 type Expected = { success: true }426 type verify = Expect<Equal<Expected, Actual>>427 })428429 test('app.on(method[], path, ...10 handlers) - last handler response type should be inferred', () => {430 const noop: MiddlewareHandler = async (_c, next) => {431 await next()432 }433 const route = app.on(434 ['GET'],435 '/x10-arr',436 noop,437 noop,438 noop,439 noop,440 noop,441 noop,442 noop,443 noop,444 noop,445 (c) => c.json({ success: true })446 )447 type Actual = ExtractSchema<typeof route>['/x10-arr']['$get']['output']448 type Expected = { success: true }449 type verify = Expect<Equal<Expected, Actual>>450 })451})452453describe('TypedResponse', () => {454 test('unknown', () => {455 type Actual = TypedResponse456 type Expected = {457 _data: unknown458 _status: StatusCode459 _format: ResponseFormat460 }461 type verify = Expect<Equal<Expected, Actual>>462 })463464 test('text auto infer', () => {465 type Actual = TypedResponse<string>466 type Expected = {467 _data: string468 _status: StatusCode469 _format: 'text'470 }471 type verify = Expect<Equal<Expected, Actual>>472 })473474 test('json auto infer', () => {475 type Actual = TypedResponse<{ ok: true }>476 type Expected = {477 _data: { ok: true }478 _status: StatusCode479 _format: 'json'480 }481 type verify = Expect<Equal<Expected, Actual>>482 })483})484485describe('Schema', () => {486 test('Schema', () => {487 type AppType = Hono<488 Env,489 ToSchema<490 'post',491 '/api/posts/:id',492 {493 in: {494 json: {495 id: number496 title: string497 }498 }499 },500 TypedResponse<501 {502 message: string503 success: boolean504 },505 StatusCode,506 'json'507 >508 >509 >510511 type Actual = ExtractSchema<AppType>512 type Expected = {513 '/api/posts/:id': {514 $post: {515 input: {516 json: {517 id: number518 title: string519 }520 } & {521 param: {522 id: string523 }524 }525 output: {526 message: string527 success: boolean528 }529 outputFormat: 'json'530 status: StatusCode531 }532 }533 }534 type verify = Expect<Equal<Expected, Actual>>535 })536})537538describe('Support c.json(undefined)', () => {539 it('Should return a correct type', () => {540 const app = new Hono().get('/this/is/a/test', async (c) => {541 return c.json(undefined)542 })543 type Actual = ExtractSchema<typeof app>544 type Expected = {545 '/this/is/a/test': {546 $get: {547 input: {}548 output: never549 outputFormat: 'json'550 status: ContentfulStatusCode551 }552 }553 }554 type verify = Expect<Equal<Expected, Actual>>555 })556})557558describe('Test types of Handler', () => {559 type E = {560 Variables: {561 foo: number562 }563 }564565 const url = 'http://localhost/'566567 test('Env', async () => {568 const app = new Hono<E>()569 const handler: Handler<E> = (c) => {570 const foo = c.get('foo')571 expectTypeOf(foo).toEqualTypeOf<number>()572 const id = c.req.param('id')573 expectTypeOf(id).toEqualTypeOf<string | undefined>()574 return c.text('Hi')575 }576 app.get('/', handler)577 const res = await app.request(url)578 expect(res.status).toBe(200)579 })580581 test('Env, Path', async () => {582 const app = new Hono<E>()583 const handler: Handler<E, '/'> = (c) => {584 const foo = c.get('foo')585 expectTypeOf(foo).toEqualTypeOf<number>()586 return c.text('Hi')587 }588 app.get('/', handler)589590 const res = await app.request(url)591 expect(res.status).toBe(200)592 })593594 type User = {595 name: string596 age: number597 }598599 test('Env, Path, Type', async () => {600 const app = new Hono<E>()601 const handler: Handler<E, '/', { in: { json: User }; out: { json: User } }> = (c) => {602 const foo = c.get('foo')603 expectTypeOf(foo).toEqualTypeOf<number>()604 const { name } = c.req.valid('json')605 expectTypeOf(name).toEqualTypeOf<string>()606 return c.text('Hi')607 }608 })609})610611describe('`json()`', () => {612 const app = new Hono<{ Variables: { foo: string } }>()613 app.get('/post/:id', (c) => {614 c.req.param('id')615 const id = c.req.param('id')616 return c.text('foo')617 })618619 test('json', () => {620 const route = app.get('/hello', (c) => {621 return c.json({622 message: 'Hello!',623 })624 })625 type Actual = ExtractSchema<typeof route>626 type Expected = {627 '/hello': {628 $get: {629 input: {}630 output: {631 message: string632 }633 outputFormat: 'json'634 status: ContentfulStatusCode635 }636 }637 }638 type verify = Expect<Equal<Expected, Actual>>639 })640641 test('json with specific status code', () => {642 const route = app.get('/hello', (c) => {643 return c.json(644 {645 message: 'Hello!',646 },647 200648 )649 })650 type Actual = ExtractSchema<typeof route>651 type Expected = {652 '/hello': {653 $get: {654 input: {}655 output: {656 message: string657 }658 outputFormat: 'json'659 status: 200660 }661 }662 }663 type verify = Expect<Equal<Expected, Actual>>664 })665})666667describe('Path parameters', () => {668 test('ParamKeys', () => {669 type Actual = ParamKeys<'/posts/:postId/comment/:commentId'>670 type Expected = 'postId' | 'commentId'671 type verify = Expect<Equal<Expected, Actual>>672 })673674 describe('ParamKeyToRecord', () => {675 test('With ?', () => {676 type Actual = ParamKeyToRecord<'/animal/type?'>677 type Expected = { [K in '/animal/type']: string | undefined }678 type verify = Expect<Equal<Expected, Actual>>679 })680 test('Without ?', () => {681 type Actual = ParamKeyToRecord<'/animal/type'>682 type Expected = { [K in '/animal/type']: string }683 type verify = Expect<Equal<Expected, Actual>>684 })685 })686687 describe('Path parameters in app', () => {688 test('Optional parameters - /api/:a/:b?', () => {689 const app = new Hono()690 const routes = app.get('/api/:a/:b?', (c) => {691 const a = c.req.param('a')692 const b = c.req.param('b')693 expectTypeOf(a).toEqualTypeOf<string>()694 expectTypeOf(b).toEqualTypeOf<string | undefined>()695 return c.json({ a, b })696 })697 type T = ExtractSchema<typeof routes>698 type Output = T['/api/:a/:b?']['$get']['output']699 type Expected = {700 a: string701 b: string | undefined702 }703 type verify = Expect<Equal<Expected, Output>>704 })705 })706})707708describe('For HonoRequest', () => {709 type Input = {710 json: {711 id: number712 title: string713 }714 query: {715 page: string716 }717 }718719 test('InputToDataByType with value', () => {720 type Actual = InputToDataByTarget<Input, 'json'>721 type Expected = {722 id: number723 title: string724 }725 type verify = Expect<Equal<Expected, Actual>>726 })727728 test('InputToDataByType without value', () => {729 type Actual = InputToDataByTarget<Input, 'form'>730 type verify = Expect<Equal<never, Actual>>731 })732733 test('RemoveQuestion', () => {734 type Actual = RemoveQuestion<'/animal/type?'>735 type verify = Expect<Equal<'/animal/type', Actual>>736 })737})738739describe('AddParam', () => {740 it('Should add params to input correctly', () => {741 type Actual = AddParam<742 {743 param: {744 id: string745 }746 } & {747 query: {748 page: string749 }750 },751 '/:id'752 >753 type Expected = {754 query: {755 page: string756 }757 } & {758 param: {759 id: string760 }761 }762 type verify = Expect<Equal<Expected, Actual>>763 })764})765766describe('ToSchema', () => {767 it('Should convert parameters to schema correctly', () => {768 type Actual = ToSchema<769 'get',770 '/:id',771 { in: { param: { id: string }; query: { page: string } } },772 TypedResponse<{}>773 >774 type Expected = {775 '/:id': {776 $get: {777 input: {778 param: {779 id: string780 }781 query: {782 page: string783 }784 }785 output: {}786 outputFormat: 'json'787 status: StatusCode788 }789 }790 }791 type verify = Expect<Equal<Expected, Actual>>792 })793})794795describe('MergePath', () => {796 it('Should merge paths correctly', () => {797 type path1 = MergePath<'/api', '/book'>798 type verify1 = Expect<Equal<'/api/book', path1>>799 type path2 = MergePath<'/api/', '/book'>800 type verify2 = Expect<Equal<'/api/book', path2>>801 type path3 = MergePath<'/api/', '/'>802 type verify3 = Expect<Equal<'/api/', path3>>803 type path4 = MergePath<'/api', '/'>804 type verify4 = Expect<Equal<'/api', path4>>805 type path5 = MergePath<'/', ''>806 type verify5 = Expect<Equal<'/', path5>>807 type path6 = MergePath<'', '/'>808 type verify6 = Expect<Equal<'/', path6>>809 type path7 = MergePath<'/', '/'>810 type verify7 = Expect<Equal<'/', path7>>811 type path8 = MergePath<'', ''>812 type verify8 = Expect<Equal<'/', path8>>813 })814})815816describe('MergeSchemaPath', () => {817 it('Should merge schema and sub path correctly', () => {818 type Sub = ToSchema<819 'post',820 '/posts',821 {822 in: {823 json: {824 id: number825 title: string826 }827 }828 },829 TypedResponse<{830 message: string831 }>832 > &833 ToSchema<834 'get',835 '/posts',836 {},837 TypedResponse<{838 ok: boolean839 }>840 >841842 type Actual = MergeSchemaPath<Sub, '/api'>843844 type Expected = {845 '/api/posts': {846 $post: {847 input: {848 json: {849 id: number850 title: string851 }852 }853 output: {854 message: string855 }856 outputFormat: 'json'857 status: StatusCode858 }859 $get: {860 input: {}861 output: {862 ok: boolean863 }864 outputFormat: 'json'865 status: StatusCode866 }867 }868 }869870 type verify = Expect<Equal<Expected, Actual>>871 })872873 it('Should merge schema which has params and sub path does not have params', () => {874 type Actual = MergeSchemaPath<875 {876 '/': {877 $get: {878 input: {879 param: {880 id: string881 }882 query: {883 page: string884 }885 }886 output: {}887 outputFormat: 'json'888 status: StatusCode889 }890 }891 },892 '/something'893 >894 type Expected = {895 '/something': {896 $get: {897 input: {898 param: {899 id: string900 }901 query: {902 page: string903 }904 }905 output: {}906 outputFormat: 'json'907 status: StatusCode908 }909 }910 }911 type verify = Expect<Equal<Expected, Actual>>912 })913914 type GetKey<T> = T extends Record<infer K, unknown> ? K : never915916 it('Should remove a slash - `/` + `/`', () => {917 type Sub = ToSchema<'get', '/', {}, TypedResponse<{}>>918 type Actual = MergeSchemaPath<Sub, '/'>919 type verify = Expect<Equal<'/', GetKey<Actual>>>920 })921922 it('Should remove a slash - `/tags` + `/`', () => {923 type Sub = ToSchema<'get', '/tags', {}, TypedResponse<{}>>924 type Actual = MergeSchemaPath<Sub, '/'>925 type verify = Expect<Equal<'/tags', GetKey<Actual>>>926 })927928 it('Should remove a slash - `/` + `/tags`', () => {929 type Sub = ToSchema<'get', '/', {}, TypedResponse<{}>>930 type Actual = MergeSchemaPath<Sub, '/tags'>931 type verify = Expect<Equal<'/tags', GetKey<Actual>>>932 })933934 test('MergeSchemaPath - SubPath has path params', () => {935 type Actual = MergeSchemaPath<ToSchema<'get', '/', {}, TypedResponse>, '/a/:b'>936 type Expected = {937 '/a/:b': {938 $get: {939 input: {940 param: {941 b: string942 }943 }944 output: {}945 outputFormat: ResponseFormat946 status: StatusCode947 }948 }949 }950 type verify = Expect<Equal<Expected, Actual>>951 })952953 test('MergeSchemaPath - Path and SubPath have path params', () => {954 type Actual = MergeSchemaPath<ToSchema<'get', '/c/:d', {}, TypedResponse<{}>>, '/a/:b'>955 type Expected = {956 '/a/:b/c/:d': {957 $get: {958 input: {959 param: {960 d: string961 } & {962 b: string963 }964 }965 output: {}966 outputFormat: 'json'967 status: StatusCode968 }969 }970 }971 type verify = Expect<Equal<Expected, Actual>>972 })973974 test('MergeSchemaPath - Path and SubPath have regexp path params', () => {975 type Actual = MergeSchemaPath<ToSchema<'get', '/c/:d{.+}', {}, TypedResponse<{}>>, '/a/:b{.+}'>976 type Expected = {977 '/a/:b{.+}/c/:d{.+}': {978 $get: {979 input: {980 param: {981 d: string982 } & {983 b: string984 }985 }986 output: {}987 outputFormat: 'json'988 status: StatusCode989 }990 }991 }992 type verify = Expect<Equal<Expected, Actual>>993 })994995 test('MergeSchemaPath - Method has Endpoints as Union', () => {996 type Actual = MergeSchemaPath<997 {998 '/': {999 $get:1000 | {1001 input: {}1002 output: {1003 error: string1004 }1005 outputFormat: 'json'1006 status: 4041007 }1008 | {1009 input: {}1010 output: {1011 success: boolean1012 }1013 outputFormat: 'json'1014 status: 2001015 }1016 }1017 },1018 '/api/hello'1019 >1020 type Expected = {1021 '/api/hello': {1022 $get:1023 | {1024 input: {}1025 output: {1026 error: string1027 }1028 outputFormat: 'json'1029 status: 4041030 }1031 | {1032 input: {}1033 output: {1034 success: boolean1035 }1036 outputFormat: 'json'1037 status: 2001038 }1039 }1040 }1041 type verify = Expect<Equal<Expected, Actual>>1042 })1043})10441045describe('Different types using json()', () => {1046 describe('no path pattern', () => {1047 const app = new Hono()10481049 test('Three different types', () => {1050 const route = app.get((c) => {1051 const flag = false1052 if (flag) {1053 return c.json({1054 ng: true,1055 })1056 }1057 if (!flag) {1058 return c.json({1059 ok: true,1060 })1061 }1062 return c.json({1063 default: true,1064 })1065 })1066 type Actual = ExtractSchema<typeof route>1067 type Expected = {1068 '/': {1069 $get:1070 | {1071 input: {}1072 output: {1073 ng: true1074 }1075 outputFormat: 'json'1076 status: ContentfulStatusCode1077 }1078 | {1079 input: {}1080 output: {1081 ok: true1082 }1083 outputFormat: 'json'1084 status: ContentfulStatusCode1085 }1086 | {1087 input: {}1088 output: {1089 default: true1090 }1091 outputFormat: 'json'1092 status: ContentfulStatusCode1093 }1094 }1095 }1096 type verify = Expect<Equal<Expected, Actual>>1097 })10981099 test('Three different types and status codes', () => {1100 const route = app.get((c) => {1101 const flag = false1102 if (flag) {1103 return c.json(1104 {1105 ng: true,1106 },1107 4001108 )1109 }1110 if (!flag) {1111 return c.json(1112 {1113 ok: true,1114 },1115 2001116 )1117 }1118 return c.json({1119 default: true,1120 })1121 })1122 type Actual = ExtractSchema<typeof route>1123 type Expected = {1124 '/': {1125 $get:1126 | {1127 input: {}1128 output: {1129 ng: true1130 }1131 outputFormat: 'json'1132 status: 4001133 }1134 | {1135 input: {}1136 output: {1137 ok: true1138 }1139 outputFormat: 'json'1140 status: 2001141 }1142 | {1143 input: {}1144 output: {1145 default: true1146 }1147 outputFormat: 'json'1148 status: ContentfulStatusCode1149 }1150 }1151 }1152 type verify = Expect<Equal<Expected, Actual>>1153 })1154 })11551156 describe('path pattern', () => {1157 const app = new Hono()11581159 test('Three different types', () => {1160 const route = app.get('/foo', (c) => {1161 const flag = false1162 if (flag) {1163 return c.json({1164 ng: true,1165 })1166 }1167 if (!flag) {1168 return c.json({1169 ok: true,1170 })1171 }1172 return c.json({1173 default: true,1174 })1175 })1176 type Actual = ExtractSchema<typeof route>1177 type Expected = {1178 '/foo': {1179 $get:1180 | {1181 input: {}1182 output: {1183 ng: true1184 }1185 outputFormat: 'json'1186 status: ContentfulStatusCode1187 }1188 | {1189 input: {}1190 output: {1191 ok: true1192 }1193 outputFormat: 'json'1194 status: ContentfulStatusCode1195 }1196 | {1197 input: {}1198 output: {1199 default: true1200 }1201 outputFormat: 'json'1202 status: ContentfulStatusCode1203 }1204 }1205 }1206 type verify = Expect<Equal<Expected, Actual>>1207 })12081209 test('Three different types and status codes', () => {1210 const route = app.get('/foo', (c) => {1211 const flag = false1212 if (flag) {1213 return c.json(1214 {1215 ng: true,1216 },1217 4001218 )1219 }1220 if (!flag) {1221 return c.json(1222 {1223 ok: true,1224 },1225 2001226 )1227 }1228 return c.json({1229 default: true,1230 })1231 })1232 type Actual = ExtractSchema<typeof route>1233 type Expected = {1234 '/foo': {1235 $get:1236 | {1237 input: {}1238 output: {1239 ng: true1240 }1241 outputFormat: 'json'1242 status: 4001243 }1244 | {1245 input: {}1246 output: {1247 ok: true1248 }1249 outputFormat: 'json'1250 status: 2001251 }1252 | {1253 input: {}1254 output: {1255 default: true1256 }1257 outputFormat: 'json'1258 status: ContentfulStatusCode1259 }1260 }1261 }1262 type verify = Expect<Equal<Expected, Actual>>1263 })1264 })1265})12661267describe('json() in an async handler', () => {1268 const app = new Hono()12691270 test('json', () => {1271 const route = app.get(async (c) => {1272 return c.json({1273 ok: true,1274 })1275 })1276 type Actual = ExtractSchema<typeof route>1277 type Expected = {1278 '/': {1279 $get: {1280 input: {}1281 output: {1282 ok: true1283 }1284 outputFormat: 'json'1285 status: ContentfulStatusCode1286 }1287 }1288 }1289 type verify = Expect<Equal<Expected, Actual>>1290 })12911292 test('json with specific status code', () => {1293 const route = app.get(async (c) => {1294 return c.json(1295 {1296 ok: true,1297 },1298 2001299 )1300 })1301 type Actual = ExtractSchema<typeof route>1302 type Expected = {1303 '/': {1304 $get: {1305 input: {}1306 output: {1307 ok: true1308 }1309 outputFormat: 'json'1310 status: 2001311 }1312 }1313 }1314 type verify = Expect<Equal<Expected, Actual>>1315 })1316})13171318/**1319 * Other tests for `c.var` are written in `hono.test.ts`.1320 * This tests are only for types.1321 */1322describe('c.var with chaining - test only types', () => {1323 const mw1 = createMiddleware<1324 { Variables: { foo1: string } },1325 string,1326 { out: { query: { bar1: number } } }1327 >(async () => {})1328 const mw2 = createMiddleware<1329 { Variables: { foo2: string } },1330 string,1331 { out: { query: { bar2: number } } }1332 >(async () => {})1333 const mw3 = createMiddleware<1334 { Variables: { foo3: string } },1335 string,1336 { out: { query: { bar3: number } } }1337 >(async () => {})1338 const mw4 = createMiddleware<1339 { Variables: { foo4: string } },1340 string,1341 { out: { query: { bar4: number } } }1342 >(async () => {})1343 const mw5 = createMiddleware<1344 { Variables: { foo5: string } },1345 string,1346 { out: { query: { bar5: number } } }1347 >(async () => {})1348 const mw6 = createMiddleware<1349 { Variables: { foo6: string } },1350 string,1351 { out: { query: { bar6: number } } }1352 >(async () => {})1353 const mw7 = createMiddleware<1354 { Variables: { foo7: string } },1355 string,1356 { out: { query: { bar7: number } } }1357 >(async () => {})1358 const mw8 = createMiddleware<1359 { Variables: { foo8: string } },1360 string,1361 { out: { query: { bar8: number } } }1362 >(async () => {})1363 const mw9 = createMiddleware<1364 { Variables: { foo9: string } },1365 string,1366 { out: { query: { bar9: number } } }1367 >(async () => {})1368 const mw10 = createMiddleware<1369 { Variables: { foo10: string } },1370 string,1371 { out: { query: { bar10: number } } }1372 >(async () => {})13731374 it('Should not throw type errors', () => {1375 // app.get(handler...)13761377 new Hono().get(mw1).get('/', (c) => {1378 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1379 return c.json(0)1380 })13811382 new Hono().get(mw1, mw2).get('/', (c) => {1383 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1384 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1385 return c.json(0)1386 })13871388 new Hono().get(mw1, mw2, mw3).get('/', (c) => {1389 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1390 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1391 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1392 return c.json(0)1393 })13941395 new Hono().get(mw1, mw2, mw3, mw4).get('/', (c) => {1396 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1397 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1398 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1399 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1400 return c.json(0)1401 })14021403 new Hono().get(mw1, mw2, mw3, mw4, mw5).get('/', (c) => {1404 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1405 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1406 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1407 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1408 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1409 return c.json(0)1410 })14111412 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6).get('/', (c) => {1413 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1414 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1415 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1416 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1417 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1418 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1419 return c.json(0)1420 })14211422 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7).get('/', (c) => {1423 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1424 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1425 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1426 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1427 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1428 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1429 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1430 return c.json(0)1431 })14321433 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8).get('/', (c) => {1434 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1435 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1436 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1437 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1438 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1439 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1440 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1441 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1442 return c.json(0)1443 })14441445 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9).get('/', (c) => {1446 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1447 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1448 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1449 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1450 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1451 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1452 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1453 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1454 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1455 return c.json(0)1456 })14571458 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, mw10).get('/', (c) => {1459 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1460 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1461 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1462 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1463 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1464 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1465 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1466 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1467 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1468 expectTypeOf(c.var.foo10).toEqualTypeOf<string>()1469 return c.json(0)1470 })14711472 new Hono().get(1473 mw1,1474 mw2,1475 mw3,1476 mw4,1477 mw5,1478 mw6,1479 mw7,1480 mw8,1481 async (c) => {1482 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1483 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1484 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1485 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1486 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1487 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1488 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1489 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1490 },1491 (c) => c.json(0)1492 )14931494 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1495 expectTypeOf(c.req.valid('query')).toMatchTypeOf<{1496 bar1: number1497 bar2: number1498 bar3: number1499 bar4: number1500 bar5: number1501 bar6: number1502 bar7: number1503 bar8: number1504 bar9: number1505 }>()15061507 return c.json(0)1508 })15091510 new Hono().get(1511 '/',1512 mw1,1513 mw2,1514 mw3,1515 mw4,1516 mw5,1517 mw6,1518 mw7,1519 mw8,1520 async (c) => {1521 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1522 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1523 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1524 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1525 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1526 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1527 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1528 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1529 },1530 (c) => c.json(0)1531 )15321533 new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1534 expectTypeOf(c.req.valid('query')).toMatchTypeOf<{1535 bar1: number1536 bar2: number1537 bar3: number1538 bar4: number1539 bar5: number1540 bar6: number1541 bar7: number1542 bar8: number1543 bar9: number1544 }>()15451546 return c.json(0)1547 })15481549 type Env = {1550 Variables: {1551 init: number1552 }1553 }15541555 new Hono<Env>()1556 .get('/', mw1, (c) => {1557 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1558 expectTypeOf(c.var.init).toEqualTypeOf<number>()1559 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1560 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1561 return c.json(0)1562 })1563 .get('/', (c) => {1564 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1565 expectTypeOf(c.var.init).toEqualTypeOf<number>()1566 // @ts-expect-error foo1 is not typed1567 c.get('foo1')1568 // @ts-expect-error foo1 is not typed1569 c.var.foo11570 return c.json(0)1571 })1572 new Hono<Env>()1573 .get('/', mw1, mw2, (c) => {1574 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1575 expectTypeOf(c.var.init).toEqualTypeOf<number>()1576 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1577 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1578 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1579 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1580 return c.json(0)1581 })1582 .get('/', (c) => {1583 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1584 expectTypeOf(c.var.init).toEqualTypeOf<number>()1585 // @ts-expect-error foo1 is not typed1586 c.get('foo1')1587 // @ts-expect-error foo1 is not typed1588 c.var.foo11589 // @ts-expect-error foo2 is not typed1590 c.get('foo2')1591 // @ts-expect-error foo2 is not typed1592 c.var.foo21593 return c.json(0)1594 })1595 new Hono<Env>()1596 .get('/', mw1, mw2, mw3, (c) => {1597 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1598 expectTypeOf(c.var.init).toEqualTypeOf<number>()1599 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1600 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1601 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1602 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1603 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1604 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1605 return c.json(0)1606 })1607 .get('/', (c) => {1608 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1609 expectTypeOf(c.var.init).toEqualTypeOf<number>()1610 // @ts-expect-error foo1 is not typed1611 c.get('foo1')1612 // @ts-expect-error foo1 is not typed1613 c.var.foo11614 // @ts-expect-error foo2 is not typed1615 c.get('foo2')1616 // @ts-expect-error foo2 is not typed1617 c.var.foo21618 // @ts-expect-error foo3 is not typed1619 c.get('foo3')1620 // @ts-expect-error foo3 is not typed1621 c.var.foo31622 return c.json(0)1623 })1624 new Hono<Env>()1625 .get('/', mw1, mw2, mw3, mw4, (c) => {1626 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1627 expectTypeOf(c.var.init).toEqualTypeOf<number>()1628 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1629 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1630 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1631 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1632 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1633 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1634 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1635 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1636 return c.json(0)1637 })1638 .get('/', (c) => {1639 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1640 expectTypeOf(c.var.init).toEqualTypeOf<number>()1641 // @ts-expect-error foo1 is not typed1642 c.get('foo1')1643 // @ts-expect-error foo1 is not typed1644 c.var.foo11645 // @ts-expect-error foo2 is not typed1646 c.get('foo2')1647 // @ts-expect-error foo2 is not typed1648 c.var.foo21649 // @ts-expect-error foo3 is not typed1650 c.get('foo3')1651 // @ts-expect-error foo3 is not typed1652 c.var.foo31653 // @ts-expect-error foo4 is not typed1654 c.get('foo4')1655 // @ts-expect-error foo4 is not typed1656 c.var.foo41657 return c.json(0)1658 })16591660 new Hono<Env>()1661 .get('/', mw1, mw2, mw3, mw4, mw5, (c) => {1662 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1663 expectTypeOf(c.var.init).toEqualTypeOf<number>()1664 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1665 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1666 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1667 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1668 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1669 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1670 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1671 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1672 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1673 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1674 return c.json(0)1675 })1676 .get('/', (c) => {1677 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1678 expectTypeOf(c.var.init).toEqualTypeOf<number>()1679 // @ts-expect-error foo1 is not typed1680 c.get('foo1')1681 // @ts-expect-error foo1 is not typed1682 c.var.foo11683 // @ts-expect-error foo2 is not typed1684 c.get('foo2')1685 // @ts-expect-error foo2 is not typed1686 c.var.foo21687 // @ts-expect-error foo3 is not typed1688 c.get('foo3')1689 // @ts-expect-error foo3 is not typed1690 c.var.foo31691 // @ts-expect-error foo4 is not typed1692 c.get('foo4')1693 // @ts-expect-error foo4 is not typed1694 c.var.foo41695 // @ts-expect-error foo5 is not typed1696 c.get('foo5')1697 // @ts-expect-error foo5 is not typed1698 c.var.foo51699 return c.json(0)1700 })1701 new Hono<Env>()1702 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, (c) => {1703 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1704 expectTypeOf(c.var.init).toEqualTypeOf<number>()1705 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1706 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1707 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1708 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1709 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1710 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1711 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1712 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1713 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1714 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1715 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1716 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1717 return c.json(0)1718 })1719 .get('/', (c) => {1720 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1721 expectTypeOf(c.var.init).toEqualTypeOf<number>()1722 // @ts-expect-error foo1 is not typed1723 c.get('foo1')1724 // @ts-expect-error foo1 is not typed1725 c.var.foo11726 // @ts-expect-error foo2 is not typed1727 c.get('foo2')1728 // @ts-expect-error foo2 is not typed1729 c.var.foo21730 // @ts-expect-error foo3 is not typed1731 c.get('foo3')1732 // @ts-expect-error foo3 is not typed1733 c.var.foo31734 // @ts-expect-error foo4 is not typed1735 c.get('foo4')1736 // @ts-expect-error foo4 is not typed1737 c.var.foo41738 // @ts-expect-error foo5 is not typed1739 c.get('foo5')1740 // @ts-expect-error foo5 is not typed1741 c.var.foo51742 // @ts-expect-error foo6 is not typed1743 c.get('foo6')1744 // @ts-expect-error foo6 is not typed1745 c.var.foo61746 return c.json(0)1747 })1748 new Hono<Env>()1749 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, (c) => {1750 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1751 expectTypeOf(c.var.init).toEqualTypeOf<number>()1752 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1753 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1754 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1755 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1756 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1757 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1758 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1759 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1760 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1761 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1762 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1763 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1764 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1765 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1766 return c.json(0)1767 })1768 .get('/', (c) => {1769 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1770 expectTypeOf(c.var.init).toEqualTypeOf<number>()1771 // @ts-expect-error foo1 is not typed1772 c.get('foo1')1773 // @ts-expect-error foo1 is not typed1774 c.var.foo11775 // @ts-expect-error foo2 is not typed1776 c.get('foo2')1777 // @ts-expect-error foo2 is not typed1778 c.var.foo21779 // @ts-expect-error foo3 is not typed1780 c.get('foo3')1781 // @ts-expect-error foo3 is not typed1782 c.var.foo31783 // @ts-expect-error foo4 is not typed1784 c.get('foo4')1785 // @ts-expect-error foo4 is not typed1786 c.var.foo41787 // @ts-expect-error foo5 is not typed1788 c.get('foo5')1789 // @ts-expect-error foo5 is not typed1790 c.var.foo51791 // @ts-expect-error foo6 is not typed1792 c.get('foo6')1793 // @ts-expect-error foo6 is not typed1794 c.var.foo61795 // @ts-expect-error foo7 is not typed1796 c.get('foo7')1797 // @ts-expect-error foo7 is not typed1798 c.var.foo71799 return c.json(0)1800 })1801 new Hono<Env>()1802 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, (c) => {1803 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1804 expectTypeOf(c.var.init).toEqualTypeOf<number>()1805 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1806 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1807 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1808 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1809 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1810 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1811 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1812 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1813 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1814 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1815 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1816 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1817 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1818 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1819 expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()1820 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1821 return c.json(0)1822 })1823 .get('/', (c) => {1824 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1825 expectTypeOf(c.var.init).toEqualTypeOf<number>()1826 // @ts-expect-error foo1 is not typed1827 c.get('foo1')1828 // @ts-expect-error foo1 is not typed1829 c.var.foo11830 // @ts-expect-error foo2 is not typed1831 c.get('foo2')1832 // @ts-expect-error foo2 is not typed1833 c.var.foo21834 // @ts-expect-error foo3 is not typed1835 c.get('foo3')1836 // @ts-expect-error foo3 is not typed1837 c.var.foo31838 // @ts-expect-error foo4 is not typed1839 c.get('foo4')1840 // @ts-expect-error foo4 is not typed1841 c.var.foo41842 // @ts-expect-error foo5 is not typed1843 c.get('foo5')1844 // @ts-expect-error foo5 is not typed1845 c.var.foo51846 // @ts-expect-error foo6 is not typed1847 c.get('foo6')1848 // @ts-expect-error foo6 is not typed1849 c.var.foo61850 // @ts-expect-error foo7 is not typed1851 c.get('foo7')1852 // @ts-expect-error foo7 is not typed1853 c.var.foo71854 // @ts-expect-error foo8 is not typed1855 c.get('foo8')1856 // @ts-expect-error foo8 is not typed1857 c.var.foo81858 return c.json(0)1859 })18601861 new Hono<Env>()1862 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1863 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1864 expectTypeOf(c.var.init).toEqualTypeOf<number>()1865 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1866 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1867 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1868 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1869 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1870 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1871 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1872 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1873 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1874 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1875 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1876 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1877 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1878 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1879 expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()1880 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1881 expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()1882 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1883 return c.json(0)1884 })1885 .get('/', (c) => {1886 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1887 expectTypeOf(c.var.init).toEqualTypeOf<number>()1888 // @ts-expect-error foo1 is not typed1889 c.get('foo1')1890 // @ts-expect-error foo1 is not typed1891 c.var.foo11892 // @ts-expect-error foo2 is not typed1893 c.get('foo2')1894 // @ts-expect-error foo2 is not typed1895 c.var.foo21896 // @ts-expect-error foo3 is not typed1897 c.get('foo3')1898 // @ts-expect-error foo3 is not typed1899 c.var.foo31900 // @ts-expect-error foo4 is not typed1901 c.get('foo4')1902 // @ts-expect-error foo4 is not typed1903 c.var.foo41904 // @ts-expect-error foo5 is not typed1905 c.get('foo5')1906 // @ts-expect-error foo5 is not typed1907 c.var.foo51908 // @ts-expect-error foo6 is not typed1909 c.get('foo6')1910 // @ts-expect-error foo6 is not typed1911 c.var.foo61912 // @ts-expect-error foo7 is not typed1913 c.get('foo7')1914 // @ts-expect-error foo7 is not typed1915 c.var.foo71916 // @ts-expect-error foo8 is not typed1917 c.get('foo8')1918 // @ts-expect-error foo8 is not typed1919 c.var.foo81920 // @ts-expect-error foo9 is not typed1921 c.get('foo9')1922 // @ts-expect-error foo9 is not typed1923 c.var.foo91924 return c.json(0)1925 })1926 })1927})19281929/**1930 *1931 * Declaring a ContextVariableMap for testing.1932 */1933declare module './context' {1934 interface ContextVariableMap {1935 payload: string1936 }1937}19381939describe('ContextVariableMap type tests', () => {1940 it('Should not throw type errors with c.var', () => {1941 new Hono().get((c) => {1942 expectTypeOf(c.get('payload')).toEqualTypeOf<string>()1943 return c.json(0)1944 })1945 new Hono().get((c) => {1946 expectTypeOf(c.var.payload).toEqualTypeOf<string>()1947 return c.json(0)1948 })1949 })19501951 it('Should override ContextVariableMap with env variables', () => {1952 const middleware = createMiddleware<{1953 Variables: {1954 payload: number1955 }1956 }>(async (c, next) => {1957 c.set('payload', 123)1958 await next()1959 })19601961 new Hono().get(middleware, (c) => {1962 expectTypeOf(c.get('payload')).toEqualTypeOf<number>()1963 return c.json(0)1964 })1965 })19661967 it('Should use ContextVariableMap when c is Context<any>', () => {1968 const c = new Context(new Request('http://localhost'))1969 expectTypeOf(c.get('payload')).toEqualTypeOf<string>()1970 expectTypeOf(c.var.payload).toEqualTypeOf<string>()1971 // @ts-expect-error the value of payload should be string1972 expectTypeOf(c.set('payload', 123))1973 })1974})19751976/**1977 * It's challenge to test all cases. This is a minimal pattern.1978 */1979describe('Env types with chained routes - test only types', () => {1980 const app = new Hono<{ Variables: { testVar: string } }>()1981 it('Should not throw a type error', () => {1982 app1983 .post(1984 '/',1985 validator('json', (v) => v),1986 async (c) => {1987 expectTypeOf(c.get('testVar')).toEqualTypeOf<string>()1988 return c.json({ success: true })1989 }1990 )1991 .patch(1992 '/',1993 validator('json', (v) => v),1994 async (c) => {1995 expectTypeOf(c.get('testVar')).toEqualTypeOf<string>()1996 return c.json({ success: true })1997 }1998 )1999 })2000})The first 2,000 of 4,139 lines.