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:
parent
9f9a41cc08
commit
5c9d8b4172
@ -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
|
||||
|
@ -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
25
src/client/types.test.ts
Normal 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)
|
||||
})
|
||||
})
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user