Skip to content

Commit c008f24

Browse files
authored
fix(kit): restore rpc module augmentation (#551)
1 parent ad73dd2 commit c008f24

7 files changed

Lines changed: 97 additions & 15 deletions

File tree

packages/kit/src/client/connection.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import type { DevframeRpcClient, DevframeRpcClientOptions } from '@devframes/hub/client'
2+
import type { DevframeRpcClientFunctions } from '@devframes/hub/types'
3+
import type { BirpcReturn } from 'devframe/rpc'
4+
import type { DevToolsRpcServerFunctions } from '../types/rpc-augments'
25
import { getDevframeRpcClient } from '@devframes/hub/client'
36

7+
export type DevToolsRpcClientCall = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$call']
8+
export type DevToolsRpcClientCallEvent = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$callEvent']
9+
export type DevToolsRpcClientCallOptional = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$callOptional']
10+
export type DevToolsRpcClientOptions = DevframeRpcClientOptions
11+
12+
export interface DevToolsRpcClient extends Omit<
13+
DevframeRpcClient,
14+
'call' | 'callEvent' | 'callOptional'
15+
> {
16+
call: DevToolsRpcClientCall
17+
callEvent: DevToolsRpcClientCallEvent
18+
callOptional: DevToolsRpcClientCallOptional
19+
}
20+
421
/**
522
* The Vite DevTools flavour of devframe's {@link getDevframeRpcClient}. Kept as
623
* a dedicated export for naming symmetry with the kit's other `DevTools*`
@@ -17,10 +34,10 @@ import { getDevframeRpcClient } from '@devframes/hub/client'
1734
* devframe's native browser-prompt fallback for every kit-managed connection.
1835
*/
1936
export function getDevToolsRpcClient(
20-
options: DevframeRpcClientOptions = {},
21-
): Promise<DevframeRpcClient> {
37+
options: DevToolsRpcClientOptions = {},
38+
): Promise<DevToolsRpcClient> {
2239
return getDevframeRpcClient({
2340
...options,
2441
simpleAuth: false,
25-
})
42+
}) as Promise<DevToolsRpcClient>
2643
}

packages/kit/src/client/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ export * from './remote'
77

