|
1 | | -import type { RpcFunctionDefinition, RpcFunctionSetupResult, RpcFunctionType, Thenable } from 'birpc-x' |
2 | | -import type { GenericSchema, InferInput } from 'valibot' |
| 1 | +import type { RpcFunctionDefinition, RpcFunctionType } from 'birpc-x' |
| 2 | +import type { GenericSchema } from 'valibot' |
3 | 3 | import type { DevToolsNodeContext } from '../types' |
4 | 4 | import { createDefineWrapperWithContext } from 'birpc-x' |
5 | 5 |
|
6 | | -type InferInputsTuple<AS> |
7 | | - = AS extends readonly GenericSchema[] |
8 | | - ? { -readonly [K in keyof AS]: AS[K] extends GenericSchema ? InferInput<AS[K]> : any } |
9 | | - : any[] |
10 | | - |
11 | 6 | export interface RpcOptions< |
12 | | - AS, |
13 | | - RS, |
14 | 7 | NAME extends string, |
15 | 8 | TYPE extends RpcFunctionType, |
16 | | -> extends Omit< |
17 | | - RpcFunctionDefinition< |
18 | | - NAME, |
19 | | - TYPE, |
20 | | - InferInputsTuple<AS>, |
21 | | - RS extends GenericSchema ? InferInput<RS> : any, |
22 | | - DevToolsNodeContext |
23 | | - >, |
24 | | - 'setup' |
25 | | - > { |
| 9 | + A extends any[], |
| 10 | + R, |
| 11 | + AS extends GenericSchema[] | undefined = undefined, |
| 12 | + RS extends GenericSchema | undefined = undefined, |
| 13 | +> |
| 14 | + extends RpcFunctionDefinition<NAME, TYPE, A, R, DevToolsNodeContext> { |
26 | 15 | args?: AS |
27 | | - returns?: RS |
28 | | - |
29 | | - setup: (ctx: DevToolsNodeContext) => Thenable< |
30 | | - RpcFunctionSetupResult<InferInputsTuple<AS>, RS extends GenericSchema ? InferInput<RS> : any> |
31 | | - > |
| 16 | + return?: RS |
32 | 17 | } |
33 | 18 |
|
34 | | -export function defineRpcFunction<AS = undefined, RS = undefined, NAME extends string = string, TYPE extends RpcFunctionType = 'query'>( |
35 | | - options: RpcOptions<AS, RS, NAME, TYPE>, |
| 19 | +export function defineRpcFunction< |
| 20 | + NAME extends string, |
| 21 | + TYPE extends RpcFunctionType, |
| 22 | + A extends any[], |
| 23 | + R, |
| 24 | + AS extends GenericSchema[] | undefined = undefined, |
| 25 | + RS extends GenericSchema | undefined = undefined, |
| 26 | +>( |
| 27 | + options: RpcOptions<NAME, TYPE, A, R, AS, RS>, |
36 | 28 | ) { |
37 | | - const { args: argsSchema, returns: returnsSchema, ...rest } = options |
| 29 | + const { args, return: ret, ...rest } = options |
38 | 30 | const birpc = createDefineWrapperWithContext<DevToolsNodeContext>() |
39 | 31 |
|
40 | | - return { |
41 | | - fn: birpc<any, TYPE, InferInputsTuple<AS>, RS extends GenericSchema ? InferInput<RS> : any>({ |
42 | | - ...rest, |
43 | | - }), |
44 | | - args: argsSchema, |
45 | | - returns: returnsSchema, |
46 | | - } |
| 32 | + const fn = birpc(rest) |
| 33 | + |
| 34 | + return fn as typeof fn & { argsSchema?: AS, returnSchema?: RS } |
47 | 35 | } |
0 commit comments