@camstack/sdk 0.1.35 → 0.1.37

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/dist/nvr.d.ts CHANGED
@@ -4,20 +4,8 @@
4
4
  * These are the canonical types from the proxy's providers/types.ts,
5
5
  * now owned by the SDK so both app and proxy can share them.
6
6
  */
7
- export interface StreamOption {
8
- /** Unique identifier for this option (e.g. "auto", "cam_main", "mjpeg"). */
9
- id: string;
10
- /** Display label (e.g. "Auto", "High (WebRTC)", "MJPEG"). */
11
- label: string;
12
- /** Streaming technology. */
13
- method: "webrtc" | "mjpeg" | "hls" | "rtsp";
14
- /** go2rtc stream name (required for WebRTC). */
15
- streamName?: string;
16
- /** Raw source URL (RTSP, etc.) — for display/copy in admin UI. */
17
- sourceUrl?: string;
18
- /** Group label for UI separators (e.g. "go2rtc", "Frigate", "Scrypted"). */
19
- group?: string;
20
- }
7
+ import type { StreamSourceEntry } from '@camstack/types';
8
+ export type { StreamSourceEntry };
21
9
  export interface NvrCamera {
22
10
  name: string;
23
11
  /** Display label (may differ from internal name). */
@@ -27,8 +15,8 @@ export interface NvrCamera {
27
15
  hasPtz: boolean;
28
16
  /** Available stream names for this camera (e.g. ["cam_main", "cam_sub"]). */
29
17
  streams: string[];
30
- /** Available streaming options with method and quality info. Always includes "Auto". */
31
- streamOptions: StreamOption[];
18
+ /** Available streaming options. */
19
+ streamOptions: StreamSourceEntry[];
32
20
  /** Accessories/actions available on this camera (siren, floodlight, PTZ, etc.). */
33
21
  accessories?: CameraAccessory[];
34
22
  }
@@ -282,4 +270,3 @@ export interface ProviderConfigs {
282
270
  }>;
283
271
  enabledDetectionSources?: string[];
284
272
  }
