|
1 | 1 | # API Reference |
2 | 2 |
|
| 3 | +## Overview |
| 4 | + |
| 5 | +The public API is split into four parts: |
| 6 | + |
| 7 | +- Bridge creation: define the registry, payload, and per-key behavior. |
| 8 | +- Components: create scoped Boundary providers. |
| 9 | +- Hooks: register, read, and navigate APIs inside React components. |
| 10 | +- Methods: read or wait for APIs outside React components. |
| 11 | + |
| 12 | +For most use cases, start with: |
| 13 | + |
| 14 | +1. `createBridge()` |
| 15 | +2. `createBoundary()` |
| 16 | +3. `useRegister()` |
| 17 | +4. `useAPI()` |
| 18 | + |
| 19 | +## Shared Option Types |
| 20 | + |
| 21 | +### `APIOptions` |
| 22 | + |
| 23 | +```ts |
| 24 | +type APIOptions = { |
| 25 | + isMulti?: boolean; |
| 26 | +} |
| 27 | +``` |
| 28 | +
|
| 29 | +- `isMulti`: allows one API key to keep multiple active providers at the same time. |
| 30 | +
|
| 31 | +### `BridgeAPIOptions<APIs>` |
| 32 | +
|
| 33 | +```ts |
| 34 | +type BridgeAPIOptions<APIs> = Partial<Record<keyof APIs, APIOptions>>; |
| 35 | +``` |
| 36 | + |
| 37 | +- Configures per-key bridge behavior. |
| 38 | +- Pass it to the function returned by `createBridge(...)`. |
| 39 | + |
| 40 | +### `BaseOptions<APIs, Options, Payload>` |
| 41 | + |
| 42 | +```ts |
| 43 | +type BaseOptions<APIs, Options, Payload> = { |
| 44 | + contextValue?: BoundaryContextValue<APIs, Payload, Options>; |
| 45 | +} |
| 46 | +``` |
| 47 | +
|
| 48 | +- `contextValue`: overrides the current boundary scope and explicitly targets another boundary context. |
| 49 | +
|
| 50 | +### `GetAPIOptions<APIs, Name, Options, Payload>` |
| 51 | +
|
| 52 | +```ts |
| 53 | +type GetAPIOptions<APIs, Name, Options, Payload> = |
| 54 | + BaseOptions<APIs, Options, Payload> & { |
| 55 | + onInit?: ResolveInit<APIs, Options, Name>; |
| 56 | + }; |
| 57 | +``` |
| 58 | + |
| 59 | +- `contextValue`: read from a specific boundary scope. |
| 60 | +- `onInit`: runs when the resolved API becomes available. |
| 61 | + |
| 62 | +### `UpperOptions<APIs, Options, Payload>` |
| 63 | + |
| 64 | +```ts |
| 65 | +type UpperOptions<APIs, Options, Payload> = |
| 66 | + BaseOptions<APIs, Options, Payload> & { |
| 67 | + shouldForwardYield?: (boundaryDetail) => any; |
| 68 | + }; |
| 69 | +``` |
| 70 | + |
| 71 | +- `contextValue`: sets the starting boundary for upward lookup. |
| 72 | +- `shouldForwardYield`: controls which upper boundary should be selected. |
| 73 | + It receives: |
| 74 | + - `payload`: the current candidate boundary payload. |
| 75 | + - `parent`: the next parent boundary. |
| 76 | + - `allAPI`: all APIs currently registered on that candidate boundary. |
| 77 | + |
| 78 | +### `GetUpperAPIOptions<APIs, Name, Options, Payload>` |
| 79 | + |
| 80 | +- Combines `GetAPIOptions` and `UpperOptions`. |
| 81 | +- Supports `contextValue`, `onInit`, and `shouldForwardYield`. |
| 82 | + |
| 83 | +### `GetAPIAsyncOptions<APIs, Options, Payload>` |
| 84 | + |
| 85 | +```ts |
| 86 | +type GetAPIAsyncOptions<APIs, Options, Payload> = |
| 87 | + BaseOptions<APIs, Options, Payload> & { |
| 88 | + initial?: boolean; |
| 89 | + }; |
| 90 | +``` |
| 91 | + |
| 92 | +- `contextValue`: waits in a specific boundary scope. |
| 93 | +- `initial`: defaults to `true`. Reuses the initial waiting slot for that scope. Set it to `false` to wait for a later registration instead. |
| 94 | + |
3 | 95 | ## Bridge Creation |
4 | 96 |
|
5 | | -### `createBridge<APIs, PayloadType>(globalPayload?, options?)` |
| 97 | +### `createBridge<APIs, PayloadType>(globalPayload?)(options?)` |
6 | 98 |
|
7 | 99 | Creates a bridge instance. |
8 | 100 |
|
9 | | -Use it to define: |
| 101 | +Call pattern: |
| 102 | + |
| 103 | +- `createBridge<APIs, PayloadType>(globalPayload?)`: creates the bridge and optionally defines the global payload. |
| 104 | +- `(...)(options?)`: optionally configures per-key bridge behavior such as `isMulti: true`. |
| 105 | + |
| 106 | +First call parameters: |
| 107 | + |
| 108 | +- `globalPayload`: optional payload stored on the global scope when no Boundary is used. |
| 109 | + |
| 110 | +Second call parameters: |
| 111 | + |
| 112 | +- `options`: optional `BridgeAPIOptions<APIs>` for per-key behavior such as `isMulti: true`. |
| 113 | + |
| 114 | +Example: |
| 115 | + |
| 116 | +```ts |
| 117 | +const bridge = createBridge<{ |
| 118 | + notification: { |
| 119 | + id: string; |
| 120 | + show: (message: string) => void; |
| 121 | + }; |
| 122 | +}>()({ |
| 123 | + notification: { isMulti: true } |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +Returns: |
| 128 | + |
| 129 | +- A bridge object used by all hooks, methods, and Boundaries created from it. |
| 130 | + |
| 131 | +## Components |
| 132 | + |
| 133 | +### `createBoundary(bridge)` |
| 134 | + |
| 135 | +Creates a boundary component factory for scoped API access. |
| 136 | + |
| 137 | +Parameters: |
| 138 | + |
| 139 | +- `bridge`: the bridge created by `createBridge(...)`. |
| 140 | + |
| 141 | +Boundary props: |
| 142 | + |
| 143 | +- `payload`: the payload attached to this boundary scope. |
| 144 | +- `contextValue`: an existing boundary context value created by `useBoundaryContext(...)`. |
10 | 145 |
|
11 | | -- API names |
12 | | -- API shapes |
13 | | -- optional global payload |
14 | | -- optional API options such as multi-instance behavior |
| 146 | +Boundary ref value: |
| 147 | + |
| 148 | +- `payload`: the payload on this boundary. |
| 149 | +- `parent`: the parent boundary context, if any. |
| 150 | +- `getAPI(name)`: gets an API from this boundary scope. |
15 | 151 |
|
16 | 152 | ## Hooks |
17 | 153 |
|
18 | | -### `useRegister(bridge, name, factory, deps, options?)` |
| 154 | +### `useRegister(bridge, name, factory, deps?, options?)` |
19 | 155 |
|
20 | 156 | Registers a component API under `name`. |
21 | 157 |
|
| 158 | +Options type: |
| 159 | + |
| 160 | +- `BaseOptions` |
| 161 | + |
| 162 | +Parameters: |
| 163 | + |
| 164 | +- `bridge`: the target bridge. |
| 165 | +- `name`: the API key to register. |
| 166 | +- `factory`: returns the imperative API object or function exposed by this component. |
| 167 | +- `deps`: dependency list passed to `useImperativeHandle`. Rebuilds the API when these values change. |
| 168 | +- `options`: optional `BaseOptions`. |
| 169 | + - `contextValue`: registers into a specific boundary scope. |
| 170 | + |
22 | 171 | ### `useAPI(bridge, name, options?)` |
23 | 172 |
|
24 | 173 | Reads the API in the current boundary scope. |
25 | 174 |
|
| 175 | +Options type: |
| 176 | + |
| 177 | +- `GetAPIOptions` |
| 178 | + |
| 179 | +Parameters: |
| 180 | + |
| 181 | +- `bridge`: the target bridge. |
| 182 | +- `name`: the API key to read. |
| 183 | +- `options`: optional `GetAPIOptions`. |
| 184 | + - `contextValue`: reads from a specific boundary scope. |
| 185 | + - `onInit`: runs once the API becomes available. |
| 186 | + |
26 | 187 | ### `useUpperAPI(bridge, name, options?)` |
27 | 188 |
|
28 | 189 | Reads a matching API from an upper boundary. |
29 | 190 |
|
| 191 | +Options type: |
| 192 | + |
| 193 | +- `GetUpperAPIOptions` |
| 194 | + |
| 195 | +Parameters: |
| 196 | + |
| 197 | +- `bridge`: the target bridge. |
| 198 | +- `name`: the API key to read. |
| 199 | +- `options`: optional `GetUpperAPIOptions`. |
| 200 | + - `contextValue`: chooses the starting boundary. |
| 201 | + - `onInit`: runs once the resolved upper API becomes available. |
| 202 | + - `shouldForwardYield`: controls how far the upward lookup continues. |
| 203 | + |
30 | 204 | ### `useBoundaryPayload(bridge, options?)` |
31 | 205 |
|
32 | 206 | Reads the payload attached to the current boundary. |
33 | 207 |
|
| 208 | +Options type: |
| 209 | + |
| 210 | +- `BaseOptions` |
| 211 | + |
| 212 | +Parameters: |
| 213 | + |
| 214 | +- `bridge`: the target bridge. |
| 215 | +- `options`: optional `BaseOptions`. |
| 216 | + - `contextValue`: reads payload from a specific boundary context. |
| 217 | + |
34 | 218 | ### `useUpperBoundaryPayload(bridge, options?)` |
35 | 219 |
|
36 | 220 | Reads payload from an upper boundary. |
37 | 221 |
|
| 222 | +Options type: |
| 223 | + |
| 224 | +- `UpperOptions` |
| 225 | + |
| 226 | +Parameters: |
| 227 | + |
| 228 | +- `bridge`: the target bridge. |
| 229 | +- `options`: optional `UpperOptions`. |
| 230 | + - `contextValue`: chooses the starting boundary. |
| 231 | + - `shouldForwardYield`: controls which upper boundary is selected. |
| 232 | + |
38 | 233 | ### `useBoundaryContext(bridge, payload?)` |
39 | 234 |
|
40 | 235 | Creates a reusable boundary context value. |
41 | 236 |
|
| 237 | +Parameters: |
| 238 | + |
| 239 | +- `bridge`: the target bridge. |
| 240 | +- `payload`: the payload for the new boundary context value. |
| 241 | + |
| 242 | +Returns: |
| 243 | + |
| 244 | +- A `BoundaryContextValue` object that can be passed into `<Boundary contextValue={...} />` or any API that accepts `contextValue`. |
| 245 | + |
| 246 | +### `useBoundaryRef(bridge)` |
| 247 | + |
| 248 | +Creates a ref for a Boundary component. |
| 249 | + |
| 250 | +Parameters: |
| 251 | + |
| 252 | +- `bridge`: the target bridge. |
| 253 | + |
| 254 | +Returns: |
| 255 | + |
| 256 | +- A React ref whose `.current` exposes `payload`, `parent`, and `getAPI(name)`. |
| 257 | + |
42 | 258 | ### `useTools(bridge, options?)` |
43 | 259 |
|
44 | 260 | Returns helper methods for programmatic access inside components. |
45 | 261 |
|
| 262 | +Options type: |
| 263 | + |
| 264 | +- `BaseOptions` |
| 265 | + |
| 266 | +Parameters: |
| 267 | + |
| 268 | +- `bridge`: the target bridge. |
| 269 | +- `options`: optional `BaseOptions`. |
| 270 | + - `contextValue`: sets the default boundary scope for all returned helpers. |
| 271 | + |
| 272 | +Returned helpers: |
| 273 | + |
| 274 | +- `getAPI(name, options?)`: reads an API like `getBridgeAPI`, but defaults to the current component scope. |
| 275 | +- `getBoundaryPayload(options?)`: reads boundary payload. |
| 276 | +- `getUpperAPI(name, options?)`: reads an API from an upper boundary. |
| 277 | +- `getUpperBoundaryPayload(options?)`: reads payload from an upper boundary. |
| 278 | +- `getAPIAsync(name, options?)`: waits for an API like `getBridgeAPIAsync`, but defaults to the current component scope. |
| 279 | + |
46 | 280 | ## Methods |
47 | 281 |
|
48 | 282 | ### `getBridgeAPI(bridge, name, options?)` |
49 | 283 |
|
50 | 284 | Gets an API outside components. By default, it reads from the global scope. |
51 | 285 |
|
| 286 | +Options type: |
| 287 | + |
| 288 | +- `BaseOptions` |
| 289 | + |
| 290 | +Parameters: |
| 291 | + |
| 292 | +- `bridge`: the target bridge. |
| 293 | +- `name`: the API key to read. |
| 294 | +- `options`: optional `BaseOptions`. |
| 295 | + - `contextValue`: reads from a specific boundary scope instead of the global scope. |
| 296 | + |
52 | 297 | ### `getBridgeAPIAsync(bridge, name, options?)` |
53 | 298 |
|
54 | 299 | Waits for an API to be registered and returns it as a promise. |
55 | 300 |
|
56 | | -## Components |
| 301 | +Options type: |
57 | 302 |
|
58 | | -### `createBoundary(bridge)` |
| 303 | +- `GetAPIAsyncOptions` |
59 | 304 |
|
60 | | -Creates a boundary component factory for scoped API access. |
| 305 | +Parameters: |
| 306 | + |
| 307 | +- `bridge`: the target bridge. |
| 308 | +- `name`: the API key to wait for. |
| 309 | +- `options`: optional `GetAPIAsyncOptions`. |
| 310 | + - `contextValue`: waits in a specific boundary scope. |
| 311 | + - `initial`: reuses the initial waiter by default, or waits for a later registration when set to `false`. |
61 | 312 |
|
62 | 313 | ## Notes |
63 | 314 |
|
64 | 315 | - Most consumers should start with `createBridge`, `createBoundary`, `useRegister`, and `useAPI` |
65 | 316 | - Reach for `useUpperAPI` and `getBridgeAPIAsync` when you need more advanced coordination |
66 | 317 | - Use `isMulti: true` when one API key needs many active providers |
| 318 | +- Add Boundary only when you need local scope isolation; otherwise APIs resolve from the shared global scope |
0 commit comments