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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147/* eslint-disable @typescript-eslint/no-unused-vars */import { expectTypeOf } from 'vitest'import { hc } from '.'import { Hono } from '..'import { upgradeWebSocket } from '../adapter/deno/websocket'import type { TypedURL } from './types'
describe('WebSockets', () => { const app = new Hono() .get( '/ws', upgradeWebSocket(() => ({})) ) .get('/', (c) => c.json({})) const client = hc<typeof app>('/')
it('WebSocket route', () => { expectTypeOf(client.ws).toMatchTypeOf<{ $ws: () => WebSocket }>() }) it('Not WebSocket Route', () => { expectTypeOf< typeof client.index extends { $ws: () => WebSocket } ? false : true >().toEqualTypeOf(true) })})
describe('without the leading slash', () => { const app = new Hono() .get('foo', (c) => c.json({})) .get('foo/bar', (c) => c.json({})) .get('foo/:id/baz', (c) => c.json({})) const client = hc<typeof app, 'http://localhost'>('http://localhost') it('`foo` should have `$get`', () => { expectTypeOf(client.foo).toHaveProperty('$get') expectTypeOf(client.foo.$url()).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo', ''>>() expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>() }) it('`foo.bar` should not have `$get`', () => { expectTypeOf(client.foo.bar).toHaveProperty('$get') expectTypeOf(client.foo.bar.$url()).toEqualTypeOf< TypedURL<'http:', 'localhost', '', '/foo/bar', ''> >() expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>() }) it('`foo[":id"].baz` should have `$get`', () => { expectTypeOf(client.foo[':id'].baz).toHaveProperty('$get') expectTypeOf(client.foo[':id'].baz.$url()).toEqualTypeOf< TypedURL<'http:', 'localhost', '', '/foo/:id/baz', ''> >() expectTypeOf( client.foo[':id'].baz.$url({ param: { id: '123' }, }) ).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo/123/baz', ''>>() expectTypeOf( client.foo[':id'].baz.$url({ param: { id: '123' }, query: { q: 'hono' }, }) ).toEqualTypeOf<TypedURL<'http:', 'localhost', '', '/foo/123/baz', `?${string}`>>()
expectTypeOf(client.foo[':id'].baz.$path()).toEqualTypeOf<'/foo/:id/baz'>() expectTypeOf( client.foo[':id'].baz.$path({ param: { id: '123' }, }) ).toEqualTypeOf<'/foo/123/baz'>() expectTypeOf( client.foo[':id'].baz.$path({ param: { id: '123' }, query: { q: 'hono' }, }) ).toEqualTypeOf<`/foo/123/baz?${string}`>() })})
describe('with the leading slash', () => { const app = new Hono() .get('/foo', (c) => c.json({})) .get('/foo/bar', (c) => c.json({})) .get('/foo/:id/baz', (c) => c.json({})) const client = hc<typeof app>('') it('`foo` should have `$get`', () => { expectTypeOf(client.foo).toHaveProperty('$get') }) it('`foo.bar` should not have `$get`', () => { expectTypeOf(client.foo.bar).toHaveProperty('$get') }) it('`foo[":id"].baz` should have `$get`', () => { expectTypeOf(client.foo[':id'].baz).toHaveProperty('$get') })})
describe('app.all()', () => { const app = new Hono() .all('/all-route', (c) => c.json({ msg: 'all methods' })) .get('/get-route', (c) => c.json({ msg: 'get only' })) const client = hc<typeof app>('http://localhost', { fetch: app.request })
it('should NOT expose $all on the client', () => { expectTypeOf< (typeof client)['all-route'] extends { $all: unknown } ? true : false >().toEqualTypeOf<false>() })
it('should still expose valid HTTP methods like $get', () => { expectTypeOf(client['get-route']).toHaveProperty('$get') })
it('should expose all standard HTTP methods for routes defined with app.all()', () => { // $all routes should have all standard HTTP methods typed expectTypeOf(client['all-route']).toHaveProperty('$get') expectTypeOf(client['all-route']).toHaveProperty('$post') expectTypeOf(client['all-route']).toHaveProperty('$put') expectTypeOf(client['all-route']).toHaveProperty('$delete') expectTypeOf(client['all-route']).toHaveProperty('$options') expectTypeOf(client['all-route']).toHaveProperty('$patch') expectTypeOf(client['all-route']).toHaveProperty('$query') })
it('should have correct return type for expanded methods', async () => { // The response type should match the original handler's return type const res = await client['all-route'].$get() expectTypeOf(res.json()).resolves.toEqualTypeOf<{ msg: string }>() })})
describe('with base URL pathname', () => { const app = new Hono() .get('foo', (c) => c.json({})) .get('foo/bar', (c) => c.json({})) .get('foo/:id/baz', (c) => c.json({})) const client = hc<typeof app, 'http://localhost/api'>('http://localhost/api') it('$path', () => { expectTypeOf(client.foo.$path()).toEqualTypeOf<'/foo'>() expectTypeOf(client.foo.bar.$path()).toEqualTypeOf<'/foo/bar'>() expectTypeOf( client.foo[':id'].baz.$path({ param: { id: '123' }, query: { q: 'hono' }, }) ).toEqualTypeOf<`/foo/123/baz?${string}`>() })})