285
- export {};
@@ -0,0 +1,297 @@
1
+ /**
2
+ * System — single, unified entry point for the camstack client API.
3
+ *
4
+ * Devices are returned as typed `DeviceProxy` objects (auto-injecting
5
+ * `deviceId` + `nodeId`), system caps are exposed as typed namespaces
6
+ * (`system.userManagement`, `system.storage`, etc.), and live events
7
+ * flow through a single `subscribeEvent` helper.
8
+ *
9
+ * The escape hatches (`trpcClient`, `wsClient`) are still public so the
10
+ * ui-library Provider can seed React Query with the same WS connection
11
+ * — a future phase will narrow the surface further.
12
+ */
13
+ import { createWSClient, type TRPCClient } from '@trpc/client';
14
+ import type { DeviceProxy, DeviceQueryFilters, DeviceLifecycleListener, SystemProxy } from '@camstack/types';
15
+ import type { BackendAppRouter } from './backend-router.js';
16
+ import type { BackendConnectionState } from './types.js';
17
+ /**
18
+ * Canonical user-info shape returned by `getMe()`. Mirrors the server's
19
+ * `auth.me` output — kept narrow so SDK consumers don't need the full
20
+ * tRPC inference chain to type a login flow.
21
+ */
22
+ export interface UserInfo {
23
+ readonly id: string;
24
+ readonly username: string;
25
+ readonly role: string;
26
+ readonly permissions?: {
27
+ readonly allowedAddons?: ReadonlyArray<string> | '*';
28
+ readonly allowedDevices?: Readonly<Record<string, ReadonlyArray<string> | '*'>>;
29
+ };
30
+ }
31
+ export interface SystemConfig {
32
+ /** Backend server URL (e.g. "http://localhost:4443"). */
33
+ readonly serverUrl: string;
34
+ /** JWT token for authentication. */
35
+ readonly token?: string;
36
+ /** Use WebSocket transport (default: true in browser, false in Node). */
37
+ readonly useWebSocket?: boolean;
38
+ /** Optional per-event connection-state callback. */
39
+ readonly onConnectionChange?: (state: BackendConnectionState) => void;
40
+ /** Initial WS reconnect delay in ms (default 2000, capped via maxRetryDelayMs). */
41
+ readonly retryDelayMs?: number;
42
+ /** Max WS reconnect delay in ms (default 30_000). */
43
+ readonly maxRetryDelayMs?: number;
44
+ }
45
+ type ConnectionListener = (state: BackendConnectionState, version: number) => void;
46
+ export interface SystemLiveEvent<TData = unknown> {
47
+ readonly id: string;
48
+ readonly timestamp: Date | string;
49
+ readonly source: {
50
+ readonly type: string;
51
+ readonly id: string | number;
52
+ };
53
+ readonly category: string;
54
+ readonly data: TData;
55
+ }
56
+ export type SystemLiveEventListener<TData = unknown> = (event: SystemLiveEvent<TData>) => void;
57
+ type TrpcClient = TRPCClient<BackendAppRouter>;
58
+ type TrpcWsClient = ReturnType<typeof createWSClient>;
59
+ export declare class System {
60
+ /** Active server base URL. Mutable via `switchServerUrl()` so the
61
+ * endpoint-race helper can pivot the transport onto a LAN candidate
62
+ * after the initial public-URL bootstrap. */
63
+ private _serverUrl;
64
+ private readonly useWs;
65
+ private readonly baseRetryMs;
66
+ private readonly maxRetryMs;
67
+ private readonly onConnectionChange;
68
+ private token;
69
+ private _trpcClient;
70
+ private _wsClient;
71
+ private mirror;
72
+ private mirrorInit;
73
+ private connected;
74
+ private connectedPromise;
75
+ private _systemProxy;
76
+ private _connectionVersion;
77
+ private readonly connectionListeners;
78
+ constructor(config: SystemConfig);
79
+ /** Active server base URL (no trailing slash). */
80
+ get serverUrl(): string;
81
+ get connectionVersion(): number;
82
+ /**
83
+ * Subscribe to connection-state transitions. Called with `('connected'
84
+ * | 'disconnected' | 'connecting', version)` whenever the SDK opens,
85
+ * closes, or starts a manual reconnect. Listener errors are swallowed
86
+ * so a misbehaving consumer cannot break the SDK's event loop.
87
+ */
88
+ subscribeConnectionEvents(cb: ConnectionListener): () => void;
89
+ private emitConnectionEvent;
90
+ /**
91
+ * Wait until the underlying tRPC transport is connected AND the
92
+ * server has responded to a cheap auth round-trip (`auth.me`). This
93
+ * is the canonical "ready to issue queries" gate.
94
+ *
95
+ * Why a probe, not just `ws.readyState === OPEN`?
96
+ * The WS handshake completes asynchronously: tRPC's `wsLink`
97
+ * queues outgoing messages and only flushes them after `open()`
98
+ * resolves (post `connectionParams` send). On the server, the
99
+ * tRPC context is created lazily once the connectionParams
100
+ * message is received. A query fired between WS-open and
101
+ * connection-params-processed is technically queued by tRPC, but
102
+ * the auth context for that query is only resolved once the
103
+ * handshake message is decoded server-side. A probe round-trip is
104
+ * the safest way to confirm both sides have agreed on the auth
105
+ * identity before the React tree starts firing parallel queries
106
+ * (which can otherwise land before any addon-side service
107
+ * discovery has settled, returning empty results that get cached).
108
+ *
109
+ * Idempotent — concurrent callers await the same in-flight Promise.
110
+ * Bounded by `timeoutMs` (default 15s) — beyond which a
111
+ * `Error('System.awaitConnected: probe timed out after Xms')` is
112
+ * thrown so the host can render a clear error state instead of
113
+ * hanging on a bricked socket.
114
+ */
115
+ awaitConnected(timeoutMs?: number): Promise<void>;
116
+ /**
117
+ * Warm-boot the device mirror. Awaits the transport probe first
118
+ * (`awaitConnected`) so the three mirror round-trips
119
+ * (`getAllBindings` + `getAllSnapshots` + `listAll`) cannot race
120
+ * against the WS auth handshake. Subsequent `getDevice(id)` calls
121
+ * are sync; live `device.*` event subscriptions keep the caches
122
+ * fresh.
123
+ *
124
+ * Idempotent — concurrent callers await the same in-flight Promise.
125
+ */
126
+ init(timeoutMs?: number): Promise<void>;
127
+ /** Promise that resolves once `init()` has completed. */
128
+ awaitReady(): Promise<void>;
129
+ /** True after `init()` resolves. */
130
+ isReady(): boolean;
131
+ /** True after the transport probe has succeeded at least once. */
132
+ isConnected(): boolean;
133
+ /**
134
+ * Force a fresh WebSocket handshake. Tears down the wsClient + tRPC
135
+ * client + mirror (the mirror captures the tRPC reference at
136
+ * construction time and would otherwise dispatch through a closed
137
+ * client) and rebuilds them. No-op for HTTP transport.
138
+ */
139
+ reconnect(): void;
140
+ /**
141
+ * Pivot the underlying tRPC transport onto a different base URL.
142
+ * Used by the endpoint-race flow: the SDK opens against the public
143
+ * URL the operator provided, calls `localNetwork.getConnectionEndpoints`
144
+ * to discover LAN candidates, races them, and (when a faster one wins)
145
+ * calls `switchServerUrl(winner)` to migrate every subsequent query
146
+ * onto the LAN path without losing auth state.
147
+ *
148
+ * Keeps the auth token. Tears down the WS + mirror and rebuilds them
149
+ * against the new URL — same machinery as `reconnect()` but with a
150
+ * different target.
151
+ */
152
+ switchServerUrl(nextUrl: string): void;
153
+ /**
154
+ * Race the candidate base URLs reported by the hub's `local-network`
155
+ * cap, pick the fastest one that responds, and (if it's different
156
+ * from the current URL) pivot the transport onto it.
157
+ *
158
+ * Flow:
159
+ * 1. Query `localNetwork.getConnectionEndpoints({ port })` over the
160
+ * already-authenticated tRPC channel — the cap is auth-gated,
161
+ * so the LAN IPs never leak to anonymous callers.
162
+ * 2. For each candidate, fire a HEAD on `{baseUrl}/trpc/health`
163
+ * with a short timeout (default 1500ms). The first 2xx wins.
164
+ * 3. If the winner differs from `this.serverUrl`, call
165
+ * `switchServerUrl(winner)` and return it. Otherwise return
166
+ * the current URL unchanged.
167
+ *
168
+ * Bounded — if every candidate times out we keep the current URL.
169
+ * Idempotent — safe to call on every connect / reconnect / network
170
+ * change event.
171
+ */
172
+ raceConnectionEndpoints(options?: {
173
+ /** Per-candidate probe timeout. Default 1500ms. */
174
+ readonly perCandidateTimeoutMs?: number;
175
+ /** Skip IPv6 candidates. Default `false`. */
176
+ readonly ipv4Only?: boolean;
177
+ }): Promise<{
178
+ winner: string;
179
+ switched: boolean;
180
+ }>;
181
+ /** Tear down WS connection + mirror. The instance is unusable afterwards. */
182
+ close(): void;
183
+ private disposeMirror;
184
+ login(username: string, password: string): Promise<{
185
+ token: string;
186
+ }>;
187
+ logout(): Promise<void>;
188
+ getMe(): Promise<UserInfo>;
189
+ /** Update the auth token (e.g. after login or token refresh). */
190
+ setToken(token: string): void;
191
+ /**
192
+ * Synchronous snapshot of every device matching the optional filters.
193
+ * Backed by the `SystemMirror` warm-boot cache — call `init()` first
194
+ * (or `awaitReady()`) before invoking. Returns an empty array if the
195
+ * mirror has not yet been booted.
196
+ *
197
+ * Each returned proxy has `binding` populated from the mirror's
198
+ * binding cache (Phase 5 dedup), so consumers no longer need to
199
+ * make a separate `deviceManager.getBindings` round-trip.
200
+ */
201
+ listDevices(filters?: DeviceQueryFilters): readonly DeviceProxy[];
202
+ /**
203
+ * Sync lookup by numeric id. `null` if the mirror has not been booted
204
+ * or the device is unknown.
205
+ */
206
+ getDevice(deviceId: number): DeviceProxy | null;
207
+ /** Sync lookup by display name (exact match). */
208
+ getDeviceByName(name: string): DeviceProxy | null;
209
+ /** Sync lookup by stableId. */
210
+ getDeviceByStableId(stableId: string): DeviceProxy | null;
211
+ /**
212
+ * Resolve when a device with `deviceId` becomes available. Resolves
213
+ * immediately if already known; rejects with a timeout error
214
+ * otherwise (default 30s).
215
+ */
216
+ waitForDevice(deviceId: number, timeoutMs?: number): Promise<DeviceProxy>;
217
+ /** Subscribe to `device.registered` events. */
218
+ onDeviceAdded(cb: DeviceLifecycleListener): () => void;
219
+ /** Subscribe to `device.unregistered` events. */
220
+ onDeviceRemoved(cb: DeviceLifecycleListener): () => void;
221
+ /**
222
+ * Patch the proxy's `binding` field from the mirror's cache. The
223
+ * generated `createDeviceProxy()` already sets `binding` to the
224
+ * binding it was constructed with — this is a defensive overwrite
225
+ * that uses the latest cached entry in case a `binding-changed`
226
+ * event landed between proxy creation and access.
227
+ */
228
+ private attachBinding;
229
+ /** Fetch the latest cached binding from the mirror, or `null`. */
230
+ private lookupBinding;
231
+ get addonPages(): SystemProxy['addonPages'];
232
+ get addonSettings(): SystemProxy['addonSettings'];
233
+ get alerts(): SystemProxy['alerts'];
234
+ get audioAnalyzer(): SystemProxy['audioAnalyzer'];
235
+ get audioCodec(): SystemProxy['audioCodec'];
236
+ get backup(): SystemProxy['backup'];
237
+ get decoder(): SystemProxy['decoder'];
238
+ get deviceManager(): SystemProxy['deviceManager'];
239
+ get deviceProvider(): SystemProxy['deviceProvider'];
240
+ get deviceState(): SystemProxy['deviceState'];
241
+ get metricsProvider(): SystemProxy['metricsProvider'];
242
+ get notificationOutput(): SystemProxy['notificationOutput'];
243
+ get pipelineExecutor(): SystemProxy['pipelineExecutor'];
244
+ get pipelineOrchestrator(): SystemProxy['pipelineOrchestrator'];
245
+ get pipelineRunner(): SystemProxy['pipelineRunner'];
246
+ get platformProbe(): SystemProxy['platformProbe'];
247
+ get recordingEngine(): SystemProxy['recordingEngine'];
248
+ get settingsStore(): SystemProxy['settingsStore'];
249
+ get storage(): SystemProxy['storage'];
250
+ get streamBroker(): SystemProxy['streamBroker'];
251
+ get turnProvider(): SystemProxy['turnProvider'];
252
+ get userManagement(): SystemProxy['userManagement'];
253
+ /**
254
+ * Subscribe to a single event category. Returns an unsubscribe
255
+ * handle. Errors thrown by the listener are swallowed so a single
256
+ * misbehaving consumer cannot tear down the WS subscription.
257
+ *
258
+ * Categories should be values from the `EventCategory` enum
259
+ * (`@camstack/types`) — passing a raw string works for forward-compat
260
+ * but loses type safety. The SDK forwards the value verbatim to the
261
+ * server's `live.onEvent` subscription.
262
+ */
263
+ subscribeEvent<TData = unknown>(category: string, cb: SystemLiveEventListener<TData>): () => void;
264
+ /** Direct tRPC client. Read once per call; rebuilt on `reconnect()`. */
265
+ get trpcClient(): TrpcClient;
266
+ /**
267
+ * Underlying WSClient (or `null` for HTTP transport). Used by
268
+ * advanced consumers that need direct access to the WebSocket
269
+ * (e.g. for keep-alive metrics). Rebuilt on `reconnect()`.
270
+ */
271
+ get wsClient(): TrpcWsClient | null;
272
+ private buildTrpcClient;
273
+ }
274
+ /** Create a `System` instance. Convenience factory. */
275
+ export declare function createSystem(config: SystemConfig): System;
276
+ /**
277
+ * Race a list of candidate base URLs and return the first one that
278
+ * responds to `GET {baseUrl}/trpc/health` with a 2xx within
279
+ * `timeoutMs`. Returns `null` if every candidate fails / times out.
280
+ *
281
+ * Implementation notes:
282
+ * - Uses `fetch` with `AbortController` for per-candidate cutoff so a
283
+ * stalled candidate doesn't pin the wallclock for the whole race.
284
+ * - Cancels every still-pending probe as soon as the first one
285
+ * succeeds — no wasted bandwidth on the loser candidates.
286
+ * - `/trpc/health` is the only endpoint guaranteed to respond on every
287
+ * CamStack deployment (registered alongside the tRPC plugin). It is
288
+ * intentionally NOT auth-gated since it's used by load balancers /
289
+ * uptime probes — same surface every reverse proxy already monitors.
290
+ * - HEAD would be nicer but Cloudflare Tunnel + some browsers
291
+ * mishandle HEAD on the ingress path; GET is safer.
292
+ *
293
+ * Exported so non-System callers (CLI helpers, tests) can race their
294
+ * own candidate lists without instantiating a full System.
295
+ */
296
+ export declare function raceFastestEndpoint(candidates: ReadonlyArray<string>, timeoutMs: number): Promise<string | null>;
297
+ export {};
@@ -167,11 +167,3 @@ export interface ReelEventsResult {
167
167
  events: ReelEvent[];
168
168
  total: number;
169
169
  }
170
- /** @deprecated Use DetectionEvent instead. */
171
- export type TimelineDetectionEvent = DetectionEvent;
172
- /** @deprecated Use MotionItem instead. */
173
- export type TimelineMotionItem = MotionItem;
174
- /** @deprecated Use TimelineArtifact instead. */
175
- export type TimelineArtifactItem = TimelineArtifact;
176
- /** @deprecated Use TimelineCluster instead. */
177
- export type TimelineClusterFromServer = TimelineCluster;
package/dist/types.d.ts CHANGED
@@ -32,13 +32,6 @@ export interface DirectClientConfig {
32
32
  username?: string;
33
33
  password?: string;
34
34
  }
35
- export interface BackendClientConfig {
36
- /** Backend server URL (e.g. "http://localhost:4443") */
37
- readonly serverUrl: string;
38
- /** JWT token for authentication */
39
- readonly token?: string;
40
- /** Connection timeout in ms (default: 10000) */
41
- readonly connectTimeoutMs?: number;
42
- }
35
+ export type BackendConnectionState = 'connected' | 'disconnected' | 'connecting';
43
36
  export type CamStackClientConfig = ProxyClientConfig | DirectClientConfig;
44
37
  export declare function isProxyConfig(config: CamStackClientConfig): config is ProxyClientConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/sdk",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
@@ -16,7 +16,7 @@
16
16
  "dist"
17
17
  ],
18
18
  "scripts": {
19
- "build": "tsup && tsc -p tsconfig.build.json",
19
+ "build": "vite build && tsc -p tsconfig.build.json",
20
20
  "typecheck": "tsc --noEmit",
21
21
  "publish": "npm publish --access public"
22
22
  },
@@ -32,6 +32,7 @@
32
32
  "@trpc/server": "^11.16.0",
33
33
  "@types/node": "^22.19.13",
34
34
  "tsup": "^8.5.1",
35
- "typescript": "^5.7.0"
35
+ "typescript": "^5.7.0",
36
+ "zod": "^4.3.6"
36
37
  }
37
38
  }