@vitejs/devtools-rpc 0.0.0-alpha.3 → 0.0.0-alpha.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +214 -0
- package/dist/client.d.mts +9 -0
- package/dist/client.mjs +15 -0
- package/dist/index.d.mts +257 -0
- package/dist/index.mjs +321 -0
- package/dist/{index-BOWJO778.d.ts → presets/index.d.mts} +1 -1
- package/dist/presets/ws/client.d.mts +14 -0
- package/dist/presets/ws/{client.js → client.mjs} +5 -4
- package/dist/presets/ws/server.d.mts +22 -0
- package/dist/presets/ws/{server.js → server.mjs} +331 -329
- package/dist/server.d.mts +9 -0
- package/dist/server.mjs +14 -0
- package/package.json +17 -14
- package/dist/index.d.ts +0 -15
- package/dist/index.js +0 -22
- package/dist/presets/index.d.ts +0 -2
- package/dist/presets/index.js +0 -3
- package/dist/presets/ws/client.d.ts +0 -17
- package/dist/presets/ws/server.d.ts +0 -13
- /package/dist/{presets-CXNlz3L-.js → presets/index.mjs} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# @vitejs/devtools-rpc
|
|
2
|
+
|
|
3
|
+
DevTools RPC for Vite, featuring extensible [birpc](https://github.com/antfu-collective/birpc) interfaces with advanced type-safe function definitions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Type-safe function definitions** with automatic type inference
|
|
8
|
+
- **Dynamic function registration** with hot updates
|
|
9
|
+
- **User-provided function context** for setup and handlers
|
|
10
|
+
- **Schema validation** via [`valibot`](https://valibot.dev)
|
|
11
|
+
- **Cache Manager** for RPC result caching
|
|
12
|
+
- **Dump feature** for pre-computing results (static hosting, testing, offline mode)
|
|
13
|
+
- **Basic RPC Client/Server** built on birpc
|
|
14
|
+
- **WebSocket Presets** ready-to-use transport presets
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm install @vitejs/devtools-rpc
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic RPC Client/Server
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { createRpcClient } from '@vitejs/devtools-rpc/client'
|
|
28
|
+
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
|
|
29
|
+
import { createRpcServer } from '@vitejs/devtools-rpc/server'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Defining Functions
|
|
33
|
+
|
|
34
|
+
Use `defineRpcFunction` to create type-safe RPC function definitions:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineRpcFunction } from '@vitejs/devtools-rpc'
|
|
38
|
+
|
|
39
|
+
// Simple function
|
|
40
|
+
const greet = defineRpcFunction({
|
|
41
|
+
name: 'greet',
|
|
42
|
+
handler: (name: string) => `Hello, ${name}!`
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can provide a context to functions for setup and initialization:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { defineRpcFunction } from '@vitejs/devtools-rpc'
|
|
50
|
+
|
|
51
|
+
// With setup and context
|
|
52
|
+
const getUser = defineRpcFunction({
|
|
53
|
+
name: 'getUser',
|
|
54
|
+
setup: (context) => {
|
|
55
|
+
console.log(context)
|
|
56
|
+
return {
|
|
57
|
+
handler: (id: string) => context.users[id]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Schema Validation
|
|
64
|
+
|
|
65
|
+
Use Valibot schemas for automatic argument and return value validation:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { defineRpcFunction } from '@vitejs/devtools-rpc'
|
|
69
|
+
import * as v from 'valibot'
|
|
70
|
+
|
|
71
|
+
const add = defineRpcFunction({
|
|
72
|
+
name: 'add',
|
|
73
|
+
args: [v.number(), v.number()] as const,
|
|
74
|
+
returns: v.number(),
|
|
75
|
+
handler: (a, b) => a + b // Types are automatically inferred
|
|
76
|
+
})
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Function Collector
|
|
80
|
+
|
|
81
|
+
`RpcFunctionsCollector` manages dynamic function registration and provides a type-safe proxy for accessing functions:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { defineRpcFunction, RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
|
|
85
|
+
|
|
86
|
+
// Provide a custom context to the collector
|
|
87
|
+
const collector = new RpcFunctionsCollectorBase({ users: [/* ... */] })
|
|
88
|
+
|
|
89
|
+
// Register functions
|
|
90
|
+
collector.register(defineRpcFunction({
|
|
91
|
+
name: 'greet',
|
|
92
|
+
handler: (name: string) => `Hello, ${name}!`,
|
|
93
|
+
}))
|
|
94
|
+
collector.register(defineRpcFunction({
|
|
95
|
+
name: 'getUser',
|
|
96
|
+
setup: (context) => {
|
|
97
|
+
return {
|
|
98
|
+
handler: (id: string) => context.users.find((user: { id: string }) => user.id === id)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}))
|
|
102
|
+
|
|
103
|
+
// Access via proxy
|
|
104
|
+
await collector.functions.greet('Alice') // "Hello, Alice!"
|
|
105
|
+
|
|
106
|
+
// Listen for changes
|
|
107
|
+
const unsubscribe = collector.onChanged((fnName) => {
|
|
108
|
+
console.log(`Function ${fnName} changed`)
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Dump Feature
|
|
113
|
+
|
|
114
|
+
The dump feature allows pre-computing RPC results for static hosting, testing, or offline mode. This is useful for static sites or when you want to avoid runtime computation.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { createClientFromDump, defineRpcFunction, dumpFunctions } from '@vitejs/devtools-rpc'
|
|
118
|
+
|
|
119
|
+
// Define functions with dump configurations
|
|
120
|
+
const greet = defineRpcFunction({
|
|
121
|
+
name: 'greet',
|
|
122
|
+
handler: (name: string) => `Hello, ${name}!`,
|
|
123
|
+
dump: {
|
|
124
|
+
inputs: [
|
|
125
|
+
['Alice'],
|
|
126
|
+
['Bob'],
|
|
127
|
+
['Charlie']
|
|
128
|
+
],
|
|
129
|
+
fallback: 'Hello, stranger!'
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// Collect pre-computed results
|
|
134
|
+
const store = await dumpFunctions([greet])
|
|
135
|
+
|
|
136
|
+
// Create a client that serves from the dump store
|
|
137
|
+
const client = createClientFromDump(store)
|
|
138
|
+
|
|
139
|
+
await client.greet('Alice') // Returns pre-computed: "Hello, Alice!"
|
|
140
|
+
await client.greet('Unknown') // Returns fallback: "Hello, stranger!"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Functions with `type: 'static'` automatically get dumped with empty arguments if no dump configuration is provided.
|
|
144
|
+
|
|
145
|
+
#### Pre-computed Records
|
|
146
|
+
|
|
147
|
+
You can provide pre-computed records directly to bypass function execution:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { defineRpcFunction } from '@vitejs/devtools-rpc'
|
|
151
|
+
|
|
152
|
+
const multiply = defineRpcFunction({
|
|
153
|
+
name: 'multiply',
|
|
154
|
+
handler: (a: number, b: number) => a * b,
|
|
155
|
+
dump: {
|
|
156
|
+
records: [
|
|
157
|
+
{ inputs: [2, 3], output: 6 },
|
|
158
|
+
{ inputs: [4, 5], output: 20 },
|
|
159
|
+
],
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
You can also mix computed (`inputs`) and pre-computed (`records`) in the same dump configuration.
|
|
165
|
+
|
|
166
|
+
#### Parallel Execution
|
|
167
|
+
|
|
168
|
+
Enable parallel processing for faster dump collection:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { dumpFunctions } from '@vitejs/devtools-rpc'
|
|
172
|
+
|
|
173
|
+
// Enable parallel with default concurrency of 5
|
|
174
|
+
const store = await dumpFunctions([greet], context, {
|
|
175
|
+
concurrency: true
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
// Or specify a custom concurrency limit
|
|
179
|
+
const store = await dumpFunctions([greet], context, {
|
|
180
|
+
concurrency: 10 // Limit to 10 concurrent executions
|
|
181
|
+
})
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Set `concurrency` to `true` for parallel execution (default limit: 5) or a number to specify the exact concurrency limit.
|
|
185
|
+
|
|
186
|
+
## Package Exports
|
|
187
|
+
|
|
188
|
+
- **`.`** - Type-safe function definitions and utilities (main export)
|
|
189
|
+
- `RpcFunctionsCollectorBase`, `defineRpcFunction`, `createDefineWrapperWithContext`
|
|
190
|
+
- `dumpFunctions`, `createClientFromDump`, `RpcCacheManager`
|
|
191
|
+
- Type definitions and utilities
|
|
192
|
+
|
|
193
|
+
- **`./client`** - RPC client
|
|
194
|
+
- `createRpcClient`
|
|
195
|
+
|
|
196
|
+
- **`./server`** - RPC server
|
|
197
|
+
- `createRpcServer`
|
|
198
|
+
|
|
199
|
+
- **`./presets`** - RPC presets
|
|
200
|
+
- `defineRpcClientPreset`, `defineRpcServerPreset`
|
|
201
|
+
|
|
202
|
+
- **`./presets/ws/client`** - WebSocket client preset
|
|
203
|
+
- `createWsRpcPreset`
|
|
204
|
+
|
|
205
|
+
- **`./presets/ws/server`** - WebSocket server preset
|
|
206
|
+
- `createWsRpcPreset`
|
|
207
|
+
|
|
208
|
+
## Examples
|
|
209
|
+
|
|
210
|
+
See [src/examples](./src/examples) and [test files](./src) for complete integration examples.
|
|
211
|
+
|
|
212
|
+
## License
|
|
213
|
+
|
|
214
|
+
MIT License © [VoidZero Inc.](https://github.com/vitejs)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BirpcOptions, BirpcReturn } from "birpc";
|
|
2
|
+
|
|
3
|
+
//#region src/client.d.ts
|
|
4
|
+
declare function createRpcClient<ServerFunctions extends object = Record<string, never>, ClientFunctions extends object = Record<string, never>>(functions: ClientFunctions, options: {
|
|
5
|
+
preset: BirpcOptions<ServerFunctions, ClientFunctions, false>;
|
|
6
|
+
rpcOptions?: Partial<BirpcOptions<ServerFunctions, ClientFunctions, boolean>>;
|
|
7
|
+
}): BirpcReturn<ServerFunctions, ClientFunctions, false>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { createRpcClient };
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createBirpc } from "birpc";
|
|
2
|
+
|
|
3
|
+
//#region src/client.ts
|
|
4
|
+
function createRpcClient(functions, options) {
|
|
5
|
+
const { preset, rpcOptions = {} } = options;
|
|
6
|
+
return createBirpc(functions, {
|
|
7
|
+
...preset,
|
|
8
|
+
timeout: -1,
|
|
9
|
+
...rpcOptions,
|
|
10
|
+
proxify: false
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { createRpcClient };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { BirpcFn, BirpcReturn } from "birpc";
|
|
2
|
+
import { GenericSchema, InferInput } from "valibot";
|
|
3
|
+
|
|
4
|
+
//#region src/cache.d.ts
|
|
5
|
+
interface RpcCacheOptions {
|
|
6
|
+
functions: string[];
|
|
7
|
+
keySerializer?: (args: unknown[]) => string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @experimental API is expected to change.
|
|
11
|
+
*/
|
|
12
|
+
declare class RpcCacheManager {
|
|
13
|
+
private cacheMap;
|
|
14
|
+
private options;
|
|
15
|
+
private keySerializer;
|
|
16
|
+
constructor(options: RpcCacheOptions);
|
|
17
|
+
updateOptions(options: Partial<RpcCacheOptions>): void;
|
|
18
|
+
cached<T>(m: string, a: unknown[]): T | undefined;
|
|
19
|
+
apply(req: {
|
|
20
|
+
m: string;
|
|
21
|
+
a: unknown[];
|
|
22
|
+
}, res: unknown): void;
|
|
23
|
+
validate(m: string): boolean;
|
|
24
|
+
clear(fn?: string): void;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/utils.d.ts
|
|
28
|
+
/** Infers TypeScript tuple type from Valibot schema array */
|
|
29
|
+
type InferArgsType<S extends RpcArgsSchema | undefined> = S extends readonly [] ? [] : S extends readonly [infer H, ...infer T] ? H extends GenericSchema ? T extends readonly GenericSchema[] ? [InferInput<H>, ...InferArgsType<T>] : never : never : never;
|
|
30
|
+
/** Infers TypeScript return type from Valibot return schema */
|
|
31
|
+
type InferReturnType<S extends RpcReturnSchema | undefined> = S extends RpcReturnSchema ? InferInput<S> : void;
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/types.d.ts
|
|
34
|
+
type Thenable<T> = T | Promise<T>;
|
|
35
|
+
type EntriesToObject<T extends readonly [string, any][]> = { [K in T[number] as K[0]]: K[1] };
|
|
36
|
+
/**
|
|
37
|
+
* Type of the RPC function,
|
|
38
|
+
* - static: A function that returns a static data, no arguments (can be cached and dumped)
|
|
39
|
+
* - action: A function that performs an action (no data returned)
|
|
40
|
+
* - event: A function that emits an event (no data returned), and does not wait for a response
|
|
41
|
+
* - query: A function that queries a resource
|
|
42
|
+
*
|
|
43
|
+
* By default, the function is a query function.
|
|
44
|
+
*/
|
|
45
|
+
type RpcFunctionType = 'static' | 'action' | 'event' | 'query';
|
|
46
|
+
/**
|
|
47
|
+
* Manages dynamic function registration and provides a type-safe proxy for accessing functions.
|
|
48
|
+
*/
|
|
49
|
+
interface RpcFunctionsCollector<LocalFunctions, SetupContext = undefined> {
|
|
50
|
+
/** User-provided context passed to setup functions */
|
|
51
|
+
context: SetupContext;
|
|
52
|
+
/** Type-safe proxy for calling registered functions */
|
|
53
|
+
readonly functions: LocalFunctions;
|
|
54
|
+
/** Map of registered function definitions keyed by function name */
|
|
55
|
+
readonly definitions: Map<string, RpcFunctionDefinitionAnyWithContext<SetupContext>>;
|
|
56
|
+
/** Register a new function definition */
|
|
57
|
+
register: (fn: RpcFunctionDefinitionAnyWithContext<SetupContext>) => void;
|
|
58
|
+
/** Update an existing function definition */
|
|
59
|
+
update: (fn: RpcFunctionDefinitionAnyWithContext<SetupContext>) => void;
|
|
60
|
+
/** Subscribe to function changes, returns unsubscribe function */
|
|
61
|
+
onChanged: (fn: (id?: string) => void) => (() => void);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Result returned by a function's setup method.
|
|
65
|
+
*/
|
|
66
|
+
interface RpcFunctionSetupResult<ARGS extends any[], RETURN = void> {
|
|
67
|
+
/** Function handler */
|
|
68
|
+
handler?: (...args: ARGS) => RETURN;
|
|
69
|
+
/** Optional dump definition (overrides definition-level dump) */
|
|
70
|
+
dump?: RpcDumpDefinition<ARGS, RETURN>;
|
|
71
|
+
}
|
|
72
|
+
/** Valibot schema array for validating function arguments */
|
|
73
|
+
type RpcArgsSchema = readonly GenericSchema[];
|
|
74
|
+
/** Valibot schema for validating function return value */
|
|
75
|
+
type RpcReturnSchema = GenericSchema;
|
|
76
|
+
/**
|
|
77
|
+
* Single record in a dump store with pre-computed results.
|
|
78
|
+
*/
|
|
79
|
+
interface RpcDumpRecord<ARGS extends any[] = any[], RETURN = any> {
|
|
80
|
+
/** Function arguments */
|
|
81
|
+
inputs: ARGS;
|
|
82
|
+
/** Result (value or lazy function) */
|
|
83
|
+
output?: RETURN;
|
|
84
|
+
/** Error if execution failed */
|
|
85
|
+
error?: {
|
|
86
|
+
/** Error message */message: string; /** Error type name (e.g., "Error", "TypeError") */
|
|
87
|
+
name: string;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Defines argument combinations to pre-compute for a function.
|
|
92
|
+
*/
|
|
93
|
+
interface RpcDumpDefinition<ARGS extends any[] = any[], RETURN = any> {
|
|
94
|
+
/** Argument combinations to pre-compute by executing handler */
|
|
95
|
+
inputs?: ARGS[];
|
|
96
|
+
/** Pre-computed records to use directly (bypasses handler execution) */
|
|
97
|
+
records?: RpcDumpRecord<ARGS, RETURN>[];
|
|
98
|
+
/** Fallback value when no match found */
|
|
99
|
+
fallback?: RETURN;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Dynamically generates dump definitions based on context.
|
|
103
|
+
*/
|
|
104
|
+
type RpcDumpGetter<ARGS extends any[] = any[], RETURN = any, CONTEXT = any> = (context: CONTEXT, handler: (...args: ARGS) => RETURN) => Thenable<RpcDumpDefinition<ARGS, RETURN>>;
|
|
105
|
+
/**
|
|
106
|
+
* Dump configuration (static object or dynamic function).
|
|
107
|
+
*/
|
|
108
|
+
type RpcDump<ARGS extends any[] = any[], RETURN = any, CONTEXT = any> = RpcDumpDefinition<ARGS, RETURN> | RpcDumpGetter<ARGS, RETURN, CONTEXT>;
|
|
109
|
+
/**
|
|
110
|
+
* Base function definition metadata.
|
|
111
|
+
*/
|
|
112
|
+
interface RpcFunctionDefinitionBase {
|
|
113
|
+
/** Function name (unique identifier) */
|
|
114
|
+
name: string;
|
|
115
|
+
/** Function type (static, action, event, or query) */
|
|
116
|
+
type?: RpcFunctionType;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Dump store containing pre-computed results.
|
|
120
|
+
* Flat structure for serialization and efficient lookups.
|
|
121
|
+
*/
|
|
122
|
+
interface RpcDumpStore<T = any> {
|
|
123
|
+
/** Function definitions keyed by name */
|
|
124
|
+
definitions: Record<string, RpcFunctionDefinitionBase>;
|
|
125
|
+
/** Records keyed by '<function-name>---<hash>' or '<function-name>---fallback' */
|
|
126
|
+
records: Record<string, RpcDumpRecord | (() => Promise<RpcDumpRecord>)>;
|
|
127
|
+
/** @internal */
|
|
128
|
+
_functions?: T;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Dump client options.
|
|
132
|
+
*/
|
|
133
|
+
interface RpcDumpClientOptions {
|
|
134
|
+
/** Called when arguments don't match any pre-computed entry */
|
|
135
|
+
onMiss?: (functionName: string, args: any[]) => void;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Options for collecting dumps.
|
|
139
|
+
*/
|
|
140
|
+
interface RpcDumpCollectionOptions {
|
|
141
|
+
/**
|
|
142
|
+
* Concurrency control for parallel execution.
|
|
143
|
+
* - `false` or `undefined`: sequential execution (default)
|
|
144
|
+
* - `true`: parallel execution with concurrency limit of 5
|
|
145
|
+
* - `number`: parallel execution with specified concurrency limit
|
|
146
|
+
*/
|
|
147
|
+
concurrency?: boolean | number | null;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* RPC function definition with optional dump support.
|
|
151
|
+
*/
|
|
152
|
+
type RpcFunctionDefinition<NAME extends string, TYPE extends RpcFunctionType = 'query', ARGS extends any[] = [], RETURN = void, AS extends RpcArgsSchema | undefined = undefined, RS extends RpcReturnSchema | undefined = undefined, CONTEXT = undefined> = [AS, RS] extends [undefined, undefined] ? {
|
|
153
|
+
/** Function name (unique identifier) */name: NAME; /** Function type (static, action, event, or query) */
|
|
154
|
+
type?: TYPE; /** Whether the function results should be cached */
|
|
155
|
+
cacheable?: boolean; /** Valibot schema array for validating function arguments */
|
|
156
|
+
args?: AS; /** Valibot schema for validating function return value */
|
|
157
|
+
returns?: RS; /** Setup function called with context to initialize handler and dump */
|
|
158
|
+
setup?: (context: CONTEXT) => Thenable<RpcFunctionSetupResult<ARGS, RETURN>>; /** Function implementation (required if setup doesn't provide one) */
|
|
159
|
+
handler?: (...args: ARGS) => RETURN; /** Dump definition (setup dump takes priority) */
|
|
160
|
+
dump?: RpcDump<ARGS, RETURN, CONTEXT>;
|
|
161
|
+
__resolved?: RpcFunctionSetupResult<ARGS, RETURN>;
|
|
162
|
+
__promise?: Thenable<RpcFunctionSetupResult<ARGS, RETURN>>;
|
|
163
|
+
} : {
|
|
164
|
+
/** Function name (unique identifier) */name: NAME; /** Function type (static, action, event, or query) */
|
|
165
|
+
type?: TYPE; /** Whether the function results should be cached */
|
|
166
|
+
cacheable?: boolean; /** Valibot schema array for validating function arguments */
|
|
167
|
+
args: AS; /** Valibot schema for validating function return value */
|
|
168
|
+
returns: RS; /** Setup function called with context to initialize handler and dump */
|
|
169
|
+
setup?: (context: CONTEXT) => Thenable<RpcFunctionSetupResult<InferArgsType<AS>, InferReturnType<RS>>>; /** Function implementation (required if setup doesn't provide one) */
|
|
170
|
+
handler?: (...args: InferArgsType<AS>) => InferReturnType<RS>; /** Dump definition (setup dump takes priority) */
|
|
171
|
+
dump?: RpcDump<InferArgsType<AS>, InferReturnType<RS>, CONTEXT>;
|
|
172
|
+
__resolved?: RpcFunctionSetupResult<InferArgsType<AS>, InferReturnType<RS>>;
|
|
173
|
+
__promise?: Thenable<RpcFunctionSetupResult<InferArgsType<AS>, InferReturnType<RS>>>;
|
|
174
|
+
};
|
|
175
|
+
type RpcFunctionDefinitionToFunction<T extends RpcFunctionDefinitionAny> = T extends {
|
|
176
|
+
args: infer AS;
|
|
177
|
+
returns: infer RS;
|
|
178
|
+
} ? AS extends RpcArgsSchema ? RS extends RpcReturnSchema ? (...args: InferArgsType<AS>) => InferReturnType<RS> : never : never : T extends RpcFunctionDefinition<string, any, infer ARGS, infer RETURN, any, any, any> ? (...args: ARGS) => RETURN : never;
|
|
179
|
+
type RpcFunctionDefinitionAny = RpcFunctionDefinition<string, any, any, any, any, any, any>;
|
|
180
|
+
type RpcFunctionDefinitionAnyWithContext<CONTEXT = undefined> = RpcFunctionDefinition<string, any, any, any, any, any, CONTEXT>;
|
|
181
|
+
type RpcDefinitionsToFunctions<T extends readonly RpcFunctionDefinitionAny[]> = EntriesToObject<{ [K in keyof T]: [T[K]['name'], RpcFunctionDefinitionToFunction<T[K]>] }>;
|
|
182
|
+
type RpcDefinitionsFilter<T extends readonly RpcFunctionDefinitionAny[], Type extends RpcFunctionType> = { [K in keyof T]: T[K] extends {
|
|
183
|
+
type: Type;
|
|
184
|
+
} ? T[K] : never };
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/collector.d.ts
|
|
187
|
+
declare class RpcFunctionsCollectorBase<LocalFunctions extends Record<string, any>, SetupContext> implements RpcFunctionsCollector<LocalFunctions, SetupContext> {
|
|
188
|
+
readonly context: SetupContext;
|
|
189
|
+
readonly definitions: Map<string, RpcFunctionDefinition<string, any, any, any, any, any, SetupContext>>;
|
|
190
|
+
readonly functions: LocalFunctions;
|
|
191
|
+
private readonly _onChanged;
|
|
192
|
+
constructor(context: SetupContext);
|
|
193
|
+
register(fn: RpcFunctionDefinition<string, any, any, any, any, any, SetupContext>, force?: boolean): void;
|
|
194
|
+
update(fn: RpcFunctionDefinition<string, any, any, any, any, any, SetupContext>, force?: boolean): void;
|
|
195
|
+
onChanged(fn: (id?: string) => void): () => void;
|
|
196
|
+
getHandler<T extends keyof LocalFunctions>(name: T): Promise<LocalFunctions[T]>;
|
|
197
|
+
getSchema<T extends keyof LocalFunctions>(name: T): {
|
|
198
|
+
args: RpcArgsSchema | undefined;
|
|
199
|
+
returns: RpcReturnSchema | undefined;
|
|
200
|
+
};
|
|
201
|
+
has(name: string): boolean;
|
|
202
|
+
get(name: string): RpcFunctionDefinition<string, any, any, any, any, any, SetupContext> | undefined;
|
|
203
|
+
list(): string[];
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/define.d.ts
|
|
207
|
+
declare function defineRpcFunction<NAME extends string, TYPE extends RpcFunctionType, ARGS extends any[], RETURN = void, const AS extends RpcArgsSchema | undefined = undefined, const RS extends RpcReturnSchema | undefined = undefined>(definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS>): RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS>;
|
|
208
|
+
declare function createDefineWrapperWithContext<CONTEXT>(): <NAME extends string, TYPE extends RpcFunctionType, ARGS extends any[], RETURN = void, const AS extends RpcArgsSchema | undefined = undefined, const RS extends RpcReturnSchema | undefined = undefined>(definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS, CONTEXT>) => RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, AS, RS, CONTEXT>;
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/dumps.d.ts
|
|
211
|
+
/**
|
|
212
|
+
* Collects pre-computed dumps by executing functions with their defined input combinations.
|
|
213
|
+
* Static functions without dump config automatically get `{ inputs: [[]] }`.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const store = await dumpFunctions([greet], context, { concurrency: 10 })
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function dumpFunctions<T extends readonly RpcFunctionDefinitionAny[]>(definitions: T, context?: any, options?: RpcDumpCollectionOptions): Promise<RpcDumpStore<RpcDefinitionsToFunctions<T>>>;
|
|
221
|
+
/**
|
|
222
|
+
* Creates a client that serves pre-computed results from a dump store.
|
|
223
|
+
* Uses argument hashing to match calls to stored records.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* const client = createClientFromDump(store)
|
|
228
|
+
* await client.greet('Alice')
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
declare function createClientFromDump<T extends Record<string, any>>(store: RpcDumpStore<T>, options?: RpcDumpClientOptions): BirpcReturn<T>;
|
|
232
|
+
/**
|
|
233
|
+
* Filters function definitions to only those with dump definitions.
|
|
234
|
+
* Note: Only checks the definition itself, not setup results.
|
|
235
|
+
*/
|
|
236
|
+
declare function getDefinitionsWithDumps<T extends readonly RpcFunctionDefinitionAny[]>(definitions: T): RpcFunctionDefinitionAny[];
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/handler.d.ts
|
|
239
|
+
declare function getRpcResolvedSetupResult<NAME extends string, TYPE extends RpcFunctionType, ARGS extends any[], RETURN = void, CONTEXT = undefined>(definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, any, any, CONTEXT>, context: CONTEXT): Promise<RpcFunctionSetupResult<ARGS, RETURN>>;
|
|
240
|
+
declare function getRpcHandler<NAME extends string, TYPE extends RpcFunctionType, ARGS extends any[], RETURN = void, CONTEXT = undefined>(definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN, any, any, CONTEXT>, context: CONTEXT): Promise<(...args: ARGS) => RETURN>;
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/validation.d.ts
|
|
243
|
+
/**
|
|
244
|
+
* Validates RPC function definitions.
|
|
245
|
+
* Action and event functions cannot have dumps (side effects should not be cached).
|
|
246
|
+
*
|
|
247
|
+
* @throws {Error} If an action or event function has a dump configuration
|
|
248
|
+
*/
|
|
249
|
+
declare function validateDefinitions(definitions: readonly RpcFunctionDefinitionAny[]): void;
|
|
250
|
+
/**
|
|
251
|
+
* Validates a single RPC function definition.
|
|
252
|
+
*
|
|
253
|
+
* @throws {Error} If an action or event function has a dump configuration
|
|
254
|
+
*/
|
|
255
|
+
declare function validateDefinition(definition: RpcFunctionDefinitionAny): void;
|
|
256
|
+
//#endregion
|
|
257
|
+
export { type BirpcFn, type BirpcReturn, EntriesToObject, RpcArgsSchema, RpcCacheManager, RpcCacheOptions, RpcDefinitionsFilter, RpcDefinitionsToFunctions, RpcDump, RpcDumpClientOptions, RpcDumpCollectionOptions, RpcDumpDefinition, RpcDumpGetter, RpcDumpRecord, RpcDumpStore, RpcFunctionDefinition, RpcFunctionDefinitionAny, RpcFunctionDefinitionAnyWithContext, RpcFunctionDefinitionBase, RpcFunctionDefinitionToFunction, RpcFunctionSetupResult, RpcFunctionType, RpcFunctionsCollector, RpcFunctionsCollectorBase, RpcReturnSchema, Thenable, createClientFromDump, createDefineWrapperWithContext, defineRpcFunction, dumpFunctions, getDefinitionsWithDumps, getRpcHandler, getRpcResolvedSetupResult, validateDefinition, validateDefinitions };
|