types.test.ts
This file
Path
src/client/types.test.ts
145 lines · 5.0 KiB · TypeScript
Last changed by
Merge pull request #4735 from honojs/nextYusuke Wadabefore adoption01e78ad
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
4.9.0Yusuke Wadabefore adoption82bdc4d
lines 1–4why, one step back1/* eslint-disable @typescript-eslint/no-unused-vars */2import { expectTypeOf } from 'vitest'3import { Hono } from '..'4import { upgradeWebSocket } from '../adapter/deno/websocket'Merge pull request #4574 from honojs/nextYusuke Wadabefore adoption0d739b6
line 5why, one step back5import type { TypedURL } from './types'4.9.0Yusuke Wadabefore adoption82bdc4d
lines 6–33why, one step back6import { hc } from '.'78describe('WebSockets', () => {9 const app = new Hono()10 .get(11 '/ws',12 upgradeWebSocket(() => ({}))13 )14 .get('/', (c) => c.json({}))15 const client = hc<typeof app>('/')1617 it('WebSocket route', () => {18 expectTypeOf(client.ws).toMatchTypeOf<{19 $ws: () => WebSocket20 }>()21 })22 it('Not WebSocket Route', () => {23 expectTypeOf<24 typeof client.index extends { $ws: () => WebSocket } ? false : true25 >().toEqualTypeOf(true)26 })27})2829describe('without the leading slash', () => {30 const app = new Hono()31 .get('foo', (c) => c.json({}))32 .get('foo/bar', (c) => c.json({}))33 .get('foo/:id/baz', (c) => c.json({}))Merge pull request #4574 from honojs/nextYusuke Wadabefore adoption0d739b6
line 34why, one step back34 const client = hc<typeof app, 'http://localhost'>('http://localhost')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 35–36why, one step back35 it('`foo` should have `$get`', () => {36 expectTypeOf(client.foo).toHaveProperty('$get')Merge pull request #4574 from honojs/nextYusuke Wadabefore adoption0d739b6
line 37why, one step back37 expectTypeOf(client.foo.$url()).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo', ''>>()Merge pull request #4735 from honojs/nextYusuke Wadabefore adoption01e78ad
line 38why, one step back38 expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>()4.9.0Yusuke Wadabefore adoption82bdc4d
lines 39–41why, one step back39 })40 it('`foo.bar` should not have `$get`', () => {41 expectTypeOf(client.foo.bar).toHaveProperty('$get')Merge pull request #4574 from honojs/nextYusuke Wadabefore adoption0d739b6
lines 42–44why, one step back42 expectTypeOf(client.foo.bar.$url()).toEqualTypeOf<43 TypedURL<'http:', 'localhost', '', '/foo/bar', ''>44 >()Merge pull request #4735 from honojs/nextYusuke Wadabefore adoption01e78ad
line 45why, one step back45 expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>()4.9.0Yusuke Wadabefore adoption82bdc4d
lines 46–48why, one step back46 })47 it('`foo[":id"].baz` should have `$get`', () => {48 expectTypeOf(client.foo[':id'].baz).toHaveProperty('$get')Merge pull request #4574 from honojs/nextYusuke Wadabefore adoption0d739b6
lines 49–62why, one step back49 expectTypeOf(client.foo[':id'].baz.$url()).toEqualTypeOf<50 TypedURL<'http:', 'localhost', '', '/foo/:id/baz', ''>51 >()52 expectTypeOf(53 client.foo[':id'].baz.$url({54 param: { id: '123' },55 })56 ).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo/123/baz', ''>>()57 expectTypeOf(58 client.foo[':id'].baz.$url({59 param: { id: '123' },60 query: { q: 'hono' },61 })62 ).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo/123/baz', `?${string}`>>()Merge pull request #4735 from honojs/nextYusuke Wadabefore adoption01e78ad
lines 63–75why, one step back6364 expectTypeOf(client.foo[':id'].baz.$path()).toEqualTypeOf<'/foo/:id/baz'>()65 expectTypeOf(66 client.foo[':id'].baz.$path({67 param: { id: '123' },68 })69 ).toEqualTypeOf<'/foo/123/baz'>()70 expectTypeOf(71 client.foo[':id'].baz.$path({72 param: { id: '123' },73 query: { q: 'hono' },74 })75 ).toEqualTypeOf<`/foo/123/baz?${string}`>()4.9.0Yusuke Wadabefore adoption82bdc4d
lines 76–94why, one step back76 })77})7879describe('with the leading slash', () => {80 const app = new Hono()81 .get('/foo', (c) => c.json({}))82 .get('/foo/bar', (c) => c.json({}))83 .get('/foo/:id/baz', (c) => c.json({}))84 const client = hc<typeof app>('')85 it('`foo` should have `$get`', () => {86 expectTypeOf(client.foo).toHaveProperty('$get')87 })88 it('`foo.bar` should not have `$get`', () => {89 expectTypeOf(client.foo.bar).toHaveProperty('$get')90 })91 it('`foo[":id"].baz` should have `$get`', () => {92 expectTypeOf(client.foo[':id'].baz).toHaveProperty('$get')93 })94})fix(client): exclude $all from ClientRequest type (#4611)Ryota Ikezawabefore adoption5d4e6aa
lines 95–125why, one step back9596describe('app.all()', () => {97 const app = new Hono()98 .all('/all-route', (c) => c.json({ msg: 'all methods' }))99 .get('/get-route', (c) => c.json({ msg: 'get only' }))100 const client = hc<typeof app>('http://localhost', { fetch: app.request })101102 it('should NOT expose $all on the client', () => {103 expectTypeOf<104 (typeof client)['all-route'] extends { $all: unknown } ? true : false105 >().toEqualTypeOf<false>()106 })107108 it('should still expose valid HTTP methods like $get', () => {109 expectTypeOf(client['get-route']).toHaveProperty('$get')110 })111112 it('should expose all standard HTTP methods for routes defined with app.all()', () => {113 // $all routes should have all standard HTTP methods typed114 expectTypeOf(client['all-route']).toHaveProperty('$get')115 expectTypeOf(client['all-route']).toHaveProperty('$post')116 expectTypeOf(client['all-route']).toHaveProperty('$put')117 expectTypeOf(client['all-route']).toHaveProperty('$delete')118 expectTypeOf(client['all-route']).toHaveProperty('$options')119 expectTypeOf(client['all-route']).toHaveProperty('$patch')120 })121122 it('should have correct return type for expanded methods', async () => {123 // The response type should match the original handler's return type124 const res = await client['all-route'].$get()125 expectTypeOf(res.json()).resolves.toEqualTypeOf<{ msg: string }>()Merge pull request #4735 from honojs/nextYusuke Wadabefore adoption01e78ad
lines 126–143why, one step back126 })127})128129describe('with base URL pathname', () => {130 const app = new Hono()131 .get('foo', (c) => c.json({}))132 .get('foo/bar', (c) => c.json({}))133 .get('foo/:id/baz', (c) => c.json({}))134 const client = hc<typeof app, 'http://localhost/api'>('http://localhost/api')135 it('$path', () => {136 expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>()137 expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>()138 expectTypeOf(139 client.foo[':id'].baz.$path({140 param: { id: '123' },141 query: { q: 'hono' },142 })143 ).toEqualTypeOf<`/foo/123/baz?${string}`>()fix(client): exclude $all from ClientRequest type (#4611)Ryota Ikezawabefore adoption5d4e6aa
lines 144–145why, one step back144 })145})