mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2025-12-11 20:15:30 +01:00
3.6 KiB
3.6 KiB
@spacedrive/client
Type-safe TypeScript client for the Spacedrive daemon, automatically generated from Rust core types using Specta.
Features
- Fully Type-Safe - All types generated directly from Rust core
- Real-time Events - Subscribe to daemon events with full type safety
- Unix Domain Sockets - Direct communication with daemon
- Auto-Generated - Types stay in sync with Rust core automatically
- Rich Documentation - JSDoc comments from Rust code
Installation
npm install @spacedrive/client
Quick Start
import { SpacedriveClient } from '@spacedrive/client';
const client = new SpacedriveClient();
// Test connection
await client.ping();
// Get libraries (fully typed)
const libraries = await client.getLibraries();
console.log('Libraries:', libraries);
// Get jobs with status filtering
const jobs = await client.getJobs('running');
console.log('Running jobs:', jobs);
// Create a new library
const newLibrary = await client.createLibrary('My Photos', '/Users/me/Photos');
console.log('Created library:', newLibrary);
Event Subscription
// Subscribe to specific event types
await client.subscribe(['JobStarted', 'JobProgress', 'JobCompleted']);
// Listen for events (fully typed)
client.on('event', (event) => {
switch (event) {
case 'CoreStarted':
console.log('Daemon started');
break;
default:
if ('JobStarted' in event) {
console.log(`Job started: ${event.JobStarted.job_type} (${event.JobStarted.job_id})`);
} else if ('JobProgress' in event) {
console.log(`Job progress: ${event.JobProgress.progress * 100}%`);
} else if ('JobCompleted' in event) {
console.log(`Job completed: ${event.JobCompleted.job_type}`);
console.log('Output:', event.JobCompleted.output);
}
break;
}
});
client.on('error', (error) => {
console.error('Client error:', error);
});
client.on('disconnected', () => {
console.log('Disconnected from daemon');
});
Type Generation
Types are automatically generated from the Rust core using Specta. To regenerate types:
npm run generate-types
API Reference
Core Methods
ping()- Test daemon connectivityexecuteQuery<Q, R>(query: Q, method: string): Promise<R>- Execute a query operationexecuteAction<A, R>(action: A, method: string): Promise<R>- Execute an action operationsubscribe(eventTypes?: string[]): Promise<void>- Subscribe to daemon events
Convenience Methods
getLibraries(includeStats?: boolean): Promise<LibraryInfo[]>- Get all librariescreateLibrary(name: string, path?: string): Promise<LibraryCreateOutput>- Create a new librarygetJobs(status?: JobStatus): Promise<JobListOutput>- Get job list with optional filtering
Event Types
All event types are fully typed TypeScript unions:
Event- Main event union typeJobOutput- Job completion output (adjacently tagged)JobStatus- Job status enum ("queued" | "running" | "completed" | ...)FileOperation- File operation typesProgress- Progress information with multiple formats
Architecture
This client uses the same architecture as the Swift client:
- JSON API Layer - Communicates via JSON instead of bincode for external clients
- Unix Domain Sockets - Direct, efficient communication with daemon
- Type Generation - Rust types → TypeScript via Specta
- Event Streaming - Real-time event subscription with line-delimited JSON
Development
# Install dependencies
npm install
# Build the client
npm run build
# Run tests
npm test
# Watch mode for development
npm run dev