types.test.ts
This file
Path
src/types.test.ts
3,816 lines 路 113.3 KiB 路 TypeScript
Last changed by
fix(request): return `string | undefined` from param() when path type is any (#4723)Andrew D'Ameliobefore adoption0f49915
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 })404})405406describe('TypedResponse', () => {407 test('unknown', () => {408 type Actual = TypedResponse409 type Expected = {410 _data: unknown411 _status: StatusCode412 _format: ResponseFormat413 }414 type verify = Expect<Equal<Expected, Actual>>415 })416417 test('text auto infer', () => {418 type Actual = TypedResponse<string>419 type Expected = {420 _data: string421 _status: StatusCode422 _format: 'text'423 }424 type verify = Expect<Equal<Expected, Actual>>425 })426427 test('json auto infer', () => {428 type Actual = TypedResponse<{ ok: true }>429 type Expected = {430 _data: { ok: true }431 _status: StatusCode432 _format: 'json'433 }434 type verify = Expect<Equal<Expected, Actual>>435 })436})437438describe('Schema', () => {439 test('Schema', () => {440 type AppType = Hono<441 Env,442 ToSchema<443 'post',444 '/api/posts/:id',445 {446 in: {447 json: {448 id: number449 title: string450 }451 }452 },453 TypedResponse<454 {455 message: string456 success: boolean457 },458 StatusCode,459 'json'460 >461 >462 >463464 type Actual = ExtractSchema<AppType>465 type Expected = {466 '/api/posts/:id': {467 $post: {468 input: {469 json: {470 id: number471 title: string472 }473 } & {474 param: {475 id: string476 }477 }478 output: {479 message: string480 success: boolean481 }482 outputFormat: 'json'483 status: StatusCode484 }485 }486 }487 type verify = Expect<Equal<Expected, Actual>>488 })489})490491describe('Support c.json(undefined)', () => {492 it('Should return a correct type', () => {493 const app = new Hono().get('/this/is/a/test', async (c) => {494 return c.json(undefined)495 })496 type Actual = ExtractSchema<typeof app>497 type Expected = {498 '/this/is/a/test': {499 $get: {500 input: {}501 output: never502 outputFormat: 'json'503 status: ContentfulStatusCode504 }505 }506 }507 type verify = Expect<Equal<Expected, Actual>>508 })509})510511describe('Test types of Handler', () => {512 type E = {513 Variables: {514 foo: number515 }516 }517518 const url = 'http://localhost/'519520 test('Env', async () => {521 const app = new Hono<E>()522 const handler: Handler<E> = (c) => {523 const foo = c.get('foo')524 expectTypeOf(foo).toEqualTypeOf<number>()525 const id = c.req.param('id')526 expectTypeOf(id).toEqualTypeOf<string | undefined>()527 return c.text('Hi')528 }529 app.get('/', handler)530 const res = await app.request(url)531 expect(res.status).toBe(200)532 })533534 test('Env, Path', async () => {535 const app = new Hono<E>()536 const handler: Handler<E, '/'> = (c) => {537 const foo = c.get('foo')538 expectTypeOf(foo).toEqualTypeOf<number>()539 return c.text('Hi')540 }541 app.get('/', handler)542543 const res = await app.request(url)544 expect(res.status).toBe(200)545 })546547 type User = {548 name: string549 age: number550 }551552 test('Env, Path, Type', async () => {553 const app = new Hono<E>()554 const handler: Handler<E, '/', { in: { json: User }; out: { json: User } }> = (c) => {555 const foo = c.get('foo')556 expectTypeOf(foo).toEqualTypeOf<number>()557 const { name } = c.req.valid('json')558 expectTypeOf(name).toEqualTypeOf<string>()559 return c.text('Hi')560 }561 })562})563564describe('`json()`', () => {565 const app = new Hono<{ Variables: { foo: string } }>()566 app.get('/post/:id', (c) => {567 c.req.param('id')568 const id = c.req.param('id')569 return c.text('foo')570 })571572 test('json', () => {573 const route = app.get('/hello', (c) => {574 return c.json({575 message: 'Hello!',576 })577 })578 type Actual = ExtractSchema<typeof route>579 type Expected = {580 '/hello': {581 $get: {582 input: {}583 output: {584 message: string585 }586 outputFormat: 'json'587 status: ContentfulStatusCode588 }589 }590 }591 type verify = Expect<Equal<Expected, Actual>>592 })593594 test('json with specific status code', () => {595 const route = app.get('/hello', (c) => {596 return c.json(597 {598 message: 'Hello!',599 },600 200601 )602 })603 type Actual = ExtractSchema<typeof route>604 type Expected = {605 '/hello': {606 $get: {607 input: {}608 output: {609 message: string610 }611 outputFormat: 'json'612 status: 200613 }614 }615 }616 type verify = Expect<Equal<Expected, Actual>>617 })618})619620describe('Path parameters', () => {621 test('ParamKeys', () => {622 type Actual = ParamKeys<'/posts/:postId/comment/:commentId'>623 type Expected = 'postId' | 'commentId'624 type verify = Expect<Equal<Expected, Actual>>625 })626627 describe('ParamKeyToRecord', () => {628 test('With ?', () => {629 type Actual = ParamKeyToRecord<'/animal/type?'>630 type Expected = { [K in '/animal/type']: string | undefined }631 type verify = Expect<Equal<Expected, Actual>>632 })633 test('Without ?', () => {634 type Actual = ParamKeyToRecord<'/animal/type'>635 type Expected = { [K in '/animal/type']: string }636 type verify = Expect<Equal<Expected, Actual>>637 })638 })639640 describe('Path parameters in app', () => {641 test('Optional parameters - /api/:a/:b?', () => {642 const app = new Hono()643 const routes = app.get('/api/:a/:b?', (c) => {644 const a = c.req.param('a')645 const b = c.req.param('b')646 expectTypeOf(a).toEqualTypeOf<string>()647 expectTypeOf(b).toEqualTypeOf<string | undefined>()648 return c.json({ a, b })649 })650 type T = ExtractSchema<typeof routes>651 type Output = T['/api/:a/:b?']['$get']['output']652 type Expected = {653 a: string654 b: string | undefined655 }656 type verify = Expect<Equal<Expected, Output>>657 })658 })659})660661describe('For HonoRequest', () => {662 type Input = {663 json: {664 id: number665 title: string666 }667 query: {668 page: string669 }670 }671672 test('InputToDataByType with value', () => {673 type Actual = InputToDataByTarget<Input, 'json'>674 type Expected = {675 id: number676 title: string677 }678 type verify = Expect<Equal<Expected, Actual>>679 })680681 test('InputToDataByType without value', () => {682 type Actual = InputToDataByTarget<Input, 'form'>683 type verify = Expect<Equal<never, Actual>>684 })685686 test('RemoveQuestion', () => {687 type Actual = RemoveQuestion<'/animal/type?'>688 type verify = Expect<Equal<'/animal/type', Actual>>689 })690})691692describe('AddParam', () => {693 it('Should add params to input correctly', () => {694 type Actual = AddParam<695 {696 param: {697 id: string698 }699 } & {700 query: {701 page: string702 }703 },704 '/:id'705 >706 type Expected = {707 query: {708 page: string709 }710 } & {711 param: {712 id: string713 }714 }715 type verify = Expect<Equal<Expected, Actual>>716 })717})718719describe('ToSchema', () => {720 it('Should convert parameters to schema correctly', () => {721 type Actual = ToSchema<722 'get',723 '/:id',724 { in: { param: { id: string }; query: { page: string } } },725 TypedResponse<{}>726 >727 type Expected = {728 '/:id': {729 $get: {730 input: {731 param: {732 id: string733 }734 query: {735 page: string736 }737 }738 output: {}739 outputFormat: 'json'740 status: StatusCode741 }742 }743 }744 type verify = Expect<Equal<Expected, Actual>>745 })746})747748describe('MergePath', () => {749 it('Should merge paths correctly', () => {750 type path1 = MergePath<'/api', '/book'>751 type verify1 = Expect<Equal<'/api/book', path1>>752 type path2 = MergePath<'/api/', '/book'>753 type verify2 = Expect<Equal<'/api/book', path2>>754 type path3 = MergePath<'/api/', '/'>755 type verify3 = Expect<Equal<'/api/', path3>>756 type path4 = MergePath<'/api', '/'>757 type verify4 = Expect<Equal<'/api', path4>>758 type path5 = MergePath<'/', ''>759 type verify5 = Expect<Equal<'/', path5>>760 type path6 = MergePath<'', '/'>761 type verify6 = Expect<Equal<'/', path6>>762 type path7 = MergePath<'/', '/'>763 type verify7 = Expect<Equal<'/', path7>>764 type path8 = MergePath<'', ''>765 type verify8 = Expect<Equal<'/', path8>>766 })767})768769describe('MergeSchemaPath', () => {770 it('Should merge schema and sub path correctly', () => {771 type Sub = ToSchema<772 'post',773 '/posts',774 {775 in: {776 json: {777 id: number778 title: string779 }780 }781 },782 TypedResponse<{783 message: string784 }>785 > &786 ToSchema<787 'get',788 '/posts',789 {},790 TypedResponse<{791 ok: boolean792 }>793 >794795 type Actual = MergeSchemaPath<Sub, '/api'>796797 type Expected = {798 '/api/posts': {799 $post: {800 input: {801 json: {802 id: number803 title: string804 }805 }806 output: {807 message: string808 }809 outputFormat: 'json'810 status: StatusCode811 }812 $get: {813 input: {}814 output: {815 ok: boolean816 }817 outputFormat: 'json'818 status: StatusCode819 }820 }821 }822823 type verify = Expect<Equal<Expected, Actual>>824 })825826 it('Should merge schema which has params and sub path does not have params', () => {827 type Actual = MergeSchemaPath<828 {829 '/': {830 $get: {831 input: {832 param: {833 id: string834 }835 query: {836 page: string837 }838 }839 output: {}840 outputFormat: 'json'841 status: StatusCode842 }843 }844 },845 '/something'846 >847 type Expected = {848 '/something': {849 $get: {850 input: {851 param: {852 id: string853 }854 query: {855 page: string856 }857 }858 output: {}859 outputFormat: 'json'860 status: StatusCode861 }862 }863 }864 type verify = Expect<Equal<Expected, Actual>>865 })866867 type GetKey<T> = T extends Record<infer K, unknown> ? K : never868869 it('Should remove a slash - `/` + `/`', () => {870 type Sub = ToSchema<'get', '/', {}, TypedResponse<{}>>871 type Actual = MergeSchemaPath<Sub, '/'>872 type verify = Expect<Equal<'/', GetKey<Actual>>>873 })874875 it('Should remove a slash - `/tags` + `/`', () => {876 type Sub = ToSchema<'get', '/tags', {}, TypedResponse<{}>>877 type Actual = MergeSchemaPath<Sub, '/'>878 type verify = Expect<Equal<'/tags', GetKey<Actual>>>879 })880881 it('Should remove a slash - `/` + `/tags`', () => {882 type Sub = ToSchema<'get', '/', {}, TypedResponse<{}>>883 type Actual = MergeSchemaPath<Sub, '/tags'>884 type verify = Expect<Equal<'/tags', GetKey<Actual>>>885 })886887 test('MergeSchemaPath - SubPath has path params', () => {888 type Actual = MergeSchemaPath<ToSchema<'get', '/', {}, TypedResponse>, '/a/:b'>889 type Expected = {890 '/a/:b': {891 $get: {892 input: {893 param: {894 b: string895 }896 }897 output: {}898 outputFormat: ResponseFormat899 status: StatusCode900 }901 }902 }903 type verify = Expect<Equal<Expected, Actual>>904 })905906 test('MergeSchemaPath - Path and SubPath have path params', () => {907 type Actual = MergeSchemaPath<ToSchema<'get', '/c/:d', {}, TypedResponse<{}>>, '/a/:b'>908 type Expected = {909 '/a/:b/c/:d': {910 $get: {911 input: {912 param: {913 d: string914 } & {915 b: string916 }917 }918 output: {}919 outputFormat: 'json'920 status: StatusCode921 }922 }923 }924 type verify = Expect<Equal<Expected, Actual>>925 })926927 test('MergeSchemaPath - Path and SubPath have regexp path params', () => {928 type Actual = MergeSchemaPath<ToSchema<'get', '/c/:d{.+}', {}, TypedResponse<{}>>, '/a/:b{.+}'>929 type Expected = {930 '/a/:b{.+}/c/:d{.+}': {931 $get: {932 input: {933 param: {934 d: string935 } & {936 b: string937 }938 }939 output: {}940 outputFormat: 'json'941 status: StatusCode942 }943 }944 }945 type verify = Expect<Equal<Expected, Actual>>946 })947948 test('MergeSchemaPath - Method has Endpoints as Union', () => {949 type Actual = MergeSchemaPath<950 {951 '/': {952 $get:953 | {954 input: {}955 output: {956 error: string957 }958 outputFormat: 'json'959 status: 404960 }961 | {962 input: {}963 output: {964 success: boolean965 }966 outputFormat: 'json'967 status: 200968 }969 }970 },971 '/api/hello'972 >973 type Expected = {974 '/api/hello': {975 $get:976 | {977 input: {}978 output: {979 error: string980 }981 outputFormat: 'json'982 status: 404983 }984 | {985 input: {}986 output: {987 success: boolean988 }989 outputFormat: 'json'990 status: 200991 }992 }993 }994 type verify = Expect<Equal<Expected, Actual>>995 })996})997998describe('Different types using json()', () => {999 describe('no path pattern', () => {1000 const app = new Hono()10011002 test('Three different types', () => {1003 const route = app.get((c) => {1004 const flag = false1005 if (flag) {1006 return c.json({1007 ng: true,1008 })1009 }1010 if (!flag) {1011 return c.json({1012 ok: true,1013 })1014 }1015 return c.json({1016 default: true,1017 })1018 })1019 type Actual = ExtractSchema<typeof route>1020 type Expected = {1021 '/': {1022 $get:1023 | {1024 input: {}1025 output: {1026 ng: true1027 }1028 outputFormat: 'json'1029 status: ContentfulStatusCode1030 }1031 | {1032 input: {}1033 output: {1034 ok: true1035 }1036 outputFormat: 'json'1037 status: ContentfulStatusCode1038 }1039 | {1040 input: {}1041 output: {1042 default: true1043 }1044 outputFormat: 'json'1045 status: ContentfulStatusCode1046 }1047 }1048 }1049 type verify = Expect<Equal<Expected, Actual>>1050 })10511052 test('Three different types and status codes', () => {1053 const route = app.get((c) => {1054 const flag = false1055 if (flag) {1056 return c.json(1057 {1058 ng: true,1059 },1060 4001061 )1062 }1063 if (!flag) {1064 return c.json(1065 {1066 ok: true,1067 },1068 2001069 )1070 }1071 return c.json({1072 default: true,1073 })1074 })1075 type Actual = ExtractSchema<typeof route>1076 type Expected = {1077 '/': {1078 $get:1079 | {1080 input: {}1081 output: {1082 ng: true1083 }1084 outputFormat: 'json'1085 status: 4001086 }1087 | {1088 input: {}1089 output: {1090 ok: true1091 }1092 outputFormat: 'json'1093 status: 2001094 }1095 | {1096 input: {}1097 output: {1098 default: true1099 }1100 outputFormat: 'json'1101 status: ContentfulStatusCode1102 }1103 }1104 }1105 type verify = Expect<Equal<Expected, Actual>>1106 })1107 })11081109 describe('path pattern', () => {1110 const app = new Hono()11111112 test('Three different types', () => {1113 const route = app.get('/foo', (c) => {1114 const flag = false1115 if (flag) {1116 return c.json({1117 ng: true,1118 })1119 }1120 if (!flag) {1121 return c.json({1122 ok: true,1123 })1124 }1125 return c.json({1126 default: true,1127 })1128 })1129 type Actual = ExtractSchema<typeof route>1130 type Expected = {1131 '/foo': {1132 $get:1133 | {1134 input: {}1135 output: {1136 ng: true1137 }1138 outputFormat: 'json'1139 status: ContentfulStatusCode1140 }1141 | {1142 input: {}1143 output: {1144 ok: true1145 }1146 outputFormat: 'json'1147 status: ContentfulStatusCode1148 }1149 | {1150 input: {}1151 output: {1152 default: true1153 }1154 outputFormat: 'json'1155 status: ContentfulStatusCode1156 }1157 }1158 }1159 type verify = Expect<Equal<Expected, Actual>>1160 })11611162 test('Three different types and status codes', () => {1163 const route = app.get('/foo', (c) => {1164 const flag = false1165 if (flag) {1166 return c.json(1167 {1168 ng: true,1169 },1170 4001171 )1172 }1173 if (!flag) {1174 return c.json(1175 {1176 ok: true,1177 },1178 2001179 )1180 }1181 return c.json({1182 default: true,1183 })1184 })1185 type Actual = ExtractSchema<typeof route>1186 type Expected = {1187 '/foo': {1188 $get:1189 | {1190 input: {}1191 output: {1192 ng: true1193 }1194 outputFormat: 'json'1195 status: 4001196 }1197 | {1198 input: {}1199 output: {1200 ok: true1201 }1202 outputFormat: 'json'1203 status: 2001204 }1205 | {1206 input: {}1207 output: {1208 default: true1209 }1210 outputFormat: 'json'1211 status: ContentfulStatusCode1212 }1213 }1214 }1215 type verify = Expect<Equal<Expected, Actual>>1216 })1217 })1218})12191220describe('json() in an async handler', () => {1221 const app = new Hono()12221223 test('json', () => {1224 const route = app.get(async (c) => {1225 return c.json({1226 ok: true,1227 })1228 })1229 type Actual = ExtractSchema<typeof route>1230 type Expected = {1231 '/': {1232 $get: {1233 input: {}1234 output: {1235 ok: true1236 }1237 outputFormat: 'json'1238 status: ContentfulStatusCode1239 }1240 }1241 }1242 type verify = Expect<Equal<Expected, Actual>>1243 })12441245 test('json with specific status code', () => {1246 const route = app.get(async (c) => {1247 return c.json(1248 {1249 ok: true,1250 },1251 2001252 )1253 })1254 type Actual = ExtractSchema<typeof route>1255 type Expected = {1256 '/': {1257 $get: {1258 input: {}1259 output: {1260 ok: true1261 }1262 outputFormat: 'json'1263 status: 2001264 }1265 }1266 }1267 type verify = Expect<Equal<Expected, Actual>>1268 })1269})12701271/**1272 * Other tests for `c.var` are written in `hono.test.ts`.1273 * This tests are only for types.1274 */1275describe('c.var with chaining - test only types', () => {1276 const mw1 = createMiddleware<1277 { Variables: { foo1: string } },1278 string,1279 { out: { query: { bar1: number } } }1280 >(async () => {})1281 const mw2 = createMiddleware<1282 { Variables: { foo2: string } },1283 string,1284 { out: { query: { bar2: number } } }1285 >(async () => {})1286 const mw3 = createMiddleware<1287 { Variables: { foo3: string } },1288 string,1289 { out: { query: { bar3: number } } }1290 >(async () => {})1291 const mw4 = createMiddleware<1292 { Variables: { foo4: string } },1293 string,1294 { out: { query: { bar4: number } } }1295 >(async () => {})1296 const mw5 = createMiddleware<1297 { Variables: { foo5: string } },1298 string,1299 { out: { query: { bar5: number } } }1300 >(async () => {})1301 const mw6 = createMiddleware<1302 { Variables: { foo6: string } },1303 string,1304 { out: { query: { bar6: number } } }1305 >(async () => {})1306 const mw7 = createMiddleware<1307 { Variables: { foo7: string } },1308 string,1309 { out: { query: { bar7: number } } }1310 >(async () => {})1311 const mw8 = createMiddleware<1312 { Variables: { foo8: string } },1313 string,1314 { out: { query: { bar8: number } } }1315 >(async () => {})1316 const mw9 = createMiddleware<1317 { Variables: { foo9: string } },1318 string,1319 { out: { query: { bar9: number } } }1320 >(async () => {})1321 const mw10 = createMiddleware<1322 { Variables: { foo10: string } },1323 string,1324 { out: { query: { bar10: number } } }1325 >(async () => {})13261327 it('Should not throw type errors', () => {1328 // app.get(handler...)13291330 new Hono().get(mw1).get('/', (c) => {1331 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1332 return c.json(0)1333 })13341335 new Hono().get(mw1, mw2).get('/', (c) => {1336 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1337 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1338 return c.json(0)1339 })13401341 new Hono().get(mw1, mw2, mw3).get('/', (c) => {1342 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1343 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1344 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1345 return c.json(0)1346 })13471348 new Hono().get(mw1, mw2, mw3, mw4).get('/', (c) => {1349 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1350 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1351 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1352 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1353 return c.json(0)1354 })13551356 new Hono().get(mw1, mw2, mw3, mw4, mw5).get('/', (c) => {1357 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1358 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1359 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1360 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1361 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1362 return c.json(0)1363 })13641365 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6).get('/', (c) => {1366 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1367 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1368 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1369 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1370 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1371 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1372 return c.json(0)1373 })13741375 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7).get('/', (c) => {1376 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1377 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1378 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1379 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1380 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1381 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1382 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1383 return c.json(0)1384 })13851386 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8).get('/', (c) => {1387 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1388 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1389 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1390 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1391 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1392 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1393 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1394 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1395 return c.json(0)1396 })13971398 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9).get('/', (c) => {1399 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1400 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1401 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1402 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1403 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1404 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1405 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1406 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1407 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1408 return c.json(0)1409 })14101411 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, mw10).get('/', (c) => {1412 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1413 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1414 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1415 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1416 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1417 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1418 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1419 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1420 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1421 expectTypeOf(c.var.foo10).toEqualTypeOf<string>()1422 return c.json(0)1423 })14241425 new Hono().get(1426 mw1,1427 mw2,1428 mw3,1429 mw4,1430 mw5,1431 mw6,1432 mw7,1433 mw8,1434 async (c) => {1435 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1436 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1437 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1438 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1439 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1440 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1441 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1442 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1443 },1444 (c) => c.json(0)1445 )14461447 new Hono().get(mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1448 expectTypeOf(c.req.valid('query')).toMatchTypeOf<{1449 bar1: number1450 bar2: number1451 bar3: number1452 bar4: number1453 bar5: number1454 bar6: number1455 bar7: number1456 bar8: number1457 bar9: number1458 }>()14591460 return c.json(0)1461 })14621463 new Hono().get(1464 '/',1465 mw1,1466 mw2,1467 mw3,1468 mw4,1469 mw5,1470 mw6,1471 mw7,1472 mw8,1473 async (c) => {1474 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1475 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1476 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1477 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1478 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1479 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1480 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1481 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1482 },1483 (c) => c.json(0)1484 )14851486 new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1487 expectTypeOf(c.req.valid('query')).toMatchTypeOf<{1488 bar1: number1489 bar2: number1490 bar3: number1491 bar4: number1492 bar5: number1493 bar6: number1494 bar7: number1495 bar8: number1496 bar9: number1497 }>()14981499 return c.json(0)1500 })15011502 type Env = {1503 Variables: {1504 init: number1505 }1506 }15071508 new Hono<Env>()1509 .get('/', mw1, (c) => {1510 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1511 expectTypeOf(c.var.init).toEqualTypeOf<number>()1512 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1513 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1514 return c.json(0)1515 })1516 .get('/', (c) => {1517 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1518 expectTypeOf(c.var.init).toEqualTypeOf<number>()1519 // @ts-expect-error foo1 is not typed1520 c.get('foo1')1521 // @ts-expect-error foo1 is not typed1522 c.var.foo11523 return c.json(0)1524 })1525 new Hono<Env>()1526 .get('/', mw1, mw2, (c) => {1527 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1528 expectTypeOf(c.var.init).toEqualTypeOf<number>()1529 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1530 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1531 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1532 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1533 return c.json(0)1534 })1535 .get('/', (c) => {1536 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1537 expectTypeOf(c.var.init).toEqualTypeOf<number>()1538 // @ts-expect-error foo1 is not typed1539 c.get('foo1')1540 // @ts-expect-error foo1 is not typed1541 c.var.foo11542 // @ts-expect-error foo2 is not typed1543 c.get('foo2')1544 // @ts-expect-error foo2 is not typed1545 c.var.foo21546 return c.json(0)1547 })1548 new Hono<Env>()1549 .get('/', mw1, mw2, mw3, (c) => {1550 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1551 expectTypeOf(c.var.init).toEqualTypeOf<number>()1552 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1553 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1554 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1555 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1556 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1557 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1558 return c.json(0)1559 })1560 .get('/', (c) => {1561 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1562 expectTypeOf(c.var.init).toEqualTypeOf<number>()1563 // @ts-expect-error foo1 is not typed1564 c.get('foo1')1565 // @ts-expect-error foo1 is not typed1566 c.var.foo11567 // @ts-expect-error foo2 is not typed1568 c.get('foo2')1569 // @ts-expect-error foo2 is not typed1570 c.var.foo21571 // @ts-expect-error foo3 is not typed1572 c.get('foo3')1573 // @ts-expect-error foo3 is not typed1574 c.var.foo31575 return c.json(0)1576 })1577 new Hono<Env>()1578 .get('/', mw1, mw2, mw3, mw4, (c) => {1579 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1580 expectTypeOf(c.var.init).toEqualTypeOf<number>()1581 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1582 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1583 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1584 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1585 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1586 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1587 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1588 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1589 return c.json(0)1590 })1591 .get('/', (c) => {1592 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1593 expectTypeOf(c.var.init).toEqualTypeOf<number>()1594 // @ts-expect-error foo1 is not typed1595 c.get('foo1')1596 // @ts-expect-error foo1 is not typed1597 c.var.foo11598 // @ts-expect-error foo2 is not typed1599 c.get('foo2')1600 // @ts-expect-error foo2 is not typed1601 c.var.foo21602 // @ts-expect-error foo3 is not typed1603 c.get('foo3')1604 // @ts-expect-error foo3 is not typed1605 c.var.foo31606 // @ts-expect-error foo4 is not typed1607 c.get('foo4')1608 // @ts-expect-error foo4 is not typed1609 c.var.foo41610 return c.json(0)1611 })16121613 new Hono<Env>()1614 .get('/', mw1, mw2, mw3, mw4, mw5, (c) => {1615 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1616 expectTypeOf(c.var.init).toEqualTypeOf<number>()1617 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1618 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1619 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1620 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1621 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1622 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1623 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1624 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1625 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1626 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1627 return c.json(0)1628 })1629 .get('/', (c) => {1630 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1631 expectTypeOf(c.var.init).toEqualTypeOf<number>()1632 // @ts-expect-error foo1 is not typed1633 c.get('foo1')1634 // @ts-expect-error foo1 is not typed1635 c.var.foo11636 // @ts-expect-error foo2 is not typed1637 c.get('foo2')1638 // @ts-expect-error foo2 is not typed1639 c.var.foo21640 // @ts-expect-error foo3 is not typed1641 c.get('foo3')1642 // @ts-expect-error foo3 is not typed1643 c.var.foo31644 // @ts-expect-error foo4 is not typed1645 c.get('foo4')1646 // @ts-expect-error foo4 is not typed1647 c.var.foo41648 // @ts-expect-error foo5 is not typed1649 c.get('foo5')1650 // @ts-expect-error foo5 is not typed1651 c.var.foo51652 return c.json(0)1653 })1654 new Hono<Env>()1655 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, (c) => {1656 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1657 expectTypeOf(c.var.init).toEqualTypeOf<number>()1658 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1659 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1660 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1661 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1662 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1663 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1664 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1665 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1666 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1667 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1668 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1669 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1670 return c.json(0)1671 })1672 .get('/', (c) => {1673 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1674 expectTypeOf(c.var.init).toEqualTypeOf<number>()1675 // @ts-expect-error foo1 is not typed1676 c.get('foo1')1677 // @ts-expect-error foo1 is not typed1678 c.var.foo11679 // @ts-expect-error foo2 is not typed1680 c.get('foo2')1681 // @ts-expect-error foo2 is not typed1682 c.var.foo21683 // @ts-expect-error foo3 is not typed1684 c.get('foo3')1685 // @ts-expect-error foo3 is not typed1686 c.var.foo31687 // @ts-expect-error foo4 is not typed1688 c.get('foo4')1689 // @ts-expect-error foo4 is not typed1690 c.var.foo41691 // @ts-expect-error foo5 is not typed1692 c.get('foo5')1693 // @ts-expect-error foo5 is not typed1694 c.var.foo51695 // @ts-expect-error foo6 is not typed1696 c.get('foo6')1697 // @ts-expect-error foo6 is not typed1698 c.var.foo61699 return c.json(0)1700 })1701 new Hono<Env>()1702 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, (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 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1718 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1719 return c.json(0)1720 })1721 .get('/', (c) => {1722 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1723 expectTypeOf(c.var.init).toEqualTypeOf<number>()1724 // @ts-expect-error foo1 is not typed1725 c.get('foo1')1726 // @ts-expect-error foo1 is not typed1727 c.var.foo11728 // @ts-expect-error foo2 is not typed1729 c.get('foo2')1730 // @ts-expect-error foo2 is not typed1731 c.var.foo21732 // @ts-expect-error foo3 is not typed1733 c.get('foo3')1734 // @ts-expect-error foo3 is not typed1735 c.var.foo31736 // @ts-expect-error foo4 is not typed1737 c.get('foo4')1738 // @ts-expect-error foo4 is not typed1739 c.var.foo41740 // @ts-expect-error foo5 is not typed1741 c.get('foo5')1742 // @ts-expect-error foo5 is not typed1743 c.var.foo51744 // @ts-expect-error foo6 is not typed1745 c.get('foo6')1746 // @ts-expect-error foo6 is not typed1747 c.var.foo61748 // @ts-expect-error foo7 is not typed1749 c.get('foo7')1750 // @ts-expect-error foo7 is not typed1751 c.var.foo71752 return c.json(0)1753 })1754 new Hono<Env>()1755 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, (c) => {1756 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1757 expectTypeOf(c.var.init).toEqualTypeOf<number>()1758 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1759 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1760 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1761 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1762 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1763 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1764 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1765 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1766 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1767 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1768 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1769 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1770 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1771 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1772 expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()1773 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1774 return c.json(0)1775 })1776 .get('/', (c) => {1777 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1778 expectTypeOf(c.var.init).toEqualTypeOf<number>()1779 // @ts-expect-error foo1 is not typed1780 c.get('foo1')1781 // @ts-expect-error foo1 is not typed1782 c.var.foo11783 // @ts-expect-error foo2 is not typed1784 c.get('foo2')1785 // @ts-expect-error foo2 is not typed1786 c.var.foo21787 // @ts-expect-error foo3 is not typed1788 c.get('foo3')1789 // @ts-expect-error foo3 is not typed1790 c.var.foo31791 // @ts-expect-error foo4 is not typed1792 c.get('foo4')1793 // @ts-expect-error foo4 is not typed1794 c.var.foo41795 // @ts-expect-error foo5 is not typed1796 c.get('foo5')1797 // @ts-expect-error foo5 is not typed1798 c.var.foo51799 // @ts-expect-error foo6 is not typed1800 c.get('foo6')1801 // @ts-expect-error foo6 is not typed1802 c.var.foo61803 // @ts-expect-error foo7 is not typed1804 c.get('foo7')1805 // @ts-expect-error foo7 is not typed1806 c.var.foo71807 // @ts-expect-error foo8 is not typed1808 c.get('foo8')1809 // @ts-expect-error foo8 is not typed1810 c.var.foo81811 return c.json(0)1812 })18131814 new Hono<Env>()1815 .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => {1816 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1817 expectTypeOf(c.var.init).toEqualTypeOf<number>()1818 expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()1819 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()1820 expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()1821 expectTypeOf(c.var.foo2).toEqualTypeOf<string>()1822 expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()1823 expectTypeOf(c.var.foo3).toEqualTypeOf<string>()1824 expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()1825 expectTypeOf(c.var.foo4).toEqualTypeOf<string>()1826 expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()1827 expectTypeOf(c.var.foo5).toEqualTypeOf<string>()1828 expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()1829 expectTypeOf(c.var.foo6).toEqualTypeOf<string>()1830 expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()1831 expectTypeOf(c.var.foo7).toEqualTypeOf<string>()1832 expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()1833 expectTypeOf(c.var.foo8).toEqualTypeOf<string>()1834 expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()1835 expectTypeOf(c.var.foo9).toEqualTypeOf<string>()1836 return c.json(0)1837 })1838 .get('/', (c) => {1839 expectTypeOf(c.get('init')).toEqualTypeOf<number>()1840 expectTypeOf(c.var.init).toEqualTypeOf<number>()1841 // @ts-expect-error foo1 is not typed1842 c.get('foo1')1843 // @ts-expect-error foo1 is not typed1844 c.var.foo11845 // @ts-expect-error foo2 is not typed1846 c.get('foo2')1847 // @ts-expect-error foo2 is not typed1848 c.var.foo21849 // @ts-expect-error foo3 is not typed1850 c.get('foo3')1851 // @ts-expect-error foo3 is not typed1852 c.var.foo31853 // @ts-expect-error foo4 is not typed1854 c.get('foo4')1855 // @ts-expect-error foo4 is not typed1856 c.var.foo41857 // @ts-expect-error foo5 is not typed1858 c.get('foo5')1859 // @ts-expect-error foo5 is not typed1860 c.var.foo51861 // @ts-expect-error foo6 is not typed1862 c.get('foo6')1863 // @ts-expect-error foo6 is not typed1864 c.var.foo61865 // @ts-expect-error foo7 is not typed1866 c.get('foo7')1867 // @ts-expect-error foo7 is not typed1868 c.var.foo71869 // @ts-expect-error foo8 is not typed1870 c.get('foo8')1871 // @ts-expect-error foo8 is not typed1872 c.var.foo81873 // @ts-expect-error foo9 is not typed1874 c.get('foo9')1875 // @ts-expect-error foo9 is not typed1876 c.var.foo91877 return c.json(0)1878 })1879 })1880})18811882/**1883 *1884 * Declaring a ContextVariableMap for testing.1885 */1886declare module './context' {1887 interface ContextVariableMap {1888 payload: string1889 }1890}18911892describe('ContextVariableMap type tests', () => {1893 it('Should not throw type errors with c.var', () => {1894 new Hono().get((c) => {1895 expectTypeOf(c.get('payload')).toEqualTypeOf<string>()1896 return c.json(0)1897 })1898 new Hono().get((c) => {1899 expectTypeOf(c.var.payload).toEqualTypeOf<string>()1900 return c.json(0)1901 })1902 })19031904 it('Should override ContextVariableMap with env variables', () => {1905 const middleware = createMiddleware<{1906 Variables: {1907 payload: number1908 }1909 }>(async (c, next) => {1910 c.set('payload', 123)1911 await next()1912 })19131914 new Hono().get(middleware, (c) => {1915 expectTypeOf(c.get('payload')).toEqualTypeOf<number>()1916 return c.json(0)1917 })1918 })19191920 it('Should use ContextVariableMap when c is Context<any>', () => {1921 const c = new Context(new Request('http://localhost'))1922 expectTypeOf(c.get('payload')).toEqualTypeOf<string>()1923 expectTypeOf(c.var.payload).toEqualTypeOf<string>()1924 // @ts-expect-error the value of payload should be string1925 expectTypeOf(c.set('payload', 123))1926 })1927})19281929/**1930 * It's challenge to test all cases. This is a minimal pattern.1931 */1932describe('Env types with chained routes - test only types', () => {1933 const app = new Hono<{ Variables: { testVar: string } }>()1934 it('Should not throw a type error', () => {1935 app1936 .post(1937 '/',1938 validator('json', (v) => v),1939 async (c) => {1940 expectTypeOf(c.get('testVar')).toEqualTypeOf<string>()1941 return c.json({ success: true })1942 }1943 )1944 .patch(1945 '/',1946 validator('json', (v) => v),1947 async (c) => {1948 expectTypeOf(c.get('testVar')).toEqualTypeOf<string>()1949 return c.json({ success: true })1950 }1951 )1952 })1953})19541955/**1956 * Ref: https://github.com/honojs/hono/issues/30271957 */1958describe('Env types with validator as first middleware - test only types', () => {1959 const app = new Hono<{ Variables: { testVar: string } }>()1960 it('Should not throw a type error', () => {1961 const testApp = app.get(1962 validator('json', () => {1963 return {1964 cd: 'bar',1965 }1966 }),1967 async (c) => {1968 const foo = c.req.valid('json') // Error here1969 return c.json(1)1970 }1971 )19721973 const dummyMiddleware1 = createMiddleware(async (c, next) => {1974 await next()1975 })1976 // Multiple levels of middleware1977 const testApp2 = app.post(1978 validator('json', () => {1979 return {1980 cd: 'bar',1981 }1982 }),1983 dummyMiddleware1,1984 createMiddleware(async (c, next) => {1985 await next()1986 }),1987 async (c) => {1988 const foo = c.req.valid('json') // Error here also1989 return c.json(1)1990 }1991 )1992 })1993})19941995// https://github.com/honojs/hono/issues/47731996describe('c.req.valid() in non-last handler after validator middleware - test only types', () => {1997 it('Should not throw a type error', () => {1998 const app = new Hono()1999 app.get(2000 '/',The first 2,000 of 3,816 lines.