-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprotocol.ts
More file actions
210 lines (189 loc) · 6.17 KB
/
protocol.ts
File metadata and controls
210 lines (189 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Type-safe IPC protocol for VS Code webview communication.
*
* Inspired by tRPC's approach: types are carried in a phantom `_types` property
* that exists only for TypeScript inference, not at runtime.
*/
// --- Message definitions ---
/** Request definition: params P, response R */
export interface RequestDef<P = void, R = void> {
readonly kind: "request";
readonly method: string;
/** @internal Phantom types for inference - not present at runtime */
readonly _types?: { params: P; response: R };
}
/** Command definition: params P, no response */
export interface CommandDef<P = void> {
readonly kind: "command";
readonly method: string;
/** @internal Phantom type for inference - not present at runtime */
readonly _types?: { params: P };
}
/** Notification definition: data D (extension to webview) */
export interface NotificationDef<D = void> {
readonly kind: "notification";
readonly method: string;
/** @internal Phantom type for inference - not present at runtime */
readonly _types?: { data: D };
}
// --- Factory functions ---
/** Define a request with typed params and response */
export function defineRequest<P = void, R = void>(
method: string,
): RequestDef<P, R> {
return { kind: "request", method } as RequestDef<P, R>;
}
/** Define a fire-and-forget command */
export function defineCommand<P = void>(method: string): CommandDef<P> {
return { kind: "command", method } as CommandDef<P>;
}
/** Define a push notification (extension to webview) */
export function defineNotification<D = void>(
method: string,
): NotificationDef<D> {
return { kind: "notification", method } as NotificationDef<D>;
}
// --- Wire format ---
/** Request from webview to extension */
export interface IpcRequest<P = unknown> {
readonly requestId: string;
readonly method: string;
readonly params?: P;
}
/** Response from extension to webview */
export interface IpcResponse<T = unknown> {
readonly requestId: string;
readonly method: string;
readonly success: boolean;
readonly data?: T;
readonly error?: string;
}
/** Push notification from extension to webview */
export interface IpcNotification<D = unknown> {
readonly type: string;
readonly data?: D;
}
// --- Mapped types for handler completeness ---
/** Requires a handler for every RequestDef in Api. Compile error if one is missing. */
export type RequestHandlerMap<Api> = {
[K in keyof Api as Api[K] extends { kind: "request" }
? K
: never]: Api[K] extends RequestDef<infer P, infer R>
? (params: P) => Promise<R>
: never;
};
/** Requires a handler for every CommandDef in Api. Compile error if one is missing. */
export type CommandHandlerMap<Api> = {
[K in keyof Api as Api[K] extends { kind: "command" }
? K
: never]: Api[K] extends CommandDef<infer P>
? (params: P) => void | Promise<void>
: never;
};
// --- API hook type ---
/** Derives a fully typed hook interface from an API definition object. */
export type ApiHook<Api> = {
[K in keyof Api as Api[K] extends { kind: "request" }
? K
: never]: Api[K] extends RequestDef<infer P, infer R>
? (...args: P extends void ? [] : [params: P]) => Promise<R>
: never;
} & {
[K in keyof Api as Api[K] extends { kind: "command" }
? K
: never]: Api[K] extends CommandDef<infer P>
? (...args: P extends void ? [] : [params: P]) => void
: never;
} & {
[K in keyof Api as Api[K] extends { kind: "notification" }
? `on${Capitalize<K & string>}`
: never]: Api[K] extends NotificationDef<infer D>
? D extends void
? (cb: () => void) => () => void
: (cb: (data: D) => void) => () => void
: never;
};
// --- Builder functions ---
/** Build a method-indexed map of request handlers with compile-time completeness. */
export function buildRequestHandlers<
Api extends Record<string, { method: string }>,
>(
api: Api,
handlers: RequestHandlerMap<Api>,
): Record<string, (params: unknown) => Promise<unknown>>;
export function buildRequestHandlers(
api: Record<string, { method: string }>,
handlers: Record<string, (params: unknown) => Promise<unknown>>,
) {
const result: Record<string, (params: unknown) => Promise<unknown>> = {};
for (const key of Object.keys(handlers)) {
result[api[key].method] = handlers[key];
}
return result;
}
/** Build a method-indexed map of command handlers with compile-time completeness. */
export function buildCommandHandlers<
Api extends Record<string, { method: string }>,
>(
api: Api,
handlers: CommandHandlerMap<Api>,
): Record<string, (params: unknown) => void | Promise<void>>;
export function buildCommandHandlers(
api: Record<string, { method: string }>,
handlers: Record<string, (params: unknown) => void | Promise<void>>,
) {
const result: Record<string, (params: unknown) => void | Promise<void>> = {};
for (const key of Object.keys(handlers)) {
result[api[key].method] = handlers[key];
}
return result;
}
/** Build a typed API hook from an API definition and IPC primitives. */
export function buildApiHook<
Api extends Record<string, { kind: string; method: string }>,
>(
api: Api,
ipc: {
request: <P, R>(
def: { method: string; _types?: { params: P; response: R } },
...args: P extends void ? [] : [params: P]
) => Promise<R>;
command: <P>(
def: { method: string; _types?: { params: P } },
...args: P extends void ? [] : [params: P]
) => void;
onNotification: <D>(
def: { method: string; _types?: { data: D } },
cb: (data: D) => void,
) => () => void;
},
): ApiHook<Api>;
export function buildApiHook(
api: Record<string, { kind: string; method: string }>,
ipc: {
request: (def: { method: string }, params?: unknown) => Promise<unknown>;
command: (def: { method: string }, params?: unknown) => void;
onNotification: (
def: { method: string },
cb: (data: unknown) => void,
) => () => void;
},
) {
const result: Record<string, unknown> = {};
for (const [key, def] of Object.entries(api)) {
switch (def.kind) {
case "request":
result[key] = (params: unknown) => ipc.request(def, params);
break;
case "command":
result[key] = (params: unknown) => ipc.command(def, params);
break;
case "notification":
result[`on${key[0].toUpperCase()}${key.slice(1)}`] = (
cb: (data: unknown) => void,
) => ipc.onNotification(def, cb);
break;
}
}
return result;
}