88
export {
99
type DevframeClientRpcHost as DevToolsClientRpcHost,
10-
type DevframeRpcClient as DevToolsRpcClient,
11-
type DevframeRpcClientCall as DevToolsRpcClientCall,
12-
type DevframeRpcClientCallEvent as DevToolsRpcClientCallEvent,
13-
type DevframeRpcClientCallOptional as DevToolsRpcClientCallOptional,
1410
type DevframeRpcClientMode as DevToolsRpcClientMode,
15-
type DevframeRpcClientOptions as DevToolsRpcClientOptions,
1611
type DevframeRpcContext as DevToolsRpcContext,
1712
type RpcStreamingClientHost,
1813
type StreamingSubscribeOptions,

packages/kit/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './commands'
66
export * from './docks'
77
export * from './json-render'
88
export * from './messages'
9+
export * from './rpc-augments'
910
export * from './settings'
1011
export * from './terminals'
1112
export * from './vite-augment'
@@ -19,7 +20,6 @@ export type {
1920
DevframeHost as DevToolsHost,
2021
DevframeNodeRpcSession as DevToolsNodeRpcSession,
2122
DevframeRpcClientFunctions as DevToolsRpcClientFunctions,
22-
DevframeRpcServerFunctions as DevToolsRpcServerFunctions,
2323
DevframeRpcSharedStates as DevToolsRpcSharedStates,
2424
DevframeViewHost as DevToolsViewHost,
2525
EventEmitter,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { DevframeRpcServerFunctions } from '@devframes/hub/types'
2+
3+
/**
4+
* Server-side RPC functions exposed by Vite DevTools integrations.
5+
*
6+
* Extend this interface with module augmentation to type `rpc.call()`.
7+
*/
8+
export interface DevToolsRpcServerFunctions extends DevframeRpcServerFunctions {}

test/__snapshots__/tsnapi/@vitejs/devtools-kit/client.snapshot.d.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
/**
22
* Generated by tsnapi — public API snapshot of `@vitejs/devtools-kit/client`
33
*/
4+
// #region Interfaces
5+
export interface DevToolsRpcClient extends Omit<DevframeRpcClient, 'call' | 'callEvent' | 'callOptional'> {
6+
call: DevToolsRpcClientCall;
7+
callEvent: DevToolsRpcClientCallEvent;
8+
callOptional: DevToolsRpcClientCallOptional;
9+
}
10+
// #endregion
11+
12+
// #region Types
13+
export type DevToolsRpcClientCall = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$call'];
14+
export type DevToolsRpcClientCallEvent = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$callEvent'];
15+
export type DevToolsRpcClientCallOptional = BirpcReturn<DevToolsRpcServerFunctions, DevframeRpcClientFunctions>['$callOptional'];
16+
export type DevToolsRpcClientOptions = DevframeRpcClientOptions;
17+
// #endregion
18+
419
// #region Functions
520
export declare function getDevToolsClientContext(): DevToolsClientContext | undefined;
6-
export declare function getDevToolsRpcClient(_?: DevframeRpcClientOptions): Promise<DevframeRpcClient>;
21+
export declare function getDevToolsRpcClient(_?: DevToolsRpcClientOptions): Promise<DevToolsRpcClient>;
722
// #endregion
823

924
// #region Variables
@@ -25,12 +40,7 @@ export { DevToolsFrameNavHostMessage }
2540
export { DevToolsFrameNavHostPayload }
2641
export { DevToolsFrameNavListenTarget }
2742
export { DevToolsFrameTab }
28-
export { DevToolsRpcClient }
29-
export { DevToolsRpcClientCall }
30-
export { DevToolsRpcClientCallEvent }
31-
export { DevToolsRpcClientCallOptional }
3243
export { DevToolsRpcClientMode }
33-
export { DevToolsRpcClientOptions }
3444
export { DevToolsRpcContext }
3545
export { DockClientScriptContext }
3646
export { DockClientType }

test/fixtures/rpc-augmentation.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getDevToolsRpcClient } from '@vitejs/devtools-kit/client'
2+
import '@vitejs/devtools-kit'
3+
4+
declare module '@vitejs/devtools-kit' {
5+
interface DevToolsRpcServerFunctions {
6+
'test:get-modules': (filter?: string) => Promise<string[]>
7+
}
8+
}
9+
10+
export async function testRpcAugmentation() {
11+
const rpc = await getDevToolsRpcClient()
12+
const modules = await rpc.call('test:get-modules', 'src/')
13+
14+
modules satisfies string[]
15+
16+
// @ts-expect-error Unknown RPC names remain rejected.
17+
await rpc.call('test:unknown')
18+
// @ts-expect-error Registered RPC arguments remain checked.
19+
await rpc.call('test:get-modules', 42)
20+
}

test/rpc-augmentation.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { resolve } from 'node:path'
2+
import ts from 'typescript'
3+
import { describe, expect, it } from 'vitest'
4+
5+
describe('rpc module augmentation', () => {
6+
it('types getDevToolsRpcClient calls through the kit registry', () => {
7+
const root = resolve(import.meta.dirname, '..')
8+
const fixture = resolve(import.meta.dirname, 'fixtures/rpc-augmentation.ts')
9+
const program = ts.createProgram([fixture], {
10+
baseUrl: root,
11+
ignoreDeprecations: '6.0',
12+
module: ts.ModuleKind.ESNext,
13+
moduleResolution: ts.ModuleResolutionKind.Bundler,
14+
noEmit: true,
15+
paths: {
16+
'@vitejs/devtools-kit': ['packages/kit/src/index.ts'],
17+
'@vitejs/devtools-kit/client': ['packages/kit/src/client/index.ts'],
18+
},
19+
skipLibCheck: true,
20+
strict: true,
21+
target: ts.ScriptTarget.ESNext,
22+
})
23+
const diagnostics = ts.getPreEmitDiagnostics(program)
24+
const output = ts.formatDiagnosticsWithColorAndContext(diagnostics, {
25+
getCanonicalFileName: file => file,
26+
getCurrentDirectory: () => root,
27+
getNewLine: () => '\n',
28+
})
29+
30+
expect(output).toBe('')
31+
})
32+
})

0 commit comments

Comments
 (0)