@qm-hub/sync-client-types 0.2.1

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/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # @qm-hub/sync-client-types
2
+
3
+ TypeScript types for qm-sync-client - generated from Rust with Specta.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @qm-hub/sync-client-types
9
+ # or for local development
10
+ npm link
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import {
17
+ AuthHeaders,
18
+ HttpRequest,
19
+ HttpResponse,
20
+ Checkpoint,
21
+ SyncRecord,
22
+ PushRequest,
23
+ PullResponse,
24
+ DeltaRequest,
25
+ DeltaResponse,
26
+ createSyncClientConfig,
27
+ createAuthHeaders,
28
+ withBearer,
29
+ } from '@qm-hub/sync-client-types';
30
+
31
+ // Create config
32
+ const config = createSyncClientConfig(
33
+ 'https://sync.example.com',
34
+ 'my-app-id',
35
+ 'my-api-key'
36
+ );
37
+
38
+ // Create headers
39
+ const headers = withBearer(
40
+ createAuthHeaders(config.apiKey, config.appId),
41
+ accessToken
42
+ );
43
+
44
+ // Build request
45
+ const request: HttpRequest = {
46
+ method: 'POST',
47
+ url: `${config.serverUrl}/api/v1/sync/${config.appId}/delta`,
48
+ headers,
49
+ body: JSON.stringify(deltaRequest),
50
+ };
51
+ ```
52
+
53
+ ## Regenerating Types
54
+
55
+ Types are generated from Rust using Specta. To regenerate:
56
+
57
+ ```bash
58
+ cd ../qm-sync-client
59
+ GENERATE_TS_TYPES=1 cargo build
60
+ ```
61
+
62
+ Then update `src/index.ts` with the generated types.
63
+
64
+ ## Local Development
65
+
66
+ To use in a local project without publishing:
67
+
68
+ ```bash
69
+ # In this directory
70
+ npm install
71
+ npm run build
72
+ npm link
73
+
74
+ # In your project
75
+ npm link @qm-hub/sync-client-types
76
+ ```
77
+
78
+ ## Public package on NPM
79
+ ```bash
80
+ npm publish --access public
81
+
82
+ npm view @qm-hub/sync-client-types
83
+ ```
@@ -0,0 +1,227 @@
1
+ /**
2
+ * QM Sync Client for Browser/Node.js
3
+ *
4
+ * TypeScript implementation of qm-sync-client using fetch for HTTP transport.
5
+ * Matches the API of the Rust SyncClient from qm-sync-client crate.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+
10
+ /**
11
+ * HTTP client function type.
12
+ * Implement this to provide custom HTTP transport.
13
+ */
14
+ type HttpClientFn = (request: HttpRequest) => Promise<HttpResponse>;
15
+ /**
16
+ * Default fetch-based HTTP client implementation.
17
+ */
18
+ declare function fetchHttpClient(request: HttpRequest): Promise<HttpResponse>;
19
+ /**
20
+ * Sync client for communicating with qm-hub-server.
21
+ * TypeScript implementation matching the Rust SyncClient API.
22
+ */
23
+ declare class QmSyncClient {
24
+ readonly config: SyncClientConfig;
25
+ private readonly http;
26
+ private _accessToken;
27
+ private _refreshToken;
28
+ private _userId;
29
+ constructor(config: SyncClientConfig, http?: HttpClientFn);
30
+ /**
31
+ * Register a new user.
32
+ */
33
+ register(username: string, email: string, password: string): Promise<AuthResponse>;
34
+ /**
35
+ * Login with email and password.
36
+ */
37
+ login(email: string, password: string): Promise<AuthResponse>;
38
+ /**
39
+ * Refresh the access token.
40
+ * Note: Named `refreshToken` to match Rust API (singular).
41
+ */
42
+ refreshToken(): Promise<void>;
43
+ /**
44
+ * Logout and clear tokens.
45
+ */
46
+ logout(): void;
47
+ /**
48
+ * Check if the client is authenticated.
49
+ */
50
+ isAuthenticated(): boolean;
51
+ /**
52
+ * Set tokens directly (for restoring from storage).
53
+ */
54
+ setTokens(accessToken: string, refreshToken: string, userId?: string): void;
55
+ /**
56
+ * Get current tokens (for persisting to storage).
57
+ */
58
+ getTokens(): {
59
+ accessToken: string | null;
60
+ refreshToken: string | null;
61
+ };
62
+ /**
63
+ * Get current user ID.
64
+ */
65
+ getUserId(): string | null;
66
+ /**
67
+ * Push local changes to the server.
68
+ */
69
+ push(records: SyncRecord[]): Promise<PushResponse>;
70
+ /**
71
+ * Pull changes from the server.
72
+ */
73
+ pull(checkpoint?: Checkpoint, batchSize?: number): Promise<PullResponse>;
74
+ /**
75
+ * Perform a delta sync (push + pull in one request).
76
+ */
77
+ delta(records: SyncRecord[], checkpoint?: Checkpoint): Promise<DeltaResponse>;
78
+ private buildHeaders;
79
+ private storeTokens;
80
+ private isSuccess;
81
+ private isUnauthorized;
82
+ private authenticatedPost;
83
+ private syncToPushRecord;
84
+ }
85
+
86
+ /**
87
+ * @qm-hub/sync-client-types
88
+ *
89
+ * TypeScript types for qm-sync-client - generated from Rust with Specta.
90
+ * These types match the Rust structs with #[derive(specta::Type)].
91
+ *
92
+ * @packageDocumentation
93
+ */
94
+ /** Structured auth headers for sync API requests */
95
+ interface AuthHeaders {
96
+ /** Bearer token: "Authorization: Bearer {token}" */
97
+ authorization?: string;
98
+ /** API key header: "X-API-Key" */
99
+ apiKey: string;
100
+ /** App ID header: "X-App-Id" */
101
+ appId: string;
102
+ /** Content type (always "application/json") */
103
+ contentType: string;
104
+ }
105
+ /** HTTP request to be executed */
106
+ interface HttpRequest {
107
+ method: string;
108
+ url: string;
109
+ headers: AuthHeaders;
110
+ body?: string;
111
+ }
112
+ /** HTTP response from the request */
113
+ interface HttpResponse {
114
+ status: number;
115
+ body: string;
116
+ }
117
+ /** Composite checkpoint for deterministic ordering */
118
+ interface Checkpoint {
119
+ /** ISO 8601 timestamp of the last synced document */
120
+ updatedAt: string;
121
+ /** ID of the last synced document (tie-breaker) */
122
+ id: string;
123
+ }
124
+ /** A sync record representing a document to be synchronized */
125
+ interface SyncRecord {
126
+ tableName: string;
127
+ rowId: string;
128
+ data: Record<string, unknown>;
129
+ version: number;
130
+ deleted: boolean;
131
+ }
132
+ /** A sync record with additional server metadata */
133
+ interface SyncRecordWithMeta extends SyncRecord {
134
+ syncedAt: string;
135
+ userId: string;
136
+ }
137
+ /** Documents bundled with their checkpoint */
138
+ interface DocumentsWithCheckpoint<T, C> {
139
+ documents: T[];
140
+ checkpoint: C;
141
+ }
142
+ interface PushRequest {
143
+ records: PushRecord[];
144
+ clientTimestamp?: string;
145
+ }
146
+ interface PushRecord {
147
+ tableName: string;
148
+ rowId: string;
149
+ data: Record<string, unknown>;
150
+ version: number;
151
+ deleted: boolean;
152
+ assumedServerVersion?: number;
153
+ }
154
+ interface PushResponse {
155
+ synced: number;
156
+ conflicts: ConflictInfo[];
157
+ serverTimestamp: string;
158
+ }
159
+ interface PullRequest {
160
+ checkpoint?: Checkpoint;
161
+ batchSize: number;
162
+ tables?: string[];
163
+ }
164
+ interface PullResponse {
165
+ records: PullRecord[];
166
+ checkpoint: Checkpoint;
167
+ serverTimestamp: string;
168
+ hasMore: boolean;
169
+ }
170
+ interface PullRecord {
171
+ tableName: string;
172
+ rowId: string;
173
+ data: Record<string, unknown>;
174
+ version: number;
175
+ syncedAt: string;
176
+ deleted: boolean;
177
+ }
178
+ interface DeltaRequest {
179
+ push?: PushRequest;
180
+ pull?: PullRequest;
181
+ }
182
+ interface DeltaResponse {
183
+ push?: PushResponse;
184
+ pull?: PullResponse;
185
+ }
186
+ interface ConflictInfo {
187
+ tableName: string;
188
+ rowId: string;
189
+ clientVersion: number;
190
+ serverVersion: number;
191
+ serverState?: Record<string, unknown>;
192
+ }
193
+ interface AuthResponse {
194
+ userId: string;
195
+ accessToken: string;
196
+ refreshToken: string;
197
+ apps: string[];
198
+ isAdmin: boolean;
199
+ }
200
+ interface RefreshResponse {
201
+ accessToken: string;
202
+ refreshToken: string;
203
+ }
204
+ interface SyncClientConfig {
205
+ serverUrl: string;
206
+ appId: string;
207
+ apiKey: string;
208
+ defaultBatchSize: number;
209
+ timeoutMs: number;
210
+ }
211
+ interface SyncResult {
212
+ pushed: number;
213
+ pulled: number;
214
+ conflicts: number;
215
+ success: boolean;
216
+ error?: string;
217
+ }
218
+ /** Create default AuthHeaders */
219
+ declare function createAuthHeaders(apiKey: string, appId: string): AuthHeaders;
220
+ /** Create AuthHeaders with bearer token */
221
+ declare function withBearer(headers: AuthHeaders, token: string): AuthHeaders;
222
+ /** Create default SyncClientConfig */
223
+ declare function createSyncClientConfig(serverUrl: string, appId: string, apiKey: string, options?: Partial<Pick<SyncClientConfig, 'defaultBatchSize' | 'timeoutMs'>>): SyncClientConfig;
224
+ /** Create an initial checkpoint */
225
+ declare function initialCheckpoint(): Checkpoint;
226
+
227
+ export { type AuthHeaders, type AuthResponse, type Checkpoint, type ConflictInfo, type DeltaRequest, type DeltaResponse, type DocumentsWithCheckpoint, type HttpClientFn, type HttpRequest, type HttpResponse, type PullRecord, type PullRequest, type PullResponse, type PushRecord, type PushRequest, type PushResponse, QmSyncClient, type RefreshResponse, type SyncClientConfig, type SyncRecord, type SyncRecordWithMeta, type SyncResult, createAuthHeaders, createSyncClientConfig, fetchHttpClient, initialCheckpoint, withBearer };
@@ -0,0 +1,227 @@
1
+ /**
2
+ * QM Sync Client for Browser/Node.js
3
+ *
4
+ * TypeScript implementation of qm-sync-client using fetch for HTTP transport.
5
+ * Matches the API of the Rust SyncClient from qm-sync-client crate.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+
10
+ /**
11
+ * HTTP client function type.
12
+ * Implement this to provide custom HTTP transport.
13
+ */
14
+ type HttpClientFn = (request: HttpRequest) => Promise<HttpResponse>;
15
+ /**
16
+ * Default fetch-based HTTP client implementation.
17
+ */
18
+ declare function fetchHttpClient(request: HttpRequest): Promise<HttpResponse>;
19
+ /**
20
+ * Sync client for communicating with qm-hub-server.
21
+ * TypeScript implementation matching the Rust SyncClient API.
22
+ */
23
+ declare class QmSyncClient {
24
+ readonly config: SyncClientConfig;
25
+ private readonly http;
26
+ private _accessToken;
27
+ private _refreshToken;
28
+ private _userId;
29
+ constructor(config: SyncClientConfig, http?: HttpClientFn);
30
+ /**
31
+ * Register a new user.
32
+ */
33
+ register(username: string, email: string, password: string): Promise<AuthResponse>;
34
+ /**
35
+ * Login with email and password.
36
+ */
37
+ login(email: string, password: string): Promise<AuthResponse>;
38
+ /**
39
+ * Refresh the access token.
40
+ * Note: Named `refreshToken` to match Rust API (singular).
41
+ */
42
+ refreshToken(): Promise<void>;
43
+ /**
44
+ * Logout and clear tokens.
45
+ */
46
+ logout(): void;
47
+ /**
48
+ * Check if the client is authenticated.
49
+ */
50
+ isAuthenticated(): boolean;
51
+ /**
52
+ * Set tokens directly (for restoring from storage).
53
+ */
54
+ setTokens(accessToken: string, refreshToken: string, userId?: string): void;
55
+ /**
56
+ * Get current tokens (for persisting to storage).
57
+ */
58
+ getTokens(): {
59
+ accessToken: string | null;
60
+ refreshToken: string | null;
61
+ };
62
+ /**
63
+ * Get current user ID.
64
+ */
65
+ getUserId(): string | null;
66
+ /**
67
+ * Push local changes to the server.
68
+ */
69
+ push(records: SyncRecord[]): Promise<PushResponse>;
70
+ /**
71
+ * Pull changes from the server.
72
+ */
73
+ pull(checkpoint?: Checkpoint, batchSize?: number): Promise<PullResponse>;
74
+ /**
75
+ * Perform a delta sync (push + pull in one request).
76
+ */
77
+ delta(records: SyncRecord[], checkpoint?: Checkpoint): Promise<DeltaResponse>;
78
+ private buildHeaders;
79
+ private storeTokens;
80
+ private isSuccess;
81
+ private isUnauthorized;
82
+ private authenticatedPost;
83
+ private syncToPushRecord;
84
+ }
85
+
86
+ /**
87
+ * @qm-hub/sync-client-types
88
+ *
89
+ * TypeScript types for qm-sync-client - generated from Rust with Specta.
90
+ * These types match the Rust structs with #[derive(specta::Type)].
91
+ *
92
+ * @packageDocumentation
93
+ */
94
+ /** Structured auth headers for sync API requests */
95
+ interface AuthHeaders {
96
+ /** Bearer token: "Authorization: Bearer {token}" */
97
+ authorization?: string;
98
+ /** API key header: "X-API-Key" */
99
+ apiKey: string;
100
+ /** App ID header: "X-App-Id" */
101
+ appId: string;
102
+ /** Content type (always "application/json") */
103
+ contentType: string;
104
+ }
105
+ /** HTTP request to be executed */
106
+ interface HttpRequest {
107
+ method: string;
108
+ url: string;
109
+ headers: AuthHeaders;
110
+ body?: string;
111
+ }
112
+ /** HTTP response from the request */
113
+ interface HttpResponse {
114
+ status: number;
115
+ body: string;
116
+ }
117
+ /** Composite checkpoint for deterministic ordering */
118
+ interface Checkpoint {
119
+ /** ISO 8601 timestamp of the last synced document */
120
+ updatedAt: string;
121
+ /** ID of the last synced document (tie-breaker) */
122
+ id: string;
123
+ }
124
+ /** A sync record representing a document to be synchronized */
125
+ interface SyncRecord {
126
+ tableName: string;
127
+ rowId: string;
128
+ data: Record<string, unknown>;
129
+ version: number;
130
+ deleted: boolean;
131
+ }
132
+ /** A sync record with additional server metadata */
133
+ interface SyncRecordWithMeta extends SyncRecord {
134
+ syncedAt: string;
135
+ userId: string;
136
+ }
137
+ /** Documents bundled with their checkpoint */
138
+ interface DocumentsWithCheckpoint<T, C> {
139
+ documents: T[];
140
+ checkpoint: C;
141
+ }
142
+ interface PushRequest {
143
+ records: PushRecord[];
144
+ clientTimestamp?: string;
145
+ }
146
+ interface PushRecord {
147
+ tableName: string;
148
+ rowId: string;
149
+ data: Record<string, unknown>;
150
+ version: number;
151
+ deleted: boolean;
152
+ assumedServerVersion?: number;
153
+ }
154
+ interface PushResponse {
155
+ synced: number;
156
+ conflicts: ConflictInfo[];
157
+ serverTimestamp: string;
158
+ }
159
+ interface PullRequest {
160
+ checkpoint?: Checkpoint;
161
+ batchSize: number;
162
+ tables?: string[];
163
+ }
164
+ interface PullResponse {
165
+ records: PullRecord[];
166
+ checkpoint: Checkpoint;
167
+ serverTimestamp: string;
168
+ hasMore: boolean;
169
+ }
170
+ interface PullRecord {
171
+ tableName: string;
172
+ rowId: string;
173
+ data: Record<string, unknown>;
174
+ version: number;
175
+ syncedAt: string;
176
+ deleted: boolean;
177
+ }
178
+ interface DeltaRequest {
179
+ push?: PushRequest;
180
+ pull?: PullRequest;
181
+ }
182
+ interface DeltaResponse {
183
+ push?: PushResponse;
184
+ pull?: PullResponse;
185
+ }
186
+ interface ConflictInfo {
187
+ tableName: string;
188
+ rowId: string;
189
+ clientVersion: number;
190
+ serverVersion: number;
191
+ serverState?: Record<string, unknown>;
192
+ }
193
+ interface AuthResponse {
194
+ userId: string;
195
+ accessToken: string;
196
+ refreshToken: string;
197
+ apps: string[];
198
+ isAdmin: boolean;
199
+ }
200
+ interface RefreshResponse {
201
+ accessToken: string;
202
+ refreshToken: string;
203
+ }
204
+ interface SyncClientConfig {
205
+ serverUrl: string;
206
+ appId: string;
207
+ apiKey: string;
208
+ defaultBatchSize: number;
209
+ timeoutMs: number;
210
+ }
211
+ interface SyncResult {
212
+ pushed: number;
213
+ pulled: number;
214
+ conflicts: number;
215
+ success: boolean;
216
+ error?: string;
217
+ }
218
+ /** Create default AuthHeaders */
219
+ declare function createAuthHeaders(apiKey: string, appId: string): AuthHeaders;
220
+ /** Create AuthHeaders with bearer token */
221
+ declare function withBearer(headers: AuthHeaders, token: string): AuthHeaders;
222
+ /** Create default SyncClientConfig */
223
+ declare function createSyncClientConfig(serverUrl: string, appId: string, apiKey: string, options?: Partial<Pick<SyncClientConfig, 'defaultBatchSize' | 'timeoutMs'>>): SyncClientConfig;
224
+ /** Create an initial checkpoint */
225
+ declare function initialCheckpoint(): Checkpoint;
226
+
227
+ export { type AuthHeaders, type AuthResponse, type Checkpoint, type ConflictInfo, type DeltaRequest, type DeltaResponse, type DocumentsWithCheckpoint, type HttpClientFn, type HttpRequest, type HttpResponse, type PullRecord, type PullRequest, type PullResponse, type PushRecord, type PushRequest, type PushResponse, QmSyncClient, type RefreshResponse, type SyncClientConfig, type SyncRecord, type SyncRecordWithMeta, type SyncResult, createAuthHeaders, createSyncClientConfig, fetchHttpClient, initialCheckpoint, withBearer };