0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 11:17:33 +01:00

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
This commit is contained in:
Shotaro Nakamura 2024-04-22 08:42:09 +09:00 committed by GitHub
parent 9f9a41cc08
commit 5c9d8b4172
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 16 deletions

View File

@ -33,14 +33,13 @@ export type ClientRequest<S extends Schema> = {
: {}
: {}
) => URL
} & {
// WebSocket
$ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
? S['$get'] extends { input: infer I }
? (args?: Omit<I, 'json'>) => WebSocket
: never
: never
}
? {
$ws: (args?: Omit<I, 'json'>) => WebSocket
}
: {}
: {})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any

View File

@ -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

25
src/client/types.test.ts Normal file
View File

@ -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<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)
})
})

View File

@ -33,14 +33,13 @@ export type ClientRequest<S extends Schema> = {
: {}
: {}
) => URL
} & {
// WebSocket
$ws: S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
} & (S['$get'] extends { input: { json: UpgradedWebSocketResponseInputJSONType } }
? S['$get'] extends { input: infer I }
? (args?: Omit<I, 'json'>) => WebSocket
: never
: never
}
? {
$ws: (args?: Omit<I, 'json'>) => WebSocket
}
: {}
: {})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any

View File

@ -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