diff --git a/deno_dist/client/types.ts b/deno_dist/client/types.ts index 57e87aad..c524a4e5 100644 --- a/deno_dist/client/types.ts +++ b/deno_dist/client/types.ts @@ -33,14 +33,13 @@ export type ClientRequest = { : {} : {} ) => URL -} & { - // WebSocket - $ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } } +} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } } ? S['$get'] extends { input: infer I } - ? (args?: Omit) => WebSocket - : never - : never -} + ? { + $ws: (args?: Omit) => WebSocket + } + : {} + : {}) // eslint-disable-next-line @typescript-eslint/no-explicit-any type BlankRecordToNever = T extends any diff --git a/deno_dist/helper/websocket/index.ts b/deno_dist/helper/websocket/index.ts index d610c722..b7878b58 100644 --- a/deno_dist/helper/websocket/index.ts +++ b/deno_dist/helper/websocket/index.ts @@ -12,7 +12,7 @@ export interface WSEvents { onError?: (evt: Event, ws: WSContext) => void } -export type UpgradedWebSocketResponseInputJSONType = '__websocket' | undefined +export type UpgradedWebSocketResponseInputJSONType = '__websocket' /** * Upgrade WebSocket Type diff --git a/src/client/types.test.ts b/src/client/types.test.ts new file mode 100644 index 00000000..c1c73d36 --- /dev/null +++ b/src/client/types.test.ts @@ -0,0 +1,25 @@ +import { expectTypeOf } from 'vitest' +import { Hono } from '..' +import { upgradeWebSocket } from '../helper' +import { hc } from '.' + +describe('WebSockets', () => { + const app = new Hono() + .get( + '/ws', + upgradeWebSocket(() => ({})) + ) + .get('/', (c) => c.json({})) + const client = hc('/') + + it('WebSocket route', () => { + expectTypeOf(client.ws).toMatchTypeOf<{ + $ws: () => WebSocket + }>() + }) + it('Not WebSocket Route', () => { + expectTypeOf< + typeof client.index extends { $ws: () => WebSocket } ? false : true + >().toEqualTypeOf(true) + }) +}) diff --git a/src/client/types.ts b/src/client/types.ts index 0bb1dbd8..3dfedd71 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -33,14 +33,13 @@ export type ClientRequest = { : {} : {} ) => URL -} & { - // WebSocket - $ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } } +} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } } ? S['$get'] extends { input: infer I } - ? (args?: Omit) => WebSocket - : never - : never -} + ? { + $ws: (args?: Omit) => WebSocket + } + : {} + : {}) // eslint-disable-next-line @typescript-eslint/no-explicit-any type BlankRecordToNever = T extends any diff --git a/src/helper/websocket/index.ts b/src/helper/websocket/index.ts index 6d747410..684ef0a1 100644 --- a/src/helper/websocket/index.ts +++ b/src/helper/websocket/index.ts @@ -12,7 +12,7 @@ export interface WSEvents { onError?: (evt: Event, ws: WSContext) => void } -export type UpgradedWebSocketResponseInputJSONType = '__websocket' | undefined +export type UpgradedWebSocketResponseInputJSONType = '__websocket' /** * Upgrade WebSocket Type