Skip to content

Commit 5c9d8b4

Browse files
authored
fix(client): Don't show $ws when not used WebSockets (#2532)
* fix(client): Don't show `$ws` when not used ws * test(client): add types test * chore: format code * chore: remove no used arg
1 parent 9f9a41c commit 5c9d8b4

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

deno_dist/client/types.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export type ClientRequest<S extends Schema> = {
3333
: {}
3434
: {}
3535
) => URL
36-
} & {
37-
// WebSocket
38-
$ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
36+
} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
3937
? S['$get'] extends { input: infer I }
40-
? (args?: Omit<I, 'json'>) => WebSocket
41-
: never
42-
: never
43-
}
38+
? {
39+
$ws: (args?: Omit<I, 'json'>) => WebSocket
40+
}
41+
: {}
42+
: {})
4443

4544
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4645
type BlankRecordToNever<T> = T extends any

deno_dist/helper/websocket/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface WSEvents {
1212
onError?: (evt: Event, ws: WSContext) => void
1313
}
1414

15-
export type UpgradedWebSocketResponseInputJSONType = '__websocket' | undefined
15+
export type UpgradedWebSocketResponseInputJSONType = '__websocket'
1616

1717
/**
1818
* Upgrade WebSocket Type

src/client/types.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expectTypeOf } from 'vitest'
2+
import { Hono } from '..'
3+
import { upgradeWebSocket } from '../helper'
4+
import { hc } from '.'
5+
6+
describe('WebSockets', () => {
7+
const app = new Hono()
8+
.get(
9+
'/ws',
10+
upgradeWebSocket(() => ({}))
11+
)
12+
.get('/', (c) => c.json({}))
13+
const client = hc<typeof app>('/')
14+
15+
it('WebSocket route', () => {
16+
expectTypeOf(client.ws).toMatchTypeOf<{
17+
$ws: () => WebSocket
18+
}>()
19+
})
20+
it('Not WebSocket Route', () => {
21+
expectTypeOf<
22+
typeof client.index extends { $ws: () => WebSocket } ? false : true
23+
>().toEqualTypeOf(true)
24+
})
25+
})

src/client/types.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export type ClientRequest<S extends Schema> = {
3333
: {}
3434
: {}
3535
) => URL
36-
} & {
37-
// WebSocket
38-
$ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
36+
} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
3937
? S['$get'] extends { input: infer I }
40-
? (args?: Omit<I, 'json'>) => WebSocket
41-
: never
42-
: never
43-
}
38+
? {
39+
$ws: (args?: Omit<I, 'json'>) => WebSocket
40+
}
41+
: {}
42+
: {})
4443

4544
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4645
type BlankRecordToNever<T> = T extends any

src/helper/websocket/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface WSEvents {
1212
onError?: (evt: Event, ws: WSContext) => void
1313
}
1414

15-
export type UpgradedWebSocketResponseInputJSONType = '__websocket' | undefined
15+
export type UpgradedWebSocketResponseInputJSONType = '__websocket'
1616

1717
/**
1818
* Upgrade WebSocket Type

0 commit comments

Comments
 (0)