types.test.ts
This file
Path
src/client/types.test.ts
146 lines · 5.0 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–2why, one step back1/* eslint-disable @typescript-eslint/no-unused-vars */2import { expectTypeOf } from 'vitest'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 3why, one step back3import { hc } from '.'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 4–119why, one step back4import { Hono } from '..'5import { upgradeWebSocket } from '../adapter/deno/websocket'6import type { TypedURL } from './types'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({}))34 const client = hc<typeof app, 'http://localhost'>('http://localhost')35 it('`foo` should have `$get`', () => {36 expectTypeOf(client.foo).toHaveProperty('$get')37 expectTypeOf(client.foo.$url()).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo', ''>>()38 expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>()39 })40 it('`foo.bar` should not have `$get`', () => {41 expectTypeOf(client.foo.bar).toHaveProperty('$get')42 expectTypeOf(client.foo.bar.$url()).toEqualTypeOf<43 TypedURL<'http:', 'localhost', '', '/foo/bar', ''>44 >()45 expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>()46 })47 it('`foo[":id"].baz` should have `$get`', () => {48 expectTypeOf(client.foo[':id'].baz).toHaveProperty('$get')49 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}`>>()6364 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}`>()76 })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})9596describe('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')Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 120why, one step back120 expectTypeOf(client['all-route']).toHaveProperty('$query')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 121–146why, one step back121 })122123 it('should have correct return type for expanded methods', async () => {124 // The response type should match the original handler's return type125 const res = await client['all-route'].$get()126 expectTypeOf(res.json()).resolves.toEqualTypeOf<{ msg: string }>()127 })128})129130describe('with base URL pathname', () => {131 const app = new Hono()132 .get('foo', (c) => c.json({}))133 .get('foo/bar', (c) => c.json({}))134 .get('foo/:id/baz', (c) => c.json({}))135 const client = hc<typeof app, 'http://localhost/api'>('http://localhost/api')136 it('$path', () => {137 expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>()138 expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>()139 expectTypeOf(140 client.foo[':id'].baz.$path({141 param: { id: '123' },142 query: { q: 'hono' },143 })144 ).toEqualTypeOf<`/foo/123/baz?${string}`>()145 })146})