|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | /**
|
10 |
| - * API dispatcher |
| 10 | + * API dispatcher that maps method names to functions. |
| 11 | + * |
| 12 | + * Each method must accept `unknown[]` arguments and return `unknown`. |
| 13 | + * Use type validation libraries like [jsr:@core/unknownutil] to properly |
| 14 | + * validate and narrow argument types at runtime. |
| 15 | + * |
| 16 | + * [jsr:@core/unknownutil]: https://jsr.io/@core/unknownutil |
11 | 17 | */
|
12 | 18 | export type Dispatcher = {
|
13 | 19 | [key: string]: (...args: unknown[]) => unknown;
|
@@ -70,6 +76,34 @@ export interface Denops {
|
70 | 76 |
|
71 | 77 | /**
|
72 | 78 | * User-defined API name and method map used to dispatch API requests.
|
| 79 | + * |
| 80 | + * Each method must accept `unknown[]` arguments and return `unknown` values. |
| 81 | + * Use type validation libraries like [jsr:@core/unknownutil] to validate |
| 82 | + * argument types at runtime. These methods can be called from other plugins |
| 83 | + * using `denops.dispatch()`. |
| 84 | + * |
| 85 | + * [jsr:@core/unknownutil]: https://jsr.io/@core/unknownutil |
| 86 | + * |
| 87 | + * @example |
| 88 | + * ```ts |
| 89 | + * import { is, assert } from "jsr:@core/unknownutil"; |
| 90 | + * |
| 91 | + * denops.dispatcher = { |
| 92 | + * "hello": (name: unknown) => { |
| 93 | + * assert(name, is.String); |
| 94 | + * return `Hello, ${name}!`; |
| 95 | + * }, |
| 96 | + * "add": (a: unknown, b: unknown) => { |
| 97 | + * assert(a, is.Number); |
| 98 | + * assert(b, is.Number); |
| 99 | + * return a + b; |
| 100 | + * }, |
| 101 | + * "fetchData": async (id: unknown) => { |
| 102 | + * assert(id, is.String); |
| 103 | + * return await fetch(`/api/data/${id}`); |
| 104 | + * }, |
| 105 | + * }; |
| 106 | + * ``` |
73 | 107 | */
|
74 | 108 | dispatcher: Dispatcher;
|
75 | 109 |
|
@@ -126,9 +160,28 @@ export interface Denops {
|
126 | 160 | /**
|
127 | 161 | * Dispatch an arbitrary function of an arbitrary plugin and return the result.
|
128 | 162 | *
|
129 |
| - * @param name: A plugin registration name. |
130 |
| - * @param fn: A function name in the API registration. |
131 |
| - * @param args: Arguments of the function. |
| 163 | + * @param name A plugin registration name. |
| 164 | + * @param fn A function name in the dispatcher of the target plugin. |
| 165 | + * @param args Arguments to pass to the function. Arguments are passed as `unknown` types. |
| 166 | + * @returns A Promise that resolves to the return value of the dispatched function. |
| 167 | + * The return type is `unknown` and should be validated if needed. |
| 168 | + * |
| 169 | + * @example |
| 170 | + * ```ts |
| 171 | + * import { is } from "jsr:@core/unknownutil"; |
| 172 | + * |
| 173 | + * // Call method and validate return type |
| 174 | + * const result = await denops.dispatch("myPlugin", "hello", "world"); |
| 175 | + * if (is.String(result)) { |
| 176 | + * console.log(`Greeting: ${result}`); |
| 177 | + * } |
| 178 | + * |
| 179 | + * // Call method with multiple arguments |
| 180 | + * const sum = await denops.dispatch("calculator", "add", 5, 3); |
| 181 | + * if (is.Number(sum)) { |
| 182 | + * console.log(`Sum: ${sum}`); |
| 183 | + * } |
| 184 | + * ``` |
132 | 185 | */
|
133 | 186 | dispatch(name: string, fn: string, ...args: unknown[]): Promise<unknown>;
|
134 | 187 | }
|
|
0 commit